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_compat.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_worktree.h"
37 #include "got_opentemp.h"
39 #include "got_lib_diff.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
45 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
48 static const struct got_error *
49 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
50 off_t off, uint8_t type)
52 struct got_diff_line *p;
54 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
56 return got_error_from_errno("reallocarray");
58 (*lines)[*nlines].offset = off;
59 (*lines)[*nlines].type = type;
66 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
67 uint32_t add, uint32_t rm)
72 *maxlen = MAX(*maxlen, len);
76 *add_cols = MAX(*add_cols, d1);
80 *rm_cols = MAX(*rm_cols, d2);
83 static const struct got_error *
84 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
85 struct diff_result *r, int force_text, int status)
87 const struct got_error *err;
88 struct got_pathlist_entry *pe;
89 struct got_diff_changed_path *change = NULL;
90 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
91 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
94 change = calloc(1, sizeof(*change));
96 return got_error_from_errno("calloc");
98 if (!isbin || force_text) {
99 for (i = 0; i < r->chunks.len; ++i) {
100 struct diff_chunk *c;
103 c = diff_chunk_get(r, i);
104 clc = diff_chunk_get_left_count(c);
105 crc = diff_chunk_get_right_count(c);
114 change->status = status;
115 ds->ins += change->add;
116 ds->del += change->rm;
119 err = got_pathlist_append(ds->paths, path, change);
125 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
126 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
127 pe->path_len, change->add, change->rm);
132 static const struct got_error *
133 diff_blobs(struct got_diff_line **lines, size_t *nlines,
134 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
135 struct got_blob_object *blob2, FILE *f1, FILE *f2,
136 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
137 int diff_context, int ignore_whitespace, int force_text_diff,
138 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
139 enum got_diff_algorithm diff_algo)
141 const struct got_error *err = NULL, *free_err;
142 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
143 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
144 const char *idstr1 = NULL, *idstr2 = NULL;
145 char *modestr1 = NULL, *modestr2 = NULL;
147 struct got_diffreg_result *result = NULL;
151 if (lines && *lines && *nlines > 0)
152 outoff = (*lines)[*nlines - 1].offset;
154 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
163 err = got_opentemp_truncate(f1);
168 err = got_opentemp_truncate(f2);
175 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
176 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
181 idstr1 = "/dev/null";
185 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
186 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
191 idstr2 = "/dev/null";
196 if (mode1 && mode1 != mode2) {
200 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
201 if (asprintf(&modestr1, " (mode %o)",
202 mode1 & modebits) == -1) {
203 err = got_error_from_errno("asprintf");
207 if (mode2 && mode1 != mode2) {
211 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
212 if (asprintf(&modestr2, " (mode %o)",
213 mode2 & modebits) == -1) {
214 err = got_error_from_errno("asprintf");
218 n = fprintf(outfile, "blob - %s%s\n", idstr1,
219 modestr1 ? modestr1 : "");
224 err = add_line_metadata(lines, nlines, outoff,
225 GOT_DIFF_LINE_BLOB_MIN);
230 n = fprintf(outfile, "blob + %s%s\n", idstr2,
231 modestr2 ? modestr2 : "");
236 err = add_line_metadata(lines, nlines, outoff,
237 GOT_DIFF_LINE_BLOB_PLUS);
243 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
250 int status = GOT_STATUS_NO_CHANGE;
253 status = GOT_STATUS_ADD;
254 else if (blob2 == NULL)
255 status = GOT_STATUS_DELETE;
257 if (strcmp(idstr1, idstr2) != 0)
258 status = GOT_STATUS_MODIFY;
259 else if (mode1 != mode2)
260 status = GOT_STATUS_MODE_CHANGE;
263 if (label1 == NULL && label2 == NULL) {
264 /* diffstat of blobs, show hash instead of path */
265 if (asprintf(&path, "%.10s -> %.10s",
266 idstr1, idstr2) == -1) {
267 err = got_error_from_errno("asprintf");
271 if (label2 != NULL &&
272 (status != GOT_STATUS_DELETE || label1 == NULL))
273 path = strdup(label2);
275 path = strdup(label1);
277 err = got_error_from_errno("strdup");
282 err = get_diffstat(diffstat, path, result->result,
283 force_text_diff, status);
291 err = got_diffreg_output(lines, nlines, result,
292 blob1 != NULL, blob2 != NULL,
293 label1 ? label1 : idstr1,
294 label2 ? label2 : idstr2,
295 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
303 if (resultp && err == NULL)
306 free_err = got_diffreg_result_free(result);
307 if (free_err && err == NULL)
314 const struct got_error *
315 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
316 struct got_blob_object *blob2, FILE *f1, FILE *f2,
317 struct got_object_id *id1, struct got_object_id *id2,
318 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
319 struct got_repository *repo)
321 struct got_diff_blob_output_unidiff_arg *a = arg;
323 return diff_blobs(&a->lines, &a->nlines, NULL,
324 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
325 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
329 const struct got_error *
330 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
331 struct got_blob_object *blob1, struct got_blob_object *blob2,
332 FILE *f1, FILE *f2, const char *label1, const char *label2,
333 enum got_diff_algorithm diff_algo, int diff_context,
334 int ignore_whitespace, int force_text_diff,
335 struct got_diffstat_cb_arg *ds, FILE *outfile)
337 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
338 label1, label2, 0, 0, diff_context, ignore_whitespace,
339 force_text_diff, ds, outfile, diff_algo);
342 static const struct got_error *
343 diff_blob_file(struct got_diffreg_result **resultp,
344 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
345 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
346 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
347 int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
349 const struct got_error *err = NULL, *free_err;
350 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
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(diffstat, path, result->result,
423 force_text_diff, status);
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,
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, 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, struct got_diffstat_cb_arg *ds,
1000 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,
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 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1195 FILE *outfile, enum got_diff_algorithm diff_algo)
1197 const struct got_error *err;
1198 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1199 struct got_diff_blob_output_unidiff_arg arg;
1200 int want_linemeta = (lines != NULL && *lines != NULL);
1202 if (id1 == NULL && id2 == NULL)
1203 return got_error(GOT_ERR_NO_OBJ);
1206 err = got_object_open_as_tree(&tree1, repo, id1);
1211 err = got_object_open_as_tree(&tree2, repo, id2);
1216 arg.diff_algo = diff_algo;
1217 arg.diff_context = diff_context;
1218 arg.ignore_whitespace = ignore_whitespace;
1219 arg.force_text_diff = force_text_diff;
1221 arg.outfile = outfile;
1222 if (want_linemeta) {
1224 arg.nlines = *nlines;
1229 if (paths == NULL || TAILQ_EMPTY(paths))
1230 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1231 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1233 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1234 got_diff_blob_output_unidiff, &arg);
1235 if (want_linemeta) {
1236 *lines = arg.lines; /* was likely re-allocated */
1237 *nlines = arg.nlines;
1241 got_object_tree_close(tree1);
1243 got_object_tree_close(tree2);
1247 const struct got_error *
1248 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1249 FILE *f1, FILE *f2, int fd1, int fd2,
1250 struct got_object_id *id1, struct got_object_id *id2,
1251 struct got_pathlist_head *paths, const char *label1, const char *label2,
1252 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1253 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1254 struct got_repository *repo, FILE *outfile)
1256 const struct got_error *err;
1259 if (id1 == NULL && id2 == NULL)
1260 return got_error(GOT_ERR_NO_OBJ);
1263 err = got_object_id_str(&idstr, id1);
1266 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1272 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1279 err = got_object_id_str(&idstr, id2);
1282 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1288 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1294 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1295 paths, label1, label2, diff_context, ignore_whitespace,
1296 force_text_diff, dsa, repo, outfile, diff_algo);
1302 const struct got_error *
1303 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1304 FILE *f1, FILE *f2, int fd1, int fd2,
1305 struct got_object_id *id1, struct got_object_id *id2,
1306 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1307 int diff_context, int ignore_whitespace, int force_text_diff,
1308 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1310 const struct got_error *err;
1311 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1315 return got_error(GOT_ERR_NO_OBJ);
1318 err = got_object_open_as_commit(&commit1, repo, id1);
1321 err = got_object_id_str(&idstr, id1);
1324 err = show_object_id(lines, nlines, "commit", '-', idstr,
1331 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1337 err = got_object_open_as_commit(&commit2, repo, id2);
1341 err = got_object_id_str(&idstr, id2);
1344 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1348 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1349 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1350 got_object_commit_get_tree_id(commit2), paths, "", "",
1351 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1352 outfile, diff_algo);
1355 got_object_commit_close(commit1);
1357 got_object_commit_close(commit2);
1362 const struct got_error *
1363 got_diff_files(struct got_diffreg_result **resultp,
1364 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1365 const char *label2, int diff_context, int ignore_whitespace,
1366 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1368 const struct got_error *err = NULL;
1369 struct got_diffreg_result *diffreg_result = NULL;
1375 fprintf(outfile, "file - %s\n",
1376 f1_exists ? label1 : "/dev/null");
1377 fprintf(outfile, "file + %s\n",
1378 f2_exists ? label2 : "/dev/null");
1381 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1382 ignore_whitespace, force_text_diff);
1387 err = got_diffreg_output(NULL, NULL, diffreg_result,
1388 f1_exists, f2_exists, label1, label2,
1389 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1395 if (resultp && err == NULL)
1396 *resultp = diffreg_result;
1397 else if (diffreg_result) {
1398 const struct got_error *free_err;
1400 free_err = got_diffreg_result_free(diffreg_result);
1401 if (free_err && err == NULL)