Blame


1 7d283eee 2017-11-29 stsp /*
2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7d283eee 2017-11-29 stsp *
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
7 7d283eee 2017-11-29 stsp *
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7d283eee 2017-11-29 stsp */
16 7d283eee 2017-11-29 stsp
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
19 7d283eee 2017-11-29 stsp
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 7d283eee 2017-11-29 stsp #include <sha1.h>
25 7d283eee 2017-11-29 stsp #include <zlib.h>
26 7d283eee 2017-11-29 stsp
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 e09a504c 2019-06-28 stsp #include "got_repository.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
32 0208f208 2020-05-05 stsp #include "got_cancel.h"
33 0208f208 2020-05-05 stsp #include "got_worktree.h"
34 a558dd1b 2022-06-08 stsp #include "got_opentemp.h"
35 7d283eee 2017-11-29 stsp
36 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
37 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
38 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
39 15a94983 2018-12-23 stsp #include "got_lib_object.h"
40 5191b70b 2023-01-07 mark
41 5191b70b 2023-01-07 mark #ifndef MAX
42 5191b70b 2023-01-07 mark #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
43 5191b70b 2023-01-07 mark #endif
44 a7852263 2017-11-30 stsp
45 404c43c4 2018-06-21 stsp static const struct got_error *
46 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
47 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
48 fe621944 2020-11-10 stsp {
49 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
50 fe621944 2020-11-10 stsp
51 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
52 fe621944 2020-11-10 stsp if (p == NULL)
53 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
54 c7d5c43c 2022-08-04 mark *lines = p;
55 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
56 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
57 fe621944 2020-11-10 stsp (*nlines)++;
58 c7d5c43c 2022-08-04 mark
59 fe621944 2020-11-10 stsp return NULL;
60 fe621944 2020-11-10 stsp }
61 fe621944 2020-11-10 stsp
62 a76e88e5 2023-01-10 mark static void
63 a76e88e5 2023-01-10 mark diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
64 a76e88e5 2023-01-10 mark uint32_t add, uint32_t rm)
65 a76e88e5 2023-01-10 mark {
66 a76e88e5 2023-01-10 mark int d1 = 1, d2 = 1;
67 a76e88e5 2023-01-10 mark
68 a76e88e5 2023-01-10 mark if (maxlen)
69 a76e88e5 2023-01-10 mark *maxlen = MAX(*maxlen, len);
70 a76e88e5 2023-01-10 mark
71 a76e88e5 2023-01-10 mark while (add /= 10)
72 a76e88e5 2023-01-10 mark ++d1;
73 a76e88e5 2023-01-10 mark *add_cols = MAX(*add_cols, d1);
74 a76e88e5 2023-01-10 mark
75 a76e88e5 2023-01-10 mark while (rm /= 10)
76 a76e88e5 2023-01-10 mark ++d2;
77 a76e88e5 2023-01-10 mark *rm_cols = MAX(*rm_cols, d2);
78 a76e88e5 2023-01-10 mark }
79 a76e88e5 2023-01-10 mark
80 fe621944 2020-11-10 stsp static const struct got_error *
81 a76e88e5 2023-01-10 mark get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
82 a76e88e5 2023-01-10 mark struct diff_result *r, int force_text, int status)
83 a76e88e5 2023-01-10 mark {
84 a76e88e5 2023-01-10 mark const struct got_error *err;
85 a76e88e5 2023-01-10 mark struct got_pathlist_entry *pe;
86 a76e88e5 2023-01-10 mark struct got_diff_changed_path *change = NULL;
87 a76e88e5 2023-01-10 mark int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
88 a76e88e5 2023-01-10 mark int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
89 a76e88e5 2023-01-10 mark int i;
90 a76e88e5 2023-01-10 mark
91 a76e88e5 2023-01-10 mark change = calloc(1, sizeof(*change));
92 a76e88e5 2023-01-10 mark if (change == NULL)
93 6b37f13a 2023-01-10 mark return got_error_from_errno("calloc");
94 a76e88e5 2023-01-10 mark
95 a76e88e5 2023-01-10 mark if (!isbin || force_text) {
96 a76e88e5 2023-01-10 mark for (i = 0; i < r->chunks.len; ++i) {
97 a76e88e5 2023-01-10 mark struct diff_chunk *c;
98 a76e88e5 2023-01-10 mark int clc, crc;
99 a76e88e5 2023-01-10 mark
100 a76e88e5 2023-01-10 mark c = diff_chunk_get(r, i);
101 a76e88e5 2023-01-10 mark clc = diff_chunk_get_left_count(c);
102 a76e88e5 2023-01-10 mark crc = diff_chunk_get_right_count(c);
103 a76e88e5 2023-01-10 mark
104 a76e88e5 2023-01-10 mark if (crc && !clc)
105 a76e88e5 2023-01-10 mark change->add += crc;
106 a76e88e5 2023-01-10 mark if (clc && !crc)
107 a76e88e5 2023-01-10 mark change->rm += clc;
108 a76e88e5 2023-01-10 mark }
109 a76e88e5 2023-01-10 mark }
110 a76e88e5 2023-01-10 mark
111 a76e88e5 2023-01-10 mark change->status = status;
112 a76e88e5 2023-01-10 mark ds->ins += change->add;
113 a76e88e5 2023-01-10 mark ds->del += change->rm;
114 a76e88e5 2023-01-10 mark ++ds->nfiles;
115 a76e88e5 2023-01-10 mark
116 a76e88e5 2023-01-10 mark err = got_pathlist_append(ds->paths, path, change);
117 a76e88e5 2023-01-10 mark if (err)
118 a76e88e5 2023-01-10 mark return err;
119 a76e88e5 2023-01-10 mark
120 a76e88e5 2023-01-10 mark pe = TAILQ_LAST(ds->paths, got_pathlist_head);
121 a76e88e5 2023-01-10 mark diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
122 a76e88e5 2023-01-10 mark pe->path_len, change->add, change->rm);
123 a76e88e5 2023-01-10 mark
124 a76e88e5 2023-01-10 mark return NULL;
125 a76e88e5 2023-01-10 mark }
126 a76e88e5 2023-01-10 mark
127 a76e88e5 2023-01-10 mark static const struct got_error *
128 c7d5c43c 2022-08-04 mark diff_blobs(struct got_diff_line **lines, size_t *nlines,
129 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
130 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
131 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
132 a76e88e5 2023-01-10 mark int diff_context, int ignore_whitespace, int force_text_diff,
133 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
134 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
135 7d283eee 2017-11-29 stsp {
136 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
137 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
138 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
139 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
140 be659d10 2020-11-18 stsp off_t size1, size2;
141 3846622f 2023-01-07 mark struct got_diffreg_result *result = NULL;
142 fe621944 2020-11-10 stsp off_t outoff = 0;
143 fe621944 2020-11-10 stsp int n;
144 7d283eee 2017-11-29 stsp
145 2d61e381 2022-08-30 mark if (lines && *lines && *nlines > 0)
146 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
147 c7d5c43c 2022-08-04 mark else if (lines) {
148 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
149 9c659ea0 2020-11-22 stsp if (err)
150 9c659ea0 2020-11-22 stsp goto done;
151 9c659ea0 2020-11-22 stsp }
152 fe621944 2020-11-10 stsp
153 fe621944 2020-11-10 stsp if (resultp)
154 fe621944 2020-11-10 stsp *resultp = NULL;
155 fe621944 2020-11-10 stsp
156 b72706c3 2022-06-01 stsp if (f1) {
157 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
158 b72706c3 2022-06-01 stsp if (err)
159 b72706c3 2022-06-01 stsp goto done;
160 fe621944 2020-11-10 stsp }
161 b72706c3 2022-06-01 stsp if (f2) {
162 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
163 b72706c3 2022-06-01 stsp if (err)
164 b72706c3 2022-06-01 stsp goto done;
165 fe621944 2020-11-10 stsp }
166 7d283eee 2017-11-29 stsp
167 f934cf2c 2018-02-12 stsp size1 = 0;
168 98abbc84 2017-11-30 stsp if (blob1) {
169 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
170 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
171 6c4c42e0 2019-06-24 stsp blob1);
172 35e9ba5d 2018-06-21 stsp if (err)
173 35e9ba5d 2018-06-21 stsp goto done;
174 98abbc84 2017-11-30 stsp } else
175 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
176 7d283eee 2017-11-29 stsp
177 f934cf2c 2018-02-12 stsp size2 = 0;
178 98abbc84 2017-11-30 stsp if (blob2) {
179 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
180 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
181 6c4c42e0 2019-06-24 stsp blob2);
182 35e9ba5d 2018-06-21 stsp if (err)
183 35e9ba5d 2018-06-21 stsp goto done;
184 98abbc84 2017-11-30 stsp } else
185 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
186 7d283eee 2017-11-29 stsp
187 09de383e 2018-12-24 stsp if (outfile) {
188 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
189 40dde666 2020-07-23 stsp int modebits;
190 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
191 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
192 40dde666 2020-07-23 stsp modebits = S_IFLNK;
193 40dde666 2020-07-23 stsp else
194 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
195 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
196 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
197 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
198 46f68b20 2019-10-19 stsp goto done;
199 46f68b20 2019-10-19 stsp }
200 46f68b20 2019-10-19 stsp }
201 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
202 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
203 40dde666 2020-07-23 stsp modebits = S_IFLNK;
204 40dde666 2020-07-23 stsp else
205 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
206 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
207 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
208 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
209 46f68b20 2019-10-19 stsp goto done;
210 46f68b20 2019-10-19 stsp }
211 46f68b20 2019-10-19 stsp }
212 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
213 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
214 fe621944 2020-11-10 stsp if (n < 0)
215 fe621944 2020-11-10 stsp goto done;
216 fe621944 2020-11-10 stsp outoff += n;
217 c7d5c43c 2022-08-04 mark if (lines) {
218 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
219 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_MIN);
220 fe621944 2020-11-10 stsp if (err)
221 fe621944 2020-11-10 stsp goto done;
222 fe621944 2020-11-10 stsp }
223 fe621944 2020-11-10 stsp
224 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
225 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
226 fe621944 2020-11-10 stsp if (n < 0)
227 fe621944 2020-11-10 stsp goto done;
228 fe621944 2020-11-10 stsp outoff += n;
229 c7d5c43c 2022-08-04 mark if (lines) {
230 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
231 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_PLUS);
232 fe621944 2020-11-10 stsp if (err)
233 fe621944 2020-11-10 stsp goto done;
234 fe621944 2020-11-10 stsp }
235 fe621944 2020-11-10 stsp
236 46f68b20 2019-10-19 stsp free(modestr1);
237 46f68b20 2019-10-19 stsp free(modestr2);
238 09de383e 2018-12-24 stsp }
239 a76e88e5 2023-01-10 mark
240 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
241 4b752015 2022-06-30 stsp force_text_diff);
242 fe621944 2020-11-10 stsp if (err)
243 fe621944 2020-11-10 stsp goto done;
244 fe621944 2020-11-10 stsp
245 a76e88e5 2023-01-10 mark if (show_diffstat) {
246 a76e88e5 2023-01-10 mark char *path = NULL;
247 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
248 6b37f13a 2023-01-10 mark
249 6b37f13a 2023-01-10 mark /*
250 6b37f13a 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
251 6b37f13a 2023-01-10 mark * content change, there'll be no diffstat, and if there
252 6b37f13a 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
253 6b37f13a 2023-01-10 mark */
254 6b37f13a 2023-01-10 mark if (blob1 == NULL)
255 6b37f13a 2023-01-10 mark status = GOT_STATUS_ADD;
256 6b37f13a 2023-01-10 mark else if (blob2 == NULL)
257 6b37f13a 2023-01-10 mark status = GOT_STATUS_DELETE;
258 6b37f13a 2023-01-10 mark else
259 6b37f13a 2023-01-10 mark status = GOT_STATUS_MODIFY;
260 a76e88e5 2023-01-10 mark
261 a76e88e5 2023-01-10 mark if (label1 == NULL && label2 == NULL) {
262 a76e88e5 2023-01-10 mark /* diffstat of blobs, show hash instead of path */
263 a76e88e5 2023-01-10 mark if (asprintf(&path, "%.10s -> %.10s",
264 a76e88e5 2023-01-10 mark idstr1, idstr2) == -1) {
265 a76e88e5 2023-01-10 mark err = got_error_from_errno("asprintf");
266 a76e88e5 2023-01-10 mark goto done;
267 a76e88e5 2023-01-10 mark }
268 a76e88e5 2023-01-10 mark } else {
269 6b37f13a 2023-01-10 mark if (label2 != NULL &&
270 6b37f13a 2023-01-10 mark (status != GOT_STATUS_DELETE || label1 == NULL))
271 6b37f13a 2023-01-10 mark path = strdup(label2);
272 6b37f13a 2023-01-10 mark else
273 6b37f13a 2023-01-10 mark path = strdup(label1);
274 a76e88e5 2023-01-10 mark if (path == NULL) {
275 6b37f13a 2023-01-10 mark err = got_error_from_errno("strdup");
276 a76e88e5 2023-01-10 mark goto done;
277 a76e88e5 2023-01-10 mark }
278 a76e88e5 2023-01-10 mark }
279 a76e88e5 2023-01-10 mark
280 a76e88e5 2023-01-10 mark err = get_diffstat(ds, path, result->result, force_text_diff,
281 a76e88e5 2023-01-10 mark status);
282 a76e88e5 2023-01-10 mark if (err) {
283 a76e88e5 2023-01-10 mark free(path);
284 a76e88e5 2023-01-10 mark goto done;
285 a76e88e5 2023-01-10 mark }
286 a76e88e5 2023-01-10 mark }
287 a76e88e5 2023-01-10 mark
288 fe621944 2020-11-10 stsp if (outfile) {
289 c7d5c43c 2022-08-04 mark err = got_diffreg_output(lines, nlines, result,
290 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
291 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
292 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
293 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
294 fe621944 2020-11-10 stsp if (err)
295 fe621944 2020-11-10 stsp goto done;
296 fe621944 2020-11-10 stsp }
297 fe621944 2020-11-10 stsp
298 3846622f 2023-01-07 mark done:
299 fe621944 2020-11-10 stsp if (resultp && err == NULL)
300 fe621944 2020-11-10 stsp *resultp = result;
301 3846622f 2023-01-07 mark else if (result) {
302 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
303 fe621944 2020-11-10 stsp if (free_err && err == NULL)
304 fe621944 2020-11-10 stsp err = free_err;
305 fe621944 2020-11-10 stsp }
306 3846622f 2023-01-07 mark
307 7d283eee 2017-11-29 stsp return err;
308 aaa13589 2019-06-01 stsp }
309 aaa13589 2019-06-01 stsp
310 aaa13589 2019-06-01 stsp const struct got_error *
311 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
312 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
313 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
314 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
315 b72706c3 2022-06-01 stsp struct got_repository *repo)
316 aaa13589 2019-06-01 stsp {
317 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
318 aaa13589 2019-06-01 stsp
319 c7d5c43c 2022-08-04 mark return diff_blobs(&a->lines, &a->nlines, NULL,
320 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
321 a76e88e5 2023-01-10 mark a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
322 a76e88e5 2023-01-10 mark a->diffstat, a->outfile, a->diff_algo);
323 7d283eee 2017-11-29 stsp }
324 474b4f94 2017-11-30 stsp
325 404c43c4 2018-06-21 stsp const struct got_error *
326 c7d5c43c 2022-08-04 mark got_diff_blob(struct got_diff_line **lines, size_t*nlines,
327 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
328 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
329 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
330 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
331 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
332 404c43c4 2018-06-21 stsp {
333 c7d5c43c 2022-08-04 mark return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
334 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
335 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, ds, outfile, diff_algo);
336 404c43c4 2018-06-21 stsp }
337 404c43c4 2018-06-21 stsp
338 7f1f93af 2019-08-06 stsp static const struct got_error *
339 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
340 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
341 c87842d5 2022-09-23 mark FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
342 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
343 a76e88e5 2023-01-10 mark int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
344 a76e88e5 2023-01-10 mark FILE *outfile)
345 b72f483a 2019-02-05 stsp {
346 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
347 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
348 58e31a80 2022-06-27 op const char *idstr1 = NULL;
349 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
350 b72f483a 2019-02-05 stsp
351 fe621944 2020-11-10 stsp if (resultp)
352 fe621944 2020-11-10 stsp *resultp = NULL;
353 7f1f93af 2019-08-06 stsp
354 b72706c3 2022-06-01 stsp if (blob1)
355 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
356 b72706c3 2022-06-01 stsp else
357 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
358 b72f483a 2019-02-05 stsp
359 7f1f93af 2019-08-06 stsp if (outfile) {
360 c87842d5 2022-09-23 mark char *mode = NULL;
361 c87842d5 2022-09-23 mark
362 c87842d5 2022-09-23 mark /* display file mode for new added files only */
363 c87842d5 2022-09-23 mark if (f2_exists && blob1 == NULL) {
364 c87842d5 2022-09-23 mark int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
365 c87842d5 2022-09-23 mark
366 c87842d5 2022-09-23 mark if (S_ISLNK(sb2->st_mode))
367 c87842d5 2022-09-23 mark mmask = S_IFLNK;
368 c87842d5 2022-09-23 mark if (asprintf(&mode, " (mode %o)",
369 c87842d5 2022-09-23 mark sb2->st_mode & mmask) == -1)
370 c87842d5 2022-09-23 mark return got_error_from_errno("asprintf");
371 c87842d5 2022-09-23 mark }
372 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
373 c87842d5 2022-09-23 mark fprintf(outfile, "file + %s%s\n",
374 c87842d5 2022-09-23 mark f2_exists ? label2 : "/dev/null", mode ? mode : "");
375 c87842d5 2022-09-23 mark free(mode);
376 7f1f93af 2019-08-06 stsp }
377 fe621944 2020-11-10 stsp
378 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
379 4b752015 2022-06-30 stsp force_text_diff);
380 fe621944 2020-11-10 stsp if (err)
381 fe621944 2020-11-10 stsp goto done;
382 fe621944 2020-11-10 stsp
383 fe621944 2020-11-10 stsp if (outfile) {
384 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
385 49d4a017 2022-06-30 stsp blob1 != NULL, f2_exists,
386 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
387 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
388 1cb46f00 2020-11-21 stsp diff_context, outfile);
389 7f1f93af 2019-08-06 stsp if (err)
390 a76e88e5 2023-01-10 mark goto done;
391 a76e88e5 2023-01-10 mark }
392 a76e88e5 2023-01-10 mark
393 a76e88e5 2023-01-10 mark if (show_diffstat) {
394 a76e88e5 2023-01-10 mark char *path = NULL;
395 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
396 a76e88e5 2023-01-10 mark
397 a76e88e5 2023-01-10 mark /*
398 a76e88e5 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
399 a76e88e5 2023-01-10 mark * content change, there'll be no diffstat, and if there
400 a76e88e5 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
401 a76e88e5 2023-01-10 mark */
402 a76e88e5 2023-01-10 mark if (blob1 == NULL)
403 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
404 a76e88e5 2023-01-10 mark else if (!f2_exists)
405 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
406 a76e88e5 2023-01-10 mark else
407 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
408 6b37f13a 2023-01-10 mark
409 6b37f13a 2023-01-10 mark if (label2 != NULL &&
410 6b37f13a 2023-01-10 mark (status != GOT_STATUS_DELETE || label1 == NULL))
411 6b37f13a 2023-01-10 mark path = strdup(label2);
412 6b37f13a 2023-01-10 mark else
413 6b37f13a 2023-01-10 mark path = strdup(label1);
414 6b37f13a 2023-01-10 mark if (path == NULL) {
415 6b37f13a 2023-01-10 mark err = got_error_from_errno("strdup");
416 6b37f13a 2023-01-10 mark goto done;
417 6b37f13a 2023-01-10 mark }
418 a76e88e5 2023-01-10 mark
419 a76e88e5 2023-01-10 mark err = get_diffstat(ds, path, result->result, force_text_diff,
420 a76e88e5 2023-01-10 mark status);
421 a76e88e5 2023-01-10 mark if (err) {
422 a76e88e5 2023-01-10 mark free(path);
423 fe621944 2020-11-10 stsp goto done;
424 a76e88e5 2023-01-10 mark }
425 7f1f93af 2019-08-06 stsp }
426 fe621944 2020-11-10 stsp
427 3846622f 2023-01-07 mark done:
428 fe621944 2020-11-10 stsp if (resultp && err == NULL)
429 fe621944 2020-11-10 stsp *resultp = result;
430 fe621944 2020-11-10 stsp else if (result) {
431 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
432 fe621944 2020-11-10 stsp if (free_err && err == NULL)
433 fe621944 2020-11-10 stsp err = free_err;
434 fe621944 2020-11-10 stsp }
435 b72f483a 2019-02-05 stsp return err;
436 7f1f93af 2019-08-06 stsp }
437 7f1f93af 2019-08-06 stsp
438 7f1f93af 2019-08-06 stsp const struct got_error *
439 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
440 c87842d5 2022-09-23 mark const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
441 4b752015 2022-06-30 stsp const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
442 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
443 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
444 7f1f93af 2019-08-06 stsp {
445 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
446 c87842d5 2022-09-23 mark sb2, label2, diff_algo, diff_context, ignore_whitespace,
447 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, ds, outfile);
448 474b4f94 2017-11-30 stsp }
449 474b4f94 2017-11-30 stsp
450 474b4f94 2017-11-30 stsp static const struct got_error *
451 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
452 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
453 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
454 474b4f94 2017-11-30 stsp {
455 4e22badc 2017-11-30 stsp const struct got_error *err;
456 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
457 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
458 4e22badc 2017-11-30 stsp
459 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
460 4e22badc 2017-11-30 stsp if (err)
461 4e22badc 2017-11-30 stsp return err;
462 4e22badc 2017-11-30 stsp
463 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
464 2acfca77 2018-04-01 stsp if (err)
465 2acfca77 2018-04-01 stsp goto done;
466 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
467 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
468 2acfca77 2018-04-01 stsp done:
469 2acfca77 2018-04-01 stsp got_object_close(obj);
470 2acfca77 2018-04-01 stsp if (blob)
471 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
472 2acfca77 2018-04-01 stsp return err;
473 474b4f94 2017-11-30 stsp }
474 474b4f94 2017-11-30 stsp
475 474b4f94 2017-11-30 stsp static const struct got_error *
476 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
477 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
478 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
479 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
480 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
481 474b4f94 2017-11-30 stsp {
482 6a213ccb 2017-11-30 stsp const struct got_error *err;
483 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
484 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
485 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
486 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
487 6a213ccb 2017-11-30 stsp
488 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
489 6a213ccb 2017-11-30 stsp if (err)
490 730a8aa0 2018-04-24 stsp return err;
491 eb81bc23 2022-06-28 tracey
492 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
493 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
494 6a213ccb 2017-11-30 stsp goto done;
495 6a213ccb 2017-11-30 stsp }
496 6a213ccb 2017-11-30 stsp
497 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
498 730a8aa0 2018-04-24 stsp if (err)
499 6a213ccb 2017-11-30 stsp goto done;
500 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
501 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
502 6a213ccb 2017-11-30 stsp goto done;
503 6a213ccb 2017-11-30 stsp }
504 6a213ccb 2017-11-30 stsp
505 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
506 2acfca77 2018-04-01 stsp if (err)
507 6a213ccb 2017-11-30 stsp goto done;
508 6a213ccb 2017-11-30 stsp
509 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
510 2acfca77 2018-04-01 stsp if (err)
511 6a213ccb 2017-11-30 stsp goto done;
512 6a213ccb 2017-11-30 stsp
513 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
514 b72706c3 2022-06-01 stsp mode1, mode2, repo);
515 6a213ccb 2017-11-30 stsp done:
516 a3e2cbea 2017-12-01 stsp if (obj1)
517 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
518 a3e2cbea 2017-12-01 stsp if (obj2)
519 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
520 a3e2cbea 2017-12-01 stsp if (blob1)
521 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
522 a3e2cbea 2017-12-01 stsp if (blob2)
523 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
524 6a213ccb 2017-11-30 stsp return err;
525 474b4f94 2017-11-30 stsp }
526 474b4f94 2017-11-30 stsp
527 474b4f94 2017-11-30 stsp static const struct got_error *
528 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
529 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
530 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg)
531 474b4f94 2017-11-30 stsp {
532 365fb436 2017-11-30 stsp const struct got_error *err;
533 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
534 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
535 365fb436 2017-11-30 stsp
536 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
537 365fb436 2017-11-30 stsp if (err)
538 365fb436 2017-11-30 stsp return err;
539 365fb436 2017-11-30 stsp
540 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
541 2acfca77 2018-04-01 stsp if (err)
542 2acfca77 2018-04-01 stsp goto done;
543 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
544 b72706c3 2022-06-01 stsp mode, 0, repo);
545 2acfca77 2018-04-01 stsp done:
546 2acfca77 2018-04-01 stsp got_object_close(obj);
547 2acfca77 2018-04-01 stsp if (blob)
548 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
549 2acfca77 2018-04-01 stsp return err;
550 474b4f94 2017-11-30 stsp }
551 474b4f94 2017-11-30 stsp
552 474b4f94 2017-11-30 stsp static const struct got_error *
553 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
554 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
555 49d4a017 2022-06-30 stsp void *cb_arg, int diff_content)
556 474b4f94 2017-11-30 stsp {
557 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
558 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
559 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
560 9c70d4c3 2017-11-30 stsp
561 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
562 9c70d4c3 2017-11-30 stsp if (err)
563 9c70d4c3 2017-11-30 stsp goto done;
564 9c70d4c3 2017-11-30 stsp
565 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
566 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
567 9c70d4c3 2017-11-30 stsp goto done;
568 9c70d4c3 2017-11-30 stsp }
569 9c70d4c3 2017-11-30 stsp
570 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
571 9c70d4c3 2017-11-30 stsp if (err)
572 9c70d4c3 2017-11-30 stsp goto done;
573 9c70d4c3 2017-11-30 stsp
574 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
575 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
576 9c70d4c3 2017-11-30 stsp done:
577 9c70d4c3 2017-11-30 stsp if (tree)
578 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
579 9c70d4c3 2017-11-30 stsp if (treeobj)
580 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
581 9c70d4c3 2017-11-30 stsp return err;
582 474b4f94 2017-11-30 stsp }
583 474b4f94 2017-11-30 stsp
584 474b4f94 2017-11-30 stsp static const struct got_error *
585 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
586 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
587 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
588 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
589 b72706c3 2022-06-01 stsp int diff_content)
590 474b4f94 2017-11-30 stsp {
591 f6861a81 2018-09-13 stsp const struct got_error *err;
592 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
593 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
594 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
595 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
596 789689b5 2017-11-30 stsp
597 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
598 789689b5 2017-11-30 stsp if (err)
599 789689b5 2017-11-30 stsp goto done;
600 789689b5 2017-11-30 stsp
601 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
602 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
603 789689b5 2017-11-30 stsp goto done;
604 789689b5 2017-11-30 stsp }
605 789689b5 2017-11-30 stsp
606 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
607 789689b5 2017-11-30 stsp if (err)
608 789689b5 2017-11-30 stsp goto done;
609 789689b5 2017-11-30 stsp
610 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
611 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
612 789689b5 2017-11-30 stsp goto done;
613 789689b5 2017-11-30 stsp }
614 789689b5 2017-11-30 stsp
615 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
616 789689b5 2017-11-30 stsp if (err)
617 789689b5 2017-11-30 stsp goto done;
618 789689b5 2017-11-30 stsp
619 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
620 789689b5 2017-11-30 stsp if (err)
621 789689b5 2017-11-30 stsp goto done;
622 789689b5 2017-11-30 stsp
623 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
624 f9d37699 2022-06-28 stsp label1, label2, repo, cb, cb_arg, diff_content);
625 789689b5 2017-11-30 stsp
626 789689b5 2017-11-30 stsp done:
627 789689b5 2017-11-30 stsp if (tree1)
628 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
629 789689b5 2017-11-30 stsp if (tree2)
630 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
631 789689b5 2017-11-30 stsp if (treeobj1)
632 789689b5 2017-11-30 stsp got_object_close(treeobj1);
633 789689b5 2017-11-30 stsp if (treeobj2)
634 789689b5 2017-11-30 stsp got_object_close(treeobj2);
635 789689b5 2017-11-30 stsp return err;
636 474b4f94 2017-11-30 stsp }
637 474b4f94 2017-11-30 stsp
638 474b4f94 2017-11-30 stsp static const struct got_error *
639 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
640 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
641 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
642 474b4f94 2017-11-30 stsp {
643 f6861a81 2018-09-13 stsp const struct got_error *err;
644 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
645 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
646 2c56f2ce 2017-11-30 stsp
647 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
648 2c56f2ce 2017-11-30 stsp if (err)
649 2c56f2ce 2017-11-30 stsp goto done;
650 2c56f2ce 2017-11-30 stsp
651 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
652 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
653 2c56f2ce 2017-11-30 stsp goto done;
654 2c56f2ce 2017-11-30 stsp }
655 2c56f2ce 2017-11-30 stsp
656 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
657 2c56f2ce 2017-11-30 stsp if (err)
658 2c56f2ce 2017-11-30 stsp goto done;
659 2c56f2ce 2017-11-30 stsp
660 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
661 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
662 2c56f2ce 2017-11-30 stsp done:
663 2c56f2ce 2017-11-30 stsp if (tree)
664 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
665 2c56f2ce 2017-11-30 stsp if (treeobj)
666 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
667 2c56f2ce 2017-11-30 stsp return err;
668 474b4f94 2017-11-30 stsp }
669 474b4f94 2017-11-30 stsp
670 474b4f94 2017-11-30 stsp static const struct got_error *
671 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
672 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
673 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
674 474b4f94 2017-11-30 stsp {
675 013404a9 2017-11-30 stsp /* XXX TODO */
676 474b4f94 2017-11-30 stsp return NULL;
677 474b4f94 2017-11-30 stsp }
678 474b4f94 2017-11-30 stsp
679 474b4f94 2017-11-30 stsp static const struct got_error *
680 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
681 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
682 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
683 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
684 31b4484f 2019-07-27 stsp int diff_content)
685 474b4f94 2017-11-30 stsp {
686 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
687 19ae6da1 2018-11-05 stsp int id_match;
688 63c5ca5d 2019-08-24 stsp
689 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
690 63c5ca5d 2019-08-24 stsp return NULL;
691 474b4f94 2017-11-30 stsp
692 474b4f94 2017-11-30 stsp if (te2 == NULL) {
693 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
694 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
695 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
696 31b4484f 2019-07-27 stsp else {
697 31b4484f 2019-07-27 stsp if (diff_content)
698 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
699 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
700 31b4484f 2019-07-27 stsp else
701 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
702 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
703 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
704 31b4484f 2019-07-27 stsp }
705 f6861a81 2018-09-13 stsp return err;
706 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
707 63c5ca5d 2019-08-24 stsp return NULL;
708 474b4f94 2017-11-30 stsp
709 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
710 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
711 19ae6da1 2018-11-05 stsp if (!id_match)
712 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
713 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
714 f9d37699 2022-06-28 stsp diff_content);
715 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
716 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
717 46f68b20 2019-10-19 stsp if (!id_match ||
718 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
719 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
720 31b4484f 2019-07-27 stsp if (diff_content)
721 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
722 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
723 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
724 31b4484f 2019-07-27 stsp else
725 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
726 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
727 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
728 31b4484f 2019-07-27 stsp }
729 413ea19d 2017-11-30 stsp }
730 474b4f94 2017-11-30 stsp
731 19ae6da1 2018-11-05 stsp if (id_match)
732 f6861a81 2018-09-13 stsp return NULL;
733 f6861a81 2018-09-13 stsp
734 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
735 aaa13589 2019-06-01 stsp cb, cb_arg);
736 474b4f94 2017-11-30 stsp }
737 474b4f94 2017-11-30 stsp
738 474b4f94 2017-11-30 stsp static const struct got_error *
739 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
740 49d4a017 2022-06-30 stsp struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
741 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
742 31b4484f 2019-07-27 stsp int diff_content)
743 474b4f94 2017-11-30 stsp {
744 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
745 63c5ca5d 2019-08-24 stsp return NULL;
746 63c5ca5d 2019-08-24 stsp
747 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
748 f6861a81 2018-09-13 stsp return NULL;
749 474b4f94 2017-11-30 stsp
750 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
751 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
752 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
753 f6861a81 2018-09-13 stsp
754 31b4484f 2019-07-27 stsp if (diff_content)
755 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
756 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
757 31b4484f 2019-07-27 stsp
758 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
759 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
760 0208f208 2020-05-05 stsp }
761 0208f208 2020-05-05 stsp
762 0208f208 2020-05-05 stsp const struct got_error *
763 5191b70b 2023-01-07 mark got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
764 5191b70b 2023-01-07 mark struct got_blob_object *blob2, FILE *f1, FILE *f2,
765 5191b70b 2023-01-07 mark struct got_object_id *id1, struct got_object_id *id2,
766 5191b70b 2023-01-07 mark const char *label1, const char *label2,
767 5191b70b 2023-01-07 mark mode_t mode1, mode_t mode2, struct got_repository *repo)
768 5191b70b 2023-01-07 mark {
769 5191b70b 2023-01-07 mark const struct got_error *err = NULL;
770 5191b70b 2023-01-07 mark struct got_diffreg_result *result = NULL;
771 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg *a = arg;
772 5191b70b 2023-01-07 mark char *path = NULL;
773 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
774 5191b70b 2023-01-07 mark
775 5191b70b 2023-01-07 mark path = strdup(label2 ? label2 : label1);
776 5191b70b 2023-01-07 mark if (path == NULL)
777 6b37f13a 2023-01-10 mark return got_error_from_errno("strdup");
778 5191b70b 2023-01-07 mark
779 5191b70b 2023-01-07 mark if (id1 == NULL)
780 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
781 5191b70b 2023-01-07 mark else if (id2 == NULL)
782 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
783 5191b70b 2023-01-07 mark else {
784 5191b70b 2023-01-07 mark if (got_object_id_cmp(id1, id2) != 0)
785 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
786 5191b70b 2023-01-07 mark else if (mode1 != mode2)
787 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODE_CHANGE;
788 5191b70b 2023-01-07 mark }
789 5191b70b 2023-01-07 mark
790 5191b70b 2023-01-07 mark if (f1) {
791 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f1);
792 5191b70b 2023-01-07 mark if (err)
793 5191b70b 2023-01-07 mark goto done;
794 5191b70b 2023-01-07 mark }
795 5191b70b 2023-01-07 mark if (f2) {
796 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f2);
797 5191b70b 2023-01-07 mark if (err)
798 5191b70b 2023-01-07 mark goto done;
799 5191b70b 2023-01-07 mark }
800 5191b70b 2023-01-07 mark
801 5191b70b 2023-01-07 mark if (blob1) {
802 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
803 5191b70b 2023-01-07 mark blob1);
804 5191b70b 2023-01-07 mark if (err)
805 5191b70b 2023-01-07 mark goto done;
806 5191b70b 2023-01-07 mark }
807 5191b70b 2023-01-07 mark if (blob2) {
808 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
809 5191b70b 2023-01-07 mark blob2);
810 5191b70b 2023-01-07 mark if (err)
811 5191b70b 2023-01-07 mark goto done;
812 5191b70b 2023-01-07 mark }
813 5191b70b 2023-01-07 mark
814 5191b70b 2023-01-07 mark err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
815 5191b70b 2023-01-07 mark a->force_text);
816 5191b70b 2023-01-07 mark if (err)
817 5191b70b 2023-01-07 mark goto done;
818 5191b70b 2023-01-07 mark
819 a76e88e5 2023-01-10 mark err = get_diffstat(a, path, result->result, a->force_text, status);
820 5191b70b 2023-01-07 mark
821 5191b70b 2023-01-07 mark done:
822 5191b70b 2023-01-07 mark if (result) {
823 5191b70b 2023-01-07 mark const struct got_error *free_err;
824 5191b70b 2023-01-07 mark
825 5191b70b 2023-01-07 mark free_err = got_diffreg_result_free(result);
826 5191b70b 2023-01-07 mark if (free_err && err == NULL)
827 5191b70b 2023-01-07 mark err = free_err;
828 5191b70b 2023-01-07 mark }
829 a76e88e5 2023-01-10 mark if (err)
830 5191b70b 2023-01-07 mark free(path);
831 5191b70b 2023-01-07 mark return err;
832 5191b70b 2023-01-07 mark }
833 5191b70b 2023-01-07 mark
834 5191b70b 2023-01-07 mark const struct got_error *
835 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
836 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
837 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
838 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
839 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
840 0208f208 2020-05-05 stsp {
841 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
842 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
843 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
844 0208f208 2020-05-05 stsp char *path = NULL;
845 0208f208 2020-05-05 stsp
846 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
847 0208f208 2020-05-05 stsp if (path == NULL)
848 6b37f13a 2023-01-10 mark return got_error_from_errno("strdup");
849 0208f208 2020-05-05 stsp
850 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
851 0208f208 2020-05-05 stsp if (change == NULL) {
852 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
853 0208f208 2020-05-05 stsp goto done;
854 0208f208 2020-05-05 stsp }
855 0208f208 2020-05-05 stsp
856 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
857 0208f208 2020-05-05 stsp if (id1 == NULL)
858 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
859 0208f208 2020-05-05 stsp else if (id2 == NULL)
860 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
861 0208f208 2020-05-05 stsp else {
862 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
863 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
864 0208f208 2020-05-05 stsp else if (mode1 != mode2)
865 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
866 0208f208 2020-05-05 stsp }
867 0208f208 2020-05-05 stsp
868 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
869 0208f208 2020-05-05 stsp done:
870 0208f208 2020-05-05 stsp if (err) {
871 0208f208 2020-05-05 stsp free(path);
872 0208f208 2020-05-05 stsp free(change);
873 0208f208 2020-05-05 stsp }
874 0208f208 2020-05-05 stsp return err;
875 474b4f94 2017-11-30 stsp }
876 474b4f94 2017-11-30 stsp
877 474b4f94 2017-11-30 stsp const struct got_error *
878 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
879 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
880 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
881 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
882 b72706c3 2022-06-01 stsp int diff_content)
883 474b4f94 2017-11-30 stsp {
884 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
885 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
886 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
887 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
888 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
889 474b4f94 2017-11-30 stsp
890 883f0469 2018-06-23 stsp if (tree1) {
891 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
892 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
893 f6861a81 2018-09-13 stsp te1->name) == -1)
894 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
895 883f0469 2018-06-23 stsp }
896 883f0469 2018-06-23 stsp if (tree2) {
897 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
898 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
899 f6861a81 2018-09-13 stsp te2->name) == -1)
900 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
901 883f0469 2018-06-23 stsp }
902 474b4f94 2017-11-30 stsp
903 474b4f94 2017-11-30 stsp do {
904 474b4f94 2017-11-30 stsp if (te1) {
905 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
906 f6861a81 2018-09-13 stsp if (tree2)
907 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
908 1de5e065 2019-06-01 stsp te1->name);
909 f6861a81 2018-09-13 stsp if (te) {
910 f6861a81 2018-09-13 stsp free(l2);
911 f6861a81 2018-09-13 stsp l2 = NULL;
912 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
913 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
914 230a42bd 2019-05-11 jcs return
915 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
916 f6861a81 2018-09-13 stsp }
917 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
918 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
919 474b4f94 2017-11-30 stsp if (err)
920 474b4f94 2017-11-30 stsp break;
921 474b4f94 2017-11-30 stsp }
922 474b4f94 2017-11-30 stsp
923 474b4f94 2017-11-30 stsp if (te2) {
924 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
925 f6861a81 2018-09-13 stsp if (tree1)
926 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
927 1de5e065 2019-06-01 stsp te2->name);
928 d6ce02f1 2018-11-17 stsp free(l2);
929 d6ce02f1 2018-11-17 stsp if (te) {
930 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
931 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
932 230a42bd 2019-05-11 jcs return
933 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
934 d6ce02f1 2018-11-17 stsp } else {
935 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
936 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
937 230a42bd 2019-05-11 jcs return
938 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
939 d6ce02f1 2018-11-17 stsp }
940 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
941 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
942 474b4f94 2017-11-30 stsp if (err)
943 474b4f94 2017-11-30 stsp break;
944 474b4f94 2017-11-30 stsp }
945 474b4f94 2017-11-30 stsp
946 f6861a81 2018-09-13 stsp free(l1);
947 f6861a81 2018-09-13 stsp l1 = NULL;
948 f6861a81 2018-09-13 stsp if (te1) {
949 56e0773d 2019-11-28 stsp tidx1++;
950 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
951 f6861a81 2018-09-13 stsp if (te1 &&
952 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
953 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
954 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
955 f6861a81 2018-09-13 stsp }
956 f6861a81 2018-09-13 stsp free(l2);
957 f6861a81 2018-09-13 stsp l2 = NULL;
958 f6861a81 2018-09-13 stsp if (te2) {
959 56e0773d 2019-11-28 stsp tidx2++;
960 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
961 f6861a81 2018-09-13 stsp if (te2 &&
962 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
963 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
964 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
965 f6861a81 2018-09-13 stsp }
966 474b4f94 2017-11-30 stsp } while (te1 || te2);
967 11528a82 2018-05-19 stsp
968 11528a82 2018-05-19 stsp return err;
969 11528a82 2018-05-19 stsp }
970 11528a82 2018-05-19 stsp
971 11528a82 2018-05-19 stsp const struct got_error *
972 c7d5c43c 2022-08-04 mark got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
973 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
974 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
975 4b752015 2022-06-30 stsp const char *label1, const char *label2,
976 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
977 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
978 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
979 11528a82 2018-05-19 stsp {
980 11528a82 2018-05-19 stsp const struct got_error *err;
981 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
982 b74c7625 2018-05-20 stsp
983 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
984 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
985 eb81bc23 2022-06-28 tracey
986 15a94983 2018-12-23 stsp if (id1) {
987 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
988 cd0acaa7 2018-05-20 stsp if (err)
989 cd0acaa7 2018-05-20 stsp goto done;
990 cd0acaa7 2018-05-20 stsp }
991 15a94983 2018-12-23 stsp if (id2) {
992 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
993 cd0acaa7 2018-05-20 stsp if (err)
994 cd0acaa7 2018-05-20 stsp goto done;
995 cd0acaa7 2018-05-20 stsp }
996 c7d5c43c 2022-08-04 mark err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
997 c7d5c43c 2022-08-04 mark diff_algo, diff_context, ignore_whitespace, force_text_diff,
998 a76e88e5 2023-01-10 mark show_diffstat, ds, outfile);
999 67b631c9 2021-10-10 stsp done:
1000 67b631c9 2021-10-10 stsp if (blob1)
1001 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1002 67b631c9 2021-10-10 stsp if (blob2)
1003 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1004 67b631c9 2021-10-10 stsp return err;
1005 67b631c9 2021-10-10 stsp }
1006 67b631c9 2021-10-10 stsp
1007 67b631c9 2021-10-10 stsp static const struct got_error *
1008 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1009 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1010 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1011 67b631c9 2021-10-10 stsp {
1012 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1013 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
1014 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1015 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1016 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1017 67b631c9 2021-10-10 stsp
1018 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
1019 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1020 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
1021 67b631c9 2021-10-10 stsp
1022 67b631c9 2021-10-10 stsp free(id1);
1023 67b631c9 2021-10-10 stsp id1 = NULL;
1024 67b631c9 2021-10-10 stsp free(id2);
1025 67b631c9 2021-10-10 stsp id2 = NULL;
1026 67b631c9 2021-10-10 stsp if (subtree1) {
1027 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1028 67b631c9 2021-10-10 stsp subtree1 = NULL;
1029 67b631c9 2021-10-10 stsp }
1030 67b631c9 2021-10-10 stsp if (subtree2) {
1031 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1032 67b631c9 2021-10-10 stsp subtree2 = NULL;
1033 67b631c9 2021-10-10 stsp }
1034 67b631c9 2021-10-10 stsp if (blob1) {
1035 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1036 67b631c9 2021-10-10 stsp blob1 = NULL;
1037 67b631c9 2021-10-10 stsp }
1038 67b631c9 2021-10-10 stsp if (blob2) {
1039 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1040 67b631c9 2021-10-10 stsp blob2 = NULL;
1041 67b631c9 2021-10-10 stsp }
1042 67b631c9 2021-10-10 stsp
1043 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1044 67b631c9 2021-10-10 stsp pe->path);
1045 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1046 67b631c9 2021-10-10 stsp goto done;
1047 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1048 67b631c9 2021-10-10 stsp pe->path);
1049 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1050 67b631c9 2021-10-10 stsp goto done;
1051 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
1052 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1053 67b631c9 2021-10-10 stsp goto done;
1054 67b631c9 2021-10-10 stsp }
1055 67b631c9 2021-10-10 stsp if (id1) {
1056 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
1057 67b631c9 2021-10-10 stsp if (err)
1058 67b631c9 2021-10-10 stsp goto done;
1059 67b631c9 2021-10-10 stsp }
1060 67b631c9 2021-10-10 stsp if (id2) {
1061 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
1062 67b631c9 2021-10-10 stsp if (err)
1063 67b631c9 2021-10-10 stsp goto done;
1064 67b631c9 2021-10-10 stsp }
1065 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
1066 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
1067 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1068 67b631c9 2021-10-10 stsp goto done;
1069 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
1070 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1071 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1072 67b631c9 2021-10-10 stsp goto done;
1073 67b631c9 2021-10-10 stsp }
1074 67b631c9 2021-10-10 stsp
1075 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
1076 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
1077 67b631c9 2021-10-10 stsp if (id1) {
1078 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
1079 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
1080 67b631c9 2021-10-10 stsp if (err)
1081 67b631c9 2021-10-10 stsp goto done;
1082 67b631c9 2021-10-10 stsp }
1083 67b631c9 2021-10-10 stsp if (id2) {
1084 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
1085 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
1086 67b631c9 2021-10-10 stsp if (err)
1087 67b631c9 2021-10-10 stsp goto done;
1088 67b631c9 2021-10-10 stsp }
1089 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1090 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1091 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1092 67b631c9 2021-10-10 stsp mode1, mode2, repo);
1093 67b631c9 2021-10-10 stsp if (err)
1094 67b631c9 2021-10-10 stsp goto done;
1095 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
1096 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
1097 67b631c9 2021-10-10 stsp if (id1) {
1098 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
1099 67b631c9 2021-10-10 stsp id1);
1100 67b631c9 2021-10-10 stsp if (err)
1101 67b631c9 2021-10-10 stsp goto done;
1102 67b631c9 2021-10-10 stsp }
1103 67b631c9 2021-10-10 stsp if (id2) {
1104 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
1105 67b631c9 2021-10-10 stsp id2);
1106 67b631c9 2021-10-10 stsp if (err)
1107 67b631c9 2021-10-10 stsp goto done;
1108 67b631c9 2021-10-10 stsp }
1109 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
1110 f9d37699 2022-06-28 stsp fd1, fd2,
1111 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1112 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1113 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
1114 67b631c9 2021-10-10 stsp if (err)
1115 67b631c9 2021-10-10 stsp goto done;
1116 67b631c9 2021-10-10 stsp } else {
1117 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1118 67b631c9 2021-10-10 stsp goto done;
1119 67b631c9 2021-10-10 stsp }
1120 67b631c9 2021-10-10 stsp }
1121 11528a82 2018-05-19 stsp done:
1122 67b631c9 2021-10-10 stsp free(id1);
1123 67b631c9 2021-10-10 stsp free(id2);
1124 67b631c9 2021-10-10 stsp if (subtree1)
1125 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1126 67b631c9 2021-10-10 stsp if (subtree2)
1127 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1128 11528a82 2018-05-19 stsp if (blob1)
1129 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1130 11528a82 2018-05-19 stsp if (blob2)
1131 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1132 474b4f94 2017-11-30 stsp return err;
1133 474b4f94 2017-11-30 stsp }
1134 11528a82 2018-05-19 stsp
1135 8469d821 2022-06-25 stsp static const struct got_error *
1136 c7d5c43c 2022-08-04 mark show_object_id(struct got_diff_line **lines, size_t *nlines,
1137 c7d5c43c 2022-08-04 mark const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1138 8469d821 2022-06-25 stsp {
1139 8469d821 2022-06-25 stsp const struct got_error *err;
1140 8469d821 2022-06-25 stsp int n;
1141 8469d821 2022-06-25 stsp off_t outoff = 0;
1142 8469d821 2022-06-25 stsp
1143 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1144 36e83e5e 2022-08-16 op if (n < 0)
1145 36e83e5e 2022-08-16 op return got_error_from_errno("fprintf");
1146 36e83e5e 2022-08-16 op
1147 c7d5c43c 2022-08-04 mark if (lines != NULL && *lines != NULL) {
1148 8469d821 2022-06-25 stsp if (*nlines == 0) {
1149 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0,
1150 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1151 8469d821 2022-06-25 stsp if (err)
1152 8469d821 2022-06-25 stsp return err;
1153 8469d821 2022-06-25 stsp } else
1154 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
1155 8469d821 2022-06-25 stsp
1156 8469d821 2022-06-25 stsp outoff += n;
1157 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
1158 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1159 8469d821 2022-06-25 stsp if (err)
1160 8469d821 2022-06-25 stsp return err;
1161 8469d821 2022-06-25 stsp }
1162 8469d821 2022-06-25 stsp
1163 8469d821 2022-06-25 stsp return NULL;
1164 8469d821 2022-06-25 stsp }
1165 8469d821 2022-06-25 stsp
1166 8469d821 2022-06-25 stsp static const struct got_error *
1167 c7d5c43c 2022-08-04 mark diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1168 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1169 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1170 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1171 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1172 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *dsa,
1173 4b752015 2022-06-30 stsp struct got_repository *repo, FILE *outfile,
1174 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
1175 11528a82 2018-05-19 stsp {
1176 11528a82 2018-05-19 stsp const struct got_error *err;
1177 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1178 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1179 c7d5c43c 2022-08-04 mark int want_linemeta = (lines != NULL && *lines != NULL);
1180 11528a82 2018-05-19 stsp
1181 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1182 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1183 b74c7625 2018-05-20 stsp
1184 15a94983 2018-12-23 stsp if (id1) {
1185 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1186 cd0acaa7 2018-05-20 stsp if (err)
1187 cd0acaa7 2018-05-20 stsp goto done;
1188 cd0acaa7 2018-05-20 stsp }
1189 15a94983 2018-12-23 stsp if (id2) {
1190 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1191 cd0acaa7 2018-05-20 stsp if (err)
1192 cd0acaa7 2018-05-20 stsp goto done;
1193 cd0acaa7 2018-05-20 stsp }
1194 67b631c9 2021-10-10 stsp
1195 4b752015 2022-06-30 stsp arg.diff_algo = diff_algo;
1196 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1197 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1198 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1199 a76e88e5 2023-01-10 mark arg.show_diffstat = show_diffstat;
1200 a76e88e5 2023-01-10 mark arg.diffstat = dsa;
1201 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1202 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1203 c7d5c43c 2022-08-04 mark arg.lines = *lines;
1204 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1205 fe621944 2020-11-10 stsp } else {
1206 c7d5c43c 2022-08-04 mark arg.lines = NULL;
1207 fe621944 2020-11-10 stsp arg.nlines = 0;
1208 fe621944 2020-11-10 stsp }
1209 a76e88e5 2023-01-10 mark if (paths == NULL || TAILQ_EMPTY(paths))
1210 a76e88e5 2023-01-10 mark err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1211 a76e88e5 2023-01-10 mark label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1212 a76e88e5 2023-01-10 mark else
1213 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1214 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1215 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1216 c7d5c43c 2022-08-04 mark *lines = arg.lines; /* was likely re-allocated */
1217 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1218 fe621944 2020-11-10 stsp }
1219 11528a82 2018-05-19 stsp done:
1220 11528a82 2018-05-19 stsp if (tree1)
1221 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1222 11528a82 2018-05-19 stsp if (tree2)
1223 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1224 11528a82 2018-05-19 stsp return err;
1225 11528a82 2018-05-19 stsp }
1226 11528a82 2018-05-19 stsp
1227 11528a82 2018-05-19 stsp const struct got_error *
1228 c7d5c43c 2022-08-04 mark got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1229 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1230 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1231 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1232 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1233 a76e88e5 2023-01-10 mark int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1234 a76e88e5 2023-01-10 mark struct got_repository *repo, FILE *outfile)
1235 8469d821 2022-06-25 stsp {
1236 8469d821 2022-06-25 stsp const struct got_error *err;
1237 8469d821 2022-06-25 stsp char *idstr = NULL;
1238 8469d821 2022-06-25 stsp
1239 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1240 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1241 8469d821 2022-06-25 stsp
1242 8469d821 2022-06-25 stsp if (id1) {
1243 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1244 8469d821 2022-06-25 stsp if (err)
1245 8469d821 2022-06-25 stsp goto done;
1246 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1247 8469d821 2022-06-25 stsp if (err)
1248 8469d821 2022-06-25 stsp goto done;
1249 8469d821 2022-06-25 stsp free(idstr);
1250 8469d821 2022-06-25 stsp idstr = NULL;
1251 8469d821 2022-06-25 stsp } else {
1252 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1253 c7d5c43c 2022-08-04 mark outfile);
1254 8469d821 2022-06-25 stsp if (err)
1255 8469d821 2022-06-25 stsp goto done;
1256 8469d821 2022-06-25 stsp }
1257 8469d821 2022-06-25 stsp
1258 8469d821 2022-06-25 stsp if (id2) {
1259 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1260 8469d821 2022-06-25 stsp if (err)
1261 8469d821 2022-06-25 stsp goto done;
1262 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1263 8469d821 2022-06-25 stsp if (err)
1264 8469d821 2022-06-25 stsp goto done;
1265 8469d821 2022-06-25 stsp free(idstr);
1266 8469d821 2022-06-25 stsp idstr = NULL;
1267 8469d821 2022-06-25 stsp } else {
1268 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1269 c7d5c43c 2022-08-04 mark outfile);
1270 8469d821 2022-06-25 stsp if (err)
1271 8469d821 2022-06-25 stsp goto done;
1272 8469d821 2022-06-25 stsp }
1273 8469d821 2022-06-25 stsp
1274 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1275 c7d5c43c 2022-08-04 mark paths, label1, label2, diff_context, ignore_whitespace,
1276 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1277 8469d821 2022-06-25 stsp done:
1278 8469d821 2022-06-25 stsp free(idstr);
1279 8469d821 2022-06-25 stsp return err;
1280 8469d821 2022-06-25 stsp }
1281 8469d821 2022-06-25 stsp
1282 8469d821 2022-06-25 stsp const struct got_error *
1283 c7d5c43c 2022-08-04 mark got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1284 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1285 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1286 4b752015 2022-06-30 stsp struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1287 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1288 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *dsa,
1289 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1290 11528a82 2018-05-19 stsp {
1291 11528a82 2018-05-19 stsp const struct got_error *err;
1292 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1293 8469d821 2022-06-25 stsp char *idstr = NULL;
1294 11528a82 2018-05-19 stsp
1295 15a94983 2018-12-23 stsp if (id2 == NULL)
1296 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1297 b74c7625 2018-05-20 stsp
1298 15a94983 2018-12-23 stsp if (id1) {
1299 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1300 cd0acaa7 2018-05-20 stsp if (err)
1301 cd0acaa7 2018-05-20 stsp goto done;
1302 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1303 8469d821 2022-06-25 stsp if (err)
1304 8469d821 2022-06-25 stsp goto done;
1305 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', idstr,
1306 c7d5c43c 2022-08-04 mark outfile);
1307 8469d821 2022-06-25 stsp if (err)
1308 8469d821 2022-06-25 stsp goto done;
1309 8469d821 2022-06-25 stsp free(idstr);
1310 8469d821 2022-06-25 stsp idstr = NULL;
1311 8469d821 2022-06-25 stsp } else {
1312 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1313 c7d5c43c 2022-08-04 mark outfile);
1314 8469d821 2022-06-25 stsp if (err)
1315 8469d821 2022-06-25 stsp goto done;
1316 cd0acaa7 2018-05-20 stsp }
1317 bacc9935 2018-05-20 stsp
1318 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1319 9b697879 2018-05-20 stsp if (err)
1320 9b697879 2018-05-20 stsp goto done;
1321 9b697879 2018-05-20 stsp
1322 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1323 8469d821 2022-06-25 stsp if (err)
1324 8469d821 2022-06-25 stsp goto done;
1325 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1326 8469d821 2022-06-25 stsp if (err)
1327 8469d821 2022-06-25 stsp goto done;
1328 8469d821 2022-06-25 stsp
1329 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1330 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1331 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1332 a76e88e5 2023-01-10 mark diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1333 a76e88e5 2023-01-10 mark dsa, repo, outfile, diff_algo);
1334 11528a82 2018-05-19 stsp done:
1335 11528a82 2018-05-19 stsp if (commit1)
1336 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1337 11528a82 2018-05-19 stsp if (commit2)
1338 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1339 8469d821 2022-06-25 stsp free(idstr);
1340 11528a82 2018-05-19 stsp return err;
1341 11528a82 2018-05-19 stsp }
1342 dc424a06 2019-08-07 stsp
1343 dc424a06 2019-08-07 stsp const struct got_error *
1344 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1345 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1346 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1347 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1348 dc424a06 2019-08-07 stsp {
1349 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1350 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1351 dc424a06 2019-08-07 stsp
1352 fe621944 2020-11-10 stsp if (resultp)
1353 fe621944 2020-11-10 stsp *resultp = NULL;
1354 dc424a06 2019-08-07 stsp
1355 dc424a06 2019-08-07 stsp if (outfile) {
1356 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1357 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1358 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1359 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1360 dc424a06 2019-08-07 stsp }
1361 fe621944 2020-11-10 stsp
1362 4b752015 2022-06-30 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1363 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1364 fe621944 2020-11-10 stsp if (err)
1365 fe621944 2020-11-10 stsp goto done;
1366 fe621944 2020-11-10 stsp
1367 fe621944 2020-11-10 stsp if (outfile) {
1368 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1369 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1370 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1371 dc424a06 2019-08-07 stsp if (err)
1372 dc424a06 2019-08-07 stsp goto done;
1373 dc424a06 2019-08-07 stsp }
1374 fe621944 2020-11-10 stsp
1375 dc424a06 2019-08-07 stsp done:
1376 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1377 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1378 fe621944 2020-11-10 stsp else if (diffreg_result) {
1379 fe621944 2020-11-10 stsp const struct got_error *free_err;
1380 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1381 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1382 fe621944 2020-11-10 stsp err = free_err;
1383 dc424a06 2019-08-07 stsp }
1384 fe621944 2020-11-10 stsp
1385 dc424a06 2019-08-07 stsp return err;
1386 dc424a06 2019-08-07 stsp }