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>
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"
43 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 static const struct got_error *
47 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 off_t off, uint8_t type)
50 struct got_diff_line *p;
52 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
54 return got_error_from_errno("reallocarray");
56 (*lines)[*nlines].offset = off;
57 (*lines)[*nlines].type = type;
64 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
65 uint32_t add, uint32_t rm)
70 *maxlen = MAX(*maxlen, len);
74 *add_cols = MAX(*add_cols, d1);
78 *rm_cols = MAX(*rm_cols, d2);
81 static const struct got_error *
82 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
83 struct diff_result *r, int force_text, int status)
85 const struct got_error *err;
86 struct got_pathlist_entry *pe;
87 struct got_diff_changed_path *change = NULL;
88 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
89 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
92 change = calloc(1, sizeof(*change));
94 return got_error_from_errno("calloc");
96 if (!isbin || force_text) {
97 for (i = 0; i < r->chunks.len; ++i) {
101 c = diff_chunk_get(r, i);
102 clc = diff_chunk_get_left_count(c);
103 crc = diff_chunk_get_right_count(c);
112 change->status = status;
113 ds->ins += change->add;
114 ds->del += change->rm;
117 err = got_pathlist_append(ds->paths, path, change);
123 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
124 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
125 pe->path_len, change->add, change->rm);
130 static const struct got_error *
131 diff_blobs(struct got_diff_line **lines, size_t *nlines,
132 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
133 struct got_blob_object *blob2, FILE *f1, FILE *f2,
134 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
135 int diff_context, int ignore_whitespace, int force_text_diff,
136 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
137 enum got_diff_algorithm diff_algo)
139 const struct got_error *err = NULL, *free_err;
140 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
141 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
142 const char *idstr1 = NULL, *idstr2 = NULL;
143 char *modestr1 = NULL, *modestr2 = NULL;
145 struct got_diffreg_result *result = NULL;
149 if (lines && *lines && *nlines > 0)
150 outoff = (*lines)[*nlines - 1].offset;
152 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
161 err = got_opentemp_truncate(f1);
166 err = got_opentemp_truncate(f2);
173 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
174 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
179 idstr1 = "/dev/null";
183 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
184 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
189 idstr2 = "/dev/null";
194 if (mode1 && mode1 != mode2) {
198 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
199 if (asprintf(&modestr1, " (mode %o)",
200 mode1 & modebits) == -1) {
201 err = got_error_from_errno("asprintf");
205 if (mode2 && mode1 != mode2) {
209 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
210 if (asprintf(&modestr2, " (mode %o)",
211 mode2 & modebits) == -1) {
212 err = got_error_from_errno("asprintf");
216 n = fprintf(outfile, "blob - %s%s\n", idstr1,
217 modestr1 ? modestr1 : "");
222 err = add_line_metadata(lines, nlines, outoff,
223 GOT_DIFF_LINE_BLOB_MIN);
228 n = fprintf(outfile, "blob + %s%s\n", idstr2,
229 modestr2 ? modestr2 : "");
234 err = add_line_metadata(lines, nlines, outoff,
235 GOT_DIFF_LINE_BLOB_PLUS);
241 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
248 int status = GOT_STATUS_NO_CHANGE;
251 status = GOT_STATUS_ADD;
252 else if (blob2 == NULL)
253 status = GOT_STATUS_DELETE;
255 if (strcmp(idstr1, idstr2) != 0)
256 status = GOT_STATUS_MODIFY;
257 else if (mode1 != mode2)
258 status = GOT_STATUS_MODE_CHANGE;
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(diffstat, path, result->result,
281 force_text_diff, status);
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);
301 if (resultp && err == NULL)
304 free_err = got_diffreg_result_free(result);
305 if (free_err && err == NULL)
312 const struct got_error *
313 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
314 struct got_blob_object *blob2, FILE *f1, FILE *f2,
315 struct got_object_id *id1, struct got_object_id *id2,
316 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 struct got_repository *repo)
319 struct got_diff_blob_output_unidiff_arg *a = arg;
321 return diff_blobs(&a->lines, &a->nlines, NULL,
322 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
323 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
327 const struct got_error *
328 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
329 struct got_blob_object *blob1, struct got_blob_object *blob2,
330 FILE *f1, FILE *f2, const char *label1, const char *label2,
331 enum got_diff_algorithm diff_algo, int diff_context,
332 int ignore_whitespace, int force_text_diff,
333 struct got_diffstat_cb_arg *ds, FILE *outfile)
335 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
336 label1, label2, 0, 0, diff_context, ignore_whitespace,
337 force_text_diff, ds, outfile, diff_algo);
340 static const struct got_error *
341 diff_blob_file(struct got_diffreg_result **resultp,
342 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
343 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
344 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
345 int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
347 const struct got_error *err = NULL, *free_err;
348 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
349 const char *idstr1 = NULL;
350 struct got_diffreg_result *result = NULL;
356 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
358 idstr1 = "/dev/null";
363 /* display file mode for new added files only */
364 if (f2_exists && blob1 == NULL) {
365 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
367 if (S_ISLNK(sb2->st_mode))
369 if (asprintf(&mode, " (mode %o)",
370 sb2->st_mode & mmask) == -1)
371 return got_error_from_errno("asprintf");
373 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
374 fprintf(outfile, "file + %s%s\n",
375 f2_exists ? label2 : "/dev/null", mode ? mode : "");
379 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
385 err = got_diffreg_output(NULL, NULL, result,
386 blob1 != NULL, f2_exists,
387 label2, /* show local file's path, not a blob ID */
388 label2, GOT_DIFF_OUTPUT_UNIDIFF,
389 diff_context, outfile);
396 int status = GOT_STATUS_NO_CHANGE;
399 * Ignore 'm'ode status change: if there's no accompanying
400 * content change, there'll be no diffstat, and if there
401 * are actual changes, 'M'odified takes precedence.
404 status = GOT_STATUS_ADD;
406 status = GOT_STATUS_DELETE;
408 status = GOT_STATUS_MODIFY;
410 if (label2 != NULL &&
411 (status != GOT_STATUS_DELETE || label1 == NULL))
412 path = strdup(label2);
414 path = strdup(label1);
416 err = got_error_from_errno("strdup");
420 err = get_diffstat(diffstat, path, result->result,
421 force_text_diff, status);
429 if (resultp && err == NULL)
432 free_err = got_diffreg_result_free(result);
433 if (free_err && err == NULL)
439 const struct got_error *
440 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
441 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
442 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
443 int ignore_whitespace, int force_text_diff,
444 struct got_diffstat_cb_arg *ds, FILE *outfile)
446 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
447 sb2, label2, diff_algo, diff_context, ignore_whitespace,
448 force_text_diff, ds, outfile);
451 static const struct got_error *
452 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
453 const char *label, mode_t mode, struct got_repository *repo,
454 got_diff_blob_cb cb, void *cb_arg)
456 const struct got_error *err;
457 struct got_blob_object *blob = NULL;
458 struct got_object *obj = NULL;
460 err = got_object_open(&obj, repo, id);
464 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
467 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
468 NULL, label, 0, mode, repo);
470 got_object_close(obj);
472 got_object_blob_close(blob);
476 static const struct got_error *
477 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
478 FILE *f1, FILE *f2, int fd1, int fd2,
479 const char *label1, const char *label2,
480 mode_t mode1, mode_t mode2, struct got_repository *repo,
481 got_diff_blob_cb cb, void *cb_arg)
483 const struct got_error *err;
484 struct got_object *obj1 = NULL;
485 struct got_object *obj2 = NULL;
486 struct got_blob_object *blob1 = NULL;
487 struct got_blob_object *blob2 = NULL;
489 err = got_object_open(&obj1, repo, id1);
493 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
494 err = got_error(GOT_ERR_OBJ_TYPE);
498 err = got_object_open(&obj2, repo, id2);
501 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
510 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
514 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
518 got_object_close(obj1);
520 got_object_close(obj2);
522 got_object_blob_close(blob1);
524 got_object_blob_close(blob2);
528 static const struct got_error *
529 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
530 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
531 got_diff_blob_cb cb, void *cb_arg)
533 const struct got_error *err;
534 struct got_blob_object *blob = NULL;
535 struct got_object *obj = NULL;
537 err = got_object_open(&obj, repo, id);
541 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
544 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
547 got_object_close(obj);
549 got_object_blob_close(blob);
553 static const struct got_error *
554 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
555 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
556 void *cb_arg, int diff_content)
558 const struct got_error *err = NULL;
559 struct got_object *treeobj = NULL;
560 struct got_tree_object *tree = NULL;
562 err = got_object_open(&treeobj, repo, id);
566 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
567 err = got_error(GOT_ERR_OBJ_TYPE);
571 err = got_object_tree_open(&tree, repo, treeobj);
575 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
576 repo, cb, cb_arg, diff_content);
579 got_object_tree_close(tree);
581 got_object_close(treeobj);
585 static const struct got_error *
586 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
587 FILE *f1, FILE *f2, int fd1, int fd2,
588 const char *label1, const char *label2,
589 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
592 const struct got_error *err;
593 struct got_object *treeobj1 = NULL;
594 struct got_object *treeobj2 = NULL;
595 struct got_tree_object *tree1 = NULL;
596 struct got_tree_object *tree2 = NULL;
598 err = got_object_open(&treeobj1, repo, id1);
602 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
603 err = got_error(GOT_ERR_OBJ_TYPE);
607 err = got_object_open(&treeobj2, repo, id2);
611 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
612 err = got_error(GOT_ERR_OBJ_TYPE);
616 err = got_object_tree_open(&tree1, repo, treeobj1);
620 err = got_object_tree_open(&tree2, repo, treeobj2);
624 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
625 label1, label2, repo, cb, cb_arg, diff_content);
629 got_object_tree_close(tree1);
631 got_object_tree_close(tree2);
633 got_object_close(treeobj1);
635 got_object_close(treeobj2);
639 static const struct got_error *
640 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
641 FILE *f2, const char *label, struct got_repository *repo,
642 got_diff_blob_cb cb, void *cb_arg, int diff_content)
644 const struct got_error *err;
645 struct got_object *treeobj = NULL;
646 struct got_tree_object *tree = NULL;
648 err = got_object_open(&treeobj, repo, id);
652 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
653 err = got_error(GOT_ERR_OBJ_TYPE);
657 err = got_object_tree_open(&tree, repo, treeobj);
661 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
662 repo, cb, cb_arg, diff_content);
665 got_object_tree_close(tree);
667 got_object_close(treeobj);
671 static const struct got_error *
672 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
673 const char *label1, const char *label2, struct got_repository *repo,
674 got_diff_blob_cb cb, void *cb_arg)
680 static const struct got_error *
681 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
682 FILE *f1, FILE *f2, int fd1, int fd2,
683 const char *label1, const char *label2,
684 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
687 const struct got_error *err = NULL;
690 if (got_object_tree_entry_is_submodule(te1))
694 if (S_ISDIR(te1->mode))
695 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
696 label1, repo, cb, cb_arg, diff_content);
699 err = diff_deleted_blob(&te1->id, f1, fd1,
700 f2, label1, te1->mode, repo, cb, cb_arg);
702 err = cb(cb_arg, NULL, NULL, NULL, NULL,
703 &te1->id, NULL, label1, NULL,
707 } else if (got_object_tree_entry_is_submodule(te2))
710 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
711 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
713 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
714 fd1, fd2, label1, label2, repo, cb, cb_arg,
716 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
717 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
719 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
720 (te2->mode & (S_IFLNK | S_IXUSR))) {
722 return diff_modified_blob(&te1->id, &te2->id,
723 f1, f2, fd1, fd2, label1, label2,
724 te1->mode, te2->mode, repo, cb, cb_arg);
726 return cb(cb_arg, NULL, NULL, NULL, NULL,
727 &te1->id, &te2->id, label1, label2,
728 te1->mode, te2->mode, repo);
735 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
739 static const struct got_error *
740 diff_entry_new_old(struct got_tree_entry *te2,
741 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
742 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
745 if (te1 != NULL) /* handled by diff_entry_old_new() */
748 if (got_object_tree_entry_is_submodule(te2))
751 if (S_ISDIR(te2->mode))
752 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
753 repo, cb, cb_arg, diff_content);
756 return diff_added_blob(&te2->id, f1, f2, fd2,
757 label2, te2->mode, repo, cb, cb_arg);
759 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
760 NULL, label2, 0, te2->mode, repo);
763 const struct got_error *
764 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
765 struct got_blob_object *blob2, FILE *f1, FILE *f2,
766 struct got_object_id *id1, struct got_object_id *id2,
767 const char *label1, const char *label2,
768 mode_t mode1, mode_t mode2, struct got_repository *repo)
770 const struct got_error *err = NULL;
771 struct got_diffreg_result *result = NULL;
772 struct got_diffstat_cb_arg *a = arg;
774 int status = GOT_STATUS_NO_CHANGE;
776 path = strdup(label2 ? label2 : label1);
778 return got_error_from_errno("strdup");
781 status = GOT_STATUS_ADD;
782 else if (id2 == NULL)
783 status = GOT_STATUS_DELETE;
785 if (got_object_id_cmp(id1, id2) != 0)
786 status = GOT_STATUS_MODIFY;
787 else if (mode1 != mode2)
788 status = GOT_STATUS_MODE_CHANGE;
792 err = got_opentemp_truncate(f1);
797 err = got_opentemp_truncate(f2);
803 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
809 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
815 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
820 err = get_diffstat(a, path, result->result, a->force_text, status);
824 const struct got_error *free_err;
826 free_err = got_diffreg_result_free(result);
827 if (free_err && err == NULL)
835 const struct got_error *
836 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
837 struct got_blob_object *blob2, FILE *f1, FILE *f2,
838 struct got_object_id *id1, struct got_object_id *id2,
839 const char *label1, const char *label2,
840 mode_t mode1, mode_t mode2, struct got_repository *repo)
842 const struct got_error *err = NULL;
843 struct got_pathlist_head *paths = arg;
844 struct got_diff_changed_path *change = NULL;
847 path = strdup(label2 ? label2 : label1);
849 return got_error_from_errno("strdup");
851 change = malloc(sizeof(*change));
852 if (change == NULL) {
853 err = got_error_from_errno("malloc");
857 change->status = GOT_STATUS_NO_CHANGE;
859 change->status = GOT_STATUS_ADD;
860 else if (id2 == NULL)
861 change->status = GOT_STATUS_DELETE;
863 if (got_object_id_cmp(id1, id2) != 0)
864 change->status = GOT_STATUS_MODIFY;
865 else if (mode1 != mode2)
866 change->status = GOT_STATUS_MODE_CHANGE;
869 err = got_pathlist_append(paths, path, change);
878 const struct got_error *
879 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
880 FILE *f1, FILE *f2, int fd1, int fd2,
881 const char *label1, const char *label2,
882 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
885 const struct got_error *err = NULL;
886 struct got_tree_entry *te1 = NULL;
887 struct got_tree_entry *te2 = NULL;
888 char *l1 = NULL, *l2 = NULL;
889 int tidx1 = 0, tidx2 = 0;
892 te1 = got_object_tree_get_entry(tree1, 0);
893 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
895 return got_error_from_errno("asprintf");
898 te2 = got_object_tree_get_entry(tree2, 0);
899 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
901 err = got_error_from_errno("asprintf");
908 struct got_tree_entry *te = NULL;
911 te = got_object_tree_find_entry(tree2,
916 if (te && asprintf(&l2, "%s%s%s", label2,
917 label2[0] ? "/" : "", te->name) == -1) {
918 err = got_error_from_errno("asprintf");
923 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
924 l1, l2, repo, cb, cb_arg, diff_content);
930 struct got_tree_entry *te = NULL;
933 te = got_object_tree_find_entry(tree1,
939 if (asprintf(&l2, "%s%s%s", label2,
940 label2[0] ? "/" : "", te->name) == -1) {
941 err = got_error_from_errno("asprintf");
945 if (asprintf(&l2, "%s%s%s", label2,
946 label2[0] ? "/" : "", te2->name) == -1) {
947 err = got_error_from_errno("asprintf");
952 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
953 repo, cb, cb_arg, diff_content);
962 te1 = got_object_tree_get_entry(tree1, tidx1);
964 asprintf(&l1, "%s%s%s", label1,
965 label1[0] ? "/" : "", te1->name) == -1) {
966 err = got_error_from_errno("asprintf");
975 te2 = got_object_tree_get_entry(tree2, tidx2);
977 asprintf(&l2, "%s%s%s", label2,
978 label2[0] ? "/" : "", te2->name) == -1) {
979 err = got_error_from_errno("asprintf");
983 } while (te1 || te2);
991 const struct got_error *
992 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
993 FILE *f1, FILE *f2, int fd1, int fd2,
994 struct got_object_id *id1, struct got_object_id *id2,
995 const char *label1, const char *label2,
996 enum got_diff_algorithm diff_algo, int diff_context,
997 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
998 struct got_repository *repo, FILE *outfile)
1000 const struct got_error *err;
1001 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1003 if (id1 == NULL && id2 == NULL)
1004 return got_error(GOT_ERR_NO_OBJ);
1007 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1012 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1016 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1017 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1021 got_object_blob_close(blob1);
1023 got_object_blob_close(blob2);
1027 static const struct got_error *
1028 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1029 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1030 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1032 const struct got_error *err = NULL;
1033 struct got_pathlist_entry *pe;
1034 struct got_object_id *id1 = NULL, *id2 = NULL;
1035 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1036 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1038 TAILQ_FOREACH(pe, paths, entry) {
1039 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1040 mode_t mode1 = 0, mode2 = 0;
1047 got_object_tree_close(subtree1);
1051 got_object_tree_close(subtree2);
1055 got_object_blob_close(blob1);
1059 got_object_blob_close(blob2);
1063 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1065 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1067 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1069 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1071 if (id1 == NULL && id2 == NULL) {
1072 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1076 err = got_object_get_type(&type1, repo, id1);
1081 err = got_object_get_type(&type2, repo, id2);
1085 if (type1 == GOT_OBJ_TYPE_ANY &&
1086 type2 == GOT_OBJ_TYPE_ANY) {
1087 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1089 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1090 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1091 err = got_error(GOT_ERR_OBJ_TYPE);
1095 if (type1 == GOT_OBJ_TYPE_BLOB ||
1096 type2 == GOT_OBJ_TYPE_BLOB) {
1098 err = got_object_open_as_blob(&blob1, repo,
1104 err = got_object_open_as_blob(&blob2, repo,
1109 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1110 id1 ? pe->path : "/dev/null",
1111 id2 ? pe->path : "/dev/null",
1112 mode1, mode2, repo);
1115 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1116 type2 == GOT_OBJ_TYPE_TREE) {
1118 err = got_object_open_as_tree(&subtree1, repo,
1124 err = got_object_open_as_tree(&subtree2, repo,
1129 err = got_diff_tree(subtree1, subtree2, f1, f2,
1131 id1 ? pe->path : "/dev/null",
1132 id2 ? pe->path : "/dev/null",
1133 repo, cb, cb_arg, 1);
1137 err = got_error(GOT_ERR_OBJ_TYPE);
1145 got_object_tree_close(subtree1);
1147 got_object_tree_close(subtree2);
1149 got_object_blob_close(blob1);
1151 got_object_blob_close(blob2);
1155 static const struct got_error *
1156 show_object_id(struct got_diff_line **lines, size_t *nlines,
1157 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1159 const struct got_error *err;
1163 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1165 return got_error_from_errno("fprintf");
1167 if (lines != NULL && *lines != NULL) {
1169 err = add_line_metadata(lines, nlines, 0,
1170 GOT_DIFF_LINE_META);
1174 outoff = (*lines)[*nlines - 1].offset;
1177 err = add_line_metadata(lines, nlines, outoff,
1178 GOT_DIFF_LINE_META);
1186 static const struct got_error *
1187 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1188 FILE *f1, FILE *f2, int fd1, int fd2,
1189 struct got_object_id *id1, struct got_object_id *id2,
1190 struct got_pathlist_head *paths, const char *label1, const char *label2,
1191 int diff_context, int ignore_whitespace, int force_text_diff,
1192 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1193 FILE *outfile, enum got_diff_algorithm diff_algo)
1195 const struct got_error *err;
1196 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1197 struct got_diff_blob_output_unidiff_arg arg;
1198 int want_linemeta = (lines != NULL && *lines != NULL);
1200 if (id1 == NULL && id2 == NULL)
1201 return got_error(GOT_ERR_NO_OBJ);
1204 err = got_object_open_as_tree(&tree1, repo, id1);
1209 err = got_object_open_as_tree(&tree2, repo, id2);
1214 arg.diff_algo = diff_algo;
1215 arg.diff_context = diff_context;
1216 arg.ignore_whitespace = ignore_whitespace;
1217 arg.force_text_diff = force_text_diff;
1219 arg.outfile = outfile;
1220 if (want_linemeta) {
1222 arg.nlines = *nlines;
1227 if (paths == NULL || TAILQ_EMPTY(paths))
1228 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1229 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1231 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1232 got_diff_blob_output_unidiff, &arg);
1233 if (want_linemeta) {
1234 *lines = arg.lines; /* was likely re-allocated */
1235 *nlines = arg.nlines;
1239 got_object_tree_close(tree1);
1241 got_object_tree_close(tree2);
1245 const struct got_error *
1246 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1247 FILE *f1, FILE *f2, int fd1, int fd2,
1248 struct got_object_id *id1, struct got_object_id *id2,
1249 struct got_pathlist_head *paths, const char *label1, const char *label2,
1250 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1251 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1252 struct got_repository *repo, FILE *outfile)
1254 const struct got_error *err;
1257 if (id1 == NULL && id2 == NULL)
1258 return got_error(GOT_ERR_NO_OBJ);
1261 err = got_object_id_str(&idstr, id1);
1264 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1270 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1277 err = got_object_id_str(&idstr, id2);
1280 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1286 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1292 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1293 paths, label1, label2, diff_context, ignore_whitespace,
1294 force_text_diff, dsa, repo, outfile, diff_algo);
1300 const struct got_error *
1301 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1302 FILE *f1, FILE *f2, int fd1, int fd2,
1303 struct got_object_id *id1, struct got_object_id *id2,
1304 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1305 int diff_context, int ignore_whitespace, int force_text_diff,
1306 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1308 const struct got_error *err;
1309 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1313 return got_error(GOT_ERR_NO_OBJ);
1316 err = got_object_open_as_commit(&commit1, repo, id1);
1319 err = got_object_id_str(&idstr, id1);
1322 err = show_object_id(lines, nlines, "commit", '-', idstr,
1329 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1335 err = got_object_open_as_commit(&commit2, repo, id2);
1339 err = got_object_id_str(&idstr, id2);
1342 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1346 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1347 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1348 got_object_commit_get_tree_id(commit2), paths, "", "",
1349 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1350 outfile, diff_algo);
1353 got_object_commit_close(commit1);
1355 got_object_commit_close(commit2);
1360 const struct got_error *
1361 got_diff_files(struct got_diffreg_result **resultp,
1362 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1363 const char *label2, int diff_context, int ignore_whitespace,
1364 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1366 const struct got_error *err = NULL;
1367 struct got_diffreg_result *diffreg_result = NULL;
1373 fprintf(outfile, "file - %s\n",
1374 f1_exists ? label1 : "/dev/null");
1375 fprintf(outfile, "file + %s\n",
1376 f2_exists ? label2 : "/dev/null");
1379 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1380 ignore_whitespace, force_text_diff);
1385 err = got_diffreg_output(NULL, NULL, diffreg_result,
1386 f1_exists, f2_exists, label1, label2,
1387 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1393 if (resultp && err == NULL)
1394 *resultp = diffreg_result;
1395 else if (diffreg_result) {
1396 const struct got_error *free_err;
1398 free_err = got_diffreg_result_free(diffreg_result);
1399 if (free_err && err == NULL)