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"
42 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
45 static const struct got_error *
46 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
47 off_t off, uint8_t type)
49 struct got_diff_line *p;
51 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 return got_error_from_errno("reallocarray");
55 (*lines)[*nlines].offset = off;
56 (*lines)[*nlines].type = type;
63 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
64 uint32_t add, uint32_t rm)
69 *maxlen = MAX(*maxlen, len);
73 *add_cols = MAX(*add_cols, d1);
77 *rm_cols = MAX(*rm_cols, d2);
80 static const struct got_error *
81 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
82 struct diff_result *r, int force_text, int status)
84 const struct got_error *err;
85 struct got_pathlist_entry *pe;
86 struct got_diff_changed_path *change = NULL;
87 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
88 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
91 change = calloc(1, sizeof(*change));
93 return got_error_from_errno("calloc");
95 if (!isbin || force_text) {
96 for (i = 0; i < r->chunks.len; ++i) {
100 c = diff_chunk_get(r, i);
101 clc = diff_chunk_get_left_count(c);
102 crc = diff_chunk_get_right_count(c);
111 change->status = status;
112 ds->ins += change->add;
113 ds->del += change->rm;
116 err = got_pathlist_append(ds->paths, path, change);
120 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
121 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
122 pe->path_len, change->add, change->rm);
127 static const struct got_error *
128 diff_blobs(struct got_diff_line **lines, size_t *nlines,
129 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
130 struct got_blob_object *blob2, FILE *f1, FILE *f2,
131 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
132 int diff_context, int ignore_whitespace, int force_text_diff,
133 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
134 enum got_diff_algorithm diff_algo)
136 const struct got_error *err = NULL, *free_err;
137 char hex1[SHA1_DIGEST_STRING_LENGTH];
138 char hex2[SHA1_DIGEST_STRING_LENGTH];
139 const char *idstr1 = NULL, *idstr2 = NULL;
141 struct got_diffreg_result *result = NULL;
145 if (lines && *lines && *nlines > 0)
146 outoff = (*lines)[*nlines - 1].offset;
148 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
157 err = got_opentemp_truncate(f1);
162 err = got_opentemp_truncate(f2);
169 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
170 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
175 idstr1 = "/dev/null";
179 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
180 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
185 idstr2 = "/dev/null";
188 char *modestr1 = NULL, *modestr2 = NULL;
190 if (mode1 && mode1 != mode2) {
194 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
195 if (asprintf(&modestr1, " (mode %o)",
196 mode1 & modebits) == -1) {
197 err = got_error_from_errno("asprintf");
201 if (mode2 && mode1 != mode2) {
205 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
206 if (asprintf(&modestr2, " (mode %o)",
207 mode2 & modebits) == -1) {
208 err = got_error_from_errno("asprintf");
212 n = fprintf(outfile, "blob - %s%s\n", idstr1,
213 modestr1 ? modestr1 : "");
218 err = add_line_metadata(lines, nlines, outoff,
219 GOT_DIFF_LINE_BLOB_MIN);
224 n = fprintf(outfile, "blob + %s%s\n", idstr2,
225 modestr2 ? modestr2 : "");
230 err = add_line_metadata(lines, nlines, outoff,
231 GOT_DIFF_LINE_BLOB_PLUS);
240 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
247 int status = GOT_STATUS_NO_CHANGE;
250 * Ignore 'm'ode status change: if there's no accompanying
251 * content change, there'll be no diffstat, and if there
252 * are actual changes, 'M'odified takes precedence.
255 status = GOT_STATUS_ADD;
256 else if (blob2 == NULL)
257 status = GOT_STATUS_DELETE;
259 status = GOT_STATUS_MODIFY;
261 if (label1 == NULL && label2 == NULL) {
262 /* diffstat of blobs, show hash instead of path */
263 if (asprintf(&path, "%.10s -> %.10s",
264 idstr1, idstr2) == -1) {
265 err = got_error_from_errno("asprintf");
269 if (label2 != NULL &&
270 (status != GOT_STATUS_DELETE || label1 == NULL))
271 path = strdup(label2);
273 path = strdup(label1);
275 err = got_error_from_errno("strdup");
280 err = get_diffstat(ds, path, result->result, force_text_diff,
289 err = got_diffreg_output(lines, nlines, result,
290 blob1 != NULL, blob2 != NULL,
291 label1 ? label1 : idstr1,
292 label2 ? label2 : idstr2,
293 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
299 if (resultp && err == NULL)
302 free_err = got_diffreg_result_free(result);
303 if (free_err && err == NULL)
310 const struct got_error *
311 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
312 struct got_blob_object *blob2, FILE *f1, FILE *f2,
313 struct got_object_id *id1, struct got_object_id *id2,
314 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
315 struct got_repository *repo)
317 struct got_diff_blob_output_unidiff_arg *a = arg;
319 return diff_blobs(&a->lines, &a->nlines, NULL,
320 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
321 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
322 a->diffstat, a->outfile, a->diff_algo);
325 const struct got_error *
326 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
327 struct got_blob_object *blob1, struct got_blob_object *blob2,
328 FILE *f1, FILE *f2, const char *label1, const char *label2,
329 enum got_diff_algorithm diff_algo, int diff_context,
330 int ignore_whitespace, int force_text_diff, int show_diffstat,
331 struct got_diffstat_cb_arg *ds, FILE *outfile)
333 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
334 label1, label2, 0, 0, diff_context, ignore_whitespace,
335 force_text_diff, show_diffstat, ds, outfile, diff_algo);
338 static const struct got_error *
339 diff_blob_file(struct got_diffreg_result **resultp,
340 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
341 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
342 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
343 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
346 const struct got_error *err = NULL, *free_err;
347 char hex1[SHA1_DIGEST_STRING_LENGTH];
348 const char *idstr1 = NULL;
349 struct got_diffreg_result *result = NULL;
355 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
357 idstr1 = "/dev/null";
362 /* display file mode for new added files only */
363 if (f2_exists && blob1 == NULL) {
364 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
366 if (S_ISLNK(sb2->st_mode))
368 if (asprintf(&mode, " (mode %o)",
369 sb2->st_mode & mmask) == -1)
370 return got_error_from_errno("asprintf");
372 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
373 fprintf(outfile, "file + %s%s\n",
374 f2_exists ? label2 : "/dev/null", mode ? mode : "");
378 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
384 err = got_diffreg_output(NULL, NULL, result,
385 blob1 != NULL, f2_exists,
386 label2, /* show local file's path, not a blob ID */
387 label2, GOT_DIFF_OUTPUT_UNIDIFF,
388 diff_context, outfile);
395 int status = GOT_STATUS_NO_CHANGE;
398 * Ignore 'm'ode status change: if there's no accompanying
399 * content change, there'll be no diffstat, and if there
400 * are actual changes, 'M'odified takes precedence.
403 status = GOT_STATUS_ADD;
405 status = GOT_STATUS_DELETE;
407 status = GOT_STATUS_MODIFY;
409 if (label2 != NULL &&
410 (status != GOT_STATUS_DELETE || label1 == NULL))
411 path = strdup(label2);
413 path = strdup(label1);
415 err = got_error_from_errno("strdup");
419 err = get_diffstat(ds, path, result->result, force_text_diff,
428 if (resultp && err == NULL)
431 free_err = got_diffreg_result_free(result);
432 if (free_err && err == NULL)
438 const struct got_error *
439 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
440 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
441 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
442 int ignore_whitespace, int force_text_diff, int show_diffstat,
443 struct got_diffstat_cb_arg *ds, FILE *outfile)
445 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
446 sb2, label2, diff_algo, diff_context, ignore_whitespace,
447 force_text_diff, show_diffstat, ds, outfile);
450 static const struct got_error *
451 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
452 const char *label, mode_t mode, struct got_repository *repo,
453 got_diff_blob_cb cb, void *cb_arg)
455 const struct got_error *err;
456 struct got_blob_object *blob = NULL;
457 struct got_object *obj = NULL;
459 err = got_object_open(&obj, repo, id);
463 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
466 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
467 NULL, label, 0, mode, repo);
469 got_object_close(obj);
471 got_object_blob_close(blob);
475 static const struct got_error *
476 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
477 FILE *f1, FILE *f2, int fd1, int fd2,
478 const char *label1, const char *label2,
479 mode_t mode1, mode_t mode2, struct got_repository *repo,
480 got_diff_blob_cb cb, void *cb_arg)
482 const struct got_error *err;
483 struct got_object *obj1 = NULL;
484 struct got_object *obj2 = NULL;
485 struct got_blob_object *blob1 = NULL;
486 struct got_blob_object *blob2 = NULL;
488 err = got_object_open(&obj1, repo, id1);
492 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
493 err = got_error(GOT_ERR_OBJ_TYPE);
497 err = got_object_open(&obj2, repo, id2);
500 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
501 err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
509 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
513 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
517 got_object_close(obj1);
519 got_object_close(obj2);
521 got_object_blob_close(blob1);
523 got_object_blob_close(blob2);
527 static const struct got_error *
528 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
529 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
530 got_diff_blob_cb cb, void *cb_arg)
532 const struct got_error *err;
533 struct got_blob_object *blob = NULL;
534 struct got_object *obj = NULL;
536 err = got_object_open(&obj, repo, id);
540 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
543 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
546 got_object_close(obj);
548 got_object_blob_close(blob);
552 static const struct got_error *
553 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
554 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
555 void *cb_arg, int diff_content)
557 const struct got_error *err = NULL;
558 struct got_object *treeobj = NULL;
559 struct got_tree_object *tree = NULL;
561 err = got_object_open(&treeobj, repo, id);
565 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
566 err = got_error(GOT_ERR_OBJ_TYPE);
570 err = got_object_tree_open(&tree, repo, treeobj);
574 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
575 repo, cb, cb_arg, diff_content);
578 got_object_tree_close(tree);
580 got_object_close(treeobj);
584 static const struct got_error *
585 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
586 FILE *f1, FILE *f2, int fd1, int fd2,
587 const char *label1, const char *label2,
588 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
591 const struct got_error *err;
592 struct got_object *treeobj1 = NULL;
593 struct got_object *treeobj2 = NULL;
594 struct got_tree_object *tree1 = NULL;
595 struct got_tree_object *tree2 = NULL;
597 err = got_object_open(&treeobj1, repo, id1);
601 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
602 err = got_error(GOT_ERR_OBJ_TYPE);
606 err = got_object_open(&treeobj2, repo, id2);
610 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
611 err = got_error(GOT_ERR_OBJ_TYPE);
615 err = got_object_tree_open(&tree1, repo, treeobj1);
619 err = got_object_tree_open(&tree2, repo, treeobj2);
623 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
624 label1, label2, repo, cb, cb_arg, diff_content);
628 got_object_tree_close(tree1);
630 got_object_tree_close(tree2);
632 got_object_close(treeobj1);
634 got_object_close(treeobj2);
638 static const struct got_error *
639 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
640 FILE *f2, const char *label, struct got_repository *repo,
641 got_diff_blob_cb cb, void *cb_arg, int diff_content)
643 const struct got_error *err;
644 struct got_object *treeobj = NULL;
645 struct got_tree_object *tree = NULL;
647 err = got_object_open(&treeobj, repo, id);
651 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
652 err = got_error(GOT_ERR_OBJ_TYPE);
656 err = got_object_tree_open(&tree, repo, treeobj);
660 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
661 repo, cb, cb_arg, diff_content);
664 got_object_tree_close(tree);
666 got_object_close(treeobj);
670 static const struct got_error *
671 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
672 const char *label1, const char *label2, struct got_repository *repo,
673 got_diff_blob_cb cb, void *cb_arg)
679 static const struct got_error *
680 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
681 FILE *f1, FILE *f2, int fd1, int fd2,
682 const char *label1, const char *label2,
683 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
686 const struct got_error *err = NULL;
689 if (got_object_tree_entry_is_submodule(te1))
693 if (S_ISDIR(te1->mode))
694 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
695 label1, repo, cb, cb_arg, diff_content);
698 err = diff_deleted_blob(&te1->id, f1, fd1,
699 f2, label1, te1->mode, repo, cb, cb_arg);
701 err = cb(cb_arg, NULL, NULL, NULL, NULL,
702 &te1->id, NULL, label1, NULL,
706 } else if (got_object_tree_entry_is_submodule(te2))
709 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
710 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
712 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
713 fd1, fd2, label1, label2, repo, cb, cb_arg,
715 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
716 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
718 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
719 (te2->mode & (S_IFLNK | S_IXUSR))) {
721 return diff_modified_blob(&te1->id, &te2->id,
722 f1, f2, fd1, fd2, label1, label2,
723 te1->mode, te2->mode, repo, cb, cb_arg);
725 return cb(cb_arg, NULL, NULL, NULL, NULL,
726 &te1->id, &te2->id, label1, label2,
727 te1->mode, te2->mode, repo);
734 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
738 static const struct got_error *
739 diff_entry_new_old(struct got_tree_entry *te2,
740 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
741 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
744 if (te1 != NULL) /* handled by diff_entry_old_new() */
747 if (got_object_tree_entry_is_submodule(te2))
750 if (S_ISDIR(te2->mode))
751 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
752 repo, cb, cb_arg, diff_content);
755 return diff_added_blob(&te2->id, f1, f2, fd2,
756 label2, te2->mode, repo, cb, cb_arg);
758 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
759 NULL, label2, 0, te2->mode, repo);
762 const struct got_error *
763 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
764 struct got_blob_object *blob2, FILE *f1, FILE *f2,
765 struct got_object_id *id1, struct got_object_id *id2,
766 const char *label1, const char *label2,
767 mode_t mode1, mode_t mode2, struct got_repository *repo)
769 const struct got_error *err = NULL;
770 struct got_diffreg_result *result = NULL;
771 struct got_diffstat_cb_arg *a = arg;
773 int status = GOT_STATUS_NO_CHANGE;
775 path = strdup(label2 ? label2 : label1);
777 return got_error_from_errno("strdup");
780 status = GOT_STATUS_ADD;
781 else if (id2 == NULL)
782 status = GOT_STATUS_DELETE;
784 if (got_object_id_cmp(id1, id2) != 0)
785 status = GOT_STATUS_MODIFY;
786 else if (mode1 != mode2)
787 status = GOT_STATUS_MODE_CHANGE;
791 err = got_opentemp_truncate(f1);
796 err = got_opentemp_truncate(f2);
802 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
808 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
814 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
819 err = get_diffstat(a, path, result->result, a->force_text, status);
823 const struct got_error *free_err;
825 free_err = got_diffreg_result_free(result);
826 if (free_err && err == NULL)
834 const struct got_error *
835 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
836 struct got_blob_object *blob2, FILE *f1, FILE *f2,
837 struct got_object_id *id1, struct got_object_id *id2,
838 const char *label1, const char *label2,
839 mode_t mode1, mode_t mode2, struct got_repository *repo)
841 const struct got_error *err = NULL;
842 struct got_pathlist_head *paths = arg;
843 struct got_diff_changed_path *change = NULL;
846 path = strdup(label2 ? label2 : label1);
848 return got_error_from_errno("strdup");
850 change = malloc(sizeof(*change));
851 if (change == NULL) {
852 err = got_error_from_errno("malloc");
856 change->status = GOT_STATUS_NO_CHANGE;
858 change->status = GOT_STATUS_ADD;
859 else if (id2 == NULL)
860 change->status = GOT_STATUS_DELETE;
862 if (got_object_id_cmp(id1, id2) != 0)
863 change->status = GOT_STATUS_MODIFY;
864 else if (mode1 != mode2)
865 change->status = GOT_STATUS_MODE_CHANGE;
868 err = got_pathlist_append(paths, path, change);
877 const struct got_error *
878 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
879 FILE *f1, FILE *f2, int fd1, int fd2,
880 const char *label1, const char *label2,
881 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
884 const struct got_error *err = NULL;
885 struct got_tree_entry *te1 = NULL;
886 struct got_tree_entry *te2 = NULL;
887 char *l1 = NULL, *l2 = NULL;
888 int tidx1 = 0, tidx2 = 0;
891 te1 = got_object_tree_get_entry(tree1, 0);
892 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
894 return got_error_from_errno("asprintf");
897 te2 = got_object_tree_get_entry(tree2, 0);
898 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
900 return got_error_from_errno("asprintf");
905 struct got_tree_entry *te = NULL;
907 te = got_object_tree_find_entry(tree2,
912 if (te && asprintf(&l2, "%s%s%s", label2,
913 label2[0] ? "/" : "", te->name) == -1)
915 got_error_from_errno("asprintf");
917 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
918 l1, l2, repo, cb, cb_arg, diff_content);
924 struct got_tree_entry *te = NULL;
926 te = got_object_tree_find_entry(tree1,
930 if (asprintf(&l2, "%s%s%s", label2,
931 label2[0] ? "/" : "", te->name) == -1)
933 got_error_from_errno("asprintf");
935 if (asprintf(&l2, "%s%s%s", label2,
936 label2[0] ? "/" : "", te2->name) == -1)
938 got_error_from_errno("asprintf");
940 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
941 repo, cb, cb_arg, diff_content);
950 te1 = got_object_tree_get_entry(tree1, tidx1);
952 asprintf(&l1, "%s%s%s", label1,
953 label1[0] ? "/" : "", te1->name) == -1)
954 return got_error_from_errno("asprintf");
960 te2 = got_object_tree_get_entry(tree2, tidx2);
962 asprintf(&l2, "%s%s%s", label2,
963 label2[0] ? "/" : "", te2->name) == -1)
964 return got_error_from_errno("asprintf");
966 } while (te1 || te2);
971 const struct got_error *
972 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
973 FILE *f1, FILE *f2, int fd1, int fd2,
974 struct got_object_id *id1, struct got_object_id *id2,
975 const char *label1, const char *label2,
976 enum got_diff_algorithm diff_algo, int diff_context,
977 int ignore_whitespace, int force_text_diff, int show_diffstat,
978 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
980 const struct got_error *err;
981 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
983 if (id1 == NULL && id2 == NULL)
984 return got_error(GOT_ERR_NO_OBJ);
987 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
992 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
996 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
997 diff_algo, diff_context, ignore_whitespace, force_text_diff,
998 show_diffstat, ds, outfile);
1001 got_object_blob_close(blob1);
1003 got_object_blob_close(blob2);
1007 static const struct got_error *
1008 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1009 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1010 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1012 const struct got_error *err = NULL;
1013 struct got_pathlist_entry *pe;
1014 struct got_object_id *id1 = NULL, *id2 = NULL;
1015 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1016 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1018 TAILQ_FOREACH(pe, paths, entry) {
1019 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1020 mode_t mode1 = 0, mode2 = 0;
1027 got_object_tree_close(subtree1);
1031 got_object_tree_close(subtree2);
1035 got_object_blob_close(blob1);
1039 got_object_blob_close(blob2);
1043 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1045 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1047 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1049 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1051 if (id1 == NULL && id2 == NULL) {
1052 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1056 err = got_object_get_type(&type1, repo, id1);
1061 err = got_object_get_type(&type2, repo, id2);
1065 if (type1 == GOT_OBJ_TYPE_ANY &&
1066 type2 == GOT_OBJ_TYPE_ANY) {
1067 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1069 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1070 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1071 err = got_error(GOT_ERR_OBJ_TYPE);
1075 if (type1 == GOT_OBJ_TYPE_BLOB ||
1076 type2 == GOT_OBJ_TYPE_BLOB) {
1078 err = got_object_open_as_blob(&blob1, repo,
1084 err = got_object_open_as_blob(&blob2, repo,
1089 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1090 id1 ? pe->path : "/dev/null",
1091 id2 ? pe->path : "/dev/null",
1092 mode1, mode2, repo);
1095 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1096 type2 == GOT_OBJ_TYPE_TREE) {
1098 err = got_object_open_as_tree(&subtree1, repo,
1104 err = got_object_open_as_tree(&subtree2, repo,
1109 err = got_diff_tree(subtree1, subtree2, f1, f2,
1111 id1 ? pe->path : "/dev/null",
1112 id2 ? pe->path : "/dev/null",
1113 repo, cb, cb_arg, 1);
1117 err = got_error(GOT_ERR_OBJ_TYPE);
1125 got_object_tree_close(subtree1);
1127 got_object_tree_close(subtree2);
1129 got_object_blob_close(blob1);
1131 got_object_blob_close(blob2);
1135 static const struct got_error *
1136 show_object_id(struct got_diff_line **lines, size_t *nlines,
1137 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1139 const struct got_error *err;
1143 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1145 return got_error_from_errno("fprintf");
1147 if (lines != NULL && *lines != NULL) {
1149 err = add_line_metadata(lines, nlines, 0,
1150 GOT_DIFF_LINE_META);
1154 outoff = (*lines)[*nlines - 1].offset;
1157 err = add_line_metadata(lines, nlines, outoff,
1158 GOT_DIFF_LINE_META);
1166 static const struct got_error *
1167 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1168 FILE *f1, FILE *f2, int fd1, int fd2,
1169 struct got_object_id *id1, struct got_object_id *id2,
1170 struct got_pathlist_head *paths, const char *label1, const char *label2,
1171 int diff_context, int ignore_whitespace, int force_text_diff,
1172 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1173 struct got_repository *repo, FILE *outfile,
1174 enum got_diff_algorithm diff_algo)
1176 const struct got_error *err;
1177 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1178 struct got_diff_blob_output_unidiff_arg arg;
1179 int want_linemeta = (lines != NULL && *lines != NULL);
1181 if (id1 == NULL && id2 == NULL)
1182 return got_error(GOT_ERR_NO_OBJ);
1185 err = got_object_open_as_tree(&tree1, repo, id1);
1190 err = got_object_open_as_tree(&tree2, repo, id2);
1195 arg.diff_algo = diff_algo;
1196 arg.diff_context = diff_context;
1197 arg.ignore_whitespace = ignore_whitespace;
1198 arg.force_text_diff = force_text_diff;
1199 arg.show_diffstat = show_diffstat;
1201 arg.outfile = outfile;
1202 if (want_linemeta) {
1204 arg.nlines = *nlines;
1209 if (paths == NULL || TAILQ_EMPTY(paths))
1210 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1211 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1213 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1214 got_diff_blob_output_unidiff, &arg);
1215 if (want_linemeta) {
1216 *lines = arg.lines; /* was likely re-allocated */
1217 *nlines = arg.nlines;
1221 got_object_tree_close(tree1);
1223 got_object_tree_close(tree2);
1227 const struct got_error *
1228 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1229 FILE *f1, FILE *f2, int fd1, int fd2,
1230 struct got_object_id *id1, struct got_object_id *id2,
1231 struct got_pathlist_head *paths, const char *label1, const char *label2,
1232 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1233 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1234 struct got_repository *repo, FILE *outfile)
1236 const struct got_error *err;
1239 if (id1 == NULL && id2 == NULL)
1240 return got_error(GOT_ERR_NO_OBJ);
1243 err = got_object_id_str(&idstr, id1);
1246 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1252 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1259 err = got_object_id_str(&idstr, id2);
1262 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1268 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1274 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1275 paths, label1, label2, diff_context, ignore_whitespace,
1276 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1282 const struct got_error *
1283 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1284 FILE *f1, FILE *f2, int fd1, int fd2,
1285 struct got_object_id *id1, struct got_object_id *id2,
1286 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1287 int diff_context, int ignore_whitespace, int force_text_diff,
1288 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1289 struct got_repository *repo, FILE *outfile)
1291 const struct got_error *err;
1292 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1296 return got_error(GOT_ERR_NO_OBJ);
1299 err = got_object_open_as_commit(&commit1, repo, id1);
1302 err = got_object_id_str(&idstr, id1);
1305 err = show_object_id(lines, nlines, "commit", '-', idstr,
1312 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1318 err = got_object_open_as_commit(&commit2, repo, id2);
1322 err = got_object_id_str(&idstr, id2);
1325 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1329 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1330 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1331 got_object_commit_get_tree_id(commit2), paths, "", "",
1332 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1333 dsa, repo, outfile, diff_algo);
1336 got_object_commit_close(commit1);
1338 got_object_commit_close(commit2);
1343 const struct got_error *
1344 got_diff_files(struct got_diffreg_result **resultp,
1345 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1346 const char *label2, int diff_context, int ignore_whitespace,
1347 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1349 const struct got_error *err = NULL;
1350 struct got_diffreg_result *diffreg_result = NULL;
1356 fprintf(outfile, "file - %s\n",
1357 f1_exists ? label1 : "/dev/null");
1358 fprintf(outfile, "file + %s\n",
1359 f2_exists ? label2 : "/dev/null");
1362 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1363 ignore_whitespace, force_text_diff);
1368 err = got_diffreg_output(NULL, NULL, diffreg_result,
1369 f1_exists, f2_exists, label1, label2,
1370 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1376 if (resultp && err == NULL)
1377 *resultp = diffreg_result;
1378 else if (diffreg_result) {
1379 const struct got_error *free_err;
1380 free_err = got_diffreg_result_free(diffreg_result);
1381 if (free_err && err == NULL)