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, size_t size2, 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";
242 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
243 fprintf(outfile, "file + %s\n",
244 f2_exists ? label2 : "/dev/null");
247 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
253 err = got_diffreg_output(NULL, NULL, result,
254 blob1 != NULL, f2_exists,
255 label2, /* show local file's path, not a blob ID */
256 label2, GOT_DIFF_OUTPUT_UNIDIFF,
257 diff_context, outfile);
262 if (resultp && err == NULL)
265 free_err = got_diffreg_result_free(result);
266 if (free_err && err == NULL)
273 const struct got_error *
274 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
275 const char *label1, FILE *f2, int f2_exists, size_t size2,
276 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
277 int ignore_whitespace, int force_text_diff, FILE *outfile)
279 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
280 size2, label2, diff_algo, diff_context, ignore_whitespace,
281 force_text_diff, outfile );
284 static const struct got_error *
285 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
286 const char *label, mode_t mode, struct got_repository *repo,
287 got_diff_blob_cb cb, void *cb_arg)
289 const struct got_error *err;
290 struct got_blob_object *blob = NULL;
291 struct got_object *obj = NULL;
293 err = got_object_open(&obj, repo, id);
297 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
300 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
301 NULL, label, 0, mode, repo);
303 got_object_close(obj);
305 got_object_blob_close(blob);
309 static const struct got_error *
310 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
311 FILE *f1, FILE *f2, int fd1, int fd2,
312 const char *label1, const char *label2,
313 mode_t mode1, mode_t mode2, struct got_repository *repo,
314 got_diff_blob_cb cb, void *cb_arg)
316 const struct got_error *err;
317 struct got_object *obj1 = NULL;
318 struct got_object *obj2 = NULL;
319 struct got_blob_object *blob1 = NULL;
320 struct got_blob_object *blob2 = NULL;
322 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, fd1);
343 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
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 *f1, int fd1,
363 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
364 got_diff_blob_cb cb, void *cb_arg)
366 const struct got_error *err;
367 struct got_blob_object *blob = NULL;
368 struct got_object *obj = NULL;
370 err = got_object_open(&obj, repo, id);
374 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
377 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
380 got_object_close(obj);
382 got_object_blob_close(blob);
386 static const struct got_error *
387 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
388 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
389 void *cb_arg, int diff_content)
391 const struct got_error *err = NULL;
392 struct got_object *treeobj = NULL;
393 struct got_tree_object *tree = NULL;
395 err = got_object_open(&treeobj, repo, id);
399 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
400 err = got_error(GOT_ERR_OBJ_TYPE);
404 err = got_object_tree_open(&tree, repo, treeobj);
408 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
409 repo, cb, cb_arg, diff_content);
412 got_object_tree_close(tree);
414 got_object_close(treeobj);
418 static const struct got_error *
419 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
420 FILE *f1, FILE *f2, int fd1, int fd2,
421 const char *label1, const char *label2,
422 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
425 const struct got_error *err;
426 struct got_object *treeobj1 = NULL;
427 struct got_object *treeobj2 = NULL;
428 struct got_tree_object *tree1 = NULL;
429 struct got_tree_object *tree2 = NULL;
431 err = got_object_open(&treeobj1, repo, id1);
435 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
436 err = got_error(GOT_ERR_OBJ_TYPE);
440 err = got_object_open(&treeobj2, repo, id2);
444 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
445 err = got_error(GOT_ERR_OBJ_TYPE);
449 err = got_object_tree_open(&tree1, repo, treeobj1);
453 err = got_object_tree_open(&tree2, repo, treeobj2);
457 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
458 label1, label2, repo, cb, cb_arg, diff_content);
462 got_object_tree_close(tree1);
464 got_object_tree_close(tree2);
466 got_object_close(treeobj1);
468 got_object_close(treeobj2);
472 static const struct got_error *
473 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
474 FILE *f2, const char *label, struct got_repository *repo,
475 got_diff_blob_cb cb, void *cb_arg, int diff_content)
477 const struct got_error *err;
478 struct got_object *treeobj = NULL;
479 struct got_tree_object *tree = NULL;
481 err = got_object_open(&treeobj, repo, id);
485 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
486 err = got_error(GOT_ERR_OBJ_TYPE);
490 err = got_object_tree_open(&tree, repo, treeobj);
494 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
495 repo, cb, cb_arg, diff_content);
498 got_object_tree_close(tree);
500 got_object_close(treeobj);
504 static const struct got_error *
505 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
506 const char *label1, const char *label2, struct got_repository *repo,
507 got_diff_blob_cb cb, void *cb_arg)
513 static const struct got_error *
514 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
515 FILE *f1, FILE *f2, int fd1, int fd2,
516 const char *label1, const char *label2,
517 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
520 const struct got_error *err = NULL;
523 if (got_object_tree_entry_is_submodule(te1))
527 if (S_ISDIR(te1->mode))
528 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
529 label1, repo, cb, cb_arg, diff_content);
532 err = diff_deleted_blob(&te1->id, f1, fd1,
533 f2, label1, te1->mode, repo, cb, cb_arg);
535 err = cb(cb_arg, NULL, NULL, NULL, NULL,
536 &te1->id, NULL, label1, NULL,
540 } else if (got_object_tree_entry_is_submodule(te2))
543 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
544 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
546 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
547 fd1, fd2, label1, label2, repo, cb, cb_arg,
549 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
550 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
552 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
553 (te2->mode & (S_IFLNK | S_IXUSR))) {
555 return diff_modified_blob(&te1->id, &te2->id,
556 f1, f2, fd1, fd2, label1, label2,
557 te1->mode, te2->mode, repo, cb, cb_arg);
559 return cb(cb_arg, NULL, NULL, NULL, NULL,
560 &te1->id, &te2->id, label1, label2,
561 te1->mode, te2->mode, repo);
568 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
572 static const struct got_error *
573 diff_entry_new_old(struct got_tree_entry *te2,
574 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
575 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
578 if (te1 != NULL) /* handled by diff_entry_old_new() */
581 if (got_object_tree_entry_is_submodule(te2))
584 if (S_ISDIR(te2->mode))
585 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
586 repo, cb, cb_arg, diff_content);
589 return diff_added_blob(&te2->id, f1, f2, fd2,
590 label2, te2->mode, repo, cb, cb_arg);
592 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
593 NULL, label2, 0, te2->mode, repo);
596 const struct got_error *
597 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
598 struct got_blob_object *blob2, FILE *f1, FILE *f2,
599 struct got_object_id *id1, struct got_object_id *id2,
600 const char *label1, const char *label2,
601 mode_t mode1, mode_t mode2, struct got_repository *repo)
603 const struct got_error *err = NULL;
604 struct got_pathlist_head *paths = arg;
605 struct got_diff_changed_path *change = NULL;
608 path = strdup(label2 ? label2 : label1);
610 return got_error_from_errno("malloc");
612 change = malloc(sizeof(*change));
613 if (change == NULL) {
614 err = got_error_from_errno("malloc");
618 change->status = GOT_STATUS_NO_CHANGE;
620 change->status = GOT_STATUS_ADD;
621 else if (id2 == NULL)
622 change->status = GOT_STATUS_DELETE;
624 if (got_object_id_cmp(id1, id2) != 0)
625 change->status = GOT_STATUS_MODIFY;
626 else if (mode1 != mode2)
627 change->status = GOT_STATUS_MODE_CHANGE;
630 err = got_pathlist_append(paths, path, change);
639 const struct got_error *
640 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
641 FILE *f1, FILE *f2, int fd1, int fd2,
642 const char *label1, const char *label2,
643 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
646 const struct got_error *err = NULL;
647 struct got_tree_entry *te1 = NULL;
648 struct got_tree_entry *te2 = NULL;
649 char *l1 = NULL, *l2 = NULL;
650 int tidx1 = 0, tidx2 = 0;
653 te1 = got_object_tree_get_entry(tree1, 0);
654 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
656 return got_error_from_errno("asprintf");
659 te2 = got_object_tree_get_entry(tree2, 0);
660 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
662 return got_error_from_errno("asprintf");
667 struct got_tree_entry *te = NULL;
669 te = got_object_tree_find_entry(tree2,
674 if (te && asprintf(&l2, "%s%s%s", label2,
675 label2[0] ? "/" : "", te->name) == -1)
677 got_error_from_errno("asprintf");
679 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
680 l1, l2, repo, cb, cb_arg, diff_content);
686 struct got_tree_entry *te = NULL;
688 te = got_object_tree_find_entry(tree1,
692 if (asprintf(&l2, "%s%s%s", label2,
693 label2[0] ? "/" : "", te->name) == -1)
695 got_error_from_errno("asprintf");
697 if (asprintf(&l2, "%s%s%s", label2,
698 label2[0] ? "/" : "", te2->name) == -1)
700 got_error_from_errno("asprintf");
702 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
703 repo, cb, cb_arg, diff_content);
712 te1 = got_object_tree_get_entry(tree1, tidx1);
714 asprintf(&l1, "%s%s%s", label1,
715 label1[0] ? "/" : "", te1->name) == -1)
716 return got_error_from_errno("asprintf");
722 te2 = got_object_tree_get_entry(tree2, tidx2);
724 asprintf(&l2, "%s%s%s", label2,
725 label2[0] ? "/" : "", te2->name) == -1)
726 return got_error_from_errno("asprintf");
728 } while (te1 || te2);
733 const struct got_error *
734 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
735 FILE *f1, FILE *f2, int fd1, int fd2,
736 struct got_object_id *id1, struct got_object_id *id2,
737 const char *label1, const char *label2,
738 enum got_diff_algorithm diff_algo, int diff_context,
739 int ignore_whitespace, int force_text_diff,
740 struct got_repository *repo, FILE *outfile)
742 const struct got_error *err;
743 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
745 if (id1 == NULL && id2 == NULL)
746 return got_error(GOT_ERR_NO_OBJ);
749 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
754 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
758 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
759 diff_algo, diff_context, ignore_whitespace, force_text_diff,
763 got_object_blob_close(blob1);
765 got_object_blob_close(blob2);
769 static const struct got_error *
770 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
771 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
772 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
774 const struct got_error *err = NULL;
775 struct got_pathlist_entry *pe;
776 struct got_object_id *id1 = NULL, *id2 = NULL;
777 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
778 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
780 TAILQ_FOREACH(pe, paths, entry) {
781 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
782 mode_t mode1 = 0, mode2 = 0;
789 got_object_tree_close(subtree1);
793 got_object_tree_close(subtree2);
797 got_object_blob_close(blob1);
801 got_object_blob_close(blob2);
805 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
807 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
809 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
811 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
813 if (id1 == NULL && id2 == NULL) {
814 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
818 err = got_object_get_type(&type1, repo, id1);
823 err = got_object_get_type(&type2, repo, id2);
827 if (type1 == GOT_OBJ_TYPE_ANY &&
828 type2 == GOT_OBJ_TYPE_ANY) {
829 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
831 } else if (type1 != GOT_OBJ_TYPE_ANY &&
832 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
833 err = got_error(GOT_ERR_OBJ_TYPE);
837 if (type1 == GOT_OBJ_TYPE_BLOB ||
838 type2 == GOT_OBJ_TYPE_BLOB) {
840 err = got_object_open_as_blob(&blob1, repo,
846 err = got_object_open_as_blob(&blob2, repo,
851 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
852 id1 ? pe->path : "/dev/null",
853 id2 ? pe->path : "/dev/null",
857 } else if (type1 == GOT_OBJ_TYPE_TREE ||
858 type2 == GOT_OBJ_TYPE_TREE) {
860 err = got_object_open_as_tree(&subtree1, repo,
866 err = got_object_open_as_tree(&subtree2, repo,
871 err = got_diff_tree(subtree1, subtree2, f1, f2,
873 id1 ? pe->path : "/dev/null",
874 id2 ? pe->path : "/dev/null",
875 repo, cb, cb_arg, 1);
879 err = got_error(GOT_ERR_OBJ_TYPE);
887 got_object_tree_close(subtree1);
889 got_object_tree_close(subtree2);
891 got_object_blob_close(blob1);
893 got_object_blob_close(blob2);
897 static const struct got_error *
898 show_object_id(struct got_diff_line **lines, size_t *nlines,
899 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
901 const struct got_error *err;
905 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
907 return got_error_from_errno("fprintf");
909 if (lines != NULL && *lines != NULL) {
911 err = add_line_metadata(lines, nlines, 0,
916 outoff = (*lines)[*nlines - 1].offset;
919 err = add_line_metadata(lines, nlines, outoff,
928 static const struct got_error *
929 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
930 FILE *f1, FILE *f2, int fd1, int fd2,
931 struct got_object_id *id1, struct got_object_id *id2,
932 struct got_pathlist_head *paths, const char *label1, const char *label2,
933 int diff_context, int ignore_whitespace, int force_text_diff,
934 struct got_repository *repo, FILE *outfile,
935 enum got_diff_algorithm diff_algo)
937 const struct got_error *err;
938 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
939 struct got_diff_blob_output_unidiff_arg arg;
940 int want_linemeta = (lines != NULL && *lines != NULL);
942 if (id1 == NULL && id2 == NULL)
943 return got_error(GOT_ERR_NO_OBJ);
946 err = got_object_open_as_tree(&tree1, repo, id1);
951 err = got_object_open_as_tree(&tree2, repo, id2);
956 arg.diff_algo = diff_algo;
957 arg.diff_context = diff_context;
958 arg.ignore_whitespace = ignore_whitespace;
959 arg.force_text_diff = force_text_diff;
960 arg.outfile = outfile;
963 arg.nlines = *nlines;
968 if (paths == NULL || TAILQ_EMPTY(paths)) {
969 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
970 label1, label2, repo,
971 got_diff_blob_output_unidiff, &arg, 1);
973 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
974 got_diff_blob_output_unidiff, &arg);
977 *lines = arg.lines; /* was likely re-allocated */
978 *nlines = arg.nlines;
982 got_object_tree_close(tree1);
984 got_object_tree_close(tree2);
988 const struct got_error *
989 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
990 FILE *f1, FILE *f2, int fd1, int fd2,
991 struct got_object_id *id1, struct got_object_id *id2,
992 struct got_pathlist_head *paths, const char *label1, const char *label2,
993 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
994 int force_text_diff, struct got_repository *repo, FILE *outfile)
996 const struct got_error *err;
999 if (id1 == NULL && id2 == NULL)
1000 return got_error(GOT_ERR_NO_OBJ);
1003 err = got_object_id_str(&idstr, id1);
1006 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1012 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1019 err = got_object_id_str(&idstr, id2);
1022 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1028 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1034 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1035 paths, label1, label2, diff_context, ignore_whitespace,
1036 force_text_diff, repo, outfile, diff_algo);
1042 const struct got_error *
1043 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1044 FILE *f1, FILE *f2, int fd1, int fd2,
1045 struct got_object_id *id1, struct got_object_id *id2,
1046 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1047 int diff_context, int ignore_whitespace, int force_text_diff,
1048 struct got_repository *repo, FILE *outfile)
1050 const struct got_error *err;
1051 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1055 return got_error(GOT_ERR_NO_OBJ);
1058 err = got_object_open_as_commit(&commit1, repo, id1);
1061 err = got_object_id_str(&idstr, id1);
1064 err = show_object_id(lines, nlines, "commit", '-', idstr,
1071 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1077 err = got_object_open_as_commit(&commit2, repo, id2);
1081 err = got_object_id_str(&idstr, id2);
1084 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1088 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1089 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1090 got_object_commit_get_tree_id(commit2), paths, "", "",
1091 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1095 got_object_commit_close(commit1);
1097 got_object_commit_close(commit2);
1102 const struct got_error *
1103 got_diff_files(struct got_diffreg_result **resultp,
1104 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1105 const char *label2, int diff_context, int ignore_whitespace,
1106 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1108 const struct got_error *err = NULL;
1109 struct got_diffreg_result *diffreg_result = NULL;
1115 fprintf(outfile, "file - %s\n",
1116 f1_exists ? label1 : "/dev/null");
1117 fprintf(outfile, "file + %s\n",
1118 f2_exists ? label2 : "/dev/null");
1121 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1122 ignore_whitespace, force_text_diff);
1127 err = got_diffreg_output(NULL, NULL, diffreg_result,
1128 f1_exists, f2_exists, label1, label2,
1129 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1135 if (resultp && err == NULL)
1136 *resultp = diffreg_result;
1137 else if (diffreg_result) {
1138 const struct got_error *free_err;
1139 free_err = got_diffreg_result_free(diffreg_result);
1140 if (free_err && err == NULL)