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 a76e88e5 2023-01-10 mark return err;
120 a76e88e5 2023-01-10 mark pe = TAILQ_LAST(ds->paths, got_pathlist_head);
121 a76e88e5 2023-01-10 mark diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
122 a76e88e5 2023-01-10 mark pe->path_len, change->add, change->rm);
124 a76e88e5 2023-01-10 mark return NULL;
127 a76e88e5 2023-01-10 mark static const struct got_error *
128 c7d5c43c 2022-08-04 mark diff_blobs(struct got_diff_line **lines, size_t *nlines,
129 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
130 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
131 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
132 a76e88e5 2023-01-10 mark int diff_context, int ignore_whitespace, int force_text_diff,
133 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
134 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
136 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
137 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
138 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
139 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
140 be659d10 2020-11-18 stsp off_t size1, size2;
141 3846622f 2023-01-07 mark struct got_diffreg_result *result = NULL;
142 fe621944 2020-11-10 stsp off_t outoff = 0;
145 2d61e381 2022-08-30 mark if (lines && *lines && *nlines > 0)
146 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
147 c7d5c43c 2022-08-04 mark else if (lines) {
148 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
153 fe621944 2020-11-10 stsp if (resultp)
154 fe621944 2020-11-10 stsp *resultp = NULL;
157 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
162 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
168 98abbc84 2017-11-30 stsp if (blob1) {
169 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
170 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
175 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
178 98abbc84 2017-11-30 stsp if (blob2) {
179 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
180 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
185 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
187 09de383e 2018-12-24 stsp if (outfile) {
188 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
189 40dde666 2020-07-23 stsp int modebits;
190 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
191 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
192 40dde666 2020-07-23 stsp modebits = S_IFLNK;
194 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
195 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
196 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
197 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
201 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
202 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
203 40dde666 2020-07-23 stsp modebits = S_IFLNK;
205 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
206 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
207 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
208 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
212 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
213 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
216 fe621944 2020-11-10 stsp outoff += n;
217 c7d5c43c 2022-08-04 mark if (lines) {
218 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
219 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_MIN);
224 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
225 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
228 fe621944 2020-11-10 stsp outoff += n;
229 c7d5c43c 2022-08-04 mark if (lines) {
230 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
231 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_PLUS);
236 46f68b20 2019-10-19 stsp free(modestr1);
237 46f68b20 2019-10-19 stsp free(modestr2);
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 a76e88e5 2023-01-10 mark if (show_diffstat) {
246 a76e88e5 2023-01-10 mark char *path = NULL;
247 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
250 6b37f13a 2023-01-10 mark * Ignore 'm'ode status change: if there's no accompanying
251 6b37f13a 2023-01-10 mark * content change, there'll be no diffstat, and if there
252 6b37f13a 2023-01-10 mark * are actual changes, 'M'odified takes precedence.
254 6b37f13a 2023-01-10 mark if (blob1 == NULL)
255 6b37f13a 2023-01-10 mark status = GOT_STATUS_ADD;
256 6b37f13a 2023-01-10 mark else if (blob2 == NULL)
257 6b37f13a 2023-01-10 mark status = GOT_STATUS_DELETE;
259 6b37f13a 2023-01-10 mark status = GOT_STATUS_MODIFY;
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 a76e88e5 2023-01-10 mark err = get_diffstat(ds, path, result->result, force_text_diff,
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 fe621944 2020-11-10 stsp if (resultp && err == NULL)
300 fe621944 2020-11-10 stsp *resultp = result;
301 3846622f 2023-01-07 mark else if (result) {
302 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
303 fe621944 2020-11-10 stsp if (free_err && err == NULL)
304 fe621944 2020-11-10 stsp err = free_err;
307 7d283eee 2017-11-29 stsp return err;
310 aaa13589 2019-06-01 stsp const struct got_error *
311 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
312 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
313 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
314 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
315 b72706c3 2022-06-01 stsp struct got_repository *repo)
317 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
319 c7d5c43c 2022-08-04 mark return diff_blobs(&a->lines, &a->nlines, NULL,
320 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
321 a76e88e5 2023-01-10 mark a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
322 a76e88e5 2023-01-10 mark a->diffstat, a->outfile, a->diff_algo);
325 404c43c4 2018-06-21 stsp const struct got_error *
326 c7d5c43c 2022-08-04 mark got_diff_blob(struct got_diff_line **lines, size_t*nlines,
327 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
328 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
329 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
330 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
331 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
333 c7d5c43c 2022-08-04 mark return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
334 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
335 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, ds, outfile, diff_algo);
338 7f1f93af 2019-08-06 stsp static const struct got_error *
339 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
340 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
341 c87842d5 2022-09-23 mark FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
342 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
343 a76e88e5 2023-01-10 mark int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
344 a76e88e5 2023-01-10 mark FILE *outfile)
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 a76e88e5 2023-01-10 mark if (show_diffstat) {
394 a76e88e5 2023-01-10 mark char *path = NULL;
395 a76e88e5 2023-01-10 mark int status = GOT_STATUS_NO_CHANGE;
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 a76e88e5 2023-01-10 mark err = get_diffstat(ds, path, result->result, force_text_diff,
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 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
443 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, FILE *outfile)
445 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
446 c87842d5 2022-09-23 mark sb2, label2, diff_algo, diff_context, ignore_whitespace,
447 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, ds, outfile);
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 f6861a81 2018-09-13 stsp te2->name) == -1)
900 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
905 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
907 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
908 1de5e065 2019-06-01 stsp te1->name);
912 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
913 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
915 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
917 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
918 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
924 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
926 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
927 1de5e065 2019-06-01 stsp te2->name);
930 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
931 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
933 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
935 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
936 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
938 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
940 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
941 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
950 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
952 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
953 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
954 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
960 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
962 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
963 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
964 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
966 474b4f94 2017-11-30 stsp } while (te1 || te2);
968 11528a82 2018-05-19 stsp return err;
971 11528a82 2018-05-19 stsp const struct got_error *
972 c7d5c43c 2022-08-04 mark got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
973 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
974 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
975 4b752015 2022-06-30 stsp const char *label1, const char *label2,
976 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
977 a76e88e5 2023-01-10 mark int ignore_whitespace, int force_text_diff, int show_diffstat,
978 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
980 11528a82 2018-05-19 stsp const struct got_error *err;
981 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
983 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
984 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
987 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
992 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
996 c7d5c43c 2022-08-04 mark err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
997 c7d5c43c 2022-08-04 mark diff_algo, diff_context, ignore_whitespace, force_text_diff,
998 a76e88e5 2023-01-10 mark show_diffstat, ds, outfile);
1000 67b631c9 2021-10-10 stsp if (blob1)
1001 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1002 67b631c9 2021-10-10 stsp if (blob2)
1003 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1004 67b631c9 2021-10-10 stsp return err;
1007 67b631c9 2021-10-10 stsp static const struct got_error *
1008 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1009 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1010 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1012 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
1013 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
1014 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1015 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1016 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1018 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
1019 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1020 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
1022 67b631c9 2021-10-10 stsp free(id1);
1023 67b631c9 2021-10-10 stsp id1 = NULL;
1024 67b631c9 2021-10-10 stsp free(id2);
1025 67b631c9 2021-10-10 stsp id2 = NULL;
1026 67b631c9 2021-10-10 stsp if (subtree1) {
1027 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1028 67b631c9 2021-10-10 stsp subtree1 = NULL;
1030 67b631c9 2021-10-10 stsp if (subtree2) {
1031 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1032 67b631c9 2021-10-10 stsp subtree2 = NULL;
1034 67b631c9 2021-10-10 stsp if (blob1) {
1035 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
1036 67b631c9 2021-10-10 stsp blob1 = NULL;
1038 67b631c9 2021-10-10 stsp if (blob2) {
1039 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
1040 67b631c9 2021-10-10 stsp blob2 = NULL;
1043 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1044 67b631c9 2021-10-10 stsp pe->path);
1045 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1046 67b631c9 2021-10-10 stsp goto done;
1047 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1048 67b631c9 2021-10-10 stsp pe->path);
1049 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1050 67b631c9 2021-10-10 stsp goto done;
1051 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
1052 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1053 67b631c9 2021-10-10 stsp goto done;
1055 67b631c9 2021-10-10 stsp if (id1) {
1056 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
1058 67b631c9 2021-10-10 stsp goto done;
1060 67b631c9 2021-10-10 stsp if (id2) {
1061 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
1063 67b631c9 2021-10-10 stsp goto done;
1065 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
1066 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
1067 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1068 67b631c9 2021-10-10 stsp goto done;
1069 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
1070 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1071 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1072 67b631c9 2021-10-10 stsp goto done;
1075 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
1076 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
1077 67b631c9 2021-10-10 stsp if (id1) {
1078 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
1079 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
1081 67b631c9 2021-10-10 stsp goto done;
1083 67b631c9 2021-10-10 stsp if (id2) {
1084 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
1085 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
1087 67b631c9 2021-10-10 stsp goto done;
1089 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1090 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1091 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1092 67b631c9 2021-10-10 stsp mode1, mode2, repo);
1094 67b631c9 2021-10-10 stsp goto done;
1095 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
1096 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
1097 67b631c9 2021-10-10 stsp if (id1) {
1098 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
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_tree(&subtree2, repo,
1107 67b631c9 2021-10-10 stsp goto done;
1109 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
1111 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
1112 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
1113 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
1115 67b631c9 2021-10-10 stsp goto done;
1117 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1118 67b631c9 2021-10-10 stsp goto done;
1122 67b631c9 2021-10-10 stsp free(id1);
1123 67b631c9 2021-10-10 stsp free(id2);
1124 67b631c9 2021-10-10 stsp if (subtree1)
1125 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
1126 67b631c9 2021-10-10 stsp if (subtree2)
1127 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
1128 11528a82 2018-05-19 stsp if (blob1)
1129 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1130 11528a82 2018-05-19 stsp if (blob2)
1131 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1132 474b4f94 2017-11-30 stsp return err;
1135 8469d821 2022-06-25 stsp static const struct got_error *
1136 c7d5c43c 2022-08-04 mark show_object_id(struct got_diff_line **lines, size_t *nlines,
1137 c7d5c43c 2022-08-04 mark const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1139 8469d821 2022-06-25 stsp const struct got_error *err;
1141 8469d821 2022-06-25 stsp off_t outoff = 0;
1143 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1145 36e83e5e 2022-08-16 op return got_error_from_errno("fprintf");
1147 c7d5c43c 2022-08-04 mark if (lines != NULL && *lines != NULL) {
1148 8469d821 2022-06-25 stsp if (*nlines == 0) {
1149 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0,
1150 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1152 8469d821 2022-06-25 stsp return err;
1154 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
1156 8469d821 2022-06-25 stsp outoff += n;
1157 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
1158 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
1160 8469d821 2022-06-25 stsp return err;
1163 8469d821 2022-06-25 stsp return NULL;
1166 8469d821 2022-06-25 stsp static const struct got_error *
1167 c7d5c43c 2022-08-04 mark diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1168 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1169 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1170 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1171 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1172 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *dsa,
1173 4b752015 2022-06-30 stsp struct got_repository *repo, FILE *outfile,
1174 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
1176 11528a82 2018-05-19 stsp const struct got_error *err;
1177 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1178 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1179 c7d5c43c 2022-08-04 mark int want_linemeta = (lines != NULL && *lines != NULL);
1181 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1182 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1184 15a94983 2018-12-23 stsp if (id1) {
1185 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1187 cd0acaa7 2018-05-20 stsp goto done;
1189 15a94983 2018-12-23 stsp if (id2) {
1190 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1192 cd0acaa7 2018-05-20 stsp goto done;
1195 4b752015 2022-06-30 stsp arg.diff_algo = diff_algo;
1196 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1197 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1198 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1199 a76e88e5 2023-01-10 mark arg.show_diffstat = show_diffstat;
1200 a76e88e5 2023-01-10 mark arg.diffstat = dsa;
1201 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1202 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1203 c7d5c43c 2022-08-04 mark arg.lines = *lines;
1204 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1206 c7d5c43c 2022-08-04 mark arg.lines = NULL;
1207 fe621944 2020-11-10 stsp arg.nlines = 0;
1209 a76e88e5 2023-01-10 mark if (paths == NULL || TAILQ_EMPTY(paths))
1210 a76e88e5 2023-01-10 mark err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1211 a76e88e5 2023-01-10 mark label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1213 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1214 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1215 c7d5c43c 2022-08-04 mark if (want_linemeta) {
1216 c7d5c43c 2022-08-04 mark *lines = arg.lines; /* was likely re-allocated */
1217 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1220 11528a82 2018-05-19 stsp if (tree1)
1221 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1222 11528a82 2018-05-19 stsp if (tree2)
1223 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1224 11528a82 2018-05-19 stsp return err;
1227 11528a82 2018-05-19 stsp const struct got_error *
1228 c7d5c43c 2022-08-04 mark got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1229 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1230 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1231 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1232 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1233 a76e88e5 2023-01-10 mark int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1234 a76e88e5 2023-01-10 mark struct got_repository *repo, FILE *outfile)
1236 8469d821 2022-06-25 stsp const struct got_error *err;
1237 8469d821 2022-06-25 stsp char *idstr = NULL;
1239 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1240 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1242 8469d821 2022-06-25 stsp if (id1) {
1243 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1245 8469d821 2022-06-25 stsp goto done;
1246 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1248 8469d821 2022-06-25 stsp goto done;
1249 8469d821 2022-06-25 stsp free(idstr);
1250 8469d821 2022-06-25 stsp idstr = NULL;
1252 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1255 8469d821 2022-06-25 stsp goto done;
1258 8469d821 2022-06-25 stsp if (id2) {
1259 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1261 8469d821 2022-06-25 stsp goto done;
1262 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1264 8469d821 2022-06-25 stsp goto done;
1265 8469d821 2022-06-25 stsp free(idstr);
1266 8469d821 2022-06-25 stsp idstr = NULL;
1268 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1271 8469d821 2022-06-25 stsp goto done;
1274 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1275 c7d5c43c 2022-08-04 mark paths, label1, label2, diff_context, ignore_whitespace,
1276 a76e88e5 2023-01-10 mark force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1278 8469d821 2022-06-25 stsp free(idstr);
1279 8469d821 2022-06-25 stsp return err;
1282 8469d821 2022-06-25 stsp const struct got_error *
1283 c7d5c43c 2022-08-04 mark got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1284 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1285 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1286 4b752015 2022-06-30 stsp struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1287 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1288 a76e88e5 2023-01-10 mark int show_diffstat, struct got_diffstat_cb_arg *dsa,
1289 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1291 11528a82 2018-05-19 stsp const struct got_error *err;
1292 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1293 8469d821 2022-06-25 stsp char *idstr = NULL;
1295 15a94983 2018-12-23 stsp if (id2 == NULL)
1296 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1298 15a94983 2018-12-23 stsp if (id1) {
1299 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1301 cd0acaa7 2018-05-20 stsp goto done;
1302 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1304 8469d821 2022-06-25 stsp goto done;
1305 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', idstr,
1308 8469d821 2022-06-25 stsp goto done;
1309 8469d821 2022-06-25 stsp free(idstr);
1310 8469d821 2022-06-25 stsp idstr = NULL;
1312 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1315 8469d821 2022-06-25 stsp goto done;
1318 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1320 9b697879 2018-05-20 stsp goto done;
1322 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1324 8469d821 2022-06-25 stsp goto done;
1325 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1327 8469d821 2022-06-25 stsp goto done;
1329 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1330 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1331 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1332 a76e88e5 2023-01-10 mark diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1333 a76e88e5 2023-01-10 mark dsa, repo, outfile, diff_algo);
1335 11528a82 2018-05-19 stsp if (commit1)
1336 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1337 11528a82 2018-05-19 stsp if (commit2)
1338 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1339 8469d821 2022-06-25 stsp free(idstr);
1340 11528a82 2018-05-19 stsp return err;
1343 dc424a06 2019-08-07 stsp const struct got_error *
1344 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1345 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1346 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1347 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1349 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1350 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1352 fe621944 2020-11-10 stsp if (resultp)
1353 fe621944 2020-11-10 stsp *resultp = NULL;
1355 dc424a06 2019-08-07 stsp if (outfile) {
1356 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1357 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1358 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1359 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1362 4b752015 2022-06-30 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1363 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1365 fe621944 2020-11-10 stsp goto done;
1367 fe621944 2020-11-10 stsp if (outfile) {
1368 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1369 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1370 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1372 dc424a06 2019-08-07 stsp goto done;
1376 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1377 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1378 fe621944 2020-11-10 stsp else if (diffreg_result) {
1379 fe621944 2020-11-10 stsp const struct got_error *free_err;
1380 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1381 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1382 fe621944 2020-11-10 stsp err = free_err;
1385 dc424a06 2019-08-07 stsp return err;