2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
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 5822e79e 2023-02-23 op #include <sha2.h>
26 7d283eee 2017-11-29 stsp #include <zlib.h>
28 7d283eee 2017-11-29 stsp #include "got_object.h"
29 e09a504c 2019-06-28 stsp #include "got_repository.h"
30 7d283eee 2017-11-29 stsp #include "got_error.h"
31 789689b5 2017-11-30 stsp #include "got_diff.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 0208f208 2020-05-05 stsp #include "got_cancel.h"
34 0208f208 2020-05-05 stsp #include "got_worktree.h"
35 a558dd1b 2022-06-08 stsp #include "got_opentemp.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
38 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
39 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
40 15a94983 2018-12-23 stsp #include "got_lib_object.h"
43 5191b70b 2023-01-07 mark #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 404c43c4 2018-06-21 stsp static const struct got_error *
47 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
50 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
52 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 fe621944 2020-11-10 stsp if (p == NULL)
54 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
56 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
57 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
58 fe621944 2020-11-10 stsp (*nlines)++;
60 fe621944 2020-11-10 stsp return NULL;
64 a76e88e5 2023-01-10 mark diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
65 a76e88e5 2023-01-10 mark uint32_t add, uint32_t rm)
67 a76e88e5 2023-01-10 mark int d1 = 1, d2 = 1;
70 a76e88e5 2023-01-10 mark *maxlen = MAX(*maxlen, len);
72 a76e88e5 2023-01-10 mark while (add /= 10)
74 a76e88e5 2023-01-10 mark *add_cols = MAX(*add_cols, d1);
76 a76e88e5 2023-01-10 mark while (rm /= 10)
78 a76e88e5 2023-01-10 mark *rm_cols = MAX(*rm_cols, d2);
81 fe621944 2020-11-10 stsp static const struct got_error *
82 a76e88e5 2023-01-10 mark get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
83 a76e88e5 2023-01-10 mark struct diff_result *r, int force_text, int status)
85 a76e88e5 2023-01-10 mark const struct got_error *err;
86 a76e88e5 2023-01-10 mark struct got_pathlist_entry *pe;
87 a76e88e5 2023-01-10 mark struct got_diff_changed_path *change = NULL;
88 a76e88e5 2023-01-10 mark int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
89 a76e88e5 2023-01-10 mark int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
92 a76e88e5 2023-01-10 mark change = calloc(1, sizeof(*change));
93 a76e88e5 2023-01-10 mark if (change == NULL)
94 6b37f13a 2023-01-10 mark return got_error_from_errno("calloc");
96 a76e88e5 2023-01-10 mark if (!isbin || force_text) {
97 a76e88e5 2023-01-10 mark for (i = 0; i < r->chunks.len; ++i) {
98 a76e88e5 2023-01-10 mark struct diff_chunk *c;
99 a76e88e5 2023-01-10 mark int clc, crc;
101 a76e88e5 2023-01-10 mark c = diff_chunk_get(r, i);
102 a76e88e5 2023-01-10 mark clc = diff_chunk_get_left_count(c);
103 a76e88e5 2023-01-10 mark crc = diff_chunk_get_right_count(c);
105 a76e88e5 2023-01-10 mark if (crc && !clc)
106 a76e88e5 2023-01-10 mark change->add += crc;
107 a76e88e5 2023-01-10 mark if (clc && !crc)
108 a76e88e5 2023-01-10 mark change->rm += clc;
112 a76e88e5 2023-01-10 mark change->status = status;
113 a76e88e5 2023-01-10 mark ds->ins += change->add;
114 a76e88e5 2023-01-10 mark ds->del += change->rm;
115 a76e88e5 2023-01-10 mark ++ds->nfiles;
117 a76e88e5 2023-01-10 mark err = got_pathlist_append(ds->paths, path, change);
119 5fa52d6c 2023-01-11 mark free(change);
120 a76e88e5 2023-01-10 mark return err;
123 a76e88e5 2023-01-10 mark pe = TAILQ_LAST(ds->paths, got_pathlist_head);
124 a76e88e5 2023-01-10 mark diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
125 a76e88e5 2023-01-10 mark pe->path_len, change->add, change->rm);
127 a76e88e5 2023-01-10 mark return NULL;
130 a76e88e5 2023-01-10 mark static const struct got_error *
131 c7d5c43c 2022-08-04 mark diff_blobs(struct got_diff_line **lines, size_t *nlines,
132 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
133 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
134 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
135 a76e88e5 2023-01-10 mark int diff_context, int ignore_whitespace, int force_text_diff,
136 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *diffstat, FILE *outfile,
137 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
139 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
140 bbc740ac 2023-02-04 op char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
141 bbc740ac 2023-02-04 op char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
142 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
143 5fa52d6c 2023-01-11 mark char *modestr1 = NULL, *modestr2 = NULL;
144 be659d10 2020-11-18 stsp off_t size1, size2;
145 3846622f 2023-01-07 mark struct got_diffreg_result *result = NULL;
146 fe621944 2020-11-10 stsp off_t outoff = 0;
149 2d61e381 2022-08-30 mark if (lines && *lines && *nlines > 0)
150 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
151 c7d5c43c 2022-08-04 mark else if (lines) {
152 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
157 fe621944 2020-11-10 stsp if (resultp)
158 fe621944 2020-11-10 stsp *resultp = NULL;
161 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
166 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
172 98abbc84 2017-11-30 stsp if (blob1) {
173 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
174 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
179 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
182 98abbc84 2017-11-30 stsp if (blob2) {
183 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
184 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
189 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
191 09de383e 2018-12-24 stsp if (outfile) {
192 40dde666 2020-07-23 stsp int modebits;
194 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
195 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
196 40dde666 2020-07-23 stsp modebits = S_IFLNK;
198 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
199 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
200 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
201 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
205 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
206 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
207 40dde666 2020-07-23 stsp modebits = S_IFLNK;
209 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
210 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
211 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
212 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
216 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
217 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
220 fe621944 2020-11-10 stsp outoff += n;
221 c7d5c43c 2022-08-04 mark if (lines) {
222 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
223 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_MIN);
228 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
229 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
232 fe621944 2020-11-10 stsp outoff += n;
233 c7d5c43c 2022-08-04 mark if (lines) {
234 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
235 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_PLUS);
241 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
242 4b752015 2022-06-30 stsp force_text_diff);
246 1f3405c9 2023-01-17 mark if (diffstat) {
247 a76e88e5 2023-01-10 mark char *path = NULL;
248 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
250 6b37f13a 2023-01-10 mark if (blob1 == NULL)
251 6b37f13a 2023-01-10 mark status = GOT_STATUS_ADD;
252 6b37f13a 2023-01-10 mark else if (blob2 == NULL)
253 6b37f13a 2023-01-10 mark status = GOT_STATUS_DELETE;
255 1f3405c9 2023-01-17 mark if (strcmp(idstr1, idstr2) != 0)
256 1f3405c9 2023-01-17 mark status = GOT_STATUS_MODIFY;
257 1f3405c9 2023-01-17 mark else if (mode1 != mode2)
258 1f3405c9 2023-01-17 mark status = GOT_STATUS_MODE_CHANGE;
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");
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);
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");
280 1f3405c9 2023-01-17 mark err = get_diffstat(diffstat, path, result->result,
281 1f3405c9 2023-01-17 mark force_text_diff, status);
283 a76e88e5 2023-01-10 mark free(path);
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);
299 5fa52d6c 2023-01-11 mark free(modestr1);
300 5fa52d6c 2023-01-11 mark free(modestr2);
301 fe621944 2020-11-10 stsp if (resultp && err == NULL)
302 fe621944 2020-11-10 stsp *resultp = result;
303 3846622f 2023-01-07 mark else if (result) {
304 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
305 fe621944 2020-11-10 stsp if (free_err && err == NULL)
306 fe621944 2020-11-10 stsp err = free_err;
309 7d283eee 2017-11-29 stsp return err;
312 aaa13589 2019-06-01 stsp const struct got_error *
313 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
314 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
315 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
316 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 b72706c3 2022-06-01 stsp struct got_repository *repo)
319 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
321 c7d5c43c 2022-08-04 mark return diff_blobs(&a->lines, &a->nlines, NULL,
322 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
323 1f3405c9 2023-01-17 mark a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
324 1f3405c9 2023-01-17 mark a->diff_algo);
327 404c43c4 2018-06-21 stsp const struct got_error *
328 c7d5c43c 2022-08-04 mark got_diff_blob(struct got_diff_line **lines, size_t*nlines,
329 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
330 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
331 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
332 1f3405c9 2023-01-17 mark int ignore_whitespace, int force_text_diff,
333 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
335 c7d5c43c 2022-08-04 mark return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
336 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
337 1f3405c9 2023-01-17 mark force_text_diff, ds, outfile, diff_algo);
340 7f1f93af 2019-08-06 stsp static const struct got_error *
341 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
342 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
343 c87842d5 2022-09-23 mark FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
344 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
345 1f3405c9 2023-01-17 mark int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
347 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
348 bbc740ac 2023-02-04 op char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
349 58e31a80 2022-06-27 op const char *idstr1 = NULL;
350 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
352 fe621944 2020-11-10 stsp if (resultp)
353 fe621944 2020-11-10 stsp *resultp = NULL;
356 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
358 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
360 7f1f93af 2019-08-06 stsp if (outfile) {
361 c87842d5 2022-09-23 mark char *mode = NULL;
363 c87842d5 2022-09-23 mark /* display file mode for new added files only */
364 c87842d5 2022-09-23 mark if (f2_exists && blob1 == NULL) {
365 c87842d5 2022-09-23 mark int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
367 c87842d5 2022-09-23 mark if (S_ISLNK(sb2->st_mode))
368 c87842d5 2022-09-23 mark mmask = S_IFLNK;
369 c87842d5 2022-09-23 mark if (asprintf(&mode, " (mode %o)",
370 c87842d5 2022-09-23 mark sb2->st_mode & mmask) == -1)
371 c87842d5 2022-09-23 mark return got_error_from_errno("asprintf");
373 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
374 c87842d5 2022-09-23 mark fprintf(outfile, "file + %s%s\n",
375 c87842d5 2022-09-23 mark f2_exists ? label2 : "/dev/null", mode ? mode : "");
376 c87842d5 2022-09-23 mark free(mode);
379 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
380 4b752015 2022-06-30 stsp force_text_diff);
384 fe621944 2020-11-10 stsp if (outfile) {
385 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
386 49d4a017 2022-06-30 stsp blob1 != NULL, f2_exists,
387 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
388 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
389 1cb46f00 2020-11-21 stsp diff_context, outfile);
394 1f3405c9 2023-01-17 mark if (diffstat) {
395 a76e88e5 2023-01-10 mark char *path = NULL;
396 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
399 a76e88e5 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
400 a76e88e5 2023-01-10 mark * content change, there'll be no diffstat, and if there
401 a76e88e5 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
403 a76e88e5 2023-01-10 mark if (blob1 == NULL)
404 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
405 a76e88e5 2023-01-10 mark else if (!f2_exists)
406 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
408 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
410 6b37f13a 2023-01-10 mark if (label2 != NULL &&
411 6b37f13a 2023-01-10 mark (status != GOT_STATUS_DELETE || label1 == NULL))
412 6b37f13a 2023-01-10 mark path = strdup(label2);
414 6b37f13a 2023-01-10 mark path = strdup(label1);
415 6b37f13a 2023-01-10 mark if (path == NULL) {
416 6b37f13a 2023-01-10 mark err = got_error_from_errno("strdup");
420 1f3405c9 2023-01-17 mark err = get_diffstat(diffstat, path, result->result,
421 1f3405c9 2023-01-17 mark force_text_diff, status);
423 a76e88e5 2023-01-10 mark free(path);
429 fe621944 2020-11-10 stsp if (resultp && err == NULL)
430 fe621944 2020-11-10 stsp *resultp = result;
431 fe621944 2020-11-10 stsp else if (result) {
432 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
433 fe621944 2020-11-10 stsp if (free_err && err == NULL)
434 fe621944 2020-11-10 stsp err = free_err;
436 b72f483a 2019-02-05 stsp return err;
439 7f1f93af 2019-08-06 stsp const struct got_error *
440 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
441 c87842d5 2022-09-23 mark const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
442 4b752015 2022-06-30 stsp const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
443 1f3405c9 2023-01-17 mark int ignore_whitespace, int force_text_diff,
444 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
446 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
447 c87842d5 2022-09-23 mark sb2, label2, diff_algo, diff_context, ignore_whitespace,
448 1f3405c9 2023-01-17 mark force_text_diff, ds, outfile);
451 474b4f94 2017-11-30 stsp static const struct got_error *
452 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
453 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
454 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
456 4e22badc 2017-11-30 stsp const struct got_error *err;
457 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
458 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
460 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
462 4e22badc 2017-11-30 stsp return err;
464 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
467 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
468 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
470 2acfca77 2018-04-01 stsp got_object_close(obj);
472 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
473 2acfca77 2018-04-01 stsp return err;
476 474b4f94 2017-11-30 stsp static const struct got_error *
477 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
478 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
479 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
480 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
481 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
483 6a213ccb 2017-11-30 stsp const struct got_error *err;
484 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
485 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
486 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
487 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
489 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
491 730a8aa0 2018-04-24 stsp return err;
493 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
494 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
498 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
501 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
502 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
510 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
514 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
515 b72706c3 2022-06-01 stsp mode1, mode2, repo);
518 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
520 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
522 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
524 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
525 6a213ccb 2017-11-30 stsp return err;
528 474b4f94 2017-11-30 stsp static const struct got_error *
529 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
530 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
531 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg)
533 365fb436 2017-11-30 stsp const struct got_error *err;
534 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
535 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
537 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
539 365fb436 2017-11-30 stsp return err;
541 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
544 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
545 b72706c3 2022-06-01 stsp mode, 0, repo);
547 2acfca77 2018-04-01 stsp got_object_close(obj);
549 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
550 2acfca77 2018-04-01 stsp return err;
553 474b4f94 2017-11-30 stsp static const struct got_error *
554 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
555 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
556 49d4a017 2022-06-30 stsp void *cb_arg, int diff_content)
558 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
559 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
560 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
562 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
566 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
567 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
571 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
575 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
576 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
579 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
580 9c70d4c3 2017-11-30 stsp if (treeobj)
581 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
582 9c70d4c3 2017-11-30 stsp return err;
585 474b4f94 2017-11-30 stsp static const struct got_error *
586 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
587 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
588 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
589 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
590 b72706c3 2022-06-01 stsp int diff_content)
592 f6861a81 2018-09-13 stsp const struct got_error *err;
593 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
594 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
595 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
596 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
598 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
602 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
603 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
607 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
611 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
612 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
616 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
620 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
624 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
625 f9d37699 2022-06-28 stsp label1, label2, repo, cb, cb_arg, diff_content);
629 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
631 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
632 789689b5 2017-11-30 stsp if (treeobj1)
633 789689b5 2017-11-30 stsp got_object_close(treeobj1);
634 789689b5 2017-11-30 stsp if (treeobj2)
635 789689b5 2017-11-30 stsp got_object_close(treeobj2);
636 789689b5 2017-11-30 stsp return err;
639 474b4f94 2017-11-30 stsp static const struct got_error *
640 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
641 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
642 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
644 f6861a81 2018-09-13 stsp const struct got_error *err;
645 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
646 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
648 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
652 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
653 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
657 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
661 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
662 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
665 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
666 2c56f2ce 2017-11-30 stsp if (treeobj)
667 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
668 2c56f2ce 2017-11-30 stsp return err;
671 474b4f94 2017-11-30 stsp static const struct got_error *
672 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
673 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
674 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
676 013404a9 2017-11-30 stsp /* XXX TODO */
677 474b4f94 2017-11-30 stsp return NULL;
680 474b4f94 2017-11-30 stsp static const struct got_error *
681 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
682 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
683 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
684 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
685 31b4484f 2019-07-27 stsp int diff_content)
687 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
688 19ae6da1 2018-11-05 stsp int id_match;
690 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
691 63c5ca5d 2019-08-24 stsp return NULL;
693 474b4f94 2017-11-30 stsp if (te2 == NULL) {
694 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
695 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
696 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
698 31b4484f 2019-07-27 stsp if (diff_content)
699 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
700 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
702 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
703 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
704 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
706 f6861a81 2018-09-13 stsp return err;
707 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
708 63c5ca5d 2019-08-24 stsp return NULL;
710 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
711 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
712 19ae6da1 2018-11-05 stsp if (!id_match)
713 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
714 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
715 f9d37699 2022-06-28 stsp diff_content);
716 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
717 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
718 46f68b20 2019-10-19 stsp if (!id_match ||
719 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
720 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
721 31b4484f 2019-07-27 stsp if (diff_content)
722 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
723 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
724 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
726 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
727 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
728 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
732 19ae6da1 2018-11-05 stsp if (id_match)
733 f6861a81 2018-09-13 stsp return NULL;
735 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
736 aaa13589 2019-06-01 stsp cb, cb_arg);
739 474b4f94 2017-11-30 stsp static const struct got_error *
740 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
741 49d4a017 2022-06-30 stsp struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
742 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
743 31b4484f 2019-07-27 stsp int diff_content)
745 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
746 63c5ca5d 2019-08-24 stsp return NULL;
748 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
749 f6861a81 2018-09-13 stsp return NULL;
751 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
752 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
753 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
755 31b4484f 2019-07-27 stsp if (diff_content)
756 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
757 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
759 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
760 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
763 0208f208 2020-05-05 stsp const struct got_error *
764 5191b70b 2023-01-07 mark got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
765 5191b70b 2023-01-07 mark struct got_blob_object *blob2, FILE *f1, FILE *f2,
766 5191b70b 2023-01-07 mark struct got_object_id *id1, struct got_object_id *id2,
767 5191b70b 2023-01-07 mark const char *label1, const char *label2,
768 5191b70b 2023-01-07 mark mode_t mode1, mode_t mode2, struct got_repository *repo)
770 5191b70b 2023-01-07 mark const struct got_error *err = NULL;
771 5191b70b 2023-01-07 mark struct got_diffreg_result *result = NULL;
772 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg *a = arg;
773 5191b70b 2023-01-07 mark char *path = NULL;
774 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
776 5191b70b 2023-01-07 mark path = strdup(label2 ? label2 : label1);
777 5191b70b 2023-01-07 mark if (path == NULL)
778 6b37f13a 2023-01-10 mark return got_error_from_errno("strdup");
780 5191b70b 2023-01-07 mark if (id1 == NULL)
781 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
782 5191b70b 2023-01-07 mark else if (id2 == NULL)
783 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
785 5191b70b 2023-01-07 mark if (got_object_id_cmp(id1, id2) != 0)
786 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
787 5191b70b 2023-01-07 mark else if (mode1 != mode2)
788 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODE_CHANGE;
792 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f1);
797 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f2);
802 5191b70b 2023-01-07 mark if (blob1) {
803 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
808 5191b70b 2023-01-07 mark if (blob2) {
809 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
815 5191b70b 2023-01-07 mark err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
816 5191b70b 2023-01-07 mark a->force_text);
820 a76e88e5 2023-01-10 mark err = get_diffstat(a, path, result->result, a->force_text, status);
823 5191b70b 2023-01-07 mark if (result) {
824 5191b70b 2023-01-07 mark const struct got_error *free_err;
826 5191b70b 2023-01-07 mark free_err = got_diffreg_result_free(result);
827 5191b70b 2023-01-07 mark if (free_err && err == NULL)
828 5191b70b 2023-01-07 mark err = free_err;
831 5191b70b 2023-01-07 mark free(path);
832 5191b70b 2023-01-07 mark return err;
835 5191b70b 2023-01-07 mark const struct got_error *
836 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
837 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
838 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
839 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
840 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
842 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
843 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
844 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
845 0208f208 2020-05-05 stsp char *path = NULL;
847 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
848 0208f208 2020-05-05 stsp if (path == NULL)
849 6b37f13a 2023-01-10 mark return got_error_from_errno("strdup");
851 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
852 0208f208 2020-05-05 stsp if (change == NULL) {
853 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
857 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
858 0208f208 2020-05-05 stsp if (id1 == NULL)
859 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
860 0208f208 2020-05-05 stsp else if (id2 == NULL)
861 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
863 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
864 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
865 0208f208 2020-05-05 stsp else if (mode1 != mode2)
866 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
869 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
872 0208f208 2020-05-05 stsp free(path);
873 0208f208 2020-05-05 stsp free(change);
875 0208f208 2020-05-05 stsp return err;
878 474b4f94 2017-11-30 stsp const struct got_error *
879 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
880 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
881 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
882 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
883 b72706c3 2022-06-01 stsp int diff_content)
885 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
886 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
887 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
888 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
889 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
891 883f0469 2018-06-23 stsp if (tree1) {
892 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
893 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
894 f6861a81 2018-09-13 stsp te1->name) == -1)
895 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
897 883f0469 2018-06-23 stsp if (tree2) {
898 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
899 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
900 5fa52d6c 2023-01-11 mark te2->name) == -1) {
901 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
908 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
911 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
912 1de5e065 2019-06-01 stsp te1->name);
916 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
917 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te->name) == -1) {
918 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
923 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
924 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
930 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
933 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
934 1de5e065 2019-06-01 stsp te2->name);
939 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
940 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te->name) == -1) {
941 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
945 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
946 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te2->name) == -1) {
947 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
952 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
953 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
962 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
964 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
965 5fa52d6c 2023-01-11 mark label1[0] ? "/" : "", te1->name) == -1) {
966 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
975 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
977 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
978 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te2->name) == -1) {
979 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
983 474b4f94 2017-11-30 stsp } while (te1 || te2);
988 11528a82 2018-05-19 stsp return err;
991 11528a82 2018-05-19 stsp const struct got_error *
992 c7d5c43c 2022-08-04 mark got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
993 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
994 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
995 4b752015 2022-06-30 stsp const char *label1, const char *label2,
996 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
997 1f3405c9 2023-01-17 mark int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
998 1f3405c9 2023-01-17 mark struct got_repository *repo, FILE *outfile)
1000 11528a82 2018-05-19 stsp const struct got_error *err;
1001 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1003 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1004 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1006 15a94983 2018-12-23 stsp if (id1) {
1007 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1009 cd0acaa7 2018-05-20 stsp goto done;
1011 15a94983 2018-12-23 stsp if (id2) {
1012 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1014 cd0acaa7 2018-05-20 stsp goto done;
1016 c7d5c43c 2022-08-04 mark err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1017 c7d5c43c 2022-08-04 mark diff_algo, diff_context, ignore_whitespace, force_text_diff,
1018 1f3405c9 2023-01-17 mark ds, outfile);
1020 67b631c9 2021-10-10 stsp if (blob1)
1021 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1022 67b631c9 2021-10-10 stsp if (blob2)
1023 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1024 67b631c9 2021-10-10 stsp return err;
1027 67b631c9 2021-10-10 stsp static const struct got_error *
1028 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1029 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1030 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1032 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1033 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
1034 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1035 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1036 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1038 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
1039 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1040 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
1042 67b631c9 2021-10-10 stsp free(id1);
1043 67b631c9 2021-10-10 stsp id1 = NULL;
1044 67b631c9 2021-10-10 stsp free(id2);
1045 67b631c9 2021-10-10 stsp id2 = NULL;
1046 67b631c9 2021-10-10 stsp if (subtree1) {
1047 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1048 67b631c9 2021-10-10 stsp subtree1 = NULL;
1050 67b631c9 2021-10-10 stsp if (subtree2) {
1051 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1052 67b631c9 2021-10-10 stsp subtree2 = NULL;
1054 67b631c9 2021-10-10 stsp if (blob1) {
1055 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1056 67b631c9 2021-10-10 stsp blob1 = NULL;
1058 67b631c9 2021-10-10 stsp if (blob2) {
1059 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1060 67b631c9 2021-10-10 stsp blob2 = NULL;
1063 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1064 67b631c9 2021-10-10 stsp pe->path);
1065 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1066 67b631c9 2021-10-10 stsp goto done;
1067 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1068 67b631c9 2021-10-10 stsp pe->path);
1069 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1070 67b631c9 2021-10-10 stsp goto done;
1071 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
1072 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1073 67b631c9 2021-10-10 stsp goto done;
1075 67b631c9 2021-10-10 stsp if (id1) {
1076 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
1078 67b631c9 2021-10-10 stsp goto done;
1080 67b631c9 2021-10-10 stsp if (id2) {
1081 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
1083 67b631c9 2021-10-10 stsp goto done;
1085 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
1086 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
1087 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1088 67b631c9 2021-10-10 stsp goto done;
1089 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
1090 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1091 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1092 67b631c9 2021-10-10 stsp goto done;
1095 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
1096 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
1097 67b631c9 2021-10-10 stsp if (id1) {
1098 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
1099 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
1101 67b631c9 2021-10-10 stsp goto done;
1103 67b631c9 2021-10-10 stsp if (id2) {
1104 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
1105 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
1107 67b631c9 2021-10-10 stsp goto done;
1109 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1110 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1111 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1112 67b631c9 2021-10-10 stsp mode1, mode2, repo);
1114 67b631c9 2021-10-10 stsp goto done;
1115 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
1116 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
1117 67b631c9 2021-10-10 stsp if (id1) {
1118 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
1121 67b631c9 2021-10-10 stsp goto done;
1123 67b631c9 2021-10-10 stsp if (id2) {
1124 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
1127 67b631c9 2021-10-10 stsp goto done;
1129 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
1131 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1132 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1133 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
1135 67b631c9 2021-10-10 stsp goto done;
1137 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1138 67b631c9 2021-10-10 stsp goto done;
1142 67b631c9 2021-10-10 stsp free(id1);
1143 67b631c9 2021-10-10 stsp free(id2);
1144 67b631c9 2021-10-10 stsp if (subtree1)
1145 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1146 67b631c9 2021-10-10 stsp if (subtree2)
1147 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1148 11528a82 2018-05-19 stsp if (blob1)
1149 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1150 11528a82 2018-05-19 stsp if (blob2)
1151 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1152 474b4f94 2017-11-30 stsp return err;
1155 8469d821 2022-06-25 stsp static const struct got_error *
1156 c7d5c43c 2022-08-04 mark show_object_id(struct got_diff_line **lines, size_t *nlines,
1157 c7d5c43c 2022-08-04 mark const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1159 8469d821 2022-06-25 stsp const struct got_error *err;
1161 8469d821 2022-06-25 stsp off_t outoff = 0;
1163 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1165 36e83e5e 2022-08-16 op return got_error_from_errno("fprintf");
1167 c7d5c43c 2022-08-04 mark if (lines != NULL && *lines != NULL) {
1168 8469d821 2022-06-25 stsp if (*nlines == 0) {
1169 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0,
1170 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1172 8469d821 2022-06-25 stsp return err;
1174 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
1176 8469d821 2022-06-25 stsp outoff += n;
1177 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
1178 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1180 8469d821 2022-06-25 stsp return err;
1183 8469d821 2022-06-25 stsp return NULL;
1186 8469d821 2022-06-25 stsp static const struct got_error *
1187 c7d5c43c 2022-08-04 mark diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1188 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1189 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1190 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1191 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1192 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1193 1f3405c9 2023-01-17 mark FILE *outfile, enum got_diff_algorithm diff_algo)
1195 11528a82 2018-05-19 stsp const struct got_error *err;
1196 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1197 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1198 c7d5c43c 2022-08-04 mark int want_linemeta = (lines != NULL && *lines != NULL);
1200 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1201 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1203 15a94983 2018-12-23 stsp if (id1) {
1204 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1206 cd0acaa7 2018-05-20 stsp goto done;
1208 15a94983 2018-12-23 stsp if (id2) {
1209 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1211 cd0acaa7 2018-05-20 stsp goto done;
1214 4b752015 2022-06-30 stsp arg.diff_algo = diff_algo;
1215 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1216 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1217 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1218 a76e88e5 2023-01-10 mark arg.diffstat = dsa;
1219 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1220 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1221 c7d5c43c 2022-08-04 mark arg.lines = *lines;
1222 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1224 c7d5c43c 2022-08-04 mark arg.lines = NULL;
1225 fe621944 2020-11-10 stsp arg.nlines = 0;
1227 a76e88e5 2023-01-10 mark if (paths == NULL || TAILQ_EMPTY(paths))
1228 a76e88e5 2023-01-10 mark err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1229 a76e88e5 2023-01-10 mark label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1231 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1232 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1233 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1234 c7d5c43c 2022-08-04 mark *lines = arg.lines; /* was likely re-allocated */
1235 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1238 11528a82 2018-05-19 stsp if (tree1)
1239 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1240 11528a82 2018-05-19 stsp if (tree2)
1241 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1242 11528a82 2018-05-19 stsp return err;
1245 11528a82 2018-05-19 stsp const struct got_error *
1246 c7d5c43c 2022-08-04 mark got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1247 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1248 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1249 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1250 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1251 1f3405c9 2023-01-17 mark int force_text_diff, struct got_diffstat_cb_arg *dsa,
1252 a76e88e5 2023-01-10 mark struct got_repository *repo, FILE *outfile)
1254 8469d821 2022-06-25 stsp const struct got_error *err;
1255 8469d821 2022-06-25 stsp char *idstr = NULL;
1257 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1258 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1260 8469d821 2022-06-25 stsp if (id1) {
1261 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1263 8469d821 2022-06-25 stsp goto done;
1264 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1266 8469d821 2022-06-25 stsp goto done;
1267 8469d821 2022-06-25 stsp free(idstr);
1268 8469d821 2022-06-25 stsp idstr = NULL;
1270 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1273 8469d821 2022-06-25 stsp goto done;
1276 8469d821 2022-06-25 stsp if (id2) {
1277 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1279 8469d821 2022-06-25 stsp goto done;
1280 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1282 8469d821 2022-06-25 stsp goto done;
1283 8469d821 2022-06-25 stsp free(idstr);
1284 8469d821 2022-06-25 stsp idstr = NULL;
1286 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1289 8469d821 2022-06-25 stsp goto done;
1292 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1293 c7d5c43c 2022-08-04 mark paths, label1, label2, diff_context, ignore_whitespace,
1294 1f3405c9 2023-01-17 mark force_text_diff, dsa, repo, outfile, diff_algo);
1296 8469d821 2022-06-25 stsp free(idstr);
1297 8469d821 2022-06-25 stsp return err;
1300 8469d821 2022-06-25 stsp const struct got_error *
1301 c7d5c43c 2022-08-04 mark got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1302 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1303 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1304 4b752015 2022-06-30 stsp struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1305 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1306 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1308 11528a82 2018-05-19 stsp const struct got_error *err;
1309 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1310 8469d821 2022-06-25 stsp char *idstr = NULL;
1312 15a94983 2018-12-23 stsp if (id2 == NULL)
1313 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1315 15a94983 2018-12-23 stsp if (id1) {
1316 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1318 cd0acaa7 2018-05-20 stsp goto done;
1319 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1321 8469d821 2022-06-25 stsp goto done;
1322 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', idstr,
1325 8469d821 2022-06-25 stsp goto done;
1326 8469d821 2022-06-25 stsp free(idstr);
1327 8469d821 2022-06-25 stsp idstr = NULL;
1329 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1332 8469d821 2022-06-25 stsp goto done;
1335 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1337 9b697879 2018-05-20 stsp goto done;
1339 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1341 8469d821 2022-06-25 stsp goto done;
1342 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1344 8469d821 2022-06-25 stsp goto done;
1346 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1347 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1348 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1349 1f3405c9 2023-01-17 mark diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1350 1f3405c9 2023-01-17 mark outfile, diff_algo);
1352 11528a82 2018-05-19 stsp if (commit1)
1353 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1354 11528a82 2018-05-19 stsp if (commit2)
1355 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1356 8469d821 2022-06-25 stsp free(idstr);
1357 11528a82 2018-05-19 stsp return err;
1360 dc424a06 2019-08-07 stsp const struct got_error *
1361 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1362 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1363 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1364 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1366 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1367 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1369 fe621944 2020-11-10 stsp if (resultp)
1370 fe621944 2020-11-10 stsp *resultp = NULL;
1372 dc424a06 2019-08-07 stsp if (outfile) {
1373 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1374 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1375 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1376 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1379 4b752015 2022-06-30 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1380 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1382 fe621944 2020-11-10 stsp goto done;
1384 fe621944 2020-11-10 stsp if (outfile) {
1385 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1386 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1387 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1389 dc424a06 2019-08-07 stsp goto done;
1393 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1394 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1395 fe621944 2020-11-10 stsp else if (diffreg_result) {
1396 fe621944 2020-11-10 stsp const struct got_error *free_err;
1398 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1399 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1400 fe621944 2020-11-10 stsp err = free_err;
1403 dc424a06 2019-08-07 stsp return err;