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, size_t size2, 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";
243 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
244 fprintf(outfile, "file + %s\n",
245 f2_exists ? label2 : "/dev/null");
248 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
254 err = got_diffreg_output(NULL, NULL, result,
255 blob1 != NULL, f2_exists,
256 label2, /* show local file's path, not a blob ID */
257 label2, GOT_DIFF_OUTPUT_UNIDIFF,
258 diff_context, outfile);
263 if (resultp && err == NULL)
266 free_err = got_diffreg_result_free(result);
267 if (free_err && err == NULL)
274 const struct got_error *
275 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
276 const char *label1, FILE *f2, int f2_exists, size_t size2,
277 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
278 int ignore_whitespace, int force_text_diff, FILE *outfile)
280 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
281 size2, label2, diff_algo, diff_context, ignore_whitespace,
282 force_text_diff, outfile );
285 static const struct got_error *
286 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
287 const char *label, mode_t mode, struct got_repository *repo,
288 got_diff_blob_cb cb, void *cb_arg)
290 const struct got_error *err;
291 struct got_blob_object *blob = NULL;
292 struct got_object *obj = NULL;
294 err = got_object_open(&obj, repo, id);
298 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
301 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
302 NULL, label, 0, mode, repo);
304 got_object_close(obj);
306 got_object_blob_close(blob);
310 static const struct got_error *
311 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
312 FILE *f1, FILE *f2, int fd1, int fd2,
313 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);
327 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
328 err = got_error(GOT_ERR_OBJ_TYPE);
332 err = got_object_open(&obj2, repo, id2);
335 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
336 err = got_error(GOT_ERR_BAD_OBJ_DATA);
340 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
344 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
348 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
352 got_object_close(obj1);
354 got_object_close(obj2);
356 got_object_blob_close(blob1);
358 got_object_blob_close(blob2);
362 static const struct got_error *
363 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
364 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
365 got_diff_blob_cb cb, void *cb_arg)
367 const struct got_error *err;
368 struct got_blob_object *blob = NULL;
369 struct got_object *obj = NULL;
371 err = got_object_open(&obj, repo, id);
375 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
378 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
381 got_object_close(obj);
383 got_object_blob_close(blob);
387 static const struct got_error *
388 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
389 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
390 void *cb_arg, int diff_content)
392 const struct got_error *err = NULL;
393 struct got_object *treeobj = NULL;
394 struct got_tree_object *tree = NULL;
396 err = got_object_open(&treeobj, repo, id);
400 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
401 err = got_error(GOT_ERR_OBJ_TYPE);
405 err = got_object_tree_open(&tree, repo, treeobj);
409 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
410 repo, cb, cb_arg, diff_content);
413 got_object_tree_close(tree);
415 got_object_close(treeobj);
419 static const struct got_error *
420 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
421 FILE *f1, FILE *f2, int fd1, int fd2,
422 const char *label1, const char *label2,
423 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
426 const struct got_error *err;
427 struct got_object *treeobj1 = NULL;
428 struct got_object *treeobj2 = NULL;
429 struct got_tree_object *tree1 = NULL;
430 struct got_tree_object *tree2 = NULL;
432 err = got_object_open(&treeobj1, repo, id1);
436 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
437 err = got_error(GOT_ERR_OBJ_TYPE);
441 err = got_object_open(&treeobj2, repo, id2);
445 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
446 err = got_error(GOT_ERR_OBJ_TYPE);
450 err = got_object_tree_open(&tree1, repo, treeobj1);
454 err = got_object_tree_open(&tree2, repo, treeobj2);
458 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
459 label1, label2, repo, cb, cb_arg, diff_content);
463 got_object_tree_close(tree1);
465 got_object_tree_close(tree2);
467 got_object_close(treeobj1);
469 got_object_close(treeobj2);
473 static const struct got_error *
474 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
475 FILE *f2, const char *label, struct got_repository *repo,
476 got_diff_blob_cb cb, void *cb_arg, int diff_content)
478 const struct got_error *err;
479 struct got_object *treeobj = NULL;
480 struct got_tree_object *tree = NULL;
482 err = got_object_open(&treeobj, repo, id);
486 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
487 err = got_error(GOT_ERR_OBJ_TYPE);
491 err = got_object_tree_open(&tree, repo, treeobj);
495 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
496 repo, cb, cb_arg, diff_content);
499 got_object_tree_close(tree);
501 got_object_close(treeobj);
505 static const struct got_error *
506 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
507 const char *label1, const char *label2, struct got_repository *repo,
508 got_diff_blob_cb cb, void *cb_arg)
514 static const struct got_error *
515 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
516 FILE *f1, FILE *f2, int fd1, int fd2,
517 const char *label1, const char *label2,
518 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
521 const struct got_error *err = NULL;
524 if (got_object_tree_entry_is_submodule(te1))
528 if (S_ISDIR(te1->mode))
529 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
530 label1, repo, cb, cb_arg, diff_content);
533 err = diff_deleted_blob(&te1->id, f1, fd1,
534 f2, label1, te1->mode, repo, cb, cb_arg);
536 err = cb(cb_arg, NULL, NULL, NULL, NULL,
537 &te1->id, NULL, label1, NULL,
541 } else if (got_object_tree_entry_is_submodule(te2))
544 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
545 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
547 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
548 fd1, fd2, label1, label2, repo, cb, cb_arg,
550 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
551 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
553 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
554 (te2->mode & (S_IFLNK | S_IXUSR))) {
556 return diff_modified_blob(&te1->id, &te2->id,
557 f1, f2, fd1, fd2, label1, label2,
558 te1->mode, te2->mode, repo, cb, cb_arg);
560 return cb(cb_arg, NULL, NULL, NULL, NULL,
561 &te1->id, &te2->id, label1, label2,
562 te1->mode, te2->mode, repo);
569 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
573 static const struct got_error *
574 diff_entry_new_old(struct got_tree_entry *te2,
575 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
576 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
579 if (te1 != NULL) /* handled by diff_entry_old_new() */
582 if (got_object_tree_entry_is_submodule(te2))
585 if (S_ISDIR(te2->mode))
586 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
587 repo, cb, cb_arg, diff_content);
590 return diff_added_blob(&te2->id, f1, f2, fd2,
591 label2, te2->mode, repo, cb, cb_arg);
593 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
594 NULL, label2, 0, te2->mode, repo);
597 const struct got_error *
598 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
599 struct got_blob_object *blob2, FILE *f1, FILE *f2,
600 struct got_object_id *id1, struct got_object_id *id2,
601 const char *label1, const char *label2,
602 mode_t mode1, mode_t mode2, struct got_repository *repo)
604 const struct got_error *err = NULL;
605 struct got_pathlist_head *paths = arg;
606 struct got_diff_changed_path *change = NULL;
609 path = strdup(label2 ? label2 : label1);
611 return got_error_from_errno("malloc");
613 change = malloc(sizeof(*change));
614 if (change == NULL) {
615 err = got_error_from_errno("malloc");
619 change->status = GOT_STATUS_NO_CHANGE;
621 change->status = GOT_STATUS_ADD;
622 else if (id2 == NULL)
623 change->status = GOT_STATUS_DELETE;
625 if (got_object_id_cmp(id1, id2) != 0)
626 change->status = GOT_STATUS_MODIFY;
627 else if (mode1 != mode2)
628 change->status = GOT_STATUS_MODE_CHANGE;
631 err = got_pathlist_append(paths, path, change);
640 const struct got_error *
641 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
642 FILE *f1, FILE *f2, int fd1, int fd2,
643 const char *label1, const char *label2,
644 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
647 const struct got_error *err = NULL;
648 struct got_tree_entry *te1 = NULL;
649 struct got_tree_entry *te2 = NULL;
650 char *l1 = NULL, *l2 = NULL;
651 int tidx1 = 0, tidx2 = 0;
654 te1 = got_object_tree_get_entry(tree1, 0);
655 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
657 return got_error_from_errno("asprintf");
660 te2 = got_object_tree_get_entry(tree2, 0);
661 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
663 return got_error_from_errno("asprintf");
668 struct got_tree_entry *te = NULL;
670 te = got_object_tree_find_entry(tree2,
675 if (te && asprintf(&l2, "%s%s%s", label2,
676 label2[0] ? "/" : "", te->name) == -1)
678 got_error_from_errno("asprintf");
680 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
681 l1, l2, repo, cb, cb_arg, diff_content);
687 struct got_tree_entry *te = NULL;
689 te = got_object_tree_find_entry(tree1,
693 if (asprintf(&l2, "%s%s%s", label2,
694 label2[0] ? "/" : "", te->name) == -1)
696 got_error_from_errno("asprintf");
698 if (asprintf(&l2, "%s%s%s", label2,
699 label2[0] ? "/" : "", te2->name) == -1)
701 got_error_from_errno("asprintf");
703 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
704 repo, cb, cb_arg, diff_content);
713 te1 = got_object_tree_get_entry(tree1, tidx1);
715 asprintf(&l1, "%s%s%s", label1,
716 label1[0] ? "/" : "", te1->name) == -1)
717 return got_error_from_errno("asprintf");
723 te2 = got_object_tree_get_entry(tree2, tidx2);
725 asprintf(&l2, "%s%s%s", label2,
726 label2[0] ? "/" : "", te2->name) == -1)
727 return got_error_from_errno("asprintf");
729 } while (te1 || te2);
734 const struct got_error *
735 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
736 FILE *f1, FILE *f2, int fd1, int fd2,
737 struct got_object_id *id1, struct got_object_id *id2,
738 const char *label1, const char *label2,
739 enum got_diff_algorithm diff_algo, int diff_context,
740 int ignore_whitespace, int force_text_diff,
741 struct got_repository *repo, FILE *outfile)
743 const struct got_error *err;
744 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
746 if (id1 == NULL && id2 == NULL)
747 return got_error(GOT_ERR_NO_OBJ);
750 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
755 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
759 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
760 diff_algo, diff_context, ignore_whitespace, force_text_diff,
764 got_object_blob_close(blob1);
766 got_object_blob_close(blob2);
770 static const struct got_error *
771 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
772 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
773 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
775 const struct got_error *err = NULL;
776 struct got_pathlist_entry *pe;
777 struct got_object_id *id1 = NULL, *id2 = NULL;
778 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
779 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
781 TAILQ_FOREACH(pe, paths, entry) {
782 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
783 mode_t mode1 = 0, mode2 = 0;
790 got_object_tree_close(subtree1);
794 got_object_tree_close(subtree2);
798 got_object_blob_close(blob1);
802 got_object_blob_close(blob2);
806 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
808 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
810 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
812 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
814 if (id1 == NULL && id2 == NULL) {
815 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
819 err = got_object_get_type(&type1, repo, id1);
824 err = got_object_get_type(&type2, repo, id2);
828 if (type1 == GOT_OBJ_TYPE_ANY &&
829 type2 == GOT_OBJ_TYPE_ANY) {
830 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
832 } else if (type1 != GOT_OBJ_TYPE_ANY &&
833 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
834 err = got_error(GOT_ERR_OBJ_TYPE);
838 if (type1 == GOT_OBJ_TYPE_BLOB ||
839 type2 == GOT_OBJ_TYPE_BLOB) {
841 err = got_object_open_as_blob(&blob1, repo,
847 err = got_object_open_as_blob(&blob2, repo,
852 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
853 id1 ? pe->path : "/dev/null",
854 id2 ? pe->path : "/dev/null",
858 } else if (type1 == GOT_OBJ_TYPE_TREE ||
859 type2 == GOT_OBJ_TYPE_TREE) {
861 err = got_object_open_as_tree(&subtree1, repo,
867 err = got_object_open_as_tree(&subtree2, repo,
872 err = got_diff_tree(subtree1, subtree2, f1, f2,
874 id1 ? pe->path : "/dev/null",
875 id2 ? pe->path : "/dev/null",
876 repo, cb, cb_arg, 1);
880 err = got_error(GOT_ERR_OBJ_TYPE);
888 got_object_tree_close(subtree1);
890 got_object_tree_close(subtree2);
892 got_object_blob_close(blob1);
894 got_object_blob_close(blob2);
898 static const struct got_error *
899 show_object_id(struct got_diff_line **lines, size_t *nlines,
900 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
902 const struct got_error *err;
906 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
908 return got_error_from_errno("fprintf");
910 if (lines != NULL && *lines != NULL) {
912 err = add_line_metadata(lines, nlines, 0,
917 outoff = (*lines)[*nlines - 1].offset;
920 err = add_line_metadata(lines, nlines, outoff,
929 static const struct got_error *
930 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
931 FILE *f1, FILE *f2, int fd1, int fd2,
932 struct got_object_id *id1, struct got_object_id *id2,
933 struct got_pathlist_head *paths, const char *label1, const char *label2,
934 int diff_context, int ignore_whitespace, int force_text_diff,
935 struct got_repository *repo, FILE *outfile,
936 enum got_diff_algorithm diff_algo)
938 const struct got_error *err;
939 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
940 struct got_diff_blob_output_unidiff_arg arg;
941 int want_linemeta = (lines != NULL && *lines != NULL);
943 if (id1 == NULL && id2 == NULL)
944 return got_error(GOT_ERR_NO_OBJ);
947 err = got_object_open_as_tree(&tree1, repo, id1);
952 err = got_object_open_as_tree(&tree2, repo, id2);
957 arg.diff_algo = diff_algo;
958 arg.diff_context = diff_context;
959 arg.ignore_whitespace = ignore_whitespace;
960 arg.force_text_diff = force_text_diff;
961 arg.outfile = outfile;
964 arg.nlines = *nlines;
969 if (paths == NULL || TAILQ_EMPTY(paths)) {
970 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
971 label1, label2, repo,
972 got_diff_blob_output_unidiff, &arg, 1);
974 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
975 got_diff_blob_output_unidiff, &arg);
978 *lines = arg.lines; /* was likely re-allocated */
979 *nlines = arg.nlines;
983 got_object_tree_close(tree1);
985 got_object_tree_close(tree2);
989 const struct got_error *
990 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
991 FILE *f1, FILE *f2, int fd1, int fd2,
992 struct got_object_id *id1, struct got_object_id *id2,
993 struct got_pathlist_head *paths, const char *label1, const char *label2,
994 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
995 int force_text_diff, struct got_repository *repo, FILE *outfile)
997 const struct got_error *err;
1000 if (id1 == NULL && id2 == NULL)
1001 return got_error(GOT_ERR_NO_OBJ);
1004 err = got_object_id_str(&idstr, id1);
1007 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1013 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1020 err = got_object_id_str(&idstr, id2);
1023 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1029 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1035 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1036 paths, label1, label2, diff_context, ignore_whitespace,
1037 force_text_diff, repo, outfile, diff_algo);
1043 const struct got_error *
1044 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1045 FILE *f1, FILE *f2, int fd1, int fd2,
1046 struct got_object_id *id1, struct got_object_id *id2,
1047 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1048 int diff_context, int ignore_whitespace, int force_text_diff,
1049 struct got_repository *repo, FILE *outfile)
1051 const struct got_error *err;
1052 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1056 return got_error(GOT_ERR_NO_OBJ);
1059 err = got_object_open_as_commit(&commit1, repo, id1);
1062 err = got_object_id_str(&idstr, id1);
1065 err = show_object_id(lines, nlines, "commit", '-', idstr,
1072 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1078 err = got_object_open_as_commit(&commit2, repo, id2);
1082 err = got_object_id_str(&idstr, id2);
1085 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1089 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1090 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1091 got_object_commit_get_tree_id(commit2), paths, "", "",
1092 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1096 got_object_commit_close(commit1);
1098 got_object_commit_close(commit2);
1103 const struct got_error *
1104 got_diff_files(struct got_diffreg_result **resultp,
1105 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1106 const char *label2, int diff_context, int ignore_whitespace,
1107 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1109 const struct got_error *err = NULL;
1110 struct got_diffreg_result *diffreg_result = NULL;
1116 fprintf(outfile, "file - %s\n",
1117 f1_exists ? label1 : "/dev/null");
1118 fprintf(outfile, "file + %s\n",
1119 f2_exists ? label2 : "/dev/null");
1122 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1123 ignore_whitespace, force_text_diff);
1128 err = got_diffreg_output(NULL, NULL, diffreg_result,
1129 f1_exists, f2_exists, label1, label2,
1130 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1136 if (resultp && err == NULL)
1137 *resultp = diffreg_result;
1138 else if (diffreg_result) {
1139 const struct got_error *free_err;
1140 free_err = got_diffreg_result_free(diffreg_result);
1141 if (free_err && err == NULL)