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 <sys/queue.h>
27 #include "got_object.h"
28 #include "got_repository.h"
29 #include "got_error.h"
32 #include "got_cancel.h"
33 #include "got_worktree.h"
35 #include "got_lib_diff.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
40 static const struct got_error *
41 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
45 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
47 return got_error_from_errno("reallocarray");
49 (*line_offsets)[*nlines] = off;
54 static const struct got_error *
58 return got_error_from_errno("fpurge");
59 if (ftruncate(fileno(f), 0L) == -1)
60 return got_error_from_errno("ftruncate");
61 if (fseeko(f, 0L, SEEK_SET) == -1)
62 return got_error_from_errno("fseeko");
66 static const struct got_error *
67 diff_blobs(off_t **line_offsets, size_t *nlines,
68 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
69 struct got_blob_object *blob2, FILE *f1, FILE *f2,
70 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
71 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
73 const struct got_error *err = NULL, *free_err;
74 char hex1[SHA1_DIGEST_STRING_LENGTH];
75 char hex2[SHA1_DIGEST_STRING_LENGTH];
76 char *idstr1 = NULL, *idstr2 = NULL;
78 struct got_diffreg_result *result;
82 if (line_offsets && *line_offsets && *nlines > 0)
83 outoff = (*line_offsets)[*nlines - 1];
84 else if (line_offsets) {
85 err = add_line_offset(line_offsets, nlines, 0);
106 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
107 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
112 idstr1 = "/dev/null";
116 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
117 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
122 idstr2 = "/dev/null";
125 char *modestr1 = NULL, *modestr2 = NULL;
127 if (mode1 && mode1 != mode2) {
131 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
132 if (asprintf(&modestr1, " (mode %o)",
133 mode1 & modebits) == -1) {
134 err = got_error_from_errno("asprintf");
138 if (mode2 && mode1 != mode2) {
142 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
143 if (asprintf(&modestr2, " (mode %o)",
144 mode2 & modebits) == -1) {
145 err = got_error_from_errno("asprintf");
149 n = fprintf(outfile, "blob - %s%s\n", idstr1,
150 modestr1 ? modestr1 : "");
155 err = add_line_offset(line_offsets, nlines, outoff);
160 n = fprintf(outfile, "blob + %s%s\n", idstr2,
161 modestr2 ? modestr2 : "");
166 err = add_line_offset(line_offsets, nlines, outoff);
174 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
175 ignore_whitespace, force_text_diff);
180 err = got_diffreg_output(line_offsets, nlines, result,
181 blob1 != NULL, blob2 != NULL,
182 label1 ? label1 : idstr1,
183 label2 ? label2 : idstr2,
184 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
189 if (resultp && err == NULL)
192 free_err = got_diffreg_result_free(result);
193 if (free_err && err == NULL)
200 const struct got_error *
201 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
202 struct got_blob_object *blob2, FILE *f1, FILE *f2,
203 struct got_object_id *id1, struct got_object_id *id2,
204 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
205 struct got_repository *repo)
207 struct got_diff_blob_output_unidiff_arg *a = arg;
209 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
210 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
211 a->ignore_whitespace, a->force_text_diff, a->outfile);
214 const struct got_error *
215 got_diff_blob(off_t **line_offsets, size_t *nlines,
216 struct got_blob_object *blob1, struct got_blob_object *blob2,
217 FILE *f1, FILE *f2, const char *label1, const char *label2,
218 int diff_context, int ignore_whitespace, int force_text_diff,
221 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
222 label1, label2, 0, 0, diff_context, ignore_whitespace,
223 force_text_diff, outfile);
226 static const struct got_error *
227 diff_blob_file(struct got_diffreg_result **resultp,
228 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
229 FILE *f2, size_t size2, const char *label2, int diff_context,
230 int ignore_whitespace, int force_text_diff, FILE *outfile)
232 const struct got_error *err = NULL, *free_err;
233 char hex1[SHA1_DIGEST_STRING_LENGTH];
235 struct got_diffreg_result *result = NULL;
241 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
243 idstr1 = "/dev/null";
246 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
247 fprintf(outfile, "file + %s\n",
248 f2 == NULL ? "/dev/null" : label2);
251 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
252 ignore_whitespace, force_text_diff);
257 err = got_diffreg_output(NULL, NULL, result,
258 f1 != NULL, f2 != NULL,
259 label2, /* show local file's path, not a blob ID */
260 label2, GOT_DIFF_OUTPUT_UNIDIFF,
261 diff_context, outfile);
266 if (resultp && err == NULL)
269 free_err = got_diffreg_result_free(result);
270 if (free_err && err == NULL)
277 const struct got_error *
278 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
279 const char *label1, FILE *f2, size_t size2, const char *label2,
280 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
282 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
283 diff_context, ignore_whitespace, force_text_diff, outfile);
286 static const struct got_error *
287 diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
288 mode_t mode, struct got_repository *repo,
289 got_diff_blob_cb cb, void *cb_arg)
291 const struct got_error *err;
292 struct got_blob_object *blob = NULL;
293 struct got_object *obj = NULL;
295 err = got_object_open(&obj, repo, id);
299 err = got_object_blob_open(&blob, repo, obj, 8192);
302 err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
303 NULL, label, 0, mode, repo);
305 got_object_close(obj);
307 got_object_blob_close(blob);
311 static const struct got_error *
312 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
313 FILE *f1, FILE *f2, const char *label1, const char *label2,
314 mode_t mode1, mode_t mode2, struct got_repository *repo,
315 got_diff_blob_cb cb, void *cb_arg)
317 const struct got_error *err;
318 struct got_object *obj1 = NULL;
319 struct got_object *obj2 = NULL;
320 struct got_blob_object *blob1 = NULL;
321 struct got_blob_object *blob2 = NULL;
323 err = got_object_open(&obj1, repo, id1);
326 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
331 err = got_object_open(&obj2, repo, id2);
334 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 err = got_object_blob_open(&blob1, repo, obj1, 8192);
343 err = got_object_blob_open(&blob2, repo, obj2, 8192);
347 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
351 got_object_close(obj1);
353 got_object_close(obj2);
355 got_object_blob_close(blob1);
357 got_object_blob_close(blob2);
361 static const struct got_error *
362 diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
363 mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
365 const struct got_error *err;
366 struct got_blob_object *blob = NULL;
367 struct got_object *obj = NULL;
369 err = got_object_open(&obj, repo, id);
373 err = got_object_blob_open(&blob, repo, obj, 8192);
376 err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
379 got_object_close(obj);
381 got_object_blob_close(blob);
385 static const struct got_error *
386 diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
387 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
390 const struct got_error *err = NULL;
391 struct got_object *treeobj = NULL;
392 struct got_tree_object *tree = NULL;
394 err = got_object_open(&treeobj, repo, id);
398 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
399 err = got_error(GOT_ERR_OBJ_TYPE);
403 err = got_object_tree_open(&tree, repo, treeobj);
407 err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
408 repo, cb, cb_arg, diff_content);
411 got_object_tree_close(tree);
413 got_object_close(treeobj);
417 static const struct got_error *
418 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
419 FILE *f1, FILE *f2, const char *label1, const char *label2,
420 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
423 const struct got_error *err;
424 struct got_object *treeobj1 = NULL;
425 struct got_object *treeobj2 = NULL;
426 struct got_tree_object *tree1 = NULL;
427 struct got_tree_object *tree2 = NULL;
429 err = got_object_open(&treeobj1, repo, id1);
433 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
434 err = got_error(GOT_ERR_OBJ_TYPE);
438 err = got_object_open(&treeobj2, repo, id2);
442 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
443 err = got_error(GOT_ERR_OBJ_TYPE);
447 err = got_object_tree_open(&tree1, repo, treeobj1);
451 err = got_object_tree_open(&tree2, repo, treeobj2);
455 err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
456 repo, cb, cb_arg, diff_content);
460 got_object_tree_close(tree1);
462 got_object_tree_close(tree2);
464 got_object_close(treeobj1);
466 got_object_close(treeobj2);
470 static const struct got_error *
471 diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
472 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
475 const struct got_error *err;
476 struct got_object *treeobj = NULL;
477 struct got_tree_object *tree = NULL;
479 err = got_object_open(&treeobj, repo, id);
483 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
484 err = got_error(GOT_ERR_OBJ_TYPE);
488 err = got_object_tree_open(&tree, repo, treeobj);
492 err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
493 repo, cb, cb_arg, diff_content);
496 got_object_tree_close(tree);
498 got_object_close(treeobj);
502 static const struct got_error *
503 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
504 const char *label1, const char *label2, struct got_repository *repo,
505 got_diff_blob_cb cb, void *cb_arg)
511 static const struct got_error *
512 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
513 FILE *f1, FILE *f2, const char *label1, const char *label2,
514 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
517 const struct got_error *err = NULL;
520 if (got_object_tree_entry_is_submodule(te1))
524 if (S_ISDIR(te1->mode))
525 err = diff_deleted_tree(&te1->id, f1, label1, repo,
526 cb, cb_arg, diff_content);
529 err = diff_deleted_blob(&te1->id, f1, label1,
530 te1->mode, repo, cb, cb_arg);
532 err = cb(cb_arg, NULL, NULL, NULL, NULL,
533 &te1->id, NULL, label1, NULL,
537 } else if (got_object_tree_entry_is_submodule(te2))
540 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
541 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
543 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
544 label1, label2, repo, cb, cb_arg, diff_content);
545 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
546 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
548 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
549 (te2->mode & (S_IFLNK | S_IXUSR))) {
551 return diff_modified_blob(&te1->id, &te2->id,
552 f1, f2, label1, label2, te1->mode,
553 te2->mode, repo, cb, cb_arg);
555 return cb(cb_arg, NULL, NULL, NULL, NULL,
556 &te1->id, &te2->id, label1, label2,
557 te1->mode, te2->mode, repo);
564 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
568 static const struct got_error *
569 diff_entry_new_old(struct got_tree_entry *te2,
570 struct got_tree_entry *te1, FILE *f2, const char *label2,
571 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
574 if (te1 != NULL) /* handled by diff_entry_old_new() */
577 if (got_object_tree_entry_is_submodule(te2))
580 if (S_ISDIR(te2->mode))
581 return diff_added_tree(&te2->id, f2, label2,
582 repo, cb, cb_arg, diff_content);
585 return diff_added_blob(&te2->id, f2, label2, te2->mode,
588 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
589 NULL, label2, 0, te2->mode, repo);
592 const struct got_error *
593 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
594 struct got_blob_object *blob2, FILE *f1, FILE *f2,
595 struct got_object_id *id1, struct got_object_id *id2,
596 const char *label1, const char *label2,
597 mode_t mode1, mode_t mode2, struct got_repository *repo)
599 const struct got_error *err = NULL;
600 struct got_pathlist_head *paths = arg;
601 struct got_diff_changed_path *change = NULL;
604 path = strdup(label2 ? label2 : label1);
606 return got_error_from_errno("malloc");
608 change = malloc(sizeof(*change));
609 if (change == NULL) {
610 err = got_error_from_errno("malloc");
614 change->status = GOT_STATUS_NO_CHANGE;
616 change->status = GOT_STATUS_ADD;
617 else if (id2 == NULL)
618 change->status = GOT_STATUS_DELETE;
620 if (got_object_id_cmp(id1, id2) != 0)
621 change->status = GOT_STATUS_MODIFY;
622 else if (mode1 != mode2)
623 change->status = GOT_STATUS_MODE_CHANGE;
626 err = got_pathlist_append(paths, path, change);
635 const struct got_error *
636 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
637 FILE *f1, FILE *f2, const char *label1, const char *label2,
638 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
641 const struct got_error *err = NULL;
642 struct got_tree_entry *te1 = NULL;
643 struct got_tree_entry *te2 = NULL;
644 char *l1 = NULL, *l2 = NULL;
645 int tidx1 = 0, tidx2 = 0;
648 te1 = got_object_tree_get_entry(tree1, 0);
649 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
651 return got_error_from_errno("asprintf");
654 te2 = got_object_tree_get_entry(tree2, 0);
655 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
657 return got_error_from_errno("asprintf");
662 struct got_tree_entry *te = NULL;
664 te = got_object_tree_find_entry(tree2,
669 if (te && asprintf(&l2, "%s%s%s", label2,
670 label2[0] ? "/" : "", te->name) == -1)
672 got_error_from_errno("asprintf");
674 err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
675 repo, cb, cb_arg, diff_content);
681 struct got_tree_entry *te = NULL;
683 te = got_object_tree_find_entry(tree1,
687 if (asprintf(&l2, "%s%s%s", label2,
688 label2[0] ? "/" : "", te->name) == -1)
690 got_error_from_errno("asprintf");
692 if (asprintf(&l2, "%s%s%s", label2,
693 label2[0] ? "/" : "", te2->name) == -1)
695 got_error_from_errno("asprintf");
697 err = diff_entry_new_old(te2, te, f2, l2, repo,
698 cb, cb_arg, diff_content);
707 te1 = got_object_tree_get_entry(tree1, tidx1);
709 asprintf(&l1, "%s%s%s", label1,
710 label1[0] ? "/" : "", te1->name) == -1)
711 return got_error_from_errno("asprintf");
717 te2 = got_object_tree_get_entry(tree2, tidx2);
719 asprintf(&l2, "%s%s%s", label2,
720 label2[0] ? "/" : "", te2->name) == -1)
721 return got_error_from_errno("asprintf");
723 } while (te1 || te2);
728 const struct got_error *
729 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
730 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
731 const char *label1, const char *label2, int diff_context,
732 int ignore_whitespace, int force_text_diff,
733 struct got_repository *repo, FILE *outfile)
735 const struct got_error *err;
736 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
738 if (id1 == NULL && id2 == NULL)
739 return got_error(GOT_ERR_NO_OBJ);
742 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
747 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
751 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
752 label1, label2, diff_context, ignore_whitespace, force_text_diff,
756 got_object_blob_close(blob1);
758 got_object_blob_close(blob2);
762 static const struct got_error *
763 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
764 FILE *f1, FILE *f2, struct got_pathlist_head *paths,
765 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
767 const struct got_error *err = NULL;
768 struct got_pathlist_entry *pe;
769 struct got_object_id *id1 = NULL, *id2 = NULL;
770 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
771 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
773 TAILQ_FOREACH(pe, paths, entry) {
774 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
775 mode_t mode1 = 0, mode2 = 0;
782 got_object_tree_close(subtree1);
786 got_object_tree_close(subtree2);
790 got_object_blob_close(blob1);
794 got_object_blob_close(blob2);
798 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
800 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
802 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
804 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
806 if (id1 == NULL && id2 == NULL) {
807 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
811 err = got_object_get_type(&type1, repo, id1);
816 err = got_object_get_type(&type2, repo, id2);
820 if (type1 == GOT_OBJ_TYPE_ANY &&
821 type2 == GOT_OBJ_TYPE_ANY) {
822 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
824 } else if (type1 != GOT_OBJ_TYPE_ANY &&
825 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
826 err = got_error(GOT_ERR_OBJ_TYPE);
830 if (type1 == GOT_OBJ_TYPE_BLOB ||
831 type2 == GOT_OBJ_TYPE_BLOB) {
833 err = got_object_open_as_blob(&blob1, repo,
839 err = got_object_open_as_blob(&blob2, repo,
844 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
845 id1 ? pe->path : "/dev/null",
846 id2 ? pe->path : "/dev/null",
850 } else if (type1 == GOT_OBJ_TYPE_TREE ||
851 type2 == GOT_OBJ_TYPE_TREE) {
853 err = got_object_open_as_tree(&subtree1, repo,
859 err = got_object_open_as_tree(&subtree2, repo,
864 err = got_diff_tree(subtree1, subtree2, f1, f2,
865 id1 ? pe->path : "/dev/null",
866 id2 ? pe->path : "/dev/null",
867 repo, cb, cb_arg, 1);
871 err = got_error(GOT_ERR_OBJ_TYPE);
879 got_object_tree_close(subtree1);
881 got_object_tree_close(subtree2);
883 got_object_blob_close(blob1);
885 got_object_blob_close(blob2);
889 const struct got_error *
890 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
891 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
892 struct got_pathlist_head *paths,
893 char *label1, char *label2, int diff_context, int ignore_whitespace,
894 int force_text_diff, struct got_repository *repo, FILE *outfile)
896 const struct got_error *err;
897 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
898 struct got_diff_blob_output_unidiff_arg arg;
899 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
901 if (id1 == NULL && id2 == NULL)
902 return got_error(GOT_ERR_NO_OBJ);
905 err = got_object_open_as_tree(&tree1, repo, id1);
910 err = got_object_open_as_tree(&tree2, repo, id2);
915 arg.diff_context = diff_context;
916 arg.ignore_whitespace = ignore_whitespace;
917 arg.force_text_diff = force_text_diff;
918 arg.outfile = outfile;
919 if (want_lineoffsets) {
920 arg.line_offsets = *line_offsets;
921 arg.nlines = *nlines;
923 arg.line_offsets = NULL;
926 if (paths == NULL || TAILQ_EMPTY(paths)) {
927 err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
928 repo, got_diff_blob_output_unidiff, &arg, 1);
930 err = diff_paths(tree1, tree2, f1, f2, paths, repo,
931 got_diff_blob_output_unidiff, &arg);
933 if (want_lineoffsets) {
934 *line_offsets = arg.line_offsets; /* was likely re-allocated */
935 *nlines = arg.nlines;
939 got_object_tree_close(tree1);
941 got_object_tree_close(tree2);
945 const struct got_error *
946 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
947 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
948 struct got_pathlist_head *paths,
949 int diff_context, int ignore_whitespace, int force_text_diff,
950 struct got_repository *repo, FILE *outfile)
952 const struct got_error *err;
953 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
956 return got_error(GOT_ERR_NO_OBJ);
959 err = got_object_open_as_commit(&commit1, repo, id1);
964 err = got_object_open_as_commit(&commit2, repo, id2);
968 err = got_diff_objects_as_trees(line_offsets, nlines, f1, f2,
969 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
970 got_object_commit_get_tree_id(commit2), paths, "", "",
971 diff_context, ignore_whitespace, force_text_diff, repo, outfile);
974 got_object_commit_close(commit1);
976 got_object_commit_close(commit2);
980 const struct got_error *
981 got_diff_files(struct got_diffreg_result **resultp,
982 FILE *f1, const char *label1, FILE *f2, const char *label2,
983 int diff_context, int ignore_whitespace, int force_text_diff,
986 const struct got_error *err = NULL;
987 struct got_diffreg_result *diffreg_result = NULL;
993 fprintf(outfile, "file - %s\n",
994 f1 == NULL ? "/dev/null" : label1);
995 fprintf(outfile, "file + %s\n",
996 f2 == NULL ? "/dev/null" : label2);
999 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1000 ignore_whitespace, force_text_diff);
1005 err = got_diffreg_output(NULL, NULL, diffreg_result,
1006 f1 != NULL, f2 != NULL, label1, label2,
1007 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1013 if (resultp && err == NULL)
1014 *resultp = diffreg_result;
1015 else if (diffreg_result) {
1016 const struct got_error *free_err;
1017 free_err = got_diffreg_result_free(diffreg_result);
1018 if (free_err && err == NULL)