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"
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 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
137 enum got_diff_algorithm diff_algo)
139 const struct got_error *err = NULL, *free_err;
140 char hex1[SHA1_DIGEST_STRING_LENGTH];
141 char hex2[SHA1_DIGEST_STRING_LENGTH];
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 * Ignore 'm'ode status change: if there's no accompanying
252 * content change, there'll be no diffstat, and if there
253 * are actual changes, 'M'odified takes precedence.
256 status = GOT_STATUS_ADD;
257 else if (blob2 == NULL)
258 status = GOT_STATUS_DELETE;
260 status = GOT_STATUS_MODIFY;
262 if (label1 == NULL && label2 == NULL) {
263 /* diffstat of blobs, show hash instead of path */
264 if (asprintf(&path, "%.10s -> %.10s",
265 idstr1, idstr2) == -1) {
266 err = got_error_from_errno("asprintf");
270 if (label2 != NULL &&
271 (status != GOT_STATUS_DELETE || label1 == NULL))
272 path = strdup(label2);
274 path = strdup(label1);
276 err = got_error_from_errno("strdup");
281 err = get_diffstat(ds, path, result->result, force_text_diff,
290 err = got_diffreg_output(lines, nlines, result,
291 blob1 != NULL, blob2 != NULL,
292 label1 ? label1 : idstr1,
293 label2 ? label2 : idstr2,
294 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
302 if (resultp && err == NULL)
305 free_err = got_diffreg_result_free(result);
306 if (free_err && err == NULL)
313 const struct got_error *
314 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
315 struct got_blob_object *blob2, FILE *f1, FILE *f2,
316 struct got_object_id *id1, struct got_object_id *id2,
317 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
318 struct got_repository *repo)
320 struct got_diff_blob_output_unidiff_arg *a = arg;
322 return diff_blobs(&a->lines, &a->nlines, NULL,
323 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
324 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
325 a->diffstat, a->outfile, a->diff_algo);
328 const struct got_error *
329 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
330 struct got_blob_object *blob1, struct got_blob_object *blob2,
331 FILE *f1, FILE *f2, const char *label1, const char *label2,
332 enum got_diff_algorithm diff_algo, int diff_context,
333 int ignore_whitespace, int force_text_diff, int show_diffstat,
334 struct got_diffstat_cb_arg *ds, FILE *outfile)
336 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
337 label1, label2, 0, 0, diff_context, ignore_whitespace,
338 force_text_diff, show_diffstat, ds, outfile, diff_algo);
341 static const struct got_error *
342 diff_blob_file(struct got_diffreg_result **resultp,
343 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
344 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
345 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
346 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
349 const struct got_error *err = NULL, *free_err;
350 char hex1[SHA1_DIGEST_STRING_LENGTH];
351 const char *idstr1 = NULL;
352 struct got_diffreg_result *result = NULL;
358 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
360 idstr1 = "/dev/null";
365 /* display file mode for new added files only */
366 if (f2_exists && blob1 == NULL) {
367 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
369 if (S_ISLNK(sb2->st_mode))
371 if (asprintf(&mode, " (mode %o)",
372 sb2->st_mode & mmask) == -1)
373 return got_error_from_errno("asprintf");
375 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
376 fprintf(outfile, "file + %s%s\n",
377 f2_exists ? label2 : "/dev/null", mode ? mode : "");
381 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
387 err = got_diffreg_output(NULL, NULL, result,
388 blob1 != NULL, f2_exists,
389 label2, /* show local file's path, not a blob ID */
390 label2, GOT_DIFF_OUTPUT_UNIDIFF,
391 diff_context, outfile);
398 int status = GOT_STATUS_NO_CHANGE;
401 * Ignore 'm'ode status change: if there's no accompanying
402 * content change, there'll be no diffstat, and if there
403 * are actual changes, 'M'odified takes precedence.
406 status = GOT_STATUS_ADD;
408 status = GOT_STATUS_DELETE;
410 status = GOT_STATUS_MODIFY;
412 if (label2 != NULL &&
413 (status != GOT_STATUS_DELETE || label1 == NULL))
414 path = strdup(label2);
416 path = strdup(label1);
418 err = got_error_from_errno("strdup");
422 err = get_diffstat(ds, path, result->result, force_text_diff,
431 if (resultp && err == NULL)
434 free_err = got_diffreg_result_free(result);
435 if (free_err && err == NULL)
441 const struct got_error *
442 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
443 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
444 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
445 int ignore_whitespace, int force_text_diff, int show_diffstat,
446 struct got_diffstat_cb_arg *ds, FILE *outfile)
448 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
449 sb2, label2, diff_algo, diff_context, ignore_whitespace,
450 force_text_diff, show_diffstat, ds, outfile);
453 static const struct got_error *
454 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
455 const char *label, mode_t mode, struct got_repository *repo,
456 got_diff_blob_cb cb, void *cb_arg)
458 const struct got_error *err;
459 struct got_blob_object *blob = NULL;
460 struct got_object *obj = NULL;
462 err = got_object_open(&obj, repo, id);
466 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
469 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
470 NULL, label, 0, mode, repo);
472 got_object_close(obj);
474 got_object_blob_close(blob);
478 static const struct got_error *
479 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
480 FILE *f1, FILE *f2, int fd1, int fd2,
481 const char *label1, const char *label2,
482 mode_t mode1, mode_t mode2, struct got_repository *repo,
483 got_diff_blob_cb cb, void *cb_arg)
485 const struct got_error *err;
486 struct got_object *obj1 = NULL;
487 struct got_object *obj2 = NULL;
488 struct got_blob_object *blob1 = NULL;
489 struct got_blob_object *blob2 = NULL;
491 err = got_object_open(&obj1, repo, id1);
495 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
496 err = got_error(GOT_ERR_OBJ_TYPE);
500 err = got_object_open(&obj2, repo, id2);
503 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
508 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
512 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
516 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
520 got_object_close(obj1);
522 got_object_close(obj2);
524 got_object_blob_close(blob1);
526 got_object_blob_close(blob2);
530 static const struct got_error *
531 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
532 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
533 got_diff_blob_cb cb, void *cb_arg)
535 const struct got_error *err;
536 struct got_blob_object *blob = NULL;
537 struct got_object *obj = NULL;
539 err = got_object_open(&obj, repo, id);
543 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
546 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
549 got_object_close(obj);
551 got_object_blob_close(blob);
555 static const struct got_error *
556 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
557 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
558 void *cb_arg, int diff_content)
560 const struct got_error *err = NULL;
561 struct got_object *treeobj = NULL;
562 struct got_tree_object *tree = NULL;
564 err = got_object_open(&treeobj, repo, id);
568 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
573 err = got_object_tree_open(&tree, repo, treeobj);
577 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
578 repo, cb, cb_arg, diff_content);
581 got_object_tree_close(tree);
583 got_object_close(treeobj);
587 static const struct got_error *
588 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
589 FILE *f1, FILE *f2, int fd1, int fd2,
590 const char *label1, const char *label2,
591 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
594 const struct got_error *err;
595 struct got_object *treeobj1 = NULL;
596 struct got_object *treeobj2 = NULL;
597 struct got_tree_object *tree1 = NULL;
598 struct got_tree_object *tree2 = NULL;
600 err = got_object_open(&treeobj1, repo, id1);
604 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
605 err = got_error(GOT_ERR_OBJ_TYPE);
609 err = got_object_open(&treeobj2, repo, id2);
613 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
614 err = got_error(GOT_ERR_OBJ_TYPE);
618 err = got_object_tree_open(&tree1, repo, treeobj1);
622 err = got_object_tree_open(&tree2, repo, treeobj2);
626 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
627 label1, label2, repo, cb, cb_arg, diff_content);
631 got_object_tree_close(tree1);
633 got_object_tree_close(tree2);
635 got_object_close(treeobj1);
637 got_object_close(treeobj2);
641 static const struct got_error *
642 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
643 FILE *f2, const char *label, struct got_repository *repo,
644 got_diff_blob_cb cb, void *cb_arg, int diff_content)
646 const struct got_error *err;
647 struct got_object *treeobj = NULL;
648 struct got_tree_object *tree = NULL;
650 err = got_object_open(&treeobj, repo, id);
654 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
655 err = got_error(GOT_ERR_OBJ_TYPE);
659 err = got_object_tree_open(&tree, repo, treeobj);
663 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
664 repo, cb, cb_arg, diff_content);
667 got_object_tree_close(tree);
669 got_object_close(treeobj);
673 static const struct got_error *
674 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
675 const char *label1, const char *label2, struct got_repository *repo,
676 got_diff_blob_cb cb, void *cb_arg)
682 static const struct got_error *
683 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
684 FILE *f1, FILE *f2, int fd1, int fd2,
685 const char *label1, const char *label2,
686 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
689 const struct got_error *err = NULL;
692 if (got_object_tree_entry_is_submodule(te1))
696 if (S_ISDIR(te1->mode))
697 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
698 label1, repo, cb, cb_arg, diff_content);
701 err = diff_deleted_blob(&te1->id, f1, fd1,
702 f2, label1, te1->mode, repo, cb, cb_arg);
704 err = cb(cb_arg, NULL, NULL, NULL, NULL,
705 &te1->id, NULL, label1, NULL,
709 } else if (got_object_tree_entry_is_submodule(te2))
712 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
713 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
715 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
716 fd1, fd2, label1, label2, repo, cb, cb_arg,
718 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
719 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
721 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
722 (te2->mode & (S_IFLNK | S_IXUSR))) {
724 return diff_modified_blob(&te1->id, &te2->id,
725 f1, f2, fd1, fd2, label1, label2,
726 te1->mode, te2->mode, repo, cb, cb_arg);
728 return cb(cb_arg, NULL, NULL, NULL, NULL,
729 &te1->id, &te2->id, label1, label2,
730 te1->mode, te2->mode, repo);
737 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
741 static const struct got_error *
742 diff_entry_new_old(struct got_tree_entry *te2,
743 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
744 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
747 if (te1 != NULL) /* handled by diff_entry_old_new() */
750 if (got_object_tree_entry_is_submodule(te2))
753 if (S_ISDIR(te2->mode))
754 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
755 repo, cb, cb_arg, diff_content);
758 return diff_added_blob(&te2->id, f1, f2, fd2,
759 label2, te2->mode, repo, cb, cb_arg);
761 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
762 NULL, label2, 0, te2->mode, repo);
765 const struct got_error *
766 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
767 struct got_blob_object *blob2, FILE *f1, FILE *f2,
768 struct got_object_id *id1, struct got_object_id *id2,
769 const char *label1, const char *label2,
770 mode_t mode1, mode_t mode2, struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_diffreg_result *result = NULL;
774 struct got_diffstat_cb_arg *a = arg;
776 int status = GOT_STATUS_NO_CHANGE;
778 path = strdup(label2 ? label2 : label1);
780 return got_error_from_errno("strdup");
783 status = GOT_STATUS_ADD;
784 else if (id2 == NULL)
785 status = GOT_STATUS_DELETE;
787 if (got_object_id_cmp(id1, id2) != 0)
788 status = GOT_STATUS_MODIFY;
789 else if (mode1 != mode2)
790 status = GOT_STATUS_MODE_CHANGE;
794 err = got_opentemp_truncate(f1);
799 err = got_opentemp_truncate(f2);
805 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
811 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
817 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
822 err = get_diffstat(a, path, result->result, a->force_text, status);
826 const struct got_error *free_err;
828 free_err = got_diffreg_result_free(result);
829 if (free_err && err == NULL)
837 const struct got_error *
838 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
839 struct got_blob_object *blob2, FILE *f1, FILE *f2,
840 struct got_object_id *id1, struct got_object_id *id2,
841 const char *label1, const char *label2,
842 mode_t mode1, mode_t mode2, struct got_repository *repo)
844 const struct got_error *err = NULL;
845 struct got_pathlist_head *paths = arg;
846 struct got_diff_changed_path *change = NULL;
849 path = strdup(label2 ? label2 : label1);
851 return got_error_from_errno("strdup");
853 change = malloc(sizeof(*change));
854 if (change == NULL) {
855 err = got_error_from_errno("malloc");
859 change->status = GOT_STATUS_NO_CHANGE;
861 change->status = GOT_STATUS_ADD;
862 else if (id2 == NULL)
863 change->status = GOT_STATUS_DELETE;
865 if (got_object_id_cmp(id1, id2) != 0)
866 change->status = GOT_STATUS_MODIFY;
867 else if (mode1 != mode2)
868 change->status = GOT_STATUS_MODE_CHANGE;
871 err = got_pathlist_append(paths, path, change);
880 const struct got_error *
881 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
882 FILE *f1, FILE *f2, int fd1, int fd2,
883 const char *label1, const char *label2,
884 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
887 const struct got_error *err = NULL;
888 struct got_tree_entry *te1 = NULL;
889 struct got_tree_entry *te2 = NULL;
890 char *l1 = NULL, *l2 = NULL;
891 int tidx1 = 0, tidx2 = 0;
894 te1 = got_object_tree_get_entry(tree1, 0);
895 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
897 return got_error_from_errno("asprintf");
900 te2 = got_object_tree_get_entry(tree2, 0);
901 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
903 err = got_error_from_errno("asprintf");
910 struct got_tree_entry *te = NULL;
913 te = got_object_tree_find_entry(tree2,
918 if (te && asprintf(&l2, "%s%s%s", label2,
919 label2[0] ? "/" : "", te->name) == -1) {
920 err = got_error_from_errno("asprintf");
925 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
926 l1, l2, repo, cb, cb_arg, diff_content);
932 struct got_tree_entry *te = NULL;
935 te = got_object_tree_find_entry(tree1,
941 if (asprintf(&l2, "%s%s%s", label2,
942 label2[0] ? "/" : "", te->name) == -1) {
943 err = got_error_from_errno("asprintf");
947 if (asprintf(&l2, "%s%s%s", label2,
948 label2[0] ? "/" : "", te2->name) == -1) {
949 err = got_error_from_errno("asprintf");
954 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
955 repo, cb, cb_arg, diff_content);
964 te1 = got_object_tree_get_entry(tree1, tidx1);
966 asprintf(&l1, "%s%s%s", label1,
967 label1[0] ? "/" : "", te1->name) == -1) {
968 err = got_error_from_errno("asprintf");
977 te2 = got_object_tree_get_entry(tree2, tidx2);
979 asprintf(&l2, "%s%s%s", label2,
980 label2[0] ? "/" : "", te2->name) == -1) {
981 err = got_error_from_errno("asprintf");
985 } while (te1 || te2);
993 const struct got_error *
994 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
995 FILE *f1, FILE *f2, int fd1, int fd2,
996 struct got_object_id *id1, struct got_object_id *id2,
997 const char *label1, const char *label2,
998 enum got_diff_algorithm diff_algo, int diff_context,
999 int ignore_whitespace, int force_text_diff, int show_diffstat,
1000 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
1002 const struct got_error *err;
1003 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1005 if (id1 == NULL && id2 == NULL)
1006 return got_error(GOT_ERR_NO_OBJ);
1009 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1014 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1018 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1019 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1020 show_diffstat, ds, outfile);
1023 got_object_blob_close(blob1);
1025 got_object_blob_close(blob2);
1029 static const struct got_error *
1030 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1031 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1032 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1034 const struct got_error *err = NULL;
1035 struct got_pathlist_entry *pe;
1036 struct got_object_id *id1 = NULL, *id2 = NULL;
1037 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1038 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1040 TAILQ_FOREACH(pe, paths, entry) {
1041 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1042 mode_t mode1 = 0, mode2 = 0;
1049 got_object_tree_close(subtree1);
1053 got_object_tree_close(subtree2);
1057 got_object_blob_close(blob1);
1061 got_object_blob_close(blob2);
1065 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1067 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1069 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1071 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1073 if (id1 == NULL && id2 == NULL) {
1074 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1078 err = got_object_get_type(&type1, repo, id1);
1083 err = got_object_get_type(&type2, repo, id2);
1087 if (type1 == GOT_OBJ_TYPE_ANY &&
1088 type2 == GOT_OBJ_TYPE_ANY) {
1089 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1091 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1092 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1093 err = got_error(GOT_ERR_OBJ_TYPE);
1097 if (type1 == GOT_OBJ_TYPE_BLOB ||
1098 type2 == GOT_OBJ_TYPE_BLOB) {
1100 err = got_object_open_as_blob(&blob1, repo,
1106 err = got_object_open_as_blob(&blob2, repo,
1111 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1112 id1 ? pe->path : "/dev/null",
1113 id2 ? pe->path : "/dev/null",
1114 mode1, mode2, repo);
1117 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1118 type2 == GOT_OBJ_TYPE_TREE) {
1120 err = got_object_open_as_tree(&subtree1, repo,
1126 err = got_object_open_as_tree(&subtree2, repo,
1131 err = got_diff_tree(subtree1, subtree2, f1, f2,
1133 id1 ? pe->path : "/dev/null",
1134 id2 ? pe->path : "/dev/null",
1135 repo, cb, cb_arg, 1);
1139 err = got_error(GOT_ERR_OBJ_TYPE);
1147 got_object_tree_close(subtree1);
1149 got_object_tree_close(subtree2);
1151 got_object_blob_close(blob1);
1153 got_object_blob_close(blob2);
1157 static const struct got_error *
1158 show_object_id(struct got_diff_line **lines, size_t *nlines,
1159 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1161 const struct got_error *err;
1165 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1167 return got_error_from_errno("fprintf");
1169 if (lines != NULL && *lines != NULL) {
1171 err = add_line_metadata(lines, nlines, 0,
1172 GOT_DIFF_LINE_META);
1176 outoff = (*lines)[*nlines - 1].offset;
1179 err = add_line_metadata(lines, nlines, outoff,
1180 GOT_DIFF_LINE_META);
1188 static const struct got_error *
1189 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1190 FILE *f1, FILE *f2, int fd1, int fd2,
1191 struct got_object_id *id1, struct got_object_id *id2,
1192 struct got_pathlist_head *paths, const char *label1, const char *label2,
1193 int diff_context, int ignore_whitespace, int force_text_diff,
1194 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1195 struct got_repository *repo, FILE *outfile,
1196 enum got_diff_algorithm diff_algo)
1198 const struct got_error *err;
1199 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1200 struct got_diff_blob_output_unidiff_arg arg;
1201 int want_linemeta = (lines != NULL && *lines != NULL);
1203 if (id1 == NULL && id2 == NULL)
1204 return got_error(GOT_ERR_NO_OBJ);
1207 err = got_object_open_as_tree(&tree1, repo, id1);
1212 err = got_object_open_as_tree(&tree2, repo, id2);
1217 arg.diff_algo = diff_algo;
1218 arg.diff_context = diff_context;
1219 arg.ignore_whitespace = ignore_whitespace;
1220 arg.force_text_diff = force_text_diff;
1221 arg.show_diffstat = show_diffstat;
1223 arg.outfile = outfile;
1224 if (want_linemeta) {
1226 arg.nlines = *nlines;
1231 if (paths == NULL || TAILQ_EMPTY(paths))
1232 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1233 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1235 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1236 got_diff_blob_output_unidiff, &arg);
1237 if (want_linemeta) {
1238 *lines = arg.lines; /* was likely re-allocated */
1239 *nlines = arg.nlines;
1243 got_object_tree_close(tree1);
1245 got_object_tree_close(tree2);
1249 const struct got_error *
1250 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1251 FILE *f1, FILE *f2, int fd1, int fd2,
1252 struct got_object_id *id1, struct got_object_id *id2,
1253 struct got_pathlist_head *paths, const char *label1, const char *label2,
1254 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1255 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1256 struct got_repository *repo, FILE *outfile)
1258 const struct got_error *err;
1261 if (id1 == NULL && id2 == NULL)
1262 return got_error(GOT_ERR_NO_OBJ);
1265 err = got_object_id_str(&idstr, id1);
1268 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1274 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1281 err = got_object_id_str(&idstr, id2);
1284 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1290 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1296 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1297 paths, label1, label2, diff_context, ignore_whitespace,
1298 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1304 const struct got_error *
1305 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1306 FILE *f1, FILE *f2, int fd1, int fd2,
1307 struct got_object_id *id1, struct got_object_id *id2,
1308 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1309 int diff_context, int ignore_whitespace, int force_text_diff,
1310 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1311 struct got_repository *repo, FILE *outfile)
1313 const struct got_error *err;
1314 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1318 return got_error(GOT_ERR_NO_OBJ);
1321 err = got_object_open_as_commit(&commit1, repo, id1);
1324 err = got_object_id_str(&idstr, id1);
1327 err = show_object_id(lines, nlines, "commit", '-', idstr,
1334 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1340 err = got_object_open_as_commit(&commit2, repo, id2);
1344 err = got_object_id_str(&idstr, id2);
1347 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1351 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1352 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1353 got_object_commit_get_tree_id(commit2), paths, "", "",
1354 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1355 dsa, repo, outfile, diff_algo);
1358 got_object_commit_close(commit1);
1360 got_object_commit_close(commit2);
1365 const struct got_error *
1366 got_diff_files(struct got_diffreg_result **resultp,
1367 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1368 const char *label2, int diff_context, int ignore_whitespace,
1369 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1371 const struct got_error *err = NULL;
1372 struct got_diffreg_result *diffreg_result = NULL;
1378 fprintf(outfile, "file - %s\n",
1379 f1_exists ? label1 : "/dev/null");
1380 fprintf(outfile, "file + %s\n",
1381 f2_exists ? label2 : "/dev/null");
1384 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1385 ignore_whitespace, force_text_diff);
1390 err = got_diffreg_output(NULL, NULL, diffreg_result,
1391 f1_exists, f2_exists, label1, label2,
1392 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1398 if (resultp && err == NULL)
1399 *resultp = diffreg_result;
1400 else if (diffreg_result) {
1401 const struct got_error *free_err;
1403 free_err = got_diffreg_result_free(diffreg_result);
1404 if (free_err && err == NULL)