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);
122 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
123 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
124 pe->path_len, change->add, change->rm);
129 static const struct got_error *
130 diff_blobs(struct got_diff_line **lines, size_t *nlines,
131 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
132 struct got_blob_object *blob2, FILE *f1, FILE *f2,
133 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
134 int diff_context, int ignore_whitespace, int force_text_diff,
135 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
136 enum got_diff_algorithm diff_algo)
138 const struct got_error *err = NULL, *free_err;
139 char hex1[SHA1_DIGEST_STRING_LENGTH];
140 char hex2[SHA1_DIGEST_STRING_LENGTH];
141 const char *idstr1 = NULL, *idstr2 = NULL;
142 char *modestr1 = NULL, *modestr2 = NULL;
144 struct got_diffreg_result *result = NULL;
148 if (lines && *lines && *nlines > 0)
149 outoff = (*lines)[*nlines - 1].offset;
151 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
160 err = got_opentemp_truncate(f1);
165 err = got_opentemp_truncate(f2);
172 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
173 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
178 idstr1 = "/dev/null";
182 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
183 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
188 idstr2 = "/dev/null";
193 if (mode1 && mode1 != mode2) {
197 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
198 if (asprintf(&modestr1, " (mode %o)",
199 mode1 & modebits) == -1) {
200 err = got_error_from_errno("asprintf");
204 if (mode2 && mode1 != mode2) {
208 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
209 if (asprintf(&modestr2, " (mode %o)",
210 mode2 & modebits) == -1) {
211 err = got_error_from_errno("asprintf");
215 n = fprintf(outfile, "blob - %s%s\n", idstr1,
216 modestr1 ? modestr1 : "");
221 err = add_line_metadata(lines, nlines, outoff,
222 GOT_DIFF_LINE_BLOB_MIN);
227 n = fprintf(outfile, "blob + %s%s\n", idstr2,
228 modestr2 ? modestr2 : "");
233 err = add_line_metadata(lines, nlines, outoff,
234 GOT_DIFF_LINE_BLOB_PLUS);
240 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
247 int status = GOT_STATUS_NO_CHANGE;
250 status = GOT_STATUS_ADD;
251 else if (blob2 == NULL)
252 status = GOT_STATUS_DELETE;
254 if (strcmp(idstr1, idstr2) != 0)
255 status = GOT_STATUS_MODIFY;
256 else if (mode1 != mode2)
257 status = GOT_STATUS_MODE_CHANGE;
260 if (label1 == NULL && label2 == NULL) {
261 /* diffstat of blobs, show hash instead of path */
262 if (asprintf(&path, "%.10s -> %.10s",
263 idstr1, idstr2) == -1) {
264 err = got_error_from_errno("asprintf");
268 if (label2 != NULL &&
269 (status != GOT_STATUS_DELETE || label1 == NULL))
270 path = strdup(label2);
272 path = strdup(label1);
274 err = got_error_from_errno("strdup");
279 err = get_diffstat(diffstat, path, result->result,
280 force_text_diff, status);
288 err = got_diffreg_output(lines, nlines, result,
289 blob1 != NULL, blob2 != NULL,
290 label1 ? label1 : idstr1,
291 label2 ? label2 : idstr2,
292 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
300 if (resultp && err == NULL)
303 free_err = got_diffreg_result_free(result);
304 if (free_err && err == NULL)
311 const struct got_error *
312 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
313 struct got_blob_object *blob2, FILE *f1, FILE *f2,
314 struct got_object_id *id1, struct got_object_id *id2,
315 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
316 struct got_repository *repo)
318 struct got_diff_blob_output_unidiff_arg *a = arg;
320 return diff_blobs(&a->lines, &a->nlines, NULL,
321 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
322 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
326 const struct got_error *
327 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
328 struct got_blob_object *blob1, struct got_blob_object *blob2,
329 FILE *f1, FILE *f2, const char *label1, const char *label2,
330 enum got_diff_algorithm diff_algo, int diff_context,
331 int ignore_whitespace, int force_text_diff,
332 struct got_diffstat_cb_arg *ds, FILE *outfile)
334 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
335 label1, label2, 0, 0, diff_context, ignore_whitespace,
336 force_text_diff, ds, outfile, diff_algo);
339 static const struct got_error *
340 diff_blob_file(struct got_diffreg_result **resultp,
341 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
342 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
343 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
344 int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
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(diffstat, path, result->result,
420 force_text_diff, status);
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,
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, 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 err = got_error_from_errno("asprintf");
907 struct got_tree_entry *te = NULL;
910 te = got_object_tree_find_entry(tree2,
915 if (te && asprintf(&l2, "%s%s%s", label2,
916 label2[0] ? "/" : "", te->name) == -1) {
917 err = got_error_from_errno("asprintf");
922 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
923 l1, l2, repo, cb, cb_arg, diff_content);
929 struct got_tree_entry *te = NULL;
932 te = got_object_tree_find_entry(tree1,
938 if (asprintf(&l2, "%s%s%s", label2,
939 label2[0] ? "/" : "", te->name) == -1) {
940 err = got_error_from_errno("asprintf");
944 if (asprintf(&l2, "%s%s%s", label2,
945 label2[0] ? "/" : "", te2->name) == -1) {
946 err = got_error_from_errno("asprintf");
951 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
952 repo, cb, cb_arg, diff_content);
961 te1 = got_object_tree_get_entry(tree1, tidx1);
963 asprintf(&l1, "%s%s%s", label1,
964 label1[0] ? "/" : "", te1->name) == -1) {
965 err = got_error_from_errno("asprintf");
974 te2 = got_object_tree_get_entry(tree2, tidx2);
976 asprintf(&l2, "%s%s%s", label2,
977 label2[0] ? "/" : "", te2->name) == -1) {
978 err = got_error_from_errno("asprintf");
982 } while (te1 || te2);
990 const struct got_error *
991 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
992 FILE *f1, FILE *f2, int fd1, int fd2,
993 struct got_object_id *id1, struct got_object_id *id2,
994 const char *label1, const char *label2,
995 enum got_diff_algorithm diff_algo, int diff_context,
996 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
997 struct got_repository *repo, FILE *outfile)
999 const struct got_error *err;
1000 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1002 if (id1 == NULL && id2 == NULL)
1003 return got_error(GOT_ERR_NO_OBJ);
1006 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1011 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1015 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1016 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1020 got_object_blob_close(blob1);
1022 got_object_blob_close(blob2);
1026 static const struct got_error *
1027 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1028 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1029 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1031 const struct got_error *err = NULL;
1032 struct got_pathlist_entry *pe;
1033 struct got_object_id *id1 = NULL, *id2 = NULL;
1034 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1035 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1037 TAILQ_FOREACH(pe, paths, entry) {
1038 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1039 mode_t mode1 = 0, mode2 = 0;
1046 got_object_tree_close(subtree1);
1050 got_object_tree_close(subtree2);
1054 got_object_blob_close(blob1);
1058 got_object_blob_close(blob2);
1062 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1064 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1066 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1068 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1070 if (id1 == NULL && id2 == NULL) {
1071 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1075 err = got_object_get_type(&type1, repo, id1);
1080 err = got_object_get_type(&type2, repo, id2);
1084 if (type1 == GOT_OBJ_TYPE_ANY &&
1085 type2 == GOT_OBJ_TYPE_ANY) {
1086 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1088 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1089 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1090 err = got_error(GOT_ERR_OBJ_TYPE);
1094 if (type1 == GOT_OBJ_TYPE_BLOB ||
1095 type2 == GOT_OBJ_TYPE_BLOB) {
1097 err = got_object_open_as_blob(&blob1, repo,
1103 err = got_object_open_as_blob(&blob2, repo,
1108 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1109 id1 ? pe->path : "/dev/null",
1110 id2 ? pe->path : "/dev/null",
1111 mode1, mode2, repo);
1114 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1115 type2 == GOT_OBJ_TYPE_TREE) {
1117 err = got_object_open_as_tree(&subtree1, repo,
1123 err = got_object_open_as_tree(&subtree2, repo,
1128 err = got_diff_tree(subtree1, subtree2, f1, f2,
1130 id1 ? pe->path : "/dev/null",
1131 id2 ? pe->path : "/dev/null",
1132 repo, cb, cb_arg, 1);
1136 err = got_error(GOT_ERR_OBJ_TYPE);
1144 got_object_tree_close(subtree1);
1146 got_object_tree_close(subtree2);
1148 got_object_blob_close(blob1);
1150 got_object_blob_close(blob2);
1154 static const struct got_error *
1155 show_object_id(struct got_diff_line **lines, size_t *nlines,
1156 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1158 const struct got_error *err;
1162 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1164 return got_error_from_errno("fprintf");
1166 if (lines != NULL && *lines != NULL) {
1168 err = add_line_metadata(lines, nlines, 0,
1169 GOT_DIFF_LINE_META);
1173 outoff = (*lines)[*nlines - 1].offset;
1176 err = add_line_metadata(lines, nlines, outoff,
1177 GOT_DIFF_LINE_META);
1185 static const struct got_error *
1186 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1187 FILE *f1, FILE *f2, int fd1, int fd2,
1188 struct got_object_id *id1, struct got_object_id *id2,
1189 struct got_pathlist_head *paths, const char *label1, const char *label2,
1190 int diff_context, int ignore_whitespace, int force_text_diff,
1191 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1192 FILE *outfile, enum got_diff_algorithm diff_algo)
1194 const struct got_error *err;
1195 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1196 struct got_diff_blob_output_unidiff_arg arg;
1197 int want_linemeta = (lines != NULL && *lines != NULL);
1199 if (id1 == NULL && id2 == NULL)
1200 return got_error(GOT_ERR_NO_OBJ);
1203 err = got_object_open_as_tree(&tree1, repo, id1);
1208 err = got_object_open_as_tree(&tree2, repo, id2);
1213 arg.diff_algo = diff_algo;
1214 arg.diff_context = diff_context;
1215 arg.ignore_whitespace = ignore_whitespace;
1216 arg.force_text_diff = force_text_diff;
1218 arg.outfile = outfile;
1219 if (want_linemeta) {
1221 arg.nlines = *nlines;
1226 if (paths == NULL || TAILQ_EMPTY(paths))
1227 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1228 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1230 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1231 got_diff_blob_output_unidiff, &arg);
1232 if (want_linemeta) {
1233 *lines = arg.lines; /* was likely re-allocated */
1234 *nlines = arg.nlines;
1238 got_object_tree_close(tree1);
1240 got_object_tree_close(tree2);
1244 const struct got_error *
1245 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1246 FILE *f1, FILE *f2, int fd1, int fd2,
1247 struct got_object_id *id1, struct got_object_id *id2,
1248 struct got_pathlist_head *paths, const char *label1, const char *label2,
1249 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1250 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1251 struct got_repository *repo, FILE *outfile)
1253 const struct got_error *err;
1256 if (id1 == NULL && id2 == NULL)
1257 return got_error(GOT_ERR_NO_OBJ);
1260 err = got_object_id_str(&idstr, id1);
1263 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1269 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1276 err = got_object_id_str(&idstr, id2);
1279 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1285 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1291 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1292 paths, label1, label2, diff_context, ignore_whitespace,
1293 force_text_diff, dsa, repo, outfile, diff_algo);
1299 const struct got_error *
1300 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1301 FILE *f1, FILE *f2, int fd1, int fd2,
1302 struct got_object_id *id1, struct got_object_id *id2,
1303 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1304 int diff_context, int ignore_whitespace, int force_text_diff,
1305 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1307 const struct got_error *err;
1308 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1312 return got_error(GOT_ERR_NO_OBJ);
1315 err = got_object_open_as_commit(&commit1, repo, id1);
1318 err = got_object_id_str(&idstr, id1);
1321 err = show_object_id(lines, nlines, "commit", '-', idstr,
1328 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1334 err = got_object_open_as_commit(&commit2, repo, id2);
1338 err = got_object_id_str(&idstr, id2);
1341 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1345 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1346 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1347 got_object_commit_get_tree_id(commit2), paths, "", "",
1348 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1349 outfile, diff_algo);
1352 got_object_commit_close(commit1);
1354 got_object_commit_close(commit2);
1359 const struct got_error *
1360 got_diff_files(struct got_diffreg_result **resultp,
1361 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1362 const char *label2, int diff_context, int ignore_whitespace,
1363 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1365 const struct got_error *err = NULL;
1366 struct got_diffreg_result *diffreg_result = NULL;
1372 fprintf(outfile, "file - %s\n",
1373 f1_exists ? label1 : "/dev/null");
1374 fprintf(outfile, "file + %s\n",
1375 f2_exists ? label2 : "/dev/null");
1378 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1379 ignore_whitespace, force_text_diff);
1384 err = got_diffreg_output(NULL, NULL, diffreg_result,
1385 f1_exists, f2_exists, label1, label2,
1386 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1392 if (resultp && err == NULL)
1393 *resultp = diffreg_result;
1394 else if (diffreg_result) {
1395 const struct got_error *free_err;
1397 free_err = got_diffreg_result_free(diffreg_result);
1398 if (free_err && err == NULL)