add more info about the test suite to README.portable
[got-portable.git] / lib / diff.c
blob299edaaa7cb4bd79a85927c2b2227807da10bdaa
1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <zlib.h>
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
31 #include "got_diff.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_worktree.h"
35 #include "got_opentemp.h"
37 #include "got_lib_diff.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
42 #ifndef MAX
43 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
44 #endif
46 static const struct got_error *
47 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 off_t off, uint8_t type)
50 struct got_diff_line *p;
52 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 if (p == NULL)
54 return got_error_from_errno("reallocarray");
55 *lines = p;
56 (*lines)[*nlines].offset = off;
57 (*lines)[*nlines].type = type;
58 (*nlines)++;
60 return NULL;
63 static void
64 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
65 uint32_t add, uint32_t rm)
67 int d1 = 1, d2 = 1;
69 if (maxlen)
70 *maxlen = MAX(*maxlen, len);
72 while (add /= 10)
73 ++d1;
74 *add_cols = MAX(*add_cols, d1);
76 while (rm /= 10)
77 ++d2;
78 *rm_cols = MAX(*rm_cols, d2);
81 static const struct got_error *
82 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
83 struct diff_result *r, int force_text, int status)
85 const struct got_error *err;
86 struct got_pathlist_entry *pe;
87 struct got_diff_changed_path *change = NULL;
88 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
89 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
90 int i;
92 change = calloc(1, sizeof(*change));
93 if (change == NULL)
94 return got_error_from_errno("calloc");
96 if (!isbin || force_text) {
97 for (i = 0; i < r->chunks.len; ++i) {
98 struct diff_chunk *c;
99 int clc, crc;
101 c = diff_chunk_get(r, i);
102 clc = diff_chunk_get_left_count(c);
103 crc = diff_chunk_get_right_count(c);
105 if (crc && !clc)
106 change->add += crc;
107 if (clc && !crc)
108 change->rm += clc;
112 change->status = status;
113 ds->ins += change->add;
114 ds->del += change->rm;
115 ++ds->nfiles;
117 err = got_pathlist_insert(&pe, ds->paths, path, change);
118 if (err || pe == NULL) {
119 free(change);
120 if (err)
121 return err;
124 pe = RB_MAX(got_pathlist_head, ds->paths);
125 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
126 pe->path_len, change->add, change->rm);
128 return NULL;
131 static const struct got_error *
132 diff_blobs(struct got_diff_line **lines, size_t *nlines,
133 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
134 struct got_blob_object *blob2, FILE *f1, FILE *f2,
135 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
136 int diff_context, int ignore_whitespace, int force_text_diff,
137 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
138 enum got_diff_algorithm diff_algo)
140 const struct got_error *err = NULL, *free_err;
141 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
142 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
143 const char *idstr1 = NULL, *idstr2 = NULL;
144 char *modestr1 = NULL, *modestr2 = NULL;
145 off_t size1, size2;
146 struct got_diffreg_result *result = NULL;
147 off_t outoff = 0;
148 int n;
150 if (lines && *lines) {
151 if (*nlines > 0)
152 outoff = (*lines)[*nlines - 1].offset;
153 else {
154 err = add_line_metadata(lines, nlines,
155 0, GOT_DIFF_LINE_NONE);
156 if (err != NULL)
157 goto done;
161 if (resultp)
162 *resultp = NULL;
164 if (f1) {
165 err = got_opentemp_truncate(f1);
166 if (err)
167 goto done;
169 if (f2) {
170 err = got_opentemp_truncate(f2);
171 if (err)
172 goto done;
175 size1 = 0;
176 if (blob1) {
177 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
178 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
179 blob1);
180 if (err)
181 goto done;
182 } else
183 idstr1 = "/dev/null";
185 size2 = 0;
186 if (blob2) {
187 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
188 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
189 blob2);
190 if (err)
191 goto done;
192 } else
193 idstr2 = "/dev/null";
195 if (outfile) {
196 int modebits;
198 if (mode1 && mode1 != mode2) {
199 if (S_ISLNK(mode1))
200 modebits = S_IFLNK;
201 else
202 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
203 if (asprintf(&modestr1, " (mode %o)",
204 mode1 & modebits) == -1) {
205 err = got_error_from_errno("asprintf");
206 goto done;
209 if (mode2 && mode1 != mode2) {
210 if (S_ISLNK(mode2))
211 modebits = S_IFLNK;
212 else
213 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
214 if (asprintf(&modestr2, " (mode %o)",
215 mode2 & modebits) == -1) {
216 err = got_error_from_errno("asprintf");
217 goto done;
220 n = fprintf(outfile, "blob - %s%s\n", idstr1,
221 modestr1 ? modestr1 : "");
222 if (n < 0) {
223 err = got_error_from_errno("fprintf");
224 goto done;
226 outoff += n;
227 if (lines && *lines) {
228 err = add_line_metadata(lines, nlines, outoff,
229 GOT_DIFF_LINE_BLOB_MIN);
230 if (err)
231 goto done;
234 n = fprintf(outfile, "blob + %s%s\n", idstr2,
235 modestr2 ? modestr2 : "");
236 if (n < 0) {
237 err = got_error_from_errno("fprintf");
238 goto done;
240 outoff += n;
241 if (lines && *lines) {
242 err = add_line_metadata(lines, nlines, outoff,
243 GOT_DIFF_LINE_BLOB_PLUS);
244 if (err)
245 goto done;
249 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
250 force_text_diff);
251 if (err)
252 goto done;
254 if (diffstat) {
255 char *path = NULL;
256 int status = GOT_STATUS_NO_CHANGE;
258 if (blob1 == NULL)
259 status = GOT_STATUS_ADD;
260 else if (blob2 == NULL)
261 status = GOT_STATUS_DELETE;
262 else {
263 if (strcmp(idstr1, idstr2) != 0)
264 status = GOT_STATUS_MODIFY;
265 else if (mode1 != mode2)
266 status = GOT_STATUS_MODE_CHANGE;
269 if (label1 == NULL && label2 == NULL) {
270 /* diffstat of blobs, show hash instead of path */
271 if (asprintf(&path, "%.10s -> %.10s",
272 idstr1, idstr2) == -1) {
273 err = got_error_from_errno("asprintf");
274 goto done;
276 } else {
277 if (label2 != NULL &&
278 (status != GOT_STATUS_DELETE || label1 == NULL))
279 path = strdup(label2);
280 else
281 path = strdup(label1);
282 if (path == NULL) {
283 err = got_error_from_errno("strdup");
284 goto done;
288 err = get_diffstat(diffstat, path, result->result,
289 force_text_diff, status);
290 if (err) {
291 free(path);
292 goto done;
296 if (outfile) {
297 err = got_diffreg_output(lines, nlines, result,
298 blob1 != NULL, blob2 != NULL,
299 label1 ? label1 : idstr1,
300 label2 ? label2 : idstr2,
301 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
302 if (err)
303 goto done;
306 done:
307 free(modestr1);
308 free(modestr2);
309 if (resultp && err == NULL)
310 *resultp = result;
311 else if (result) {
312 free_err = got_diffreg_result_free(result);
313 if (free_err && err == NULL)
314 err = free_err;
317 return err;
320 const struct got_error *
321 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
322 struct got_blob_object *blob2, FILE *f1, FILE *f2,
323 struct got_object_id *id1, struct got_object_id *id2,
324 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
325 struct got_repository *repo)
327 struct got_diff_blob_output_unidiff_arg *a = arg;
329 return diff_blobs(&a->lines, &a->nlines, NULL,
330 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
331 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
332 a->diff_algo);
335 const struct got_error *
336 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
337 struct got_blob_object *blob1, struct got_blob_object *blob2,
338 FILE *f1, FILE *f2, const char *label1, const char *label2,
339 enum got_diff_algorithm diff_algo, int diff_context,
340 int ignore_whitespace, int force_text_diff,
341 struct got_diffstat_cb_arg *ds, FILE *outfile)
343 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
344 label1, label2, 0, 0, diff_context, ignore_whitespace,
345 force_text_diff, ds, outfile, diff_algo);
348 static const struct got_error *
349 diff_blob_file(struct got_diff_line **lines, size_t *nlines,
350 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
351 FILE *f1, off_t size1, const char *label1, FILE *f2, int f2_exists,
352 struct stat *sb2, const char *label2, enum got_diff_algorithm diff_algo,
353 int diff_context, int ignore_whitespace, int force_text_diff,
354 struct got_diffstat_cb_arg *diffstat, FILE *outfile)
356 const struct got_error *err = NULL, *free_err;
357 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
358 const char *idstr1 = NULL;
359 struct got_diffreg_result *result = NULL;
360 off_t outoff = 0;
361 int n;
363 if (resultp)
364 *resultp = NULL;
366 if (lines != NULL && *lines != NULL) {
367 if (*nlines == 0) {
368 err = add_line_metadata(lines, nlines, 0,
369 GOT_DIFF_LINE_NONE);
370 if (err != NULL)
371 return err;
372 } else
373 outoff = (*lines)[*nlines - 1].offset;
376 if (blob1)
377 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
378 else
379 idstr1 = "/dev/null";
381 if (outfile) {
382 char *mode = NULL;
384 n = fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
385 if (n < 0)
386 return got_error_from_errno("fprintf");
387 if (lines != NULL && *lines != NULL) {
388 outoff += n;
389 err = add_line_metadata(lines, nlines, outoff,
390 GOT_DIFF_LINE_BLOB_MIN);
391 if (err != NULL)
392 return err;
395 /* display file mode for new added files only */
396 if (f2_exists && blob1 == NULL) {
397 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
399 if (S_ISLNK(sb2->st_mode))
400 mmask = S_IFLNK;
401 if (asprintf(&mode, " (mode %o)",
402 sb2->st_mode & mmask) == -1)
403 return got_error_from_errno("asprintf");
405 n = fprintf(outfile, "file + %s%s\n",
406 f2_exists ? label2 : "/dev/null", mode ? mode : "");
407 free(mode);
408 if (n < 0)
409 return got_error_from_errno("fprintf");
410 if (lines != NULL && *lines != NULL) {
411 outoff += n;
412 err = add_line_metadata(lines, nlines, outoff,
413 GOT_DIFF_LINE_BLOB_PLUS);
414 if (err != NULL)
415 return err;
419 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
420 force_text_diff);
421 if (err) {
422 char msg[GOT_ERR_MAX_MSG_SIZE];
424 if (snprintf(msg, sizeof(msg), "%s vs %s: %s",
425 label1 ? label1 : idstr1,
426 f2_exists ? label2 : "/dev/null", err->msg) >= 0) {
427 err = got_error_msg(err->code, msg);
429 goto done;
432 if (outfile) {
433 err = got_diffreg_output(lines, nlines, result,
434 blob1 != NULL, f2_exists,
435 label2, /* show local file's path, not a blob ID */
436 label2, GOT_DIFF_OUTPUT_UNIDIFF,
437 diff_context, outfile);
438 if (err)
439 goto done;
442 if (diffstat) {
443 char *path = NULL;
444 int status = GOT_STATUS_NO_CHANGE;
447 * Ignore 'm'ode status change: if there's no accompanying
448 * content change, there'll be no diffstat, and if there
449 * are actual changes, 'M'odified takes precedence.
451 if (blob1 == NULL)
452 status = GOT_STATUS_ADD;
453 else if (!f2_exists)
454 status = GOT_STATUS_DELETE;
455 else
456 status = GOT_STATUS_MODIFY;
458 if (label2 != NULL &&
459 (status != GOT_STATUS_DELETE || label1 == NULL))
460 path = strdup(label2);
461 else
462 path = strdup(label1);
463 if (path == NULL) {
464 err = got_error_from_errno("strdup");
465 goto done;
468 err = get_diffstat(diffstat, path, result->result,
469 force_text_diff, status);
470 if (err) {
471 free(path);
472 goto done;
476 done:
477 if (resultp && err == NULL)
478 *resultp = result;
479 else if (result) {
480 free_err = got_diffreg_result_free(result);
481 if (free_err && err == NULL)
482 err = free_err;
484 return err;
487 const struct got_error *
488 got_diff_blob_file(struct got_diff_line **lines, size_t *nlines,
489 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
490 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
491 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
492 int force_text_diff, struct got_diffstat_cb_arg *ds, FILE *outfile)
494 return diff_blob_file(lines, nlines, NULL, blob1, f1, size1, label1,
495 f2, f2_exists, sb2, label2, diff_algo, diff_context,
496 ignore_whitespace, force_text_diff, ds, outfile);
499 static const struct got_error *
500 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
501 const char *label, mode_t mode, struct got_repository *repo,
502 got_diff_blob_cb cb, void *cb_arg)
504 const struct got_error *err;
505 struct got_blob_object *blob = NULL;
506 struct got_object *obj = NULL;
508 err = got_object_open(&obj, repo, id);
509 if (err)
510 return err;
512 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
513 if (err)
514 goto done;
515 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
516 NULL, label, 0, mode, repo);
517 done:
518 got_object_close(obj);
519 if (blob)
520 got_object_blob_close(blob);
521 return err;
524 static const struct got_error *
525 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
526 FILE *f1, FILE *f2, int fd1, int fd2,
527 const char *label1, const char *label2,
528 mode_t mode1, mode_t mode2, struct got_repository *repo,
529 got_diff_blob_cb cb, void *cb_arg)
531 const struct got_error *err;
532 struct got_object *obj1 = NULL;
533 struct got_object *obj2 = NULL;
534 struct got_blob_object *blob1 = NULL;
535 struct got_blob_object *blob2 = NULL;
537 err = got_object_open(&obj1, repo, id1);
538 if (err)
539 return err;
541 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
542 err = got_error(GOT_ERR_OBJ_TYPE);
543 goto done;
546 err = got_object_open(&obj2, repo, id2);
547 if (err)
548 goto done;
549 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
550 err = got_error(GOT_ERR_OBJ_TYPE);
551 goto done;
554 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
555 if (err)
556 goto done;
558 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
559 if (err)
560 goto done;
562 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
563 mode1, mode2, repo);
564 done:
565 if (obj1)
566 got_object_close(obj1);
567 if (obj2)
568 got_object_close(obj2);
569 if (blob1)
570 got_object_blob_close(blob1);
571 if (blob2)
572 got_object_blob_close(blob2);
573 return err;
576 static const struct got_error *
577 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
578 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
579 got_diff_blob_cb cb, void *cb_arg)
581 const struct got_error *err;
582 struct got_blob_object *blob = NULL;
583 struct got_object *obj = NULL;
585 err = got_object_open(&obj, repo, id);
586 if (err)
587 return err;
589 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
590 if (err)
591 goto done;
592 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
593 mode, 0, repo);
594 done:
595 got_object_close(obj);
596 if (blob)
597 got_object_blob_close(blob);
598 return err;
601 static const struct got_error *
602 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
603 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
604 void *cb_arg, int diff_content)
606 const struct got_error *err = NULL;
607 struct got_object *treeobj = NULL;
608 struct got_tree_object *tree = NULL;
610 err = got_object_open(&treeobj, repo, id);
611 if (err)
612 goto done;
614 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
615 err = got_error(GOT_ERR_OBJ_TYPE);
616 goto done;
619 err = got_object_tree_open(&tree, repo, treeobj);
620 if (err)
621 goto done;
623 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
624 repo, cb, cb_arg, diff_content);
625 done:
626 if (tree)
627 got_object_tree_close(tree);
628 if (treeobj)
629 got_object_close(treeobj);
630 return err;
633 static const struct got_error *
634 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
635 FILE *f1, FILE *f2, int fd1, int fd2,
636 const char *label1, const char *label2,
637 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
638 int diff_content)
640 const struct got_error *err;
641 struct got_object *treeobj1 = NULL;
642 struct got_object *treeobj2 = NULL;
643 struct got_tree_object *tree1 = NULL;
644 struct got_tree_object *tree2 = NULL;
646 err = got_object_open(&treeobj1, repo, id1);
647 if (err)
648 goto done;
650 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
651 err = got_error(GOT_ERR_OBJ_TYPE);
652 goto done;
655 err = got_object_open(&treeobj2, repo, id2);
656 if (err)
657 goto done;
659 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
660 err = got_error(GOT_ERR_OBJ_TYPE);
661 goto done;
664 err = got_object_tree_open(&tree1, repo, treeobj1);
665 if (err)
666 goto done;
668 err = got_object_tree_open(&tree2, repo, treeobj2);
669 if (err)
670 goto done;
672 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
673 label1, label2, repo, cb, cb_arg, diff_content);
675 done:
676 if (tree1)
677 got_object_tree_close(tree1);
678 if (tree2)
679 got_object_tree_close(tree2);
680 if (treeobj1)
681 got_object_close(treeobj1);
682 if (treeobj2)
683 got_object_close(treeobj2);
684 return err;
687 static const struct got_error *
688 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
689 FILE *f2, const char *label, struct got_repository *repo,
690 got_diff_blob_cb cb, void *cb_arg, int diff_content)
692 const struct got_error *err;
693 struct got_object *treeobj = NULL;
694 struct got_tree_object *tree = NULL;
696 err = got_object_open(&treeobj, repo, id);
697 if (err)
698 goto done;
700 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
701 err = got_error(GOT_ERR_OBJ_TYPE);
702 goto done;
705 err = got_object_tree_open(&tree, repo, treeobj);
706 if (err)
707 goto done;
709 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
710 repo, cb, cb_arg, diff_content);
711 done:
712 if (tree)
713 got_object_tree_close(tree);
714 if (treeobj)
715 got_object_close(treeobj);
716 return err;
719 static const struct got_error *
720 diff_kind_mismatch(struct got_tree_entry *te1, struct got_tree_entry *te2,
721 FILE *f1, FILE *f2, int fd1, int fd2,
722 const char *label1, const char *label2, struct got_repository *repo,
723 got_diff_blob_cb cb, void *cb_arg, int diff_content)
725 const struct got_error *err = NULL;
728 * Handle files changing into directories and vice-versa.
729 * Disregard edge cases with FIFOs, device nodes, etc for now.
731 if (!S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
732 if (S_ISREG(te1->mode)) {
733 if (diff_content) {
734 err = diff_deleted_blob(&te1->id, f1, fd1,
735 f2, label1, te1->mode, repo, cb, cb_arg);
736 } else {
737 err = cb(cb_arg, NULL, NULL, NULL, NULL,
738 &te1->id, NULL, label1, NULL,
739 te1->mode, 0, repo);
741 if (err)
742 return err;
744 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
745 repo, cb, cb_arg, diff_content);
746 } else if (S_ISDIR(te1->mode) && !S_ISDIR(te2->mode)) {
747 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
748 label1, repo, cb, cb_arg, diff_content);
749 if (err)
750 return err;
751 if (S_ISREG(te2->mode)) {
752 if (diff_content) {
753 err = diff_added_blob(&te2->id, f1, f2, fd2,
754 label2, te2->mode, repo, cb, cb_arg);
755 } else {
756 err = cb(cb_arg, NULL, NULL, NULL, NULL, NULL,
757 &te2->id, NULL, label2, 0, te2->mode, repo);
759 if (err)
760 return err;
764 return NULL;
767 static const struct got_error *
768 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
769 FILE *f1, FILE *f2, int fd1, int fd2,
770 const char *label1, const char *label2,
771 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
772 int diff_content)
774 const struct got_error *err = NULL;
775 int id_match;
777 if (got_object_tree_entry_is_submodule(te1))
778 return NULL;
780 if (te2 == NULL) {
781 if (S_ISDIR(te1->mode))
782 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
783 label1, repo, cb, cb_arg, diff_content);
784 else {
785 if (diff_content)
786 err = diff_deleted_blob(&te1->id, f1, fd1,
787 f2, label1, te1->mode, repo, cb, cb_arg);
788 else
789 err = cb(cb_arg, NULL, NULL, NULL, NULL,
790 &te1->id, NULL, label1, NULL,
791 te1->mode, 0, repo);
793 return err;
794 } else if (got_object_tree_entry_is_submodule(te2))
795 return NULL;
797 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
798 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
799 if (!id_match)
800 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
801 fd1, fd2, label1, label2, repo, cb, cb_arg,
802 diff_content);
803 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
804 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
805 if (!id_match ||
806 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
807 (te2->mode & (S_IFLNK | S_IXUSR))) {
808 if (diff_content)
809 return diff_modified_blob(&te1->id, &te2->id,
810 f1, f2, fd1, fd2, label1, label2,
811 te1->mode, te2->mode, repo, cb, cb_arg);
812 else
813 return cb(cb_arg, NULL, NULL, NULL, NULL,
814 &te1->id, &te2->id, label1, label2,
815 te1->mode, te2->mode, repo);
819 if (id_match)
820 return NULL;
822 return diff_kind_mismatch(te1, te2, f1, f2, fd1, fd2,
823 label1, label2, repo, cb, cb_arg, diff_content);
826 static const struct got_error *
827 diff_entry_new_old(struct got_tree_entry *te2,
828 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
829 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
830 int diff_content)
832 if (te1 != NULL) /* handled by diff_entry_old_new() */
833 return NULL;
835 if (got_object_tree_entry_is_submodule(te2))
836 return NULL;
838 if (S_ISDIR(te2->mode))
839 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
840 repo, cb, cb_arg, diff_content);
842 if (diff_content)
843 return diff_added_blob(&te2->id, f1, f2, fd2,
844 label2, te2->mode, repo, cb, cb_arg);
846 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
847 NULL, label2, 0, te2->mode, repo);
850 const struct got_error *
851 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
852 struct got_blob_object *blob2, FILE *f1, FILE *f2,
853 struct got_object_id *id1, struct got_object_id *id2,
854 const char *label1, const char *label2,
855 mode_t mode1, mode_t mode2, struct got_repository *repo)
857 const struct got_error *err = NULL;
858 struct got_diffreg_result *result = NULL;
859 struct got_diffstat_cb_arg *a = arg;
860 char *path = NULL;
861 int status = GOT_STATUS_NO_CHANGE;
863 path = strdup(label2 ? label2 : label1);
864 if (path == NULL)
865 return got_error_from_errno("strdup");
867 if (id1 == NULL)
868 status = GOT_STATUS_ADD;
869 else if (id2 == NULL)
870 status = GOT_STATUS_DELETE;
871 else {
872 if (got_object_id_cmp(id1, id2) != 0)
873 status = GOT_STATUS_MODIFY;
874 else if (mode1 != mode2)
875 status = GOT_STATUS_MODE_CHANGE;
878 if (f1) {
879 err = got_opentemp_truncate(f1);
880 if (err)
881 goto done;
883 if (f2) {
884 err = got_opentemp_truncate(f2);
885 if (err)
886 goto done;
889 if (blob1) {
890 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
891 blob1);
892 if (err)
893 goto done;
895 if (blob2) {
896 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
897 blob2);
898 if (err)
899 goto done;
902 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
903 a->force_text);
904 if (err)
905 goto done;
907 err = get_diffstat(a, path, result->result, a->force_text, status);
909 done:
910 if (result) {
911 const struct got_error *free_err;
913 free_err = got_diffreg_result_free(result);
914 if (free_err && err == NULL)
915 err = free_err;
917 if (err)
918 free(path);
919 return err;
922 const struct got_error *
923 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
924 struct got_blob_object *blob2, FILE *f1, FILE *f2,
925 struct got_object_id *id1, struct got_object_id *id2,
926 const char *label1, const char *label2,
927 mode_t mode1, mode_t mode2, struct got_repository *repo)
929 const struct got_error *err = NULL;
930 struct got_pathlist_head *paths = arg;
931 struct got_pathlist_entry *new;
932 struct got_diff_changed_path *change = NULL;
933 char *path = NULL;
935 path = strdup(label2 ? label2 : label1);
936 if (path == NULL)
937 return got_error_from_errno("strdup");
939 change = malloc(sizeof(*change));
940 if (change == NULL) {
941 err = got_error_from_errno("malloc");
942 goto done;
945 change->status = GOT_STATUS_NO_CHANGE;
946 if (id1 == NULL)
947 change->status = GOT_STATUS_ADD;
948 else if (id2 == NULL)
949 change->status = GOT_STATUS_DELETE;
950 else {
951 if (got_object_id_cmp(id1, id2) != 0)
952 change->status = GOT_STATUS_MODIFY;
953 else if (mode1 != mode2)
954 change->status = GOT_STATUS_MODE_CHANGE;
957 err = got_pathlist_insert(&new, paths, path, change);
958 done:
959 if (err || new == NULL) {
960 free(path);
961 free(change);
963 return err;
966 const struct got_error *
967 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
968 FILE *f1, FILE *f2, int fd1, int fd2,
969 const char *label1, const char *label2,
970 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
971 int diff_content)
973 const struct got_error *err = NULL;
974 struct got_tree_entry *te1 = NULL;
975 struct got_tree_entry *te2 = NULL;
976 char *l1 = NULL, *l2 = NULL;
977 int tidx1 = 0, tidx2 = 0;
979 if (tree1) {
980 te1 = got_object_tree_get_entry(tree1, 0);
981 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
982 te1->name) == -1)
983 return got_error_from_errno("asprintf");
985 if (tree2) {
986 te2 = got_object_tree_get_entry(tree2, 0);
987 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
988 te2->name) == -1) {
989 err = got_error_from_errno("asprintf");
990 goto done;
994 do {
995 if (te1) {
996 struct got_tree_entry *te = NULL;
998 if (tree2)
999 te = got_object_tree_find_entry(tree2,
1000 te1->name);
1001 if (te) {
1002 free(l2);
1003 l2 = NULL;
1004 if (te && asprintf(&l2, "%s%s%s", label2,
1005 label2[0] ? "/" : "", te->name) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 goto done;
1011 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
1012 l1, l2, repo, cb, cb_arg, diff_content);
1013 if (err)
1014 break;
1017 if (te2) {
1018 struct got_tree_entry *te = NULL;
1020 if (tree1)
1021 te = got_object_tree_find_entry(tree1,
1022 te2->name);
1024 free(l2);
1025 l2 = NULL;
1026 if (te) {
1027 if (asprintf(&l2, "%s%s%s", label2,
1028 label2[0] ? "/" : "", te->name) == -1) {
1029 err = got_error_from_errno("asprintf");
1030 goto done;
1032 } else {
1033 if (asprintf(&l2, "%s%s%s", label2,
1034 label2[0] ? "/" : "", te2->name) == -1) {
1035 err = got_error_from_errno("asprintf");
1036 goto done;
1040 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
1041 repo, cb, cb_arg, diff_content);
1042 if (err)
1043 break;
1046 free(l1);
1047 l1 = NULL;
1048 if (te1) {
1049 tidx1++;
1050 te1 = got_object_tree_get_entry(tree1, tidx1);
1051 if (te1 &&
1052 asprintf(&l1, "%s%s%s", label1,
1053 label1[0] ? "/" : "", te1->name) == -1) {
1054 err = got_error_from_errno("asprintf");
1055 goto done;
1059 free(l2);
1060 l2 = NULL;
1061 if (te2) {
1062 tidx2++;
1063 te2 = got_object_tree_get_entry(tree2, tidx2);
1064 if (te2 &&
1065 asprintf(&l2, "%s%s%s", label2,
1066 label2[0] ? "/" : "", te2->name) == -1) {
1067 err = got_error_from_errno("asprintf");
1068 goto done;
1071 } while (te1 || te2);
1073 done:
1074 free(l1);
1075 free(l2);
1076 return err;
1079 const struct got_error *
1080 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
1081 FILE *f1, FILE *f2, int fd1, int fd2,
1082 struct got_object_id *id1, struct got_object_id *id2,
1083 const char *label1, const char *label2,
1084 enum got_diff_algorithm diff_algo, int diff_context,
1085 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1086 struct got_repository *repo, FILE *outfile)
1088 const struct got_error *err;
1089 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1091 if (id1 == NULL && id2 == NULL)
1092 return got_error(GOT_ERR_NO_OBJ);
1094 if (id1) {
1095 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1096 if (err)
1097 goto done;
1099 if (id2) {
1100 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1101 if (err)
1102 goto done;
1104 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1105 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1106 ds, outfile);
1107 done:
1108 if (blob1)
1109 got_object_blob_close(blob1);
1110 if (blob2)
1111 got_object_blob_close(blob2);
1112 return err;
1115 static const struct got_error *
1116 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1117 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1118 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1120 const struct got_error *err = NULL;
1121 struct got_pathlist_entry *pe;
1122 struct got_object_id *id1 = NULL, *id2 = NULL;
1123 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1124 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1126 RB_FOREACH(pe, got_pathlist_head, paths) {
1127 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1128 mode_t mode1 = 0, mode2 = 0;
1130 free(id1);
1131 id1 = NULL;
1132 free(id2);
1133 id2 = NULL;
1134 if (subtree1) {
1135 got_object_tree_close(subtree1);
1136 subtree1 = NULL;
1138 if (subtree2) {
1139 got_object_tree_close(subtree2);
1140 subtree2 = NULL;
1142 if (blob1) {
1143 got_object_blob_close(blob1);
1144 blob1 = NULL;
1146 if (blob2) {
1147 got_object_blob_close(blob2);
1148 blob2 = NULL;
1150 if (tree1) {
1151 err = got_object_tree_find_path(&id1, &mode1, repo,
1152 tree1, pe->path);
1153 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1154 goto done;
1156 if (tree2) {
1157 err = got_object_tree_find_path(&id2, &mode2, repo,
1158 tree2, pe->path);
1159 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1160 goto done;
1162 if (id1 == NULL && id2 == NULL) {
1163 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1164 goto done;
1166 if (id1) {
1167 err = got_object_get_type(&type1, repo, id1);
1168 if (err)
1169 goto done;
1171 if (id2) {
1172 err = got_object_get_type(&type2, repo, id2);
1173 if (err)
1174 goto done;
1176 if (type1 == GOT_OBJ_TYPE_ANY &&
1177 type2 == GOT_OBJ_TYPE_ANY) {
1178 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1179 goto done;
1180 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1181 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1182 err = got_error(GOT_ERR_OBJ_TYPE);
1183 goto done;
1186 if (type1 == GOT_OBJ_TYPE_BLOB ||
1187 type2 == GOT_OBJ_TYPE_BLOB) {
1188 if (id1) {
1189 err = got_object_open_as_blob(&blob1, repo,
1190 id1, 8192, fd1);
1191 if (err)
1192 goto done;
1194 if (id2) {
1195 err = got_object_open_as_blob(&blob2, repo,
1196 id2, 8192, fd2);
1197 if (err)
1198 goto done;
1200 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1201 id1 ? pe->path : "/dev/null",
1202 id2 ? pe->path : "/dev/null",
1203 mode1, mode2, repo);
1204 if (err)
1205 goto done;
1206 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1207 type2 == GOT_OBJ_TYPE_TREE) {
1208 if (id1) {
1209 err = got_object_open_as_tree(&subtree1, repo,
1210 id1);
1211 if (err)
1212 goto done;
1214 if (id2) {
1215 err = got_object_open_as_tree(&subtree2, repo,
1216 id2);
1217 if (err)
1218 goto done;
1220 err = got_diff_tree(subtree1, subtree2, f1, f2,
1221 fd1, fd2,
1222 id1 ? pe->path : "/dev/null",
1223 id2 ? pe->path : "/dev/null",
1224 repo, cb, cb_arg, 1);
1225 if (err)
1226 goto done;
1227 } else {
1228 err = got_error(GOT_ERR_OBJ_TYPE);
1229 goto done;
1232 done:
1233 free(id1);
1234 free(id2);
1235 if (subtree1)
1236 got_object_tree_close(subtree1);
1237 if (subtree2)
1238 got_object_tree_close(subtree2);
1239 if (blob1)
1240 got_object_blob_close(blob1);
1241 if (blob2)
1242 got_object_blob_close(blob2);
1243 return err;
1246 static const struct got_error *
1247 show_object_id(struct got_diff_line **lines, size_t *nlines,
1248 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1250 const struct got_error *err;
1251 int n;
1252 off_t outoff = 0;
1254 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1255 if (n < 0)
1256 return got_error_from_errno("fprintf");
1258 if (lines != NULL && *lines != NULL) {
1259 if (*nlines == 0) {
1260 err = add_line_metadata(lines, nlines, 0,
1261 GOT_DIFF_LINE_META);
1262 if (err)
1263 return err;
1264 } else
1265 outoff = (*lines)[*nlines - 1].offset;
1267 outoff += n;
1268 err = add_line_metadata(lines, nlines, outoff,
1269 GOT_DIFF_LINE_META);
1270 if (err)
1271 return err;
1274 return NULL;
1277 static const struct got_error *
1278 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1279 FILE *f1, FILE *f2, int fd1, int fd2,
1280 struct got_object_id *id1, struct got_object_id *id2,
1281 struct got_pathlist_head *paths, const char *label1, const char *label2,
1282 int diff_context, int ignore_whitespace, int force_text_diff,
1283 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1284 FILE *outfile, enum got_diff_algorithm diff_algo)
1286 const struct got_error *err;
1287 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1288 struct got_diff_blob_output_unidiff_arg arg;
1289 int want_linemeta = (lines != NULL && *lines != NULL);
1291 if (id1 == NULL && id2 == NULL)
1292 return got_error(GOT_ERR_NO_OBJ);
1294 if (id1) {
1295 err = got_object_open_as_tree(&tree1, repo, id1);
1296 if (err)
1297 goto done;
1299 if (id2) {
1300 err = got_object_open_as_tree(&tree2, repo, id2);
1301 if (err)
1302 goto done;
1305 arg.diff_algo = diff_algo;
1306 arg.diff_context = diff_context;
1307 arg.ignore_whitespace = ignore_whitespace;
1308 arg.force_text_diff = force_text_diff;
1309 arg.diffstat = dsa;
1310 arg.outfile = outfile;
1311 if (want_linemeta) {
1312 arg.lines = *lines;
1313 arg.nlines = *nlines;
1314 } else {
1315 arg.lines = NULL;
1316 arg.nlines = 0;
1318 if (paths == NULL || RB_EMPTY(paths))
1319 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1320 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1321 else
1322 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1323 got_diff_blob_output_unidiff, &arg);
1324 if (want_linemeta) {
1325 *lines = arg.lines; /* was likely re-allocated */
1326 *nlines = arg.nlines;
1328 done:
1329 if (tree1)
1330 got_object_tree_close(tree1);
1331 if (tree2)
1332 got_object_tree_close(tree2);
1333 return err;
1336 const struct got_error *
1337 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1338 FILE *f1, FILE *f2, int fd1, int fd2,
1339 struct got_object_id *id1, struct got_object_id *id2,
1340 struct got_pathlist_head *paths, const char *label1, const char *label2,
1341 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1342 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1343 struct got_repository *repo, FILE *outfile)
1345 const struct got_error *err;
1346 char *idstr = NULL;
1348 if (id1 == NULL && id2 == NULL)
1349 return got_error(GOT_ERR_NO_OBJ);
1351 if (id1) {
1352 err = got_object_id_str(&idstr, id1);
1353 if (err)
1354 goto done;
1355 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1356 if (err)
1357 goto done;
1358 free(idstr);
1359 idstr = NULL;
1360 } else {
1361 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1362 outfile);
1363 if (err)
1364 goto done;
1367 if (id2) {
1368 err = got_object_id_str(&idstr, id2);
1369 if (err)
1370 goto done;
1371 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1372 if (err)
1373 goto done;
1374 free(idstr);
1375 idstr = NULL;
1376 } else {
1377 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1378 outfile);
1379 if (err)
1380 goto done;
1383 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1384 paths, label1, label2, diff_context, ignore_whitespace,
1385 force_text_diff, dsa, repo, outfile, diff_algo);
1386 done:
1387 free(idstr);
1388 return err;
1391 const struct got_error *
1392 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1393 FILE *f1, FILE *f2, int fd1, int fd2,
1394 struct got_object_id *id1, struct got_object_id *id2,
1395 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1396 int diff_context, int ignore_whitespace, int force_text_diff,
1397 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1399 const struct got_error *err;
1400 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1401 char *idstr = NULL;
1403 if (id2 == NULL)
1404 return got_error(GOT_ERR_NO_OBJ);
1406 if (id1) {
1407 err = got_object_open_as_commit(&commit1, repo, id1);
1408 if (err)
1409 goto done;
1410 err = got_object_id_str(&idstr, id1);
1411 if (err)
1412 goto done;
1413 err = show_object_id(lines, nlines, "commit", '-', idstr,
1414 outfile);
1415 if (err)
1416 goto done;
1417 free(idstr);
1418 idstr = NULL;
1419 } else {
1420 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1421 outfile);
1422 if (err)
1423 goto done;
1426 err = got_object_open_as_commit(&commit2, repo, id2);
1427 if (err)
1428 goto done;
1430 err = got_object_id_str(&idstr, id2);
1431 if (err)
1432 goto done;
1433 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1434 if (err)
1435 goto done;
1437 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1438 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1439 got_object_commit_get_tree_id(commit2), paths, "", "",
1440 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1441 outfile, diff_algo);
1442 done:
1443 if (commit1)
1444 got_object_commit_close(commit1);
1445 if (commit2)
1446 got_object_commit_close(commit2);
1447 free(idstr);
1448 return err;
1451 const struct got_error *
1452 got_diff_files(struct got_diffreg_result **resultp,
1453 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1454 const char *label2, int diff_context, int ignore_whitespace,
1455 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1457 const struct got_error *err = NULL;
1458 struct got_diffreg_result *diffreg_result = NULL;
1460 if (resultp)
1461 *resultp = NULL;
1463 if (outfile) {
1464 fprintf(outfile, "file - %s\n",
1465 f1_exists ? label1 : "/dev/null");
1466 fprintf(outfile, "file + %s\n",
1467 f2_exists ? label2 : "/dev/null");
1470 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1471 ignore_whitespace, force_text_diff);
1472 if (err)
1473 goto done;
1475 if (outfile) {
1476 err = got_diffreg_output(NULL, NULL, diffreg_result,
1477 f1_exists, f2_exists, label1, label2,
1478 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1479 if (err)
1480 goto done;
1483 done:
1484 if (resultp && err == NULL)
1485 *resultp = diffreg_result;
1486 else if (diffreg_result) {
1487 const struct got_error *free_err;
1489 free_err = got_diffreg_result_free(diffreg_result);
1490 if (free_err && err == NULL)
1491 err = free_err;
1494 return err;