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"
34 #include "got_opentemp.h"
36 #include "got_lib_diff.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
41 static const struct got_error *
42 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
43 off_t off, uint8_t type)
45 struct got_diff_line *p;
47 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
49 return got_error_from_errno("reallocarray");
51 (*lines)[*nlines].offset = off;
52 (*lines)[*nlines].type = type;
58 static const struct got_error *
59 diff_blobs(struct got_diff_line **lines, size_t *nlines,
60 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
61 struct got_blob_object *blob2, FILE *f1, FILE *f2,
62 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
63 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
64 enum got_diff_algorithm diff_algo)
66 const struct got_error *err = NULL, *free_err;
67 char hex1[SHA1_DIGEST_STRING_LENGTH];
68 char hex2[SHA1_DIGEST_STRING_LENGTH];
69 const char *idstr1 = NULL, *idstr2 = NULL;
71 struct got_diffreg_result *result;
75 if (lines && *lines && *nlines > 0)
76 outoff = (*lines)[*nlines - 1].offset;
78 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
87 err = got_opentemp_truncate(f1);
92 err = got_opentemp_truncate(f2);
99 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
100 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
105 idstr1 = "/dev/null";
109 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
110 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
115 idstr2 = "/dev/null";
118 char *modestr1 = NULL, *modestr2 = NULL;
120 if (mode1 && mode1 != mode2) {
124 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
125 if (asprintf(&modestr1, " (mode %o)",
126 mode1 & modebits) == -1) {
127 err = got_error_from_errno("asprintf");
131 if (mode2 && mode1 != mode2) {
135 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
136 if (asprintf(&modestr2, " (mode %o)",
137 mode2 & modebits) == -1) {
138 err = got_error_from_errno("asprintf");
142 n = fprintf(outfile, "blob - %s%s\n", idstr1,
143 modestr1 ? modestr1 : "");
148 err = add_line_metadata(lines, nlines, outoff,
149 GOT_DIFF_LINE_BLOB_MIN);
154 n = fprintf(outfile, "blob + %s%s\n", idstr2,
155 modestr2 ? modestr2 : "");
160 err = add_line_metadata(lines, nlines, outoff,
161 GOT_DIFF_LINE_BLOB_PLUS);
169 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
175 err = got_diffreg_output(lines, nlines, result,
176 blob1 != NULL, blob2 != NULL,
177 label1 ? label1 : idstr1,
178 label2 ? label2 : idstr2,
179 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
184 if (resultp && err == NULL)
187 free_err = got_diffreg_result_free(result);
188 if (free_err && err == NULL)
195 const struct got_error *
196 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
197 struct got_blob_object *blob2, FILE *f1, FILE *f2,
198 struct got_object_id *id1, struct got_object_id *id2,
199 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
200 struct got_repository *repo)
202 struct got_diff_blob_output_unidiff_arg *a = arg;
204 return diff_blobs(&a->lines, &a->nlines, NULL,
205 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
206 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
209 const struct got_error *
210 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
211 struct got_blob_object *blob1, struct got_blob_object *blob2,
212 FILE *f1, FILE *f2, const char *label1, const char *label2,
213 enum got_diff_algorithm diff_algo, int diff_context,
214 int ignore_whitespace, int force_text_diff, FILE *outfile)
216 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
217 label1, label2, 0, 0, diff_context, ignore_whitespace,
218 force_text_diff, outfile, diff_algo);
221 static const struct got_error *
222 diff_blob_file(struct got_diffreg_result **resultp,
223 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
224 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
225 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
226 int force_text_diff, FILE *outfile)
228 const struct got_error *err = NULL, *free_err;
229 char hex1[SHA1_DIGEST_STRING_LENGTH];
230 const char *idstr1 = NULL;
231 struct got_diffreg_result *result = NULL;
237 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
239 idstr1 = "/dev/null";
244 /* display file mode for new added files only */
245 if (f2_exists && blob1 == NULL) {
246 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
248 if (S_ISLNK(sb2->st_mode))
250 if (asprintf(&mode, " (mode %o)",
251 sb2->st_mode & mmask) == -1)
252 return got_error_from_errno("asprintf");
254 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
255 fprintf(outfile, "file + %s%s\n",
256 f2_exists ? label2 : "/dev/null", mode ? mode : "");
260 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
266 err = got_diffreg_output(NULL, NULL, result,
267 blob1 != NULL, f2_exists,
268 label2, /* show local file's path, not a blob ID */
269 label2, GOT_DIFF_OUTPUT_UNIDIFF,
270 diff_context, outfile);
275 if (resultp && err == NULL)
278 free_err = got_diffreg_result_free(result);
279 if (free_err && err == NULL)
286 const struct got_error *
287 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
288 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
289 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
290 int ignore_whitespace, int force_text_diff, FILE *outfile)
292 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
293 sb2, label2, diff_algo, diff_context, ignore_whitespace,
294 force_text_diff, outfile);
297 static const struct got_error *
298 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
299 const char *label, mode_t mode, struct got_repository *repo,
300 got_diff_blob_cb cb, void *cb_arg)
302 const struct got_error *err;
303 struct got_blob_object *blob = NULL;
304 struct got_object *obj = NULL;
306 err = got_object_open(&obj, repo, id);
310 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
313 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
314 NULL, label, 0, mode, repo);
316 got_object_close(obj);
318 got_object_blob_close(blob);
322 static const struct got_error *
323 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
324 FILE *f1, FILE *f2, int fd1, int fd2,
325 const char *label1, const char *label2,
326 mode_t mode1, mode_t mode2, struct got_repository *repo,
327 got_diff_blob_cb cb, void *cb_arg)
329 const struct got_error *err;
330 struct got_object *obj1 = NULL;
331 struct got_object *obj2 = NULL;
332 struct got_blob_object *blob1 = NULL;
333 struct got_blob_object *blob2 = NULL;
335 err = got_object_open(&obj1, repo, id1);
339 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
340 err = got_error(GOT_ERR_OBJ_TYPE);
344 err = got_object_open(&obj2, repo, id2);
347 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
348 err = got_error(GOT_ERR_BAD_OBJ_DATA);
352 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
356 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
360 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
364 got_object_close(obj1);
366 got_object_close(obj2);
368 got_object_blob_close(blob1);
370 got_object_blob_close(blob2);
374 static const struct got_error *
375 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
376 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
377 got_diff_blob_cb cb, void *cb_arg)
379 const struct got_error *err;
380 struct got_blob_object *blob = NULL;
381 struct got_object *obj = NULL;
383 err = got_object_open(&obj, repo, id);
387 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
390 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
393 got_object_close(obj);
395 got_object_blob_close(blob);
399 static const struct got_error *
400 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
401 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
402 void *cb_arg, int diff_content)
404 const struct got_error *err = NULL;
405 struct got_object *treeobj = NULL;
406 struct got_tree_object *tree = NULL;
408 err = got_object_open(&treeobj, repo, id);
412 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
413 err = got_error(GOT_ERR_OBJ_TYPE);
417 err = got_object_tree_open(&tree, repo, treeobj);
421 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
422 repo, cb, cb_arg, diff_content);
425 got_object_tree_close(tree);
427 got_object_close(treeobj);
431 static const struct got_error *
432 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
433 FILE *f1, FILE *f2, int fd1, int fd2,
434 const char *label1, const char *label2,
435 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
438 const struct got_error *err;
439 struct got_object *treeobj1 = NULL;
440 struct got_object *treeobj2 = NULL;
441 struct got_tree_object *tree1 = NULL;
442 struct got_tree_object *tree2 = NULL;
444 err = got_object_open(&treeobj1, repo, id1);
448 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
449 err = got_error(GOT_ERR_OBJ_TYPE);
453 err = got_object_open(&treeobj2, repo, id2);
457 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
458 err = got_error(GOT_ERR_OBJ_TYPE);
462 err = got_object_tree_open(&tree1, repo, treeobj1);
466 err = got_object_tree_open(&tree2, repo, treeobj2);
470 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
471 label1, label2, repo, cb, cb_arg, diff_content);
475 got_object_tree_close(tree1);
477 got_object_tree_close(tree2);
479 got_object_close(treeobj1);
481 got_object_close(treeobj2);
485 static const struct got_error *
486 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
487 FILE *f2, const char *label, struct got_repository *repo,
488 got_diff_blob_cb cb, void *cb_arg, int diff_content)
490 const struct got_error *err;
491 struct got_object *treeobj = NULL;
492 struct got_tree_object *tree = NULL;
494 err = got_object_open(&treeobj, repo, id);
498 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
499 err = got_error(GOT_ERR_OBJ_TYPE);
503 err = got_object_tree_open(&tree, repo, treeobj);
507 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
508 repo, cb, cb_arg, diff_content);
511 got_object_tree_close(tree);
513 got_object_close(treeobj);
517 static const struct got_error *
518 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
519 const char *label1, const char *label2, struct got_repository *repo,
520 got_diff_blob_cb cb, void *cb_arg)
526 static const struct got_error *
527 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
528 FILE *f1, FILE *f2, int fd1, int fd2,
529 const char *label1, const char *label2,
530 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
533 const struct got_error *err = NULL;
536 if (got_object_tree_entry_is_submodule(te1))
540 if (S_ISDIR(te1->mode))
541 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
542 label1, repo, cb, cb_arg, diff_content);
545 err = diff_deleted_blob(&te1->id, f1, fd1,
546 f2, label1, te1->mode, repo, cb, cb_arg);
548 err = cb(cb_arg, NULL, NULL, NULL, NULL,
549 &te1->id, NULL, label1, NULL,
553 } else if (got_object_tree_entry_is_submodule(te2))
556 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
557 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
559 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
560 fd1, fd2, label1, label2, repo, cb, cb_arg,
562 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
563 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
565 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
566 (te2->mode & (S_IFLNK | S_IXUSR))) {
568 return diff_modified_blob(&te1->id, &te2->id,
569 f1, f2, fd1, fd2, label1, label2,
570 te1->mode, te2->mode, repo, cb, cb_arg);
572 return cb(cb_arg, NULL, NULL, NULL, NULL,
573 &te1->id, &te2->id, label1, label2,
574 te1->mode, te2->mode, repo);
581 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
585 static const struct got_error *
586 diff_entry_new_old(struct got_tree_entry *te2,
587 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
588 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
591 if (te1 != NULL) /* handled by diff_entry_old_new() */
594 if (got_object_tree_entry_is_submodule(te2))
597 if (S_ISDIR(te2->mode))
598 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
599 repo, cb, cb_arg, diff_content);
602 return diff_added_blob(&te2->id, f1, f2, fd2,
603 label2, te2->mode, repo, cb, cb_arg);
605 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
606 NULL, label2, 0, te2->mode, repo);
609 const struct got_error *
610 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
611 struct got_blob_object *blob2, FILE *f1, FILE *f2,
612 struct got_object_id *id1, struct got_object_id *id2,
613 const char *label1, const char *label2,
614 mode_t mode1, mode_t mode2, struct got_repository *repo)
616 const struct got_error *err = NULL;
617 struct got_pathlist_head *paths = arg;
618 struct got_diff_changed_path *change = NULL;
621 path = strdup(label2 ? label2 : label1);
623 return got_error_from_errno("malloc");
625 change = malloc(sizeof(*change));
626 if (change == NULL) {
627 err = got_error_from_errno("malloc");
631 change->status = GOT_STATUS_NO_CHANGE;
633 change->status = GOT_STATUS_ADD;
634 else if (id2 == NULL)
635 change->status = GOT_STATUS_DELETE;
637 if (got_object_id_cmp(id1, id2) != 0)
638 change->status = GOT_STATUS_MODIFY;
639 else if (mode1 != mode2)
640 change->status = GOT_STATUS_MODE_CHANGE;
643 err = got_pathlist_append(paths, path, change);
652 const struct got_error *
653 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
654 FILE *f1, FILE *f2, int fd1, int fd2,
655 const char *label1, const char *label2,
656 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
659 const struct got_error *err = NULL;
660 struct got_tree_entry *te1 = NULL;
661 struct got_tree_entry *te2 = NULL;
662 char *l1 = NULL, *l2 = NULL;
663 int tidx1 = 0, tidx2 = 0;
666 te1 = got_object_tree_get_entry(tree1, 0);
667 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
669 return got_error_from_errno("asprintf");
672 te2 = got_object_tree_get_entry(tree2, 0);
673 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
675 return got_error_from_errno("asprintf");
680 struct got_tree_entry *te = NULL;
682 te = got_object_tree_find_entry(tree2,
687 if (te && asprintf(&l2, "%s%s%s", label2,
688 label2[0] ? "/" : "", te->name) == -1)
690 got_error_from_errno("asprintf");
692 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
693 l1, l2, repo, cb, cb_arg, diff_content);
699 struct got_tree_entry *te = NULL;
701 te = got_object_tree_find_entry(tree1,
705 if (asprintf(&l2, "%s%s%s", label2,
706 label2[0] ? "/" : "", te->name) == -1)
708 got_error_from_errno("asprintf");
710 if (asprintf(&l2, "%s%s%s", label2,
711 label2[0] ? "/" : "", te2->name) == -1)
713 got_error_from_errno("asprintf");
715 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
716 repo, cb, cb_arg, diff_content);
725 te1 = got_object_tree_get_entry(tree1, tidx1);
727 asprintf(&l1, "%s%s%s", label1,
728 label1[0] ? "/" : "", te1->name) == -1)
729 return got_error_from_errno("asprintf");
735 te2 = got_object_tree_get_entry(tree2, tidx2);
737 asprintf(&l2, "%s%s%s", label2,
738 label2[0] ? "/" : "", te2->name) == -1)
739 return got_error_from_errno("asprintf");
741 } while (te1 || te2);
746 const struct got_error *
747 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
748 FILE *f1, FILE *f2, int fd1, int fd2,
749 struct got_object_id *id1, struct got_object_id *id2,
750 const char *label1, const char *label2,
751 enum got_diff_algorithm diff_algo, int diff_context,
752 int ignore_whitespace, int force_text_diff,
753 struct got_repository *repo, FILE *outfile)
755 const struct got_error *err;
756 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
758 if (id1 == NULL && id2 == NULL)
759 return got_error(GOT_ERR_NO_OBJ);
762 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
767 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
771 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
772 diff_algo, diff_context, ignore_whitespace, force_text_diff,
776 got_object_blob_close(blob1);
778 got_object_blob_close(blob2);
782 static const struct got_error *
783 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
784 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
785 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
787 const struct got_error *err = NULL;
788 struct got_pathlist_entry *pe;
789 struct got_object_id *id1 = NULL, *id2 = NULL;
790 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
791 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
793 TAILQ_FOREACH(pe, paths, entry) {
794 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
795 mode_t mode1 = 0, mode2 = 0;
802 got_object_tree_close(subtree1);
806 got_object_tree_close(subtree2);
810 got_object_blob_close(blob1);
814 got_object_blob_close(blob2);
818 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
820 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
822 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
824 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
826 if (id1 == NULL && id2 == NULL) {
827 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
831 err = got_object_get_type(&type1, repo, id1);
836 err = got_object_get_type(&type2, repo, id2);
840 if (type1 == GOT_OBJ_TYPE_ANY &&
841 type2 == GOT_OBJ_TYPE_ANY) {
842 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
844 } else if (type1 != GOT_OBJ_TYPE_ANY &&
845 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
846 err = got_error(GOT_ERR_OBJ_TYPE);
850 if (type1 == GOT_OBJ_TYPE_BLOB ||
851 type2 == GOT_OBJ_TYPE_BLOB) {
853 err = got_object_open_as_blob(&blob1, repo,
859 err = got_object_open_as_blob(&blob2, repo,
864 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
865 id1 ? pe->path : "/dev/null",
866 id2 ? pe->path : "/dev/null",
870 } else if (type1 == GOT_OBJ_TYPE_TREE ||
871 type2 == GOT_OBJ_TYPE_TREE) {
873 err = got_object_open_as_tree(&subtree1, repo,
879 err = got_object_open_as_tree(&subtree2, repo,
884 err = got_diff_tree(subtree1, subtree2, f1, f2,
886 id1 ? pe->path : "/dev/null",
887 id2 ? pe->path : "/dev/null",
888 repo, cb, cb_arg, 1);
892 err = got_error(GOT_ERR_OBJ_TYPE);
900 got_object_tree_close(subtree1);
902 got_object_tree_close(subtree2);
904 got_object_blob_close(blob1);
906 got_object_blob_close(blob2);
910 static const struct got_error *
911 show_object_id(struct got_diff_line **lines, size_t *nlines,
912 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
914 const struct got_error *err;
918 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
920 return got_error_from_errno("fprintf");
922 if (lines != NULL && *lines != NULL) {
924 err = add_line_metadata(lines, nlines, 0,
929 outoff = (*lines)[*nlines - 1].offset;
932 err = add_line_metadata(lines, nlines, outoff,
941 static const struct got_error *
942 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
943 FILE *f1, FILE *f2, int fd1, int fd2,
944 struct got_object_id *id1, struct got_object_id *id2,
945 struct got_pathlist_head *paths, const char *label1, const char *label2,
946 int diff_context, int ignore_whitespace, int force_text_diff,
947 struct got_repository *repo, FILE *outfile,
948 enum got_diff_algorithm diff_algo)
950 const struct got_error *err;
951 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
952 struct got_diff_blob_output_unidiff_arg arg;
953 int want_linemeta = (lines != NULL && *lines != NULL);
955 if (id1 == NULL && id2 == NULL)
956 return got_error(GOT_ERR_NO_OBJ);
959 err = got_object_open_as_tree(&tree1, repo, id1);
964 err = got_object_open_as_tree(&tree2, repo, id2);
969 arg.diff_algo = diff_algo;
970 arg.diff_context = diff_context;
971 arg.ignore_whitespace = ignore_whitespace;
972 arg.force_text_diff = force_text_diff;
973 arg.outfile = outfile;
976 arg.nlines = *nlines;
981 if (paths == NULL || TAILQ_EMPTY(paths)) {
982 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
983 label1, label2, repo,
984 got_diff_blob_output_unidiff, &arg, 1);
986 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
987 got_diff_blob_output_unidiff, &arg);
990 *lines = arg.lines; /* was likely re-allocated */
991 *nlines = arg.nlines;
995 got_object_tree_close(tree1);
997 got_object_tree_close(tree2);
1001 const struct got_error *
1002 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1003 FILE *f1, FILE *f2, int fd1, int fd2,
1004 struct got_object_id *id1, struct got_object_id *id2,
1005 struct got_pathlist_head *paths, const char *label1, const char *label2,
1006 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1007 int force_text_diff, struct got_repository *repo, FILE *outfile)
1009 const struct got_error *err;
1012 if (id1 == NULL && id2 == NULL)
1013 return got_error(GOT_ERR_NO_OBJ);
1016 err = got_object_id_str(&idstr, id1);
1019 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1025 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1032 err = got_object_id_str(&idstr, id2);
1035 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1041 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1047 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1048 paths, label1, label2, diff_context, ignore_whitespace,
1049 force_text_diff, repo, outfile, diff_algo);
1055 const struct got_error *
1056 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1057 FILE *f1, FILE *f2, int fd1, int fd2,
1058 struct got_object_id *id1, struct got_object_id *id2,
1059 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1060 int diff_context, int ignore_whitespace, int force_text_diff,
1061 struct got_repository *repo, FILE *outfile)
1063 const struct got_error *err;
1064 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1068 return got_error(GOT_ERR_NO_OBJ);
1071 err = got_object_open_as_commit(&commit1, repo, id1);
1074 err = got_object_id_str(&idstr, id1);
1077 err = show_object_id(lines, nlines, "commit", '-', idstr,
1084 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1090 err = got_object_open_as_commit(&commit2, repo, id2);
1094 err = got_object_id_str(&idstr, id2);
1097 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1101 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1102 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1103 got_object_commit_get_tree_id(commit2), paths, "", "",
1104 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1108 got_object_commit_close(commit1);
1110 got_object_commit_close(commit2);
1115 const struct got_error *
1116 got_diff_files(struct got_diffreg_result **resultp,
1117 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1118 const char *label2, int diff_context, int ignore_whitespace,
1119 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1121 const struct got_error *err = NULL;
1122 struct got_diffreg_result *diffreg_result = NULL;
1128 fprintf(outfile, "file - %s\n",
1129 f1_exists ? label1 : "/dev/null");
1130 fprintf(outfile, "file + %s\n",
1131 f2_exists ? label2 : "/dev/null");
1134 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1135 ignore_whitespace, force_text_diff);
1140 err = got_diffreg_output(NULL, NULL, diffreg_result,
1141 f1_exists, f2_exists, label1, label2,
1142 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1148 if (resultp && err == NULL)
1149 *resultp = diffreg_result;
1150 else if (diffreg_result) {
1151 const struct got_error *free_err;
1152 free_err = got_diffreg_result_free(diffreg_result);
1153 if (free_err && err == NULL)