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 7d283eee 2017-11-29 stsp #include <zlib.h>
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"
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"
42 5191b70b 2023-01-07 mark #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
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)
49 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
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");
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)++;
59 fe621944 2020-11-10 stsp return NULL;
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)
66 a76e88e5 2023-01-10 mark int d1 = 1, d2 = 1;
69 a76e88e5 2023-01-10 mark *maxlen = MAX(*maxlen, len);
71 a76e88e5 2023-01-10 mark while (add /= 10)
73 a76e88e5 2023-01-10 mark *add_cols = MAX(*add_cols, d1);
75 a76e88e5 2023-01-10 mark while (rm /= 10)
77 a76e88e5 2023-01-10 mark *rm_cols = MAX(*rm_cols, d2);
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)
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);
91 a76e88e5 2023-01-10 mark change = calloc(1, sizeof(*change));
92 a76e88e5 2023-01-10 mark if (change == NULL)
93 6b37f13a 2023-01-10 mark return got_error_from_errno("calloc");
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;
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);
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;
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;
116 a76e88e5 2023-01-10 mark err = got_pathlist_append(ds->paths, path, change);
118 5fa52d6c 2023-01-11 mark free(change);
119 a76e88e5 2023-01-10 mark return err;
122 a76e88e5 2023-01-10 mark pe = TAILQ_LAST(ds->paths, got_pathlist_head);
123 a76e88e5 2023-01-10 mark diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
124 a76e88e5 2023-01-10 mark pe->path_len, change->add, change->rm);
126 a76e88e5 2023-01-10 mark return NULL;
129 a76e88e5 2023-01-10 mark static const struct got_error *
130 c7d5c43c 2022-08-04 mark diff_blobs(struct got_diff_line **lines, size_t *nlines,
131 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
132 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
133 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
134 a76e88e5 2023-01-10 mark int diff_context, int ignore_whitespace, int force_text_diff,
135 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *diffstat, FILE *outfile,
136 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
138 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
139 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
140 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
141 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
142 5fa52d6c 2023-01-11 mark char *modestr1 = NULL, *modestr2 = NULL;
143 be659d10 2020-11-18 stsp off_t size1, size2;
144 3846622f 2023-01-07 mark struct got_diffreg_result *result = NULL;
145 fe621944 2020-11-10 stsp off_t outoff = 0;
148 2d61e381 2022-08-30 mark if (lines && *lines && *nlines > 0)
149 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
150 c7d5c43c 2022-08-04 mark else if (lines) {
151 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
156 fe621944 2020-11-10 stsp if (resultp)
157 fe621944 2020-11-10 stsp *resultp = NULL;
160 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
165 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
171 98abbc84 2017-11-30 stsp if (blob1) {
172 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
173 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
178 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
181 98abbc84 2017-11-30 stsp if (blob2) {
182 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
183 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
188 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
190 09de383e 2018-12-24 stsp if (outfile) {
191 40dde666 2020-07-23 stsp int modebits;
193 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
194 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
195 40dde666 2020-07-23 stsp modebits = S_IFLNK;
197 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
198 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
199 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
200 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
204 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
205 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
206 40dde666 2020-07-23 stsp modebits = S_IFLNK;
208 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
209 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
210 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
211 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
215 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
216 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
219 fe621944 2020-11-10 stsp outoff += n;
220 c7d5c43c 2022-08-04 mark if (lines) {
221 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
222 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_MIN);
227 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
228 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
231 fe621944 2020-11-10 stsp outoff += n;
232 c7d5c43c 2022-08-04 mark if (lines) {
233 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
234 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_PLUS);
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);
245 1f3405c9 2023-01-17 mark if (diffstat) {
246 a76e88e5 2023-01-10 mark char *path = NULL;
247 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
249 6b37f13a 2023-01-10 mark if (blob1 == NULL)
250 6b37f13a 2023-01-10 mark status = GOT_STATUS_ADD;
251 6b37f13a 2023-01-10 mark else if (blob2 == NULL)
252 6b37f13a 2023-01-10 mark status = GOT_STATUS_DELETE;
254 1f3405c9 2023-01-17 mark if (strcmp(idstr1, idstr2) != 0)
255 1f3405c9 2023-01-17 mark status = GOT_STATUS_MODIFY;
256 1f3405c9 2023-01-17 mark else if (mode1 != mode2)
257 1f3405c9 2023-01-17 mark status = GOT_STATUS_MODE_CHANGE;
260 a76e88e5 2023-01-10 mark if (label1 == NULL && label2 == NULL) {
261 a76e88e5 2023-01-10 mark /* diffstat of blobs, show hash instead of path */
262 a76e88e5 2023-01-10 mark if (asprintf(&path, "%.10s -> %.10s",
263 a76e88e5 2023-01-10 mark idstr1, idstr2) == -1) {
264 a76e88e5 2023-01-10 mark err = got_error_from_errno("asprintf");
268 6b37f13a 2023-01-10 mark if (label2 != NULL &&
269 6b37f13a 2023-01-10 mark (status != GOT_STATUS_DELETE || label1 == NULL))
270 6b37f13a 2023-01-10 mark path = strdup(label2);
272 6b37f13a 2023-01-10 mark path = strdup(label1);
273 a76e88e5 2023-01-10 mark if (path == NULL) {
274 6b37f13a 2023-01-10 mark err = got_error_from_errno("strdup");
279 1f3405c9 2023-01-17 mark err = get_diffstat(diffstat, path, result->result,
280 1f3405c9 2023-01-17 mark force_text_diff, status);
282 a76e88e5 2023-01-10 mark free(path);
287 fe621944 2020-11-10 stsp if (outfile) {
288 c7d5c43c 2022-08-04 mark err = got_diffreg_output(lines, nlines, result,
289 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
290 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
291 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
292 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
298 5fa52d6c 2023-01-11 mark free(modestr1);
299 5fa52d6c 2023-01-11 mark free(modestr2);
300 fe621944 2020-11-10 stsp if (resultp && err == NULL)
301 fe621944 2020-11-10 stsp *resultp = result;
302 3846622f 2023-01-07 mark else if (result) {
303 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
304 fe621944 2020-11-10 stsp if (free_err && err == NULL)
305 fe621944 2020-11-10 stsp err = free_err;
308 7d283eee 2017-11-29 stsp return err;
311 aaa13589 2019-06-01 stsp const struct got_error *
312 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
313 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
314 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
315 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
316 b72706c3 2022-06-01 stsp struct got_repository *repo)
318 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
320 c7d5c43c 2022-08-04 mark return diff_blobs(&a->lines, &a->nlines, NULL,
321 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
322 1f3405c9 2023-01-17 mark a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
323 1f3405c9 2023-01-17 mark a->diff_algo);
326 404c43c4 2018-06-21 stsp const struct got_error *
327 c7d5c43c 2022-08-04 mark got_diff_blob(struct got_diff_line **lines, size_t*nlines,
328 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
329 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
330 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
331 1f3405c9 2023-01-17 mark int ignore_whitespace, int force_text_diff,
332 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
334 c7d5c43c 2022-08-04 mark return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
335 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
336 1f3405c9 2023-01-17 mark force_text_diff, ds, outfile, diff_algo);
339 7f1f93af 2019-08-06 stsp static const struct got_error *
340 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
341 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
342 c87842d5 2022-09-23 mark FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
343 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
344 1f3405c9 2023-01-17 mark int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
346 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
347 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
348 58e31a80 2022-06-27 op const char *idstr1 = NULL;
349 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
351 fe621944 2020-11-10 stsp if (resultp)
352 fe621944 2020-11-10 stsp *resultp = NULL;
355 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
357 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
359 7f1f93af 2019-08-06 stsp if (outfile) {
360 c87842d5 2022-09-23 mark char *mode = NULL;
362 c87842d5 2022-09-23 mark /* display file mode for new added files only */
363 c87842d5 2022-09-23 mark if (f2_exists && blob1 == NULL) {
364 c87842d5 2022-09-23 mark int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
366 c87842d5 2022-09-23 mark if (S_ISLNK(sb2->st_mode))
367 c87842d5 2022-09-23 mark mmask = S_IFLNK;
368 c87842d5 2022-09-23 mark if (asprintf(&mode, " (mode %o)",
369 c87842d5 2022-09-23 mark sb2->st_mode & mmask) == -1)
370 c87842d5 2022-09-23 mark return got_error_from_errno("asprintf");
372 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
373 c87842d5 2022-09-23 mark fprintf(outfile, "file + %s%s\n",
374 c87842d5 2022-09-23 mark f2_exists ? label2 : "/dev/null", mode ? mode : "");
375 c87842d5 2022-09-23 mark free(mode);
378 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
379 4b752015 2022-06-30 stsp force_text_diff);
383 fe621944 2020-11-10 stsp if (outfile) {
384 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
385 49d4a017 2022-06-30 stsp blob1 != NULL, f2_exists,
386 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
387 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
388 1cb46f00 2020-11-21 stsp diff_context, outfile);
393 1f3405c9 2023-01-17 mark if (diffstat) {
394 a76e88e5 2023-01-10 mark char *path = NULL;
395 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
398 a76e88e5 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
399 a76e88e5 2023-01-10 mark * content change, there'll be no diffstat, and if there
400 a76e88e5 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
402 a76e88e5 2023-01-10 mark if (blob1 == NULL)
403 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
404 a76e88e5 2023-01-10 mark else if (!f2_exists)
405 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
407 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
409 6b37f13a 2023-01-10 mark if (label2 != NULL &&
410 6b37f13a 2023-01-10 mark (status != GOT_STATUS_DELETE || label1 == NULL))
411 6b37f13a 2023-01-10 mark path = strdup(label2);
413 6b37f13a 2023-01-10 mark path = strdup(label1);
414 6b37f13a 2023-01-10 mark if (path == NULL) {
415 6b37f13a 2023-01-10 mark err = got_error_from_errno("strdup");
419 1f3405c9 2023-01-17 mark err = get_diffstat(diffstat, path, result->result,
420 1f3405c9 2023-01-17 mark force_text_diff, status);
422 a76e88e5 2023-01-10 mark free(path);
428 fe621944 2020-11-10 stsp if (resultp && err == NULL)
429 fe621944 2020-11-10 stsp *resultp = result;
430 fe621944 2020-11-10 stsp else if (result) {
431 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
432 fe621944 2020-11-10 stsp if (free_err && err == NULL)
433 fe621944 2020-11-10 stsp err = free_err;
435 b72f483a 2019-02-05 stsp return err;
438 7f1f93af 2019-08-06 stsp const struct got_error *
439 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
440 c87842d5 2022-09-23 mark const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
441 4b752015 2022-06-30 stsp const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
442 1f3405c9 2023-01-17 mark int ignore_whitespace, int force_text_diff,
443 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
445 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
446 c87842d5 2022-09-23 mark sb2, label2, diff_algo, diff_context, ignore_whitespace,
447 1f3405c9 2023-01-17 mark force_text_diff, ds, outfile);
450 474b4f94 2017-11-30 stsp static const struct got_error *
451 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
452 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
453 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
455 4e22badc 2017-11-30 stsp const struct got_error *err;
456 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
457 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
459 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
461 4e22badc 2017-11-30 stsp return err;
463 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
466 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
467 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
469 2acfca77 2018-04-01 stsp got_object_close(obj);
471 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
472 2acfca77 2018-04-01 stsp return err;
475 474b4f94 2017-11-30 stsp static const struct got_error *
476 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
477 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
478 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
479 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
480 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
482 6a213ccb 2017-11-30 stsp const struct got_error *err;
483 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
484 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
485 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
486 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
488 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
490 730a8aa0 2018-04-24 stsp return err;
492 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
493 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
497 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
500 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
501 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
509 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
513 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
514 b72706c3 2022-06-01 stsp mode1, mode2, repo);
517 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
519 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
521 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
523 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
524 6a213ccb 2017-11-30 stsp return err;
527 474b4f94 2017-11-30 stsp static const struct got_error *
528 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
529 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
530 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg)
532 365fb436 2017-11-30 stsp const struct got_error *err;
533 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
534 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
536 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
538 365fb436 2017-11-30 stsp return err;
540 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
543 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
544 b72706c3 2022-06-01 stsp mode, 0, repo);
546 2acfca77 2018-04-01 stsp got_object_close(obj);
548 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
549 2acfca77 2018-04-01 stsp return err;
552 474b4f94 2017-11-30 stsp static const struct got_error *
553 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
554 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
555 49d4a017 2022-06-30 stsp void *cb_arg, int diff_content)
557 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
558 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
559 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
561 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
565 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
566 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
570 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
574 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
575 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
578 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
579 9c70d4c3 2017-11-30 stsp if (treeobj)
580 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
581 9c70d4c3 2017-11-30 stsp return err;
584 474b4f94 2017-11-30 stsp static const struct got_error *
585 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
586 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
587 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
588 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
589 b72706c3 2022-06-01 stsp int diff_content)
591 f6861a81 2018-09-13 stsp const struct got_error *err;
592 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
593 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
594 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
595 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
597 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
601 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
602 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
606 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
610 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
611 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
615 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
619 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
623 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
624 f9d37699 2022-06-28 stsp label1, label2, repo, cb, cb_arg, diff_content);
628 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
630 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
631 789689b5 2017-11-30 stsp if (treeobj1)
632 789689b5 2017-11-30 stsp got_object_close(treeobj1);
633 789689b5 2017-11-30 stsp if (treeobj2)
634 789689b5 2017-11-30 stsp got_object_close(treeobj2);
635 789689b5 2017-11-30 stsp return err;
638 474b4f94 2017-11-30 stsp static const struct got_error *
639 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
640 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
641 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
643 f6861a81 2018-09-13 stsp const struct got_error *err;
644 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
645 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
647 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
651 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
652 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
656 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
660 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
661 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
664 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
665 2c56f2ce 2017-11-30 stsp if (treeobj)
666 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
667 2c56f2ce 2017-11-30 stsp return err;
670 474b4f94 2017-11-30 stsp static const struct got_error *
671 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
672 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
673 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
675 013404a9 2017-11-30 stsp /* XXX TODO */
676 474b4f94 2017-11-30 stsp return NULL;
679 474b4f94 2017-11-30 stsp static const struct got_error *
680 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
681 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
682 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
683 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
684 31b4484f 2019-07-27 stsp int diff_content)
686 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
687 19ae6da1 2018-11-05 stsp int id_match;
689 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
690 63c5ca5d 2019-08-24 stsp return NULL;
692 474b4f94 2017-11-30 stsp if (te2 == NULL) {
693 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
694 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
695 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
697 31b4484f 2019-07-27 stsp if (diff_content)
698 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
699 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
701 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
702 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
703 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
705 f6861a81 2018-09-13 stsp return err;
706 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
707 63c5ca5d 2019-08-24 stsp return NULL;
709 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
710 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
711 19ae6da1 2018-11-05 stsp if (!id_match)
712 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
713 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
714 f9d37699 2022-06-28 stsp diff_content);
715 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
716 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
717 46f68b20 2019-10-19 stsp if (!id_match ||
718 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
719 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
720 31b4484f 2019-07-27 stsp if (diff_content)
721 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
722 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
723 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
725 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
726 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
727 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
731 19ae6da1 2018-11-05 stsp if (id_match)
732 f6861a81 2018-09-13 stsp return NULL;
734 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
735 aaa13589 2019-06-01 stsp cb, cb_arg);
738 474b4f94 2017-11-30 stsp static const struct got_error *
739 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
740 49d4a017 2022-06-30 stsp struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
741 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
742 31b4484f 2019-07-27 stsp int diff_content)
744 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
745 63c5ca5d 2019-08-24 stsp return NULL;
747 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
748 f6861a81 2018-09-13 stsp return NULL;
750 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
751 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
752 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
754 31b4484f 2019-07-27 stsp if (diff_content)
755 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
756 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
758 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
759 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
762 0208f208 2020-05-05 stsp const struct got_error *
763 5191b70b 2023-01-07 mark got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
764 5191b70b 2023-01-07 mark struct got_blob_object *blob2, FILE *f1, FILE *f2,
765 5191b70b 2023-01-07 mark struct got_object_id *id1, struct got_object_id *id2,
766 5191b70b 2023-01-07 mark const char *label1, const char *label2,
767 5191b70b 2023-01-07 mark mode_t mode1, mode_t mode2, struct got_repository *repo)
769 5191b70b 2023-01-07 mark const struct got_error *err = NULL;
770 5191b70b 2023-01-07 mark struct got_diffreg_result *result = NULL;
771 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg *a = arg;
772 5191b70b 2023-01-07 mark char *path = NULL;
773 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
775 5191b70b 2023-01-07 mark path = strdup(label2 ? label2 : label1);
776 5191b70b 2023-01-07 mark if (path == NULL)
777 6b37f13a 2023-01-10 mark return got_error_from_errno("strdup");
779 5191b70b 2023-01-07 mark if (id1 == NULL)
780 a76e88e5 2023-01-10 mark status = GOT_STATUS_ADD;
781 5191b70b 2023-01-07 mark else if (id2 == NULL)
782 a76e88e5 2023-01-10 mark status = GOT_STATUS_DELETE;
784 5191b70b 2023-01-07 mark if (got_object_id_cmp(id1, id2) != 0)
785 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODIFY;
786 5191b70b 2023-01-07 mark else if (mode1 != mode2)
787 a76e88e5 2023-01-10 mark status = GOT_STATUS_MODE_CHANGE;
791 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f1);
796 5191b70b 2023-01-07 mark err = got_opentemp_truncate(f2);
801 5191b70b 2023-01-07 mark if (blob1) {
802 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
807 5191b70b 2023-01-07 mark if (blob2) {
808 5191b70b 2023-01-07 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
814 5191b70b 2023-01-07 mark err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
815 5191b70b 2023-01-07 mark a->force_text);
819 a76e88e5 2023-01-10 mark err = get_diffstat(a, path, result->result, a->force_text, status);
822 5191b70b 2023-01-07 mark if (result) {
823 5191b70b 2023-01-07 mark const struct got_error *free_err;
825 5191b70b 2023-01-07 mark free_err = got_diffreg_result_free(result);
826 5191b70b 2023-01-07 mark if (free_err && err == NULL)
827 5191b70b 2023-01-07 mark err = free_err;
830 5191b70b 2023-01-07 mark free(path);
831 5191b70b 2023-01-07 mark return err;
834 5191b70b 2023-01-07 mark const struct got_error *
835 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
836 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
837 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
838 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
839 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
841 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
842 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
843 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
844 0208f208 2020-05-05 stsp char *path = NULL;
846 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
847 0208f208 2020-05-05 stsp if (path == NULL)
848 6b37f13a 2023-01-10 mark return got_error_from_errno("strdup");
850 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
851 0208f208 2020-05-05 stsp if (change == NULL) {
852 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
856 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
857 0208f208 2020-05-05 stsp if (id1 == NULL)
858 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
859 0208f208 2020-05-05 stsp else if (id2 == NULL)
860 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
862 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
863 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
864 0208f208 2020-05-05 stsp else if (mode1 != mode2)
865 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
868 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
871 0208f208 2020-05-05 stsp free(path);
872 0208f208 2020-05-05 stsp free(change);
874 0208f208 2020-05-05 stsp return err;
877 474b4f94 2017-11-30 stsp const struct got_error *
878 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
879 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
880 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
881 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
882 b72706c3 2022-06-01 stsp int diff_content)
884 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
885 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
886 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
887 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
888 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
890 883f0469 2018-06-23 stsp if (tree1) {
891 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
892 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
893 f6861a81 2018-09-13 stsp te1->name) == -1)
894 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
896 883f0469 2018-06-23 stsp if (tree2) {
897 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
898 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
899 5fa52d6c 2023-01-11 mark te2->name) == -1) {
900 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
907 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
910 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
911 1de5e065 2019-06-01 stsp te1->name);
915 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
916 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te->name) == -1) {
917 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
922 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
923 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
929 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
932 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
933 1de5e065 2019-06-01 stsp te2->name);
938 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
939 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te->name) == -1) {
940 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
944 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
945 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te2->name) == -1) {
946 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
951 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
952 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
961 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
963 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
964 5fa52d6c 2023-01-11 mark label1[0] ? "/" : "", te1->name) == -1) {
965 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
974 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
976 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
977 5fa52d6c 2023-01-11 mark label2[0] ? "/" : "", te2->name) == -1) {
978 5fa52d6c 2023-01-11 mark err = got_error_from_errno("asprintf");
982 474b4f94 2017-11-30 stsp } while (te1 || te2);
987 11528a82 2018-05-19 stsp return err;
990 11528a82 2018-05-19 stsp const struct got_error *
991 c7d5c43c 2022-08-04 mark got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
992 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
993 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
994 4b752015 2022-06-30 stsp const char *label1, const char *label2,
995 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
996 1f3405c9 2023-01-17 mark int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
997 1f3405c9 2023-01-17 mark struct got_repository *repo, FILE *outfile)
999 11528a82 2018-05-19 stsp const struct got_error *err;
1000 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1002 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1003 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1005 15a94983 2018-12-23 stsp if (id1) {
1006 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1008 cd0acaa7 2018-05-20 stsp goto done;
1010 15a94983 2018-12-23 stsp if (id2) {
1011 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1013 cd0acaa7 2018-05-20 stsp goto done;
1015 c7d5c43c 2022-08-04 mark err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1016 c7d5c43c 2022-08-04 mark diff_algo, diff_context, ignore_whitespace, force_text_diff,
1017 1f3405c9 2023-01-17 mark ds, outfile);
1019 67b631c9 2021-10-10 stsp if (blob1)
1020 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1021 67b631c9 2021-10-10 stsp if (blob2)
1022 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1023 67b631c9 2021-10-10 stsp return err;
1026 67b631c9 2021-10-10 stsp static const struct got_error *
1027 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1028 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1029 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1031 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1032 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
1033 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1034 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1035 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1037 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
1038 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1039 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
1041 67b631c9 2021-10-10 stsp free(id1);
1042 67b631c9 2021-10-10 stsp id1 = NULL;
1043 67b631c9 2021-10-10 stsp free(id2);
1044 67b631c9 2021-10-10 stsp id2 = NULL;
1045 67b631c9 2021-10-10 stsp if (subtree1) {
1046 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1047 67b631c9 2021-10-10 stsp subtree1 = NULL;
1049 67b631c9 2021-10-10 stsp if (subtree2) {
1050 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1051 67b631c9 2021-10-10 stsp subtree2 = NULL;
1053 67b631c9 2021-10-10 stsp if (blob1) {
1054 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1055 67b631c9 2021-10-10 stsp blob1 = NULL;
1057 67b631c9 2021-10-10 stsp if (blob2) {
1058 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1059 67b631c9 2021-10-10 stsp blob2 = NULL;
1062 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1063 67b631c9 2021-10-10 stsp pe->path);
1064 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1065 67b631c9 2021-10-10 stsp goto done;
1066 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1067 67b631c9 2021-10-10 stsp pe->path);
1068 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1069 67b631c9 2021-10-10 stsp goto done;
1070 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
1071 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1072 67b631c9 2021-10-10 stsp goto done;
1074 67b631c9 2021-10-10 stsp if (id1) {
1075 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
1077 67b631c9 2021-10-10 stsp goto done;
1079 67b631c9 2021-10-10 stsp if (id2) {
1080 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
1082 67b631c9 2021-10-10 stsp goto done;
1084 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
1085 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
1086 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1087 67b631c9 2021-10-10 stsp goto done;
1088 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
1089 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1090 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1091 67b631c9 2021-10-10 stsp goto done;
1094 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
1095 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
1096 67b631c9 2021-10-10 stsp if (id1) {
1097 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
1098 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
1100 67b631c9 2021-10-10 stsp goto done;
1102 67b631c9 2021-10-10 stsp if (id2) {
1103 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
1104 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
1106 67b631c9 2021-10-10 stsp goto done;
1108 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1109 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1110 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1111 67b631c9 2021-10-10 stsp mode1, mode2, repo);
1113 67b631c9 2021-10-10 stsp goto done;
1114 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
1115 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
1116 67b631c9 2021-10-10 stsp if (id1) {
1117 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
1120 67b631c9 2021-10-10 stsp goto done;
1122 67b631c9 2021-10-10 stsp if (id2) {
1123 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
1126 67b631c9 2021-10-10 stsp goto done;
1128 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
1130 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1131 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1132 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
1134 67b631c9 2021-10-10 stsp goto done;
1136 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1137 67b631c9 2021-10-10 stsp goto done;
1141 67b631c9 2021-10-10 stsp free(id1);
1142 67b631c9 2021-10-10 stsp free(id2);
1143 67b631c9 2021-10-10 stsp if (subtree1)
1144 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1145 67b631c9 2021-10-10 stsp if (subtree2)
1146 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1147 11528a82 2018-05-19 stsp if (blob1)
1148 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1149 11528a82 2018-05-19 stsp if (blob2)
1150 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1151 474b4f94 2017-11-30 stsp return err;
1154 8469d821 2022-06-25 stsp static const struct got_error *
1155 c7d5c43c 2022-08-04 mark show_object_id(struct got_diff_line **lines, size_t *nlines,
1156 c7d5c43c 2022-08-04 mark const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1158 8469d821 2022-06-25 stsp const struct got_error *err;
1160 8469d821 2022-06-25 stsp off_t outoff = 0;
1162 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1164 36e83e5e 2022-08-16 op return got_error_from_errno("fprintf");
1166 c7d5c43c 2022-08-04 mark if (lines != NULL && *lines != NULL) {
1167 8469d821 2022-06-25 stsp if (*nlines == 0) {
1168 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0,
1169 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1171 8469d821 2022-06-25 stsp return err;
1173 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
1175 8469d821 2022-06-25 stsp outoff += n;
1176 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
1177 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1179 8469d821 2022-06-25 stsp return err;
1182 8469d821 2022-06-25 stsp return NULL;
1185 8469d821 2022-06-25 stsp static const struct got_error *
1186 c7d5c43c 2022-08-04 mark diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1187 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1188 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1189 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1190 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1191 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1192 1f3405c9 2023-01-17 mark FILE *outfile, enum got_diff_algorithm diff_algo)
1194 11528a82 2018-05-19 stsp const struct got_error *err;
1195 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1196 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1197 c7d5c43c 2022-08-04 mark int want_linemeta = (lines != NULL && *lines != NULL);
1199 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1200 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1202 15a94983 2018-12-23 stsp if (id1) {
1203 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1205 cd0acaa7 2018-05-20 stsp goto done;
1207 15a94983 2018-12-23 stsp if (id2) {
1208 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1210 cd0acaa7 2018-05-20 stsp goto done;
1213 4b752015 2022-06-30 stsp arg.diff_algo = diff_algo;
1214 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1215 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1216 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1217 a76e88e5 2023-01-10 mark arg.diffstat = dsa;
1218 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1219 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1220 c7d5c43c 2022-08-04 mark arg.lines = *lines;
1221 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1223 c7d5c43c 2022-08-04 mark arg.lines = NULL;
1224 fe621944 2020-11-10 stsp arg.nlines = 0;
1226 a76e88e5 2023-01-10 mark if (paths == NULL || TAILQ_EMPTY(paths))
1227 a76e88e5 2023-01-10 mark err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1228 a76e88e5 2023-01-10 mark label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1230 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1231 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1232 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1233 c7d5c43c 2022-08-04 mark *lines = arg.lines; /* was likely re-allocated */
1234 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1237 11528a82 2018-05-19 stsp if (tree1)
1238 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1239 11528a82 2018-05-19 stsp if (tree2)
1240 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1241 11528a82 2018-05-19 stsp return err;
1244 11528a82 2018-05-19 stsp const struct got_error *
1245 c7d5c43c 2022-08-04 mark got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1246 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1247 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1248 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1249 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1250 1f3405c9 2023-01-17 mark int force_text_diff, struct got_diffstat_cb_arg *dsa,
1251 a76e88e5 2023-01-10 mark struct got_repository *repo, FILE *outfile)
1253 8469d821 2022-06-25 stsp const struct got_error *err;
1254 8469d821 2022-06-25 stsp char *idstr = NULL;
1256 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1257 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1259 8469d821 2022-06-25 stsp if (id1) {
1260 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1262 8469d821 2022-06-25 stsp goto done;
1263 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1265 8469d821 2022-06-25 stsp goto done;
1266 8469d821 2022-06-25 stsp free(idstr);
1267 8469d821 2022-06-25 stsp idstr = NULL;
1269 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1272 8469d821 2022-06-25 stsp goto done;
1275 8469d821 2022-06-25 stsp if (id2) {
1276 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1278 8469d821 2022-06-25 stsp goto done;
1279 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1281 8469d821 2022-06-25 stsp goto done;
1282 8469d821 2022-06-25 stsp free(idstr);
1283 8469d821 2022-06-25 stsp idstr = NULL;
1285 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1288 8469d821 2022-06-25 stsp goto done;
1291 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1292 c7d5c43c 2022-08-04 mark paths, label1, label2, diff_context, ignore_whitespace,
1293 1f3405c9 2023-01-17 mark force_text_diff, dsa, repo, outfile, diff_algo);
1295 8469d821 2022-06-25 stsp free(idstr);
1296 8469d821 2022-06-25 stsp return err;
1299 8469d821 2022-06-25 stsp const struct got_error *
1300 c7d5c43c 2022-08-04 mark got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1301 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1302 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1303 4b752015 2022-06-30 stsp struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1304 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1305 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1307 11528a82 2018-05-19 stsp const struct got_error *err;
1308 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1309 8469d821 2022-06-25 stsp char *idstr = NULL;
1311 15a94983 2018-12-23 stsp if (id2 == NULL)
1312 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1314 15a94983 2018-12-23 stsp if (id1) {
1315 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1317 cd0acaa7 2018-05-20 stsp goto done;
1318 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1320 8469d821 2022-06-25 stsp goto done;
1321 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', idstr,
1324 8469d821 2022-06-25 stsp goto done;
1325 8469d821 2022-06-25 stsp free(idstr);
1326 8469d821 2022-06-25 stsp idstr = NULL;
1328 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1331 8469d821 2022-06-25 stsp goto done;
1334 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1336 9b697879 2018-05-20 stsp goto done;
1338 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1340 8469d821 2022-06-25 stsp goto done;
1341 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1343 8469d821 2022-06-25 stsp goto done;
1345 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1346 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1347 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1348 1f3405c9 2023-01-17 mark diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1349 1f3405c9 2023-01-17 mark outfile, diff_algo);
1351 11528a82 2018-05-19 stsp if (commit1)
1352 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1353 11528a82 2018-05-19 stsp if (commit2)
1354 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1355 8469d821 2022-06-25 stsp free(idstr);
1356 11528a82 2018-05-19 stsp return err;
1359 dc424a06 2019-08-07 stsp const struct got_error *
1360 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1361 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1362 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1363 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1365 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1366 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1368 fe621944 2020-11-10 stsp if (resultp)
1369 fe621944 2020-11-10 stsp *resultp = NULL;
1371 dc424a06 2019-08-07 stsp if (outfile) {
1372 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1373 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1374 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1375 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1378 4b752015 2022-06-30 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1379 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1381 fe621944 2020-11-10 stsp goto done;
1383 fe621944 2020-11-10 stsp if (outfile) {
1384 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1385 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1386 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1388 dc424a06 2019-08-07 stsp goto done;
1392 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1393 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1394 fe621944 2020-11-10 stsp else if (diffreg_result) {
1395 fe621944 2020-11-10 stsp const struct got_error *free_err;
1397 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1398 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1399 fe621944 2020-11-10 stsp err = free_err;
1402 dc424a06 2019-08-07 stsp return err;