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>
26 #include "got_compat.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.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 static const struct got_error *
43 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
44 off_t off, uint8_t type)
46 struct got_diff_line *p;
48 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
50 return got_error_from_errno("reallocarray");
52 (*lines)[*nlines].offset = off;
53 (*lines)[*nlines].type = type;
59 static const struct got_error *
60 diff_blobs(struct got_diff_line **lines, size_t *nlines,
61 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
62 struct got_blob_object *blob2, FILE *f1, FILE *f2,
63 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
64 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
65 enum got_diff_algorithm diff_algo)
67 const struct got_error *err = NULL, *free_err;
68 char hex1[SHA1_DIGEST_STRING_LENGTH];
69 char hex2[SHA1_DIGEST_STRING_LENGTH];
70 const char *idstr1 = NULL, *idstr2 = NULL;
72 struct got_diffreg_result *result;
76 if (lines && *lines && *nlines > 0)
77 outoff = (*lines)[*nlines - 1].offset;
79 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
88 err = got_opentemp_truncate(f1);
93 err = got_opentemp_truncate(f2);
100 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
101 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
106 idstr1 = "/dev/null";
110 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
111 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
116 idstr2 = "/dev/null";
119 char *modestr1 = NULL, *modestr2 = NULL;
121 if (mode1 && mode1 != mode2) {
125 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
126 if (asprintf(&modestr1, " (mode %o)",
127 mode1 & modebits) == -1) {
128 err = got_error_from_errno("asprintf");
132 if (mode2 && mode1 != mode2) {
136 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
137 if (asprintf(&modestr2, " (mode %o)",
138 mode2 & modebits) == -1) {
139 err = got_error_from_errno("asprintf");
143 n = fprintf(outfile, "blob - %s%s\n", idstr1,
144 modestr1 ? modestr1 : "");
149 err = add_line_metadata(lines, nlines, outoff,
150 GOT_DIFF_LINE_BLOB_MIN);
155 n = fprintf(outfile, "blob + %s%s\n", idstr2,
156 modestr2 ? modestr2 : "");
161 err = add_line_metadata(lines, nlines, outoff,
162 GOT_DIFF_LINE_BLOB_PLUS);
170 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
176 err = got_diffreg_output(lines, nlines, result,
177 blob1 != NULL, blob2 != NULL,
178 label1 ? label1 : idstr1,
179 label2 ? label2 : idstr2,
180 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
185 if (resultp && err == NULL)
188 free_err = got_diffreg_result_free(result);
189 if (free_err && err == NULL)
196 const struct got_error *
197 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
198 struct got_blob_object *blob2, FILE *f1, FILE *f2,
199 struct got_object_id *id1, struct got_object_id *id2,
200 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
201 struct got_repository *repo)
203 struct got_diff_blob_output_unidiff_arg *a = arg;
205 return diff_blobs(&a->lines, &a->nlines, NULL,
206 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
207 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
210 const struct got_error *
211 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
212 struct got_blob_object *blob1, struct got_blob_object *blob2,
213 FILE *f1, FILE *f2, const char *label1, const char *label2,
214 enum got_diff_algorithm diff_algo, int diff_context,
215 int ignore_whitespace, int force_text_diff, FILE *outfile)
217 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
218 label1, label2, 0, 0, diff_context, ignore_whitespace,
219 force_text_diff, outfile, diff_algo);
222 static const struct got_error *
223 diff_blob_file(struct got_diffreg_result **resultp,
224 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
225 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
226 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
227 int force_text_diff, FILE *outfile)
229 const struct got_error *err = NULL, *free_err;
230 char hex1[SHA1_DIGEST_STRING_LENGTH];
231 const char *idstr1 = NULL;
232 struct got_diffreg_result *result = NULL;
238 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
240 idstr1 = "/dev/null";
245 /* display file mode for new added files only */
246 if (f2_exists && blob1 == NULL) {
247 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
249 if (S_ISLNK(sb2->st_mode))
251 if (asprintf(&mode, " (mode %o)",
252 sb2->st_mode & mmask) == -1)
253 return got_error_from_errno("asprintf");
255 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
256 fprintf(outfile, "file + %s%s\n",
257 f2_exists ? label2 : "/dev/null", mode ? mode : "");
261 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
267 err = got_diffreg_output(NULL, NULL, result,
268 blob1 != NULL, f2_exists,
269 label2, /* show local file's path, not a blob ID */
270 label2, GOT_DIFF_OUTPUT_UNIDIFF,
271 diff_context, outfile);
276 if (resultp && err == NULL)
279 free_err = got_diffreg_result_free(result);
280 if (free_err && err == NULL)
287 const struct got_error *
288 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
289 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
290 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
291 int ignore_whitespace, int force_text_diff, FILE *outfile)
293 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
294 sb2, label2, diff_algo, diff_context, ignore_whitespace,
295 force_text_diff, outfile);
298 static const struct got_error *
299 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
300 const char *label, mode_t mode, struct got_repository *repo,
301 got_diff_blob_cb cb, void *cb_arg)
303 const struct got_error *err;
304 struct got_blob_object *blob = NULL;
305 struct got_object *obj = NULL;
307 err = got_object_open(&obj, repo, id);
311 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
314 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
315 NULL, label, 0, mode, repo);
317 got_object_close(obj);
319 got_object_blob_close(blob);
323 static const struct got_error *
324 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
325 FILE *f1, FILE *f2, int fd1, int fd2,
326 const char *label1, const char *label2,
327 mode_t mode1, mode_t mode2, struct got_repository *repo,
328 got_diff_blob_cb cb, void *cb_arg)
330 const struct got_error *err;
331 struct got_object *obj1 = NULL;
332 struct got_object *obj2 = NULL;
333 struct got_blob_object *blob1 = NULL;
334 struct got_blob_object *blob2 = NULL;
336 err = got_object_open(&obj1, repo, id1);
340 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 err = got_object_open(&obj2, repo, id2);
348 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
349 err = got_error(GOT_ERR_BAD_OBJ_DATA);
353 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
357 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
361 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
365 got_object_close(obj1);
367 got_object_close(obj2);
369 got_object_blob_close(blob1);
371 got_object_blob_close(blob2);
375 static const struct got_error *
376 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
377 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
378 got_diff_blob_cb cb, void *cb_arg)
380 const struct got_error *err;
381 struct got_blob_object *blob = NULL;
382 struct got_object *obj = NULL;
384 err = got_object_open(&obj, repo, id);
388 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
391 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
394 got_object_close(obj);
396 got_object_blob_close(blob);
400 static const struct got_error *
401 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
402 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
403 void *cb_arg, int diff_content)
405 const struct got_error *err = NULL;
406 struct got_object *treeobj = NULL;
407 struct got_tree_object *tree = NULL;
409 err = got_object_open(&treeobj, repo, id);
413 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
414 err = got_error(GOT_ERR_OBJ_TYPE);
418 err = got_object_tree_open(&tree, repo, treeobj);
422 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
423 repo, cb, cb_arg, diff_content);
426 got_object_tree_close(tree);
428 got_object_close(treeobj);
432 static const struct got_error *
433 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
434 FILE *f1, FILE *f2, int fd1, int fd2,
435 const char *label1, const char *label2,
436 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
439 const struct got_error *err;
440 struct got_object *treeobj1 = NULL;
441 struct got_object *treeobj2 = NULL;
442 struct got_tree_object *tree1 = NULL;
443 struct got_tree_object *tree2 = NULL;
445 err = got_object_open(&treeobj1, repo, id1);
449 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
450 err = got_error(GOT_ERR_OBJ_TYPE);
454 err = got_object_open(&treeobj2, repo, id2);
458 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
459 err = got_error(GOT_ERR_OBJ_TYPE);
463 err = got_object_tree_open(&tree1, repo, treeobj1);
467 err = got_object_tree_open(&tree2, repo, treeobj2);
471 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
472 label1, label2, repo, cb, cb_arg, diff_content);
476 got_object_tree_close(tree1);
478 got_object_tree_close(tree2);
480 got_object_close(treeobj1);
482 got_object_close(treeobj2);
486 static const struct got_error *
487 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
488 FILE *f2, const char *label, struct got_repository *repo,
489 got_diff_blob_cb cb, void *cb_arg, int diff_content)
491 const struct got_error *err;
492 struct got_object *treeobj = NULL;
493 struct got_tree_object *tree = NULL;
495 err = got_object_open(&treeobj, repo, id);
499 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
500 err = got_error(GOT_ERR_OBJ_TYPE);
504 err = got_object_tree_open(&tree, repo, treeobj);
508 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
509 repo, cb, cb_arg, diff_content);
512 got_object_tree_close(tree);
514 got_object_close(treeobj);
518 static const struct got_error *
519 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
520 const char *label1, const char *label2, struct got_repository *repo,
521 got_diff_blob_cb cb, void *cb_arg)
527 static const struct got_error *
528 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
529 FILE *f1, FILE *f2, int fd1, int fd2,
530 const char *label1, const char *label2,
531 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
534 const struct got_error *err = NULL;
537 if (got_object_tree_entry_is_submodule(te1))
541 if (S_ISDIR(te1->mode))
542 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
543 label1, repo, cb, cb_arg, diff_content);
546 err = diff_deleted_blob(&te1->id, f1, fd1,
547 f2, label1, te1->mode, repo, cb, cb_arg);
549 err = cb(cb_arg, NULL, NULL, NULL, NULL,
550 &te1->id, NULL, label1, NULL,
554 } else if (got_object_tree_entry_is_submodule(te2))
557 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
558 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
560 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
561 fd1, fd2, label1, label2, repo, cb, cb_arg,
563 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
564 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
566 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
567 (te2->mode & (S_IFLNK | S_IXUSR))) {
569 return diff_modified_blob(&te1->id, &te2->id,
570 f1, f2, fd1, fd2, label1, label2,
571 te1->mode, te2->mode, repo, cb, cb_arg);
573 return cb(cb_arg, NULL, NULL, NULL, NULL,
574 &te1->id, &te2->id, label1, label2,
575 te1->mode, te2->mode, repo);
582 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
586 static const struct got_error *
587 diff_entry_new_old(struct got_tree_entry *te2,
588 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
589 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
592 if (te1 != NULL) /* handled by diff_entry_old_new() */
595 if (got_object_tree_entry_is_submodule(te2))
598 if (S_ISDIR(te2->mode))
599 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
600 repo, cb, cb_arg, diff_content);
603 return diff_added_blob(&te2->id, f1, f2, fd2,
604 label2, te2->mode, repo, cb, cb_arg);
606 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
607 NULL, label2, 0, te2->mode, repo);
610 const struct got_error *
611 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
612 struct got_blob_object *blob2, FILE *f1, FILE *f2,
613 struct got_object_id *id1, struct got_object_id *id2,
614 const char *label1, const char *label2,
615 mode_t mode1, mode_t mode2, struct got_repository *repo)
617 const struct got_error *err = NULL;
618 struct got_pathlist_head *paths = arg;
619 struct got_diff_changed_path *change = NULL;
622 path = strdup(label2 ? label2 : label1);
624 return got_error_from_errno("malloc");
626 change = malloc(sizeof(*change));
627 if (change == NULL) {
628 err = got_error_from_errno("malloc");
632 change->status = GOT_STATUS_NO_CHANGE;
634 change->status = GOT_STATUS_ADD;
635 else if (id2 == NULL)
636 change->status = GOT_STATUS_DELETE;
638 if (got_object_id_cmp(id1, id2) != 0)
639 change->status = GOT_STATUS_MODIFY;
640 else if (mode1 != mode2)
641 change->status = GOT_STATUS_MODE_CHANGE;
644 err = got_pathlist_append(paths, path, change);
653 const struct got_error *
654 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
655 FILE *f1, FILE *f2, int fd1, int fd2,
656 const char *label1, const char *label2,
657 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
660 const struct got_error *err = NULL;
661 struct got_tree_entry *te1 = NULL;
662 struct got_tree_entry *te2 = NULL;
663 char *l1 = NULL, *l2 = NULL;
664 int tidx1 = 0, tidx2 = 0;
667 te1 = got_object_tree_get_entry(tree1, 0);
668 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
670 return got_error_from_errno("asprintf");
673 te2 = got_object_tree_get_entry(tree2, 0);
674 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
676 return got_error_from_errno("asprintf");
681 struct got_tree_entry *te = NULL;
683 te = got_object_tree_find_entry(tree2,
688 if (te && asprintf(&l2, "%s%s%s", label2,
689 label2[0] ? "/" : "", te->name) == -1)
691 got_error_from_errno("asprintf");
693 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
694 l1, l2, repo, cb, cb_arg, diff_content);
700 struct got_tree_entry *te = NULL;
702 te = got_object_tree_find_entry(tree1,
706 if (asprintf(&l2, "%s%s%s", label2,
707 label2[0] ? "/" : "", te->name) == -1)
709 got_error_from_errno("asprintf");
711 if (asprintf(&l2, "%s%s%s", label2,
712 label2[0] ? "/" : "", te2->name) == -1)
714 got_error_from_errno("asprintf");
716 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
717 repo, cb, cb_arg, diff_content);
726 te1 = got_object_tree_get_entry(tree1, tidx1);
728 asprintf(&l1, "%s%s%s", label1,
729 label1[0] ? "/" : "", te1->name) == -1)
730 return got_error_from_errno("asprintf");
736 te2 = got_object_tree_get_entry(tree2, tidx2);
738 asprintf(&l2, "%s%s%s", label2,
739 label2[0] ? "/" : "", te2->name) == -1)
740 return got_error_from_errno("asprintf");
742 } while (te1 || te2);
747 const struct got_error *
748 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
749 FILE *f1, FILE *f2, int fd1, int fd2,
750 struct got_object_id *id1, struct got_object_id *id2,
751 const char *label1, const char *label2,
752 enum got_diff_algorithm diff_algo, int diff_context,
753 int ignore_whitespace, int force_text_diff,
754 struct got_repository *repo, FILE *outfile)
756 const struct got_error *err;
757 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
759 if (id1 == NULL && id2 == NULL)
760 return got_error(GOT_ERR_NO_OBJ);
763 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
768 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
772 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
773 diff_algo, diff_context, ignore_whitespace, force_text_diff,
777 got_object_blob_close(blob1);
779 got_object_blob_close(blob2);
783 static const struct got_error *
784 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
785 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
786 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
788 const struct got_error *err = NULL;
789 struct got_pathlist_entry *pe;
790 struct got_object_id *id1 = NULL, *id2 = NULL;
791 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
792 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
794 TAILQ_FOREACH(pe, paths, entry) {
795 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
796 mode_t mode1 = 0, mode2 = 0;
803 got_object_tree_close(subtree1);
807 got_object_tree_close(subtree2);
811 got_object_blob_close(blob1);
815 got_object_blob_close(blob2);
819 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
821 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
823 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
825 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
827 if (id1 == NULL && id2 == NULL) {
828 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
832 err = got_object_get_type(&type1, repo, id1);
837 err = got_object_get_type(&type2, repo, id2);
841 if (type1 == GOT_OBJ_TYPE_ANY &&
842 type2 == GOT_OBJ_TYPE_ANY) {
843 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
845 } else if (type1 != GOT_OBJ_TYPE_ANY &&
846 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
847 err = got_error(GOT_ERR_OBJ_TYPE);
851 if (type1 == GOT_OBJ_TYPE_BLOB ||
852 type2 == GOT_OBJ_TYPE_BLOB) {
854 err = got_object_open_as_blob(&blob1, repo,
860 err = got_object_open_as_blob(&blob2, repo,
865 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
866 id1 ? pe->path : "/dev/null",
867 id2 ? pe->path : "/dev/null",
871 } else if (type1 == GOT_OBJ_TYPE_TREE ||
872 type2 == GOT_OBJ_TYPE_TREE) {
874 err = got_object_open_as_tree(&subtree1, repo,
880 err = got_object_open_as_tree(&subtree2, repo,
885 err = got_diff_tree(subtree1, subtree2, f1, f2,
887 id1 ? pe->path : "/dev/null",
888 id2 ? pe->path : "/dev/null",
889 repo, cb, cb_arg, 1);
893 err = got_error(GOT_ERR_OBJ_TYPE);
901 got_object_tree_close(subtree1);
903 got_object_tree_close(subtree2);
905 got_object_blob_close(blob1);
907 got_object_blob_close(blob2);
911 static const struct got_error *
912 show_object_id(struct got_diff_line **lines, size_t *nlines,
913 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
915 const struct got_error *err;
919 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
921 return got_error_from_errno("fprintf");
923 if (lines != NULL && *lines != NULL) {
925 err = add_line_metadata(lines, nlines, 0,
930 outoff = (*lines)[*nlines - 1].offset;
933 err = add_line_metadata(lines, nlines, outoff,
942 static const struct got_error *
943 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
944 FILE *f1, FILE *f2, int fd1, int fd2,
945 struct got_object_id *id1, struct got_object_id *id2,
946 struct got_pathlist_head *paths, const char *label1, const char *label2,
947 int diff_context, int ignore_whitespace, int force_text_diff,
948 struct got_repository *repo, FILE *outfile,
949 enum got_diff_algorithm diff_algo)
951 const struct got_error *err;
952 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
953 struct got_diff_blob_output_unidiff_arg arg;
954 int want_linemeta = (lines != NULL && *lines != NULL);
956 if (id1 == NULL && id2 == NULL)
957 return got_error(GOT_ERR_NO_OBJ);
960 err = got_object_open_as_tree(&tree1, repo, id1);
965 err = got_object_open_as_tree(&tree2, repo, id2);
970 arg.diff_algo = diff_algo;
971 arg.diff_context = diff_context;
972 arg.ignore_whitespace = ignore_whitespace;
973 arg.force_text_diff = force_text_diff;
974 arg.outfile = outfile;
977 arg.nlines = *nlines;
982 if (paths == NULL || TAILQ_EMPTY(paths)) {
983 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
984 label1, label2, repo,
985 got_diff_blob_output_unidiff, &arg, 1);
987 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
988 got_diff_blob_output_unidiff, &arg);
991 *lines = arg.lines; /* was likely re-allocated */
992 *nlines = arg.nlines;
996 got_object_tree_close(tree1);
998 got_object_tree_close(tree2);
1002 const struct got_error *
1003 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1004 FILE *f1, FILE *f2, int fd1, int fd2,
1005 struct got_object_id *id1, struct got_object_id *id2,
1006 struct got_pathlist_head *paths, const char *label1, const char *label2,
1007 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1008 int force_text_diff, struct got_repository *repo, FILE *outfile)
1010 const struct got_error *err;
1013 if (id1 == NULL && id2 == NULL)
1014 return got_error(GOT_ERR_NO_OBJ);
1017 err = got_object_id_str(&idstr, id1);
1020 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1026 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1033 err = got_object_id_str(&idstr, id2);
1036 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1042 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1048 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1049 paths, label1, label2, diff_context, ignore_whitespace,
1050 force_text_diff, repo, outfile, diff_algo);
1056 const struct got_error *
1057 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1058 FILE *f1, FILE *f2, int fd1, int fd2,
1059 struct got_object_id *id1, struct got_object_id *id2,
1060 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1061 int diff_context, int ignore_whitespace, int force_text_diff,
1062 struct got_repository *repo, FILE *outfile)
1064 const struct got_error *err;
1065 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1069 return got_error(GOT_ERR_NO_OBJ);
1072 err = got_object_open_as_commit(&commit1, repo, id1);
1075 err = got_object_id_str(&idstr, id1);
1078 err = show_object_id(lines, nlines, "commit", '-', idstr,
1085 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1091 err = got_object_open_as_commit(&commit2, repo, id2);
1095 err = got_object_id_str(&idstr, id2);
1098 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1102 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1103 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1104 got_object_commit_get_tree_id(commit2), paths, "", "",
1105 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1109 got_object_commit_close(commit1);
1111 got_object_commit_close(commit2);
1116 const struct got_error *
1117 got_diff_files(struct got_diffreg_result **resultp,
1118 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1119 const char *label2, int diff_context, int ignore_whitespace,
1120 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1122 const struct got_error *err = NULL;
1123 struct got_diffreg_result *diffreg_result = NULL;
1129 fprintf(outfile, "file - %s\n",
1130 f1_exists ? label1 : "/dev/null");
1131 fprintf(outfile, "file + %s\n",
1132 f2_exists ? label2 : "/dev/null");
1135 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1136 ignore_whitespace, force_text_diff);
1141 err = got_diffreg_output(NULL, NULL, diffreg_result,
1142 f1_exists, f2_exists, label1, label2,
1143 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1149 if (resultp && err == NULL)
1150 *resultp = diffreg_result;
1151 else if (diffreg_result) {
1152 const struct got_error *free_err;
1153 free_err = got_diffreg_result_free(diffreg_result);
1154 if (free_err && err == NULL)