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 a76e88e5 2023-01-10 mark return got_error_from_errno("malloc");
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 a76e88e5 2023-01-10 mark
249 a76e88e5 2023-01-10 mark if (label1 == NULL && label2 == NULL) {
250 a76e88e5 2023-01-10 mark /* diffstat of blobs, show hash instead of path */
251 a76e88e5 2023-01-10 mark if (asprintf(&path, "%.10s -> %.10s",
252 a76e88e5 2023-01-10 mark idstr1, idstr2) == -1) {
253 a76e88e5 2023-01-10 mark err = got_error_from_errno("asprintf");
254 a76e88e5 2023-01-10 mark goto done;
255 a76e88e5 2023-01-10 mark }
256 a76e88e5 2023-01-10 mark } else {
257 a76e88e5 2023-01-10 mark path = strdup(label2 ? label2 : label1);
258 a76e88e5 2023-01-10 mark if (path == NULL) {
259 a76e88e5 2023-01-10 mark err = got_error_from_errno("malloc");
260 a76e88e5 2023-01-10 mark goto done;
261 a76e88e5 2023-01-10 mark }
262 a76e88e5 2023-01-10 mark }
263 a76e88e5 2023-01-10 mark
264 a76e88e5 2023-01-10 mark /*
265 a76e88e5 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
266 a76e88e5 2023-01-10 mark * content change, there'll be no diffstat, and if there
267 a76e88e5 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
268 a76e88e5 2023-01-10 mark */
269 a76e88e5 2023-01-10 mark if (blob1 == NULL)
270 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
271 a76e88e5 2023-01-10 mark else if (blob2 == NULL)
272 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
273 a76e88e5 2023-01-10 mark else
274 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
275 a76e88e5 2023-01-10 mark
276 a76e88e5 2023-01-10 mark err = get_diffstat(ds, path, result->result, force_text_diff,
277 a76e88e5 2023-01-10 mark status);
278 a76e88e5 2023-01-10 mark if (err) {
279 a76e88e5 2023-01-10 mark free(path);
280 a76e88e5 2023-01-10 mark goto done;
281 a76e88e5 2023-01-10 mark }
282 a76e88e5 2023-01-10 mark }
283 a76e88e5 2023-01-10 mark
284 fe621944 2020-11-10 stsp if (outfile) {
285 c7d5c43c 2022-08-04 mark err = got_diffreg_output(lines, nlines, result,
286 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
287 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
288 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
289 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
290 fe621944 2020-11-10 stsp if (err)
291 fe621944 2020-11-10 stsp goto done;
292 fe621944 2020-11-10 stsp }
293 fe621944 2020-11-10 stsp
294 3846622f 2023-01-07 mark done:
295 fe621944 2020-11-10 stsp if (resultp && err == NULL)
296 fe621944 2020-11-10 stsp *resultp = result;
297 3846622f 2023-01-07 mark else if (result) {
298 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
299 fe621944 2020-11-10 stsp if (free_err && err == NULL)
300 fe621944 2020-11-10 stsp err = free_err;
301 fe621944 2020-11-10 stsp }
302 3846622f 2023-01-07 mark
303 7d283eee 2017-11-29 stsp return err;
304 aaa13589 2019-06-01 stsp }
305 aaa13589 2019-06-01 stsp
306 aaa13589 2019-06-01 stsp const struct got_error *
307 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
308 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
309 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
310 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
311 b72706c3 2022-06-01 stsp struct got_repository *repo)
312 aaa13589 2019-06-01 stsp {
313 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
314 aaa13589 2019-06-01 stsp
315 c7d5c43c 2022-08-04 mark return diff_blobs(&a->lines, &a->nlines, NULL,
316 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
317 a76e88e5 2023-01-10 mark a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
318 a76e88e5 2023-01-10 mark a->diffstat, a->outfile, a->diff_algo);
319 7d283eee 2017-11-29 stsp }
320 474b4f94 2017-11-30 stsp
321 404c43c4 2018-06-21 stsp const struct got_error *
322 c7d5c43c 2022-08-04 mark got_diff_blob(struct got_diff_line **lines, size_t*nlines,
323 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
324 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
325 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
326 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
327 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
328 404c43c4 2018-06-21 stsp {
329 c7d5c43c 2022-08-04 mark return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
330 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
331 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, ds, outfile, diff_algo);
332 404c43c4 2018-06-21 stsp }
333 404c43c4 2018-06-21 stsp
334 7f1f93af 2019-08-06 stsp static const struct got_error *
335 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
336 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
337 c87842d5 2022-09-23 mark FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
338 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
339 a76e88e5 2023-01-10 mark int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
340 a76e88e5 2023-01-10 mark FILE *outfile)
341 b72f483a 2019-02-05 stsp {
342 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
343 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
344 58e31a80 2022-06-27 op const char *idstr1 = NULL;
345 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
346 b72f483a 2019-02-05 stsp
347 fe621944 2020-11-10 stsp if (resultp)
348 fe621944 2020-11-10 stsp *resultp = NULL;
349 7f1f93af 2019-08-06 stsp
350 b72706c3 2022-06-01 stsp if (blob1)
351 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
352 b72706c3 2022-06-01 stsp else
353 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
354 b72f483a 2019-02-05 stsp
355 7f1f93af 2019-08-06 stsp if (outfile) {
356 c87842d5 2022-09-23 mark char *mode = NULL;
357 c87842d5 2022-09-23 mark
358 c87842d5 2022-09-23 mark /* display file mode for new added files only */
359 c87842d5 2022-09-23 mark if (f2_exists && blob1 == NULL) {
360 c87842d5 2022-09-23 mark int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
361 c87842d5 2022-09-23 mark
362 c87842d5 2022-09-23 mark if (S_ISLNK(sb2->st_mode))
363 c87842d5 2022-09-23 mark mmask = S_IFLNK;
364 c87842d5 2022-09-23 mark if (asprintf(&mode, " (mode %o)",
365 c87842d5 2022-09-23 mark sb2->st_mode & mmask) == -1)
366 c87842d5 2022-09-23 mark return got_error_from_errno("asprintf");
367 c87842d5 2022-09-23 mark }
368 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
369 c87842d5 2022-09-23 mark fprintf(outfile, "file + %s%s\n",
370 c87842d5 2022-09-23 mark f2_exists ? label2 : "/dev/null", mode ? mode : "");
371 c87842d5 2022-09-23 mark free(mode);
372 7f1f93af 2019-08-06 stsp }
373 fe621944 2020-11-10 stsp
374 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
375 4b752015 2022-06-30 stsp force_text_diff);
376 fe621944 2020-11-10 stsp if (err)
377 fe621944 2020-11-10 stsp goto done;
378 fe621944 2020-11-10 stsp
379 fe621944 2020-11-10 stsp if (outfile) {
380 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
381 49d4a017 2022-06-30 stsp blob1 != NULL, f2_exists,
382 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
383 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
384 1cb46f00 2020-11-21 stsp diff_context, outfile);
385 7f1f93af 2019-08-06 stsp if (err)
386 a76e88e5 2023-01-10 mark goto done;
387 a76e88e5 2023-01-10 mark }
388 a76e88e5 2023-01-10 mark
389 a76e88e5 2023-01-10 mark if (show_diffstat) {
390 a76e88e5 2023-01-10 mark char *path = NULL;
391 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
392 a76e88e5 2023-01-10 mark
393 a76e88e5 2023-01-10 mark path = strdup(label2 ? label2 : label1);
394 a76e88e5 2023-01-10 mark if (path == NULL) {
395 a76e88e5 2023-01-10 mark err = got_error_from_errno("malloc");
396 a76e88e5 2023-01-10 mark goto done;
397 a76e88e5 2023-01-10 mark }
398 a76e88e5 2023-01-10 mark
399 a76e88e5 2023-01-10 mark /*
400 a76e88e5 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
401 a76e88e5 2023-01-10 mark * content change, there'll be no diffstat, and if there
402 a76e88e5 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
403 a76e88e5 2023-01-10 mark */
404 a76e88e5 2023-01-10 mark if (blob1 == NULL)
405 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
406 a76e88e5 2023-01-10 mark else if (!f2_exists)
407 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
408 a76e88e5 2023-01-10 mark else
409 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
410 a76e88e5 2023-01-10 mark
411 a76e88e5 2023-01-10 mark err = get_diffstat(ds, path, result->result, force_text_diff,
412 a76e88e5 2023-01-10 mark status);
413 a76e88e5 2023-01-10 mark if (err) {
414 a76e88e5 2023-01-10 mark free(path);
415 fe621944 2020-11-10 stsp goto done;
416 a76e88e5 2023-01-10 mark }
417 7f1f93af 2019-08-06 stsp }
418 fe621944 2020-11-10 stsp
419 3846622f 2023-01-07 mark done:
420 fe621944 2020-11-10 stsp if (resultp && err == NULL)
421 fe621944 2020-11-10 stsp *resultp = result;
422 fe621944 2020-11-10 stsp else if (result) {
423 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
424 fe621944 2020-11-10 stsp if (free_err && err == NULL)
425 fe621944 2020-11-10 stsp err = free_err;
426 fe621944 2020-11-10 stsp }
427 b72f483a 2019-02-05 stsp return err;
428 7f1f93af 2019-08-06 stsp }
429 7f1f93af 2019-08-06 stsp
430 7f1f93af 2019-08-06 stsp const struct got_error *
431 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
432 c87842d5 2022-09-23 mark const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
433 4b752015 2022-06-30 stsp const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
434 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
435 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
436 7f1f93af 2019-08-06 stsp {
437 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
438 c87842d5 2022-09-23 mark sb2, label2, diff_algo, diff_context, ignore_whitespace,
439 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, ds, outfile);
440 474b4f94 2017-11-30 stsp }
441 474b4f94 2017-11-30 stsp
442 474b4f94 2017-11-30 stsp static const struct got_error *
443 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
444 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
445 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
446 474b4f94 2017-11-30 stsp {
447 4e22badc 2017-11-30 stsp const struct got_error *err;
448 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
449 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
450 4e22badc 2017-11-30 stsp
451 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
452 4e22badc 2017-11-30 stsp if (err)
453 4e22badc 2017-11-30 stsp return err;
454 4e22badc 2017-11-30 stsp
455 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
456 2acfca77 2018-04-01 stsp if (err)
457 2acfca77 2018-04-01 stsp goto done;
458 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
459 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
460 2acfca77 2018-04-01 stsp done:
461 2acfca77 2018-04-01 stsp got_object_close(obj);
462 2acfca77 2018-04-01 stsp if (blob)
463 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
464 2acfca77 2018-04-01 stsp return err;
465 474b4f94 2017-11-30 stsp }
466 474b4f94 2017-11-30 stsp
467 474b4f94 2017-11-30 stsp static const struct got_error *
468 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
469 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
470 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
471 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
472 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
473 474b4f94 2017-11-30 stsp {
474 6a213ccb 2017-11-30 stsp const struct got_error *err;
475 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
476 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
477 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
478 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
479 6a213ccb 2017-11-30 stsp
480 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
481 6a213ccb 2017-11-30 stsp if (err)
482 730a8aa0 2018-04-24 stsp return err;
483 eb81bc23 2022-06-28 tracey
484 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
485 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
486 6a213ccb 2017-11-30 stsp goto done;
487 6a213ccb 2017-11-30 stsp }
488 6a213ccb 2017-11-30 stsp
489 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
490 730a8aa0 2018-04-24 stsp if (err)
491 6a213ccb 2017-11-30 stsp goto done;
492 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
493 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 6a213ccb 2017-11-30 stsp goto done;
495 6a213ccb 2017-11-30 stsp }
496 6a213ccb 2017-11-30 stsp
497 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
498 2acfca77 2018-04-01 stsp if (err)
499 6a213ccb 2017-11-30 stsp goto done;
500 6a213ccb 2017-11-30 stsp
501 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
502 2acfca77 2018-04-01 stsp if (err)
503 6a213ccb 2017-11-30 stsp goto done;
504 6a213ccb 2017-11-30 stsp
505 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
506 b72706c3 2022-06-01 stsp mode1, mode2, repo);
507 6a213ccb 2017-11-30 stsp done:
508 a3e2cbea 2017-12-01 stsp if (obj1)
509 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
510 a3e2cbea 2017-12-01 stsp if (obj2)
511 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
512 a3e2cbea 2017-12-01 stsp if (blob1)
513 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
514 a3e2cbea 2017-12-01 stsp if (blob2)
515 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
516 6a213ccb 2017-11-30 stsp return err;
517 474b4f94 2017-11-30 stsp }
518 474b4f94 2017-11-30 stsp
519 474b4f94 2017-11-30 stsp static const struct got_error *
520 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
521 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
522 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg)
523 474b4f94 2017-11-30 stsp {
524 365fb436 2017-11-30 stsp const struct got_error *err;
525 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
526 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
527 365fb436 2017-11-30 stsp
528 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
529 365fb436 2017-11-30 stsp if (err)
530 365fb436 2017-11-30 stsp return err;
531 365fb436 2017-11-30 stsp
532 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
533 2acfca77 2018-04-01 stsp if (err)
534 2acfca77 2018-04-01 stsp goto done;
535 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
536 b72706c3 2022-06-01 stsp mode, 0, repo);
537 2acfca77 2018-04-01 stsp done:
538 2acfca77 2018-04-01 stsp got_object_close(obj);
539 2acfca77 2018-04-01 stsp if (blob)
540 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
541 2acfca77 2018-04-01 stsp return err;
542 474b4f94 2017-11-30 stsp }
543 474b4f94 2017-11-30 stsp
544 474b4f94 2017-11-30 stsp static const struct got_error *
545 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
546 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
547 49d4a017 2022-06-30 stsp void *cb_arg, int diff_content)
548 474b4f94 2017-11-30 stsp {
549 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
550 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
551 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
552 9c70d4c3 2017-11-30 stsp
553 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
554 9c70d4c3 2017-11-30 stsp if (err)
555 9c70d4c3 2017-11-30 stsp goto done;
556 9c70d4c3 2017-11-30 stsp
557 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
558 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
559 9c70d4c3 2017-11-30 stsp goto done;
560 9c70d4c3 2017-11-30 stsp }
561 9c70d4c3 2017-11-30 stsp
562 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
563 9c70d4c3 2017-11-30 stsp if (err)
564 9c70d4c3 2017-11-30 stsp goto done;
565 9c70d4c3 2017-11-30 stsp
566 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
567 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
568 9c70d4c3 2017-11-30 stsp done:
569 9c70d4c3 2017-11-30 stsp if (tree)
570 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
571 9c70d4c3 2017-11-30 stsp if (treeobj)
572 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
573 9c70d4c3 2017-11-30 stsp return err;
574 474b4f94 2017-11-30 stsp }
575 474b4f94 2017-11-30 stsp
576 474b4f94 2017-11-30 stsp static const struct got_error *
577 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
578 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
579 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
580 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
581 b72706c3 2022-06-01 stsp int diff_content)
582 474b4f94 2017-11-30 stsp {
583 f6861a81 2018-09-13 stsp const struct got_error *err;
584 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
585 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
586 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
587 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
588 789689b5 2017-11-30 stsp
589 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
590 789689b5 2017-11-30 stsp if (err)
591 789689b5 2017-11-30 stsp goto done;
592 789689b5 2017-11-30 stsp
593 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
594 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
595 789689b5 2017-11-30 stsp goto done;
596 789689b5 2017-11-30 stsp }
597 789689b5 2017-11-30 stsp
598 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
599 789689b5 2017-11-30 stsp if (err)
600 789689b5 2017-11-30 stsp goto done;
601 789689b5 2017-11-30 stsp
602 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
603 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
604 789689b5 2017-11-30 stsp goto done;
605 789689b5 2017-11-30 stsp }
606 789689b5 2017-11-30 stsp
607 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
608 789689b5 2017-11-30 stsp if (err)
609 789689b5 2017-11-30 stsp goto done;
610 789689b5 2017-11-30 stsp
611 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
612 789689b5 2017-11-30 stsp if (err)
613 789689b5 2017-11-30 stsp goto done;
614 789689b5 2017-11-30 stsp
615 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
616 f9d37699 2022-06-28 stsp label1, label2, repo, cb, cb_arg, diff_content);
617 789689b5 2017-11-30 stsp
618 789689b5 2017-11-30 stsp done:
619 789689b5 2017-11-30 stsp if (tree1)
620 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
621 789689b5 2017-11-30 stsp if (tree2)
622 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
623 789689b5 2017-11-30 stsp if (treeobj1)
624 789689b5 2017-11-30 stsp got_object_close(treeobj1);
625 789689b5 2017-11-30 stsp if (treeobj2)
626 789689b5 2017-11-30 stsp got_object_close(treeobj2);
627 789689b5 2017-11-30 stsp return err;
628 474b4f94 2017-11-30 stsp }
629 474b4f94 2017-11-30 stsp
630 474b4f94 2017-11-30 stsp static const struct got_error *
631 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
632 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
633 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
634 474b4f94 2017-11-30 stsp {
635 f6861a81 2018-09-13 stsp const struct got_error *err;
636 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
637 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
638 2c56f2ce 2017-11-30 stsp
639 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
640 2c56f2ce 2017-11-30 stsp if (err)
641 2c56f2ce 2017-11-30 stsp goto done;
642 2c56f2ce 2017-11-30 stsp
643 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
644 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
645 2c56f2ce 2017-11-30 stsp goto done;
646 2c56f2ce 2017-11-30 stsp }
647 2c56f2ce 2017-11-30 stsp
648 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
649 2c56f2ce 2017-11-30 stsp if (err)
650 2c56f2ce 2017-11-30 stsp goto done;
651 2c56f2ce 2017-11-30 stsp
652 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
653 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
654 2c56f2ce 2017-11-30 stsp done:
655 2c56f2ce 2017-11-30 stsp if (tree)
656 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
657 2c56f2ce 2017-11-30 stsp if (treeobj)
658 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
659 2c56f2ce 2017-11-30 stsp return err;
660 474b4f94 2017-11-30 stsp }
661 474b4f94 2017-11-30 stsp
662 474b4f94 2017-11-30 stsp static const struct got_error *
663 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
664 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
665 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
666 474b4f94 2017-11-30 stsp {
667 013404a9 2017-11-30 stsp /* XXX TODO */
668 474b4f94 2017-11-30 stsp return NULL;
669 474b4f94 2017-11-30 stsp }
670 474b4f94 2017-11-30 stsp
671 474b4f94 2017-11-30 stsp static const struct got_error *
672 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
673 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
674 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
675 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
676 31b4484f 2019-07-27 stsp int diff_content)
677 474b4f94 2017-11-30 stsp {
678 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
679 19ae6da1 2018-11-05 stsp int id_match;
680 63c5ca5d 2019-08-24 stsp
681 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
682 63c5ca5d 2019-08-24 stsp return NULL;
683 474b4f94 2017-11-30 stsp
684 474b4f94 2017-11-30 stsp if (te2 == NULL) {
685 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
686 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
687 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
688 31b4484f 2019-07-27 stsp else {
689 31b4484f 2019-07-27 stsp if (diff_content)
690 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
691 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
692 31b4484f 2019-07-27 stsp else
693 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
694 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
695 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
696 31b4484f 2019-07-27 stsp }
697 f6861a81 2018-09-13 stsp return err;
698 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
699 63c5ca5d 2019-08-24 stsp return NULL;
700 474b4f94 2017-11-30 stsp
701 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
702 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
703 19ae6da1 2018-11-05 stsp if (!id_match)
704 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
705 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
706 f9d37699 2022-06-28 stsp diff_content);
707 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
708 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
709 46f68b20 2019-10-19 stsp if (!id_match ||
710 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
711 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
712 31b4484f 2019-07-27 stsp if (diff_content)
713 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
714 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
715 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
716 31b4484f 2019-07-27 stsp else
717 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
718 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
719 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
720 31b4484f 2019-07-27 stsp }
721 413ea19d 2017-11-30 stsp }
722 474b4f94 2017-11-30 stsp
723 19ae6da1 2018-11-05 stsp if (id_match)
724 f6861a81 2018-09-13 stsp return NULL;
725 f6861a81 2018-09-13 stsp
726 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
727 aaa13589 2019-06-01 stsp cb, cb_arg);
728 474b4f94 2017-11-30 stsp }
729 474b4f94 2017-11-30 stsp
730 474b4f94 2017-11-30 stsp static const struct got_error *
731 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
732 49d4a017 2022-06-30 stsp struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
733 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
734 31b4484f 2019-07-27 stsp int diff_content)
735 474b4f94 2017-11-30 stsp {
736 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
737 63c5ca5d 2019-08-24 stsp return NULL;
738 63c5ca5d 2019-08-24 stsp
739 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
740 f6861a81 2018-09-13 stsp return NULL;
741 474b4f94 2017-11-30 stsp
742 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
743 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
744 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
745 f6861a81 2018-09-13 stsp
746 31b4484f 2019-07-27 stsp if (diff_content)
747 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
748 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
749 31b4484f 2019-07-27 stsp
750 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
751 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
752 0208f208 2020-05-05 stsp }
753 0208f208 2020-05-05 stsp
754 0208f208 2020-05-05 stsp const struct got_error *
755 5191b70b 2023-01-07 mark got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
756 5191b70b 2023-01-07 mark struct got_blob_object *blob2, FILE *f1, FILE *f2,
757 5191b70b 2023-01-07 mark struct got_object_id *id1, struct got_object_id *id2,
758 5191b70b 2023-01-07 mark const char *label1, const char *label2,
759 5191b70b 2023-01-07 mark mode_t mode1, mode_t mode2, struct got_repository *repo)
760 5191b70b 2023-01-07 mark {
761 5191b70b 2023-01-07 mark const struct got_error *err = NULL;
762 5191b70b 2023-01-07 mark struct got_diffreg_result *result = NULL;
763 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg *a = arg;
764 5191b70b 2023-01-07 mark char *path = NULL;
765 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
766 5191b70b 2023-01-07 mark
767 5191b70b 2023-01-07 mark path = strdup(label2 ? label2 : label1);
768 5191b70b 2023-01-07 mark if (path == NULL)
769 5191b70b 2023-01-07 mark return got_error_from_errno("malloc");
770 5191b70b 2023-01-07 mark
771 5191b70b 2023-01-07 mark if (id1 == NULL)
772 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
773 5191b70b 2023-01-07 mark else if (id2 == NULL)
774 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
775 5191b70b 2023-01-07 mark else {
776 5191b70b 2023-01-07 mark if (got_object_id_cmp(id1, id2) != 0)
777 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
778 5191b70b 2023-01-07 mark else if (mode1 != mode2)
779 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODE_CHANGE;
780 5191b70b 2023-01-07 mark }
781 5191b70b 2023-01-07 mark
782 5191b70b 2023-01-07 mark if (f1) {
783 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f1);
784 5191b70b 2023-01-07 mark if (err)
785 5191b70b 2023-01-07 mark goto done;
786 5191b70b 2023-01-07 mark }
787 5191b70b 2023-01-07 mark if (f2) {
788 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f2);
789 5191b70b 2023-01-07 mark if (err)
790 5191b70b 2023-01-07 mark goto done;
791 5191b70b 2023-01-07 mark }
792 5191b70b 2023-01-07 mark
793 5191b70b 2023-01-07 mark if (blob1) {
794 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
795 5191b70b 2023-01-07 mark blob1);
796 5191b70b 2023-01-07 mark if (err)
797 5191b70b 2023-01-07 mark goto done;
798 5191b70b 2023-01-07 mark }
799 5191b70b 2023-01-07 mark if (blob2) {
800 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
801 5191b70b 2023-01-07 mark blob2);
802 5191b70b 2023-01-07 mark if (err)
803 5191b70b 2023-01-07 mark goto done;
804 5191b70b 2023-01-07 mark }
805 5191b70b 2023-01-07 mark
806 5191b70b 2023-01-07 mark err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
807 5191b70b 2023-01-07 mark a->force_text);
808 5191b70b 2023-01-07 mark if (err)
809 5191b70b 2023-01-07 mark goto done;
810 5191b70b 2023-01-07 mark
811 a76e88e5 2023-01-10 mark err = get_diffstat(a, path, result->result, a->force_text, status);
812 5191b70b 2023-01-07 mark
813 5191b70b 2023-01-07 mark done:
814 5191b70b 2023-01-07 mark if (result) {
815 5191b70b 2023-01-07 mark const struct got_error *free_err;
816 5191b70b 2023-01-07 mark
817 5191b70b 2023-01-07 mark free_err = got_diffreg_result_free(result);
818 5191b70b 2023-01-07 mark if (free_err && err == NULL)
819 5191b70b 2023-01-07 mark err = free_err;
820 5191b70b 2023-01-07 mark }
821 a76e88e5 2023-01-10 mark if (err)
822 5191b70b 2023-01-07 mark free(path);
823 5191b70b 2023-01-07 mark return err;
824 5191b70b 2023-01-07 mark }
825 5191b70b 2023-01-07 mark
826 5191b70b 2023-01-07 mark const struct got_error *
827 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
828 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
829 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
830 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
831 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
832 0208f208 2020-05-05 stsp {
833 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
834 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
835 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
836 0208f208 2020-05-05 stsp char *path = NULL;
837 0208f208 2020-05-05 stsp
838 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
839 0208f208 2020-05-05 stsp if (path == NULL)
840 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
841 0208f208 2020-05-05 stsp
842 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
843 0208f208 2020-05-05 stsp if (change == NULL) {
844 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
845 0208f208 2020-05-05 stsp goto done;
846 0208f208 2020-05-05 stsp }
847 0208f208 2020-05-05 stsp
848 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
849 0208f208 2020-05-05 stsp if (id1 == NULL)
850 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
851 0208f208 2020-05-05 stsp else if (id2 == NULL)
852 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
853 0208f208 2020-05-05 stsp else {
854 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
855 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
856 0208f208 2020-05-05 stsp else if (mode1 != mode2)
857 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
858 0208f208 2020-05-05 stsp }
859 0208f208 2020-05-05 stsp
860 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
861 0208f208 2020-05-05 stsp done:
862 0208f208 2020-05-05 stsp if (err) {
863 0208f208 2020-05-05 stsp free(path);
864 0208f208 2020-05-05 stsp free(change);
865 0208f208 2020-05-05 stsp }
866 0208f208 2020-05-05 stsp return err;
867 474b4f94 2017-11-30 stsp }
868 474b4f94 2017-11-30 stsp
869 474b4f94 2017-11-30 stsp const struct got_error *
870 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
871 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
872 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
873 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
874 b72706c3 2022-06-01 stsp int diff_content)
875 474b4f94 2017-11-30 stsp {
876 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
877 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
878 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
879 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
880 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
881 474b4f94 2017-11-30 stsp
882 883f0469 2018-06-23 stsp if (tree1) {
883 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
884 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
885 f6861a81 2018-09-13 stsp te1->name) == -1)
886 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
887 883f0469 2018-06-23 stsp }
888 883f0469 2018-06-23 stsp if (tree2) {
889 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
890 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
891 f6861a81 2018-09-13 stsp te2->name) == -1)
892 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
893 883f0469 2018-06-23 stsp }
894 474b4f94 2017-11-30 stsp
895 474b4f94 2017-11-30 stsp do {
896 474b4f94 2017-11-30 stsp if (te1) {
897 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
898 f6861a81 2018-09-13 stsp if (tree2)
899 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
900 1de5e065 2019-06-01 stsp te1->name);
901 f6861a81 2018-09-13 stsp if (te) {
902 f6861a81 2018-09-13 stsp free(l2);
903 f6861a81 2018-09-13 stsp l2 = NULL;
904 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
905 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
906 230a42bd 2019-05-11 jcs return
907 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
908 f6861a81 2018-09-13 stsp }
909 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
910 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
911 474b4f94 2017-11-30 stsp if (err)
912 474b4f94 2017-11-30 stsp break;
913 474b4f94 2017-11-30 stsp }
914 474b4f94 2017-11-30 stsp
915 474b4f94 2017-11-30 stsp if (te2) {
916 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
917 f6861a81 2018-09-13 stsp if (tree1)
918 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
919 1de5e065 2019-06-01 stsp te2->name);
920 d6ce02f1 2018-11-17 stsp free(l2);
921 d6ce02f1 2018-11-17 stsp if (te) {
922 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
923 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
924 230a42bd 2019-05-11 jcs return
925 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
926 d6ce02f1 2018-11-17 stsp } else {
927 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
928 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
929 230a42bd 2019-05-11 jcs return
930 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
931 d6ce02f1 2018-11-17 stsp }
932 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
933 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
934 474b4f94 2017-11-30 stsp if (err)
935 474b4f94 2017-11-30 stsp break;
936 474b4f94 2017-11-30 stsp }
937 474b4f94 2017-11-30 stsp
938 f6861a81 2018-09-13 stsp free(l1);
939 f6861a81 2018-09-13 stsp l1 = NULL;
940 f6861a81 2018-09-13 stsp if (te1) {
941 56e0773d 2019-11-28 stsp tidx1++;
942 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
943 f6861a81 2018-09-13 stsp if (te1 &&
944 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
945 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
946 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
947 f6861a81 2018-09-13 stsp }
948 f6861a81 2018-09-13 stsp free(l2);
949 f6861a81 2018-09-13 stsp l2 = NULL;
950 f6861a81 2018-09-13 stsp if (te2) {
951 56e0773d 2019-11-28 stsp tidx2++;
952 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
953 f6861a81 2018-09-13 stsp if (te2 &&
954 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
955 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
956 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
957 f6861a81 2018-09-13 stsp }
958 474b4f94 2017-11-30 stsp } while (te1 || te2);
959 11528a82 2018-05-19 stsp
960 11528a82 2018-05-19 stsp return err;
961 11528a82 2018-05-19 stsp }
962 11528a82 2018-05-19 stsp
963 11528a82 2018-05-19 stsp const struct got_error *
964 c7d5c43c 2022-08-04 mark got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
965 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
966 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
967 4b752015 2022-06-30 stsp const char *label1, const char *label2,
968 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
969 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
970 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
971 11528a82 2018-05-19 stsp {
972 11528a82 2018-05-19 stsp const struct got_error *err;
973 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
974 b74c7625 2018-05-20 stsp
975 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
976 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
977 eb81bc23 2022-06-28 tracey
978 15a94983 2018-12-23 stsp if (id1) {
979 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
980 cd0acaa7 2018-05-20 stsp if (err)
981 cd0acaa7 2018-05-20 stsp goto done;
982 cd0acaa7 2018-05-20 stsp }
983 15a94983 2018-12-23 stsp if (id2) {
984 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
985 cd0acaa7 2018-05-20 stsp if (err)
986 cd0acaa7 2018-05-20 stsp goto done;
987 cd0acaa7 2018-05-20 stsp }
988 c7d5c43c 2022-08-04 mark err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
989 c7d5c43c 2022-08-04 mark diff_algo, diff_context, ignore_whitespace, force_text_diff,
990 a76e88e5 2023-01-10 mark show_diffstat, ds, outfile);
991 67b631c9 2021-10-10 stsp done:
992 67b631c9 2021-10-10 stsp if (blob1)
993 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
994 67b631c9 2021-10-10 stsp if (blob2)
995 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
996 67b631c9 2021-10-10 stsp return err;
997 67b631c9 2021-10-10 stsp }
998 67b631c9 2021-10-10 stsp
999 67b631c9 2021-10-10 stsp static const struct got_error *
1000 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1001 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1002 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1003 67b631c9 2021-10-10 stsp {
1004 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1005 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
1006 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1007 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1008 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1009 67b631c9 2021-10-10 stsp
1010 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
1011 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1012 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
1013 67b631c9 2021-10-10 stsp
1014 67b631c9 2021-10-10 stsp free(id1);
1015 67b631c9 2021-10-10 stsp id1 = NULL;
1016 67b631c9 2021-10-10 stsp free(id2);
1017 67b631c9 2021-10-10 stsp id2 = NULL;
1018 67b631c9 2021-10-10 stsp if (subtree1) {
1019 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1020 67b631c9 2021-10-10 stsp subtree1 = NULL;
1021 67b631c9 2021-10-10 stsp }
1022 67b631c9 2021-10-10 stsp if (subtree2) {
1023 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1024 67b631c9 2021-10-10 stsp subtree2 = NULL;
1025 67b631c9 2021-10-10 stsp }
1026 67b631c9 2021-10-10 stsp if (blob1) {
1027 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1028 67b631c9 2021-10-10 stsp blob1 = NULL;
1029 67b631c9 2021-10-10 stsp }
1030 67b631c9 2021-10-10 stsp if (blob2) {
1031 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1032 67b631c9 2021-10-10 stsp blob2 = NULL;
1033 67b631c9 2021-10-10 stsp }
1034 67b631c9 2021-10-10 stsp
1035 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1036 67b631c9 2021-10-10 stsp pe->path);
1037 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1038 67b631c9 2021-10-10 stsp goto done;
1039 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1040 67b631c9 2021-10-10 stsp pe->path);
1041 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1042 67b631c9 2021-10-10 stsp goto done;
1043 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
1044 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1045 67b631c9 2021-10-10 stsp goto done;
1046 67b631c9 2021-10-10 stsp }
1047 67b631c9 2021-10-10 stsp if (id1) {
1048 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
1049 67b631c9 2021-10-10 stsp if (err)
1050 67b631c9 2021-10-10 stsp goto done;
1051 67b631c9 2021-10-10 stsp }
1052 67b631c9 2021-10-10 stsp if (id2) {
1053 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
1054 67b631c9 2021-10-10 stsp if (err)
1055 67b631c9 2021-10-10 stsp goto done;
1056 67b631c9 2021-10-10 stsp }
1057 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
1058 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
1059 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1060 67b631c9 2021-10-10 stsp goto done;
1061 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
1062 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1063 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1064 67b631c9 2021-10-10 stsp goto done;
1065 67b631c9 2021-10-10 stsp }
1066 67b631c9 2021-10-10 stsp
1067 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
1068 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
1069 67b631c9 2021-10-10 stsp if (id1) {
1070 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
1071 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
1072 67b631c9 2021-10-10 stsp if (err)
1073 67b631c9 2021-10-10 stsp goto done;
1074 67b631c9 2021-10-10 stsp }
1075 67b631c9 2021-10-10 stsp if (id2) {
1076 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
1077 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
1078 67b631c9 2021-10-10 stsp if (err)
1079 67b631c9 2021-10-10 stsp goto done;
1080 67b631c9 2021-10-10 stsp }
1081 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1082 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1083 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1084 67b631c9 2021-10-10 stsp mode1, mode2, repo);
1085 67b631c9 2021-10-10 stsp if (err)
1086 67b631c9 2021-10-10 stsp goto done;
1087 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
1088 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
1089 67b631c9 2021-10-10 stsp if (id1) {
1090 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
1091 67b631c9 2021-10-10 stsp id1);
1092 67b631c9 2021-10-10 stsp if (err)
1093 67b631c9 2021-10-10 stsp goto done;
1094 67b631c9 2021-10-10 stsp }
1095 67b631c9 2021-10-10 stsp if (id2) {
1096 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
1097 67b631c9 2021-10-10 stsp id2);
1098 67b631c9 2021-10-10 stsp if (err)
1099 67b631c9 2021-10-10 stsp goto done;
1100 67b631c9 2021-10-10 stsp }
1101 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
1102 f9d37699 2022-06-28 stsp fd1, fd2,
1103 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1104 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1105 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
1106 67b631c9 2021-10-10 stsp if (err)
1107 67b631c9 2021-10-10 stsp goto done;
1108 67b631c9 2021-10-10 stsp } else {
1109 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1110 67b631c9 2021-10-10 stsp goto done;
1111 67b631c9 2021-10-10 stsp }
1112 67b631c9 2021-10-10 stsp }
1113 11528a82 2018-05-19 stsp done:
1114 67b631c9 2021-10-10 stsp free(id1);
1115 67b631c9 2021-10-10 stsp free(id2);
1116 67b631c9 2021-10-10 stsp if (subtree1)
1117 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1118 67b631c9 2021-10-10 stsp if (subtree2)
1119 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1120 11528a82 2018-05-19 stsp if (blob1)
1121 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1122 11528a82 2018-05-19 stsp if (blob2)
1123 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1124 474b4f94 2017-11-30 stsp return err;
1125 474b4f94 2017-11-30 stsp }
1126 11528a82 2018-05-19 stsp
1127 8469d821 2022-06-25 stsp static const struct got_error *
1128 c7d5c43c 2022-08-04 mark show_object_id(struct got_diff_line **lines, size_t *nlines,
1129 c7d5c43c 2022-08-04 mark const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1130 8469d821 2022-06-25 stsp {
1131 8469d821 2022-06-25 stsp const struct got_error *err;
1132 8469d821 2022-06-25 stsp int n;
1133 8469d821 2022-06-25 stsp off_t outoff = 0;
1134 8469d821 2022-06-25 stsp
1135 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1136 36e83e5e 2022-08-16 op if (n < 0)
1137 36e83e5e 2022-08-16 op return got_error_from_errno("fprintf");
1138 36e83e5e 2022-08-16 op
1139 c7d5c43c 2022-08-04 mark if (lines != NULL && *lines != NULL) {
1140 8469d821 2022-06-25 stsp if (*nlines == 0) {
1141 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0,
1142 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1143 8469d821 2022-06-25 stsp if (err)
1144 8469d821 2022-06-25 stsp return err;
1145 8469d821 2022-06-25 stsp } else
1146 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
1147 8469d821 2022-06-25 stsp
1148 8469d821 2022-06-25 stsp outoff += n;
1149 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
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 }
1154 8469d821 2022-06-25 stsp
1155 8469d821 2022-06-25 stsp return NULL;
1156 8469d821 2022-06-25 stsp }
1157 8469d821 2022-06-25 stsp
1158 8469d821 2022-06-25 stsp static const struct got_error *
1159 c7d5c43c 2022-08-04 mark diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1160 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1161 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1162 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1163 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1164 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *dsa,
1165 4b752015 2022-06-30 stsp struct got_repository *repo, FILE *outfile,
1166 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
1167 11528a82 2018-05-19 stsp {
1168 11528a82 2018-05-19 stsp const struct got_error *err;
1169 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1170 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1171 c7d5c43c 2022-08-04 mark int want_linemeta = (lines != NULL && *lines != NULL);
1172 11528a82 2018-05-19 stsp
1173 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1174 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1175 b74c7625 2018-05-20 stsp
1176 15a94983 2018-12-23 stsp if (id1) {
1177 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1178 cd0acaa7 2018-05-20 stsp if (err)
1179 cd0acaa7 2018-05-20 stsp goto done;
1180 cd0acaa7 2018-05-20 stsp }
1181 15a94983 2018-12-23 stsp if (id2) {
1182 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1183 cd0acaa7 2018-05-20 stsp if (err)
1184 cd0acaa7 2018-05-20 stsp goto done;
1185 cd0acaa7 2018-05-20 stsp }
1186 67b631c9 2021-10-10 stsp
1187 4b752015 2022-06-30 stsp arg.diff_algo = diff_algo;
1188 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1189 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1190 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1191 a76e88e5 2023-01-10 mark arg.show_diffstat = show_diffstat;
1192 a76e88e5 2023-01-10 mark arg.diffstat = dsa;
1193 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1194 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1195 c7d5c43c 2022-08-04 mark arg.lines = *lines;
1196 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1197 fe621944 2020-11-10 stsp } else {
1198 c7d5c43c 2022-08-04 mark arg.lines = NULL;
1199 fe621944 2020-11-10 stsp arg.nlines = 0;
1200 fe621944 2020-11-10 stsp }
1201 a76e88e5 2023-01-10 mark if (paths == NULL || TAILQ_EMPTY(paths))
1202 a76e88e5 2023-01-10 mark err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1203 a76e88e5 2023-01-10 mark label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1204 a76e88e5 2023-01-10 mark else
1205 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1206 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1207 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1208 c7d5c43c 2022-08-04 mark *lines = arg.lines; /* was likely re-allocated */
1209 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1210 fe621944 2020-11-10 stsp }
1211 11528a82 2018-05-19 stsp done:
1212 11528a82 2018-05-19 stsp if (tree1)
1213 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1214 11528a82 2018-05-19 stsp if (tree2)
1215 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1216 11528a82 2018-05-19 stsp return err;
1217 11528a82 2018-05-19 stsp }
1218 11528a82 2018-05-19 stsp
1219 11528a82 2018-05-19 stsp const struct got_error *
1220 c7d5c43c 2022-08-04 mark got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1221 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1222 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1223 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1224 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1225 a76e88e5 2023-01-10 mark int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1226 a76e88e5 2023-01-10 mark struct got_repository *repo, FILE *outfile)
1227 8469d821 2022-06-25 stsp {
1228 8469d821 2022-06-25 stsp const struct got_error *err;
1229 8469d821 2022-06-25 stsp char *idstr = NULL;
1230 8469d821 2022-06-25 stsp
1231 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1232 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1233 8469d821 2022-06-25 stsp
1234 8469d821 2022-06-25 stsp if (id1) {
1235 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1236 8469d821 2022-06-25 stsp if (err)
1237 8469d821 2022-06-25 stsp goto done;
1238 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1239 8469d821 2022-06-25 stsp if (err)
1240 8469d821 2022-06-25 stsp goto done;
1241 8469d821 2022-06-25 stsp free(idstr);
1242 8469d821 2022-06-25 stsp idstr = NULL;
1243 8469d821 2022-06-25 stsp } else {
1244 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1245 c7d5c43c 2022-08-04 mark outfile);
1246 8469d821 2022-06-25 stsp if (err)
1247 8469d821 2022-06-25 stsp goto done;
1248 8469d821 2022-06-25 stsp }
1249 8469d821 2022-06-25 stsp
1250 8469d821 2022-06-25 stsp if (id2) {
1251 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1252 8469d821 2022-06-25 stsp if (err)
1253 8469d821 2022-06-25 stsp goto done;
1254 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1255 8469d821 2022-06-25 stsp if (err)
1256 8469d821 2022-06-25 stsp goto done;
1257 8469d821 2022-06-25 stsp free(idstr);
1258 8469d821 2022-06-25 stsp idstr = NULL;
1259 8469d821 2022-06-25 stsp } else {
1260 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1261 c7d5c43c 2022-08-04 mark outfile);
1262 8469d821 2022-06-25 stsp if (err)
1263 8469d821 2022-06-25 stsp goto done;
1264 8469d821 2022-06-25 stsp }
1265 8469d821 2022-06-25 stsp
1266 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1267 c7d5c43c 2022-08-04 mark paths, label1, label2, diff_context, ignore_whitespace,
1268 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1269 8469d821 2022-06-25 stsp done:
1270 8469d821 2022-06-25 stsp free(idstr);
1271 8469d821 2022-06-25 stsp return err;
1272 8469d821 2022-06-25 stsp }
1273 8469d821 2022-06-25 stsp
1274 8469d821 2022-06-25 stsp const struct got_error *
1275 c7d5c43c 2022-08-04 mark got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1276 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1277 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1278 4b752015 2022-06-30 stsp struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1279 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1280 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *dsa,
1281 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1282 11528a82 2018-05-19 stsp {
1283 11528a82 2018-05-19 stsp const struct got_error *err;
1284 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1285 8469d821 2022-06-25 stsp char *idstr = NULL;
1286 11528a82 2018-05-19 stsp
1287 15a94983 2018-12-23 stsp if (id2 == NULL)
1288 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1289 b74c7625 2018-05-20 stsp
1290 15a94983 2018-12-23 stsp if (id1) {
1291 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1292 cd0acaa7 2018-05-20 stsp if (err)
1293 cd0acaa7 2018-05-20 stsp goto done;
1294 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1295 8469d821 2022-06-25 stsp if (err)
1296 8469d821 2022-06-25 stsp goto done;
1297 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', idstr,
1298 c7d5c43c 2022-08-04 mark outfile);
1299 8469d821 2022-06-25 stsp if (err)
1300 8469d821 2022-06-25 stsp goto done;
1301 8469d821 2022-06-25 stsp free(idstr);
1302 8469d821 2022-06-25 stsp idstr = NULL;
1303 8469d821 2022-06-25 stsp } else {
1304 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1305 c7d5c43c 2022-08-04 mark outfile);
1306 8469d821 2022-06-25 stsp if (err)
1307 8469d821 2022-06-25 stsp goto done;
1308 cd0acaa7 2018-05-20 stsp }
1309 bacc9935 2018-05-20 stsp
1310 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1311 9b697879 2018-05-20 stsp if (err)
1312 9b697879 2018-05-20 stsp goto done;
1313 9b697879 2018-05-20 stsp
1314 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1315 8469d821 2022-06-25 stsp if (err)
1316 8469d821 2022-06-25 stsp goto done;
1317 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1318 8469d821 2022-06-25 stsp if (err)
1319 8469d821 2022-06-25 stsp goto done;
1320 8469d821 2022-06-25 stsp
1321 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1322 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1323 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1324 a76e88e5 2023-01-10 mark diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1325 a76e88e5 2023-01-10 mark dsa, repo, outfile, diff_algo);
1326 11528a82 2018-05-19 stsp done:
1327 11528a82 2018-05-19 stsp if (commit1)
1328 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1329 11528a82 2018-05-19 stsp if (commit2)
1330 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1331 8469d821 2022-06-25 stsp free(idstr);
1332 11528a82 2018-05-19 stsp return err;
1333 11528a82 2018-05-19 stsp }
1334 dc424a06 2019-08-07 stsp
1335 dc424a06 2019-08-07 stsp const struct got_error *
1336 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1337 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1338 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1339 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1340 dc424a06 2019-08-07 stsp {
1341 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1342 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1343 dc424a06 2019-08-07 stsp
1344 fe621944 2020-11-10 stsp if (resultp)
1345 fe621944 2020-11-10 stsp *resultp = NULL;
1346 dc424a06 2019-08-07 stsp
1347 dc424a06 2019-08-07 stsp if (outfile) {
1348 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1349 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1350 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1351 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1352 dc424a06 2019-08-07 stsp }
1353 fe621944 2020-11-10 stsp
1354 4b752015 2022-06-30 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1355 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1356 fe621944 2020-11-10 stsp if (err)
1357 fe621944 2020-11-10 stsp goto done;
1358 fe621944 2020-11-10 stsp
1359 fe621944 2020-11-10 stsp if (outfile) {
1360 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1361 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1362 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1363 dc424a06 2019-08-07 stsp if (err)
1364 dc424a06 2019-08-07 stsp goto done;
1365 dc424a06 2019-08-07 stsp }
1366 fe621944 2020-11-10 stsp
1367 dc424a06 2019-08-07 stsp done:
1368 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1369 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1370 fe621944 2020-11-10 stsp else if (diffreg_result) {
1371 fe621944 2020-11-10 stsp const struct got_error *free_err;
1372 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1373 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1374 fe621944 2020-11-10 stsp err = free_err;
1375 dc424a06 2019-08-07 stsp }
1376 fe621944 2020-11-10 stsp
1377 dc424a06 2019-08-07 stsp return err;
1378 dc424a06 2019-08-07 stsp }