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("malloc");
95 if (!isbin || force_text) {
96 for (i = 0; i < r->chunks.len; ++i) {
100 c = diff_chunk_get(r, i);
101 clc = diff_chunk_get_left_count(c);
102 crc = diff_chunk_get_right_count(c);
111 change->status = status;
112 ds->ins += change->add;
113 ds->del += change->rm;
116 err = got_pathlist_append(ds->paths, path, change);
120 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
121 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
122 pe->path_len, change->add, change->rm);
127 static const struct got_error *
128 diff_blobs(struct got_diff_line **lines, size_t *nlines,
129 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
130 struct got_blob_object *blob2, FILE *f1, FILE *f2,
131 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
132 int diff_context, int ignore_whitespace, int force_text_diff,
133 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
134 enum got_diff_algorithm diff_algo)
136 const struct got_error *err = NULL, *free_err;
137 char hex1[SHA1_DIGEST_STRING_LENGTH];
138 char hex2[SHA1_DIGEST_STRING_LENGTH];
139 const char *idstr1 = NULL, *idstr2 = NULL;
141 struct got_diffreg_result *result = NULL;
145 if (lines && *lines && *nlines > 0)
146 outoff = (*lines)[*nlines - 1].offset;
148 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
157 err = got_opentemp_truncate(f1);
162 err = got_opentemp_truncate(f2);
169 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
170 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
175 idstr1 = "/dev/null";
179 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
180 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
185 idstr2 = "/dev/null";
188 char *modestr1 = NULL, *modestr2 = NULL;
190 if (mode1 && mode1 != mode2) {
194 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
195 if (asprintf(&modestr1, " (mode %o)",
196 mode1 & modebits) == -1) {
197 err = got_error_from_errno("asprintf");
201 if (mode2 && mode1 != mode2) {
205 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
206 if (asprintf(&modestr2, " (mode %o)",
207 mode2 & modebits) == -1) {
208 err = got_error_from_errno("asprintf");
212 n = fprintf(outfile, "blob - %s%s\n", idstr1,
213 modestr1 ? modestr1 : "");
218 err = add_line_metadata(lines, nlines, outoff,
219 GOT_DIFF_LINE_BLOB_MIN);
224 n = fprintf(outfile, "blob + %s%s\n", idstr2,
225 modestr2 ? modestr2 : "");
230 err = add_line_metadata(lines, nlines, outoff,
231 GOT_DIFF_LINE_BLOB_PLUS);
240 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
247 int status = GOT_STATUS_NO_CHANGE;
249 if (label1 == NULL && label2 == NULL) {
250 /* diffstat of blobs, show hash instead of path */
251 if (asprintf(&path, "%.10s -> %.10s",
252 idstr1, idstr2) == -1) {
253 err = got_error_from_errno("asprintf");
257 path = strdup(label2 ? label2 : label1);
259 err = got_error_from_errno("malloc");
265 * Ignore 'm'ode status change: if there's no accompanying
266 * content change, there'll be no diffstat, and if there
267 * are actual changes, 'M'odified takes precedence.
270 status = GOT_STATUS_ADD;
271 else if (blob2 == NULL)
272 status = GOT_STATUS_DELETE;
274 status = GOT_STATUS_MODIFY;
276 err = get_diffstat(ds, path, result->result, force_text_diff,
285 err = got_diffreg_output(lines, nlines, result,
286 blob1 != NULL, blob2 != NULL,
287 label1 ? label1 : idstr1,
288 label2 ? label2 : idstr2,
289 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
295 if (resultp && err == NULL)
298 free_err = got_diffreg_result_free(result);
299 if (free_err && err == NULL)
306 const struct got_error *
307 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
308 struct got_blob_object *blob2, FILE *f1, FILE *f2,
309 struct got_object_id *id1, struct got_object_id *id2,
310 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
311 struct got_repository *repo)
313 struct got_diff_blob_output_unidiff_arg *a = arg;
315 return diff_blobs(&a->lines, &a->nlines, NULL,
316 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
317 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
318 a->diffstat, a->outfile, a->diff_algo);
321 const struct got_error *
322 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
323 struct got_blob_object *blob1, struct got_blob_object *blob2,
324 FILE *f1, FILE *f2, const char *label1, const char *label2,
325 enum got_diff_algorithm diff_algo, int diff_context,
326 int ignore_whitespace, int force_text_diff, int show_diffstat,
327 struct got_diffstat_cb_arg *ds, FILE *outfile)
329 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
330 label1, label2, 0, 0, diff_context, ignore_whitespace,
331 force_text_diff, show_diffstat, ds, outfile, diff_algo);
334 static const struct got_error *
335 diff_blob_file(struct got_diffreg_result **resultp,
336 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
337 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
338 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
339 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
342 const struct got_error *err = NULL, *free_err;
343 char hex1[SHA1_DIGEST_STRING_LENGTH];
344 const char *idstr1 = NULL;
345 struct got_diffreg_result *result = NULL;
351 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
353 idstr1 = "/dev/null";
358 /* display file mode for new added files only */
359 if (f2_exists && blob1 == NULL) {
360 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
362 if (S_ISLNK(sb2->st_mode))
364 if (asprintf(&mode, " (mode %o)",
365 sb2->st_mode & mmask) == -1)
366 return got_error_from_errno("asprintf");
368 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
369 fprintf(outfile, "file + %s%s\n",
370 f2_exists ? label2 : "/dev/null", mode ? mode : "");
374 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
380 err = got_diffreg_output(NULL, NULL, result,
381 blob1 != NULL, f2_exists,
382 label2, /* show local file's path, not a blob ID */
383 label2, GOT_DIFF_OUTPUT_UNIDIFF,
384 diff_context, outfile);
391 int status = GOT_STATUS_NO_CHANGE;
393 path = strdup(label2 ? label2 : label1);
395 err = got_error_from_errno("malloc");
400 * Ignore 'm'ode status change: if there's no accompanying
401 * content change, there'll be no diffstat, and if there
402 * are actual changes, 'M'odified takes precedence.
405 status = GOT_STATUS_ADD;
407 status = GOT_STATUS_DELETE;
409 status = GOT_STATUS_MODIFY;
411 err = get_diffstat(ds, path, result->result, force_text_diff,
420 if (resultp && err == NULL)
423 free_err = got_diffreg_result_free(result);
424 if (free_err && err == NULL)
430 const struct got_error *
431 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
432 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
433 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
434 int ignore_whitespace, int force_text_diff, int show_diffstat,
435 struct got_diffstat_cb_arg *ds, FILE *outfile)
437 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
438 sb2, label2, diff_algo, diff_context, ignore_whitespace,
439 force_text_diff, show_diffstat, ds, outfile);
442 static const struct got_error *
443 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
444 const char *label, mode_t mode, struct got_repository *repo,
445 got_diff_blob_cb cb, void *cb_arg)
447 const struct got_error *err;
448 struct got_blob_object *blob = NULL;
449 struct got_object *obj = NULL;
451 err = got_object_open(&obj, repo, id);
455 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
458 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
459 NULL, label, 0, mode, repo);
461 got_object_close(obj);
463 got_object_blob_close(blob);
467 static const struct got_error *
468 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
469 FILE *f1, FILE *f2, int fd1, int fd2,
470 const char *label1, const char *label2,
471 mode_t mode1, mode_t mode2, struct got_repository *repo,
472 got_diff_blob_cb cb, void *cb_arg)
474 const struct got_error *err;
475 struct got_object *obj1 = NULL;
476 struct got_object *obj2 = NULL;
477 struct got_blob_object *blob1 = NULL;
478 struct got_blob_object *blob2 = NULL;
480 err = got_object_open(&obj1, repo, id1);
484 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
485 err = got_error(GOT_ERR_OBJ_TYPE);
489 err = got_object_open(&obj2, repo, id2);
492 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
493 err = got_error(GOT_ERR_BAD_OBJ_DATA);
497 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
501 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
505 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
509 got_object_close(obj1);
511 got_object_close(obj2);
513 got_object_blob_close(blob1);
515 got_object_blob_close(blob2);
519 static const struct got_error *
520 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
521 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
522 got_diff_blob_cb cb, void *cb_arg)
524 const struct got_error *err;
525 struct got_blob_object *blob = NULL;
526 struct got_object *obj = NULL;
528 err = got_object_open(&obj, repo, id);
532 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
535 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
538 got_object_close(obj);
540 got_object_blob_close(blob);
544 static const struct got_error *
545 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
546 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
547 void *cb_arg, int diff_content)
549 const struct got_error *err = NULL;
550 struct got_object *treeobj = NULL;
551 struct got_tree_object *tree = NULL;
553 err = got_object_open(&treeobj, repo, id);
557 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
558 err = got_error(GOT_ERR_OBJ_TYPE);
562 err = got_object_tree_open(&tree, repo, treeobj);
566 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
567 repo, cb, cb_arg, diff_content);
570 got_object_tree_close(tree);
572 got_object_close(treeobj);
576 static const struct got_error *
577 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
578 FILE *f1, FILE *f2, int fd1, int fd2,
579 const char *label1, const char *label2,
580 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
583 const struct got_error *err;
584 struct got_object *treeobj1 = NULL;
585 struct got_object *treeobj2 = NULL;
586 struct got_tree_object *tree1 = NULL;
587 struct got_tree_object *tree2 = NULL;
589 err = got_object_open(&treeobj1, repo, id1);
593 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
594 err = got_error(GOT_ERR_OBJ_TYPE);
598 err = got_object_open(&treeobj2, repo, id2);
602 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
603 err = got_error(GOT_ERR_OBJ_TYPE);
607 err = got_object_tree_open(&tree1, repo, treeobj1);
611 err = got_object_tree_open(&tree2, repo, treeobj2);
615 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
616 label1, label2, repo, cb, cb_arg, diff_content);
620 got_object_tree_close(tree1);
622 got_object_tree_close(tree2);
624 got_object_close(treeobj1);
626 got_object_close(treeobj2);
630 static const struct got_error *
631 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
632 FILE *f2, const char *label, struct got_repository *repo,
633 got_diff_blob_cb cb, void *cb_arg, int diff_content)
635 const struct got_error *err;
636 struct got_object *treeobj = NULL;
637 struct got_tree_object *tree = NULL;
639 err = got_object_open(&treeobj, repo, id);
643 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
644 err = got_error(GOT_ERR_OBJ_TYPE);
648 err = got_object_tree_open(&tree, repo, treeobj);
652 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
653 repo, cb, cb_arg, diff_content);
656 got_object_tree_close(tree);
658 got_object_close(treeobj);
662 static const struct got_error *
663 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
664 const char *label1, const char *label2, struct got_repository *repo,
665 got_diff_blob_cb cb, void *cb_arg)
671 static const struct got_error *
672 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
673 FILE *f1, FILE *f2, int fd1, int fd2,
674 const char *label1, const char *label2,
675 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
678 const struct got_error *err = NULL;
681 if (got_object_tree_entry_is_submodule(te1))
685 if (S_ISDIR(te1->mode))
686 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
687 label1, repo, cb, cb_arg, diff_content);
690 err = diff_deleted_blob(&te1->id, f1, fd1,
691 f2, label1, te1->mode, repo, cb, cb_arg);
693 err = cb(cb_arg, NULL, NULL, NULL, NULL,
694 &te1->id, NULL, label1, NULL,
698 } else if (got_object_tree_entry_is_submodule(te2))
701 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
702 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
704 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
705 fd1, fd2, label1, label2, repo, cb, cb_arg,
707 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
708 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
710 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
711 (te2->mode & (S_IFLNK | S_IXUSR))) {
713 return diff_modified_blob(&te1->id, &te2->id,
714 f1, f2, fd1, fd2, label1, label2,
715 te1->mode, te2->mode, repo, cb, cb_arg);
717 return cb(cb_arg, NULL, NULL, NULL, NULL,
718 &te1->id, &te2->id, label1, label2,
719 te1->mode, te2->mode, repo);
726 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
730 static const struct got_error *
731 diff_entry_new_old(struct got_tree_entry *te2,
732 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
733 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
736 if (te1 != NULL) /* handled by diff_entry_old_new() */
739 if (got_object_tree_entry_is_submodule(te2))
742 if (S_ISDIR(te2->mode))
743 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
744 repo, cb, cb_arg, diff_content);
747 return diff_added_blob(&te2->id, f1, f2, fd2,
748 label2, te2->mode, repo, cb, cb_arg);
750 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
751 NULL, label2, 0, te2->mode, repo);
754 const struct got_error *
755 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
756 struct got_blob_object *blob2, FILE *f1, FILE *f2,
757 struct got_object_id *id1, struct got_object_id *id2,
758 const char *label1, const char *label2,
759 mode_t mode1, mode_t mode2, struct got_repository *repo)
761 const struct got_error *err = NULL;
762 struct got_diffreg_result *result = NULL;
763 struct got_diffstat_cb_arg *a = arg;
765 int status = GOT_STATUS_NO_CHANGE;
767 path = strdup(label2 ? label2 : label1);
769 return got_error_from_errno("malloc");
772 status = GOT_STATUS_ADD;
773 else if (id2 == NULL)
774 status = GOT_STATUS_DELETE;
776 if (got_object_id_cmp(id1, id2) != 0)
777 status = GOT_STATUS_MODIFY;
778 else if (mode1 != mode2)
779 status = GOT_STATUS_MODE_CHANGE;
783 err = got_opentemp_truncate(f1);
788 err = got_opentemp_truncate(f2);
794 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
800 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
806 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
811 err = get_diffstat(a, path, result->result, a->force_text, status);
815 const struct got_error *free_err;
817 free_err = got_diffreg_result_free(result);
818 if (free_err && err == NULL)
826 const struct got_error *
827 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
828 struct got_blob_object *blob2, FILE *f1, FILE *f2,
829 struct got_object_id *id1, struct got_object_id *id2,
830 const char *label1, const char *label2,
831 mode_t mode1, mode_t mode2, struct got_repository *repo)
833 const struct got_error *err = NULL;
834 struct got_pathlist_head *paths = arg;
835 struct got_diff_changed_path *change = NULL;
838 path = strdup(label2 ? label2 : label1);
840 return got_error_from_errno("malloc");
842 change = malloc(sizeof(*change));
843 if (change == NULL) {
844 err = got_error_from_errno("malloc");
848 change->status = GOT_STATUS_NO_CHANGE;
850 change->status = GOT_STATUS_ADD;
851 else if (id2 == NULL)
852 change->status = GOT_STATUS_DELETE;
854 if (got_object_id_cmp(id1, id2) != 0)
855 change->status = GOT_STATUS_MODIFY;
856 else if (mode1 != mode2)
857 change->status = GOT_STATUS_MODE_CHANGE;
860 err = got_pathlist_append(paths, path, change);
869 const struct got_error *
870 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
871 FILE *f1, FILE *f2, int fd1, int fd2,
872 const char *label1, const char *label2,
873 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
876 const struct got_error *err = NULL;
877 struct got_tree_entry *te1 = NULL;
878 struct got_tree_entry *te2 = NULL;
879 char *l1 = NULL, *l2 = NULL;
880 int tidx1 = 0, tidx2 = 0;
883 te1 = got_object_tree_get_entry(tree1, 0);
884 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
886 return got_error_from_errno("asprintf");
889 te2 = got_object_tree_get_entry(tree2, 0);
890 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
892 return got_error_from_errno("asprintf");
897 struct got_tree_entry *te = NULL;
899 te = got_object_tree_find_entry(tree2,
904 if (te && asprintf(&l2, "%s%s%s", label2,
905 label2[0] ? "/" : "", te->name) == -1)
907 got_error_from_errno("asprintf");
909 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
910 l1, l2, repo, cb, cb_arg, diff_content);
916 struct got_tree_entry *te = NULL;
918 te = got_object_tree_find_entry(tree1,
922 if (asprintf(&l2, "%s%s%s", label2,
923 label2[0] ? "/" : "", te->name) == -1)
925 got_error_from_errno("asprintf");
927 if (asprintf(&l2, "%s%s%s", label2,
928 label2[0] ? "/" : "", te2->name) == -1)
930 got_error_from_errno("asprintf");
932 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
933 repo, cb, cb_arg, diff_content);
942 te1 = got_object_tree_get_entry(tree1, tidx1);
944 asprintf(&l1, "%s%s%s", label1,
945 label1[0] ? "/" : "", te1->name) == -1)
946 return got_error_from_errno("asprintf");
952 te2 = got_object_tree_get_entry(tree2, tidx2);
954 asprintf(&l2, "%s%s%s", label2,
955 label2[0] ? "/" : "", te2->name) == -1)
956 return got_error_from_errno("asprintf");
958 } while (te1 || te2);
963 const struct got_error *
964 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
965 FILE *f1, FILE *f2, int fd1, int fd2,
966 struct got_object_id *id1, struct got_object_id *id2,
967 const char *label1, const char *label2,
968 enum got_diff_algorithm diff_algo, int diff_context,
969 int ignore_whitespace, int force_text_diff, int show_diffstat,
970 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
972 const struct got_error *err;
973 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
975 if (id1 == NULL && id2 == NULL)
976 return got_error(GOT_ERR_NO_OBJ);
979 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
984 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
988 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
989 diff_algo, diff_context, ignore_whitespace, force_text_diff,
990 show_diffstat, ds, outfile);
993 got_object_blob_close(blob1);
995 got_object_blob_close(blob2);
999 static const struct got_error *
1000 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1001 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1002 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1004 const struct got_error *err = NULL;
1005 struct got_pathlist_entry *pe;
1006 struct got_object_id *id1 = NULL, *id2 = NULL;
1007 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1008 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1010 TAILQ_FOREACH(pe, paths, entry) {
1011 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1012 mode_t mode1 = 0, mode2 = 0;
1019 got_object_tree_close(subtree1);
1023 got_object_tree_close(subtree2);
1027 got_object_blob_close(blob1);
1031 got_object_blob_close(blob2);
1035 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1037 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1039 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1041 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1043 if (id1 == NULL && id2 == NULL) {
1044 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1048 err = got_object_get_type(&type1, repo, id1);
1053 err = got_object_get_type(&type2, repo, id2);
1057 if (type1 == GOT_OBJ_TYPE_ANY &&
1058 type2 == GOT_OBJ_TYPE_ANY) {
1059 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1061 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1062 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1063 err = got_error(GOT_ERR_OBJ_TYPE);
1067 if (type1 == GOT_OBJ_TYPE_BLOB ||
1068 type2 == GOT_OBJ_TYPE_BLOB) {
1070 err = got_object_open_as_blob(&blob1, repo,
1076 err = got_object_open_as_blob(&blob2, repo,
1081 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1082 id1 ? pe->path : "/dev/null",
1083 id2 ? pe->path : "/dev/null",
1084 mode1, mode2, repo);
1087 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1088 type2 == GOT_OBJ_TYPE_TREE) {
1090 err = got_object_open_as_tree(&subtree1, repo,
1096 err = got_object_open_as_tree(&subtree2, repo,
1101 err = got_diff_tree(subtree1, subtree2, f1, f2,
1103 id1 ? pe->path : "/dev/null",
1104 id2 ? pe->path : "/dev/null",
1105 repo, cb, cb_arg, 1);
1109 err = got_error(GOT_ERR_OBJ_TYPE);
1117 got_object_tree_close(subtree1);
1119 got_object_tree_close(subtree2);
1121 got_object_blob_close(blob1);
1123 got_object_blob_close(blob2);
1127 static const struct got_error *
1128 show_object_id(struct got_diff_line **lines, size_t *nlines,
1129 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1131 const struct got_error *err;
1135 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1137 return got_error_from_errno("fprintf");
1139 if (lines != NULL && *lines != NULL) {
1141 err = add_line_metadata(lines, nlines, 0,
1142 GOT_DIFF_LINE_META);
1146 outoff = (*lines)[*nlines - 1].offset;
1149 err = add_line_metadata(lines, nlines, outoff,
1150 GOT_DIFF_LINE_META);
1158 static const struct got_error *
1159 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1160 FILE *f1, FILE *f2, int fd1, int fd2,
1161 struct got_object_id *id1, struct got_object_id *id2,
1162 struct got_pathlist_head *paths, const char *label1, const char *label2,
1163 int diff_context, int ignore_whitespace, int force_text_diff,
1164 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1165 struct got_repository *repo, FILE *outfile,
1166 enum got_diff_algorithm diff_algo)
1168 const struct got_error *err;
1169 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1170 struct got_diff_blob_output_unidiff_arg arg;
1171 int want_linemeta = (lines != NULL && *lines != NULL);
1173 if (id1 == NULL && id2 == NULL)
1174 return got_error(GOT_ERR_NO_OBJ);
1177 err = got_object_open_as_tree(&tree1, repo, id1);
1182 err = got_object_open_as_tree(&tree2, repo, id2);
1187 arg.diff_algo = diff_algo;
1188 arg.diff_context = diff_context;
1189 arg.ignore_whitespace = ignore_whitespace;
1190 arg.force_text_diff = force_text_diff;
1191 arg.show_diffstat = show_diffstat;
1193 arg.outfile = outfile;
1194 if (want_linemeta) {
1196 arg.nlines = *nlines;
1201 if (paths == NULL || TAILQ_EMPTY(paths))
1202 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1203 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1205 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1206 got_diff_blob_output_unidiff, &arg);
1207 if (want_linemeta) {
1208 *lines = arg.lines; /* was likely re-allocated */
1209 *nlines = arg.nlines;
1213 got_object_tree_close(tree1);
1215 got_object_tree_close(tree2);
1219 const struct got_error *
1220 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1221 FILE *f1, FILE *f2, int fd1, int fd2,
1222 struct got_object_id *id1, struct got_object_id *id2,
1223 struct got_pathlist_head *paths, const char *label1, const char *label2,
1224 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1225 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1226 struct got_repository *repo, FILE *outfile)
1228 const struct got_error *err;
1231 if (id1 == NULL && id2 == NULL)
1232 return got_error(GOT_ERR_NO_OBJ);
1235 err = got_object_id_str(&idstr, id1);
1238 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1244 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1251 err = got_object_id_str(&idstr, id2);
1254 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1260 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1266 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1267 paths, label1, label2, diff_context, ignore_whitespace,
1268 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1274 const struct got_error *
1275 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1276 FILE *f1, FILE *f2, int fd1, int fd2,
1277 struct got_object_id *id1, struct got_object_id *id2,
1278 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1279 int diff_context, int ignore_whitespace, int force_text_diff,
1280 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1281 struct got_repository *repo, FILE *outfile)
1283 const struct got_error *err;
1284 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1288 return got_error(GOT_ERR_NO_OBJ);
1291 err = got_object_open_as_commit(&commit1, repo, id1);
1294 err = got_object_id_str(&idstr, id1);
1297 err = show_object_id(lines, nlines, "commit", '-', idstr,
1304 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1310 err = got_object_open_as_commit(&commit2, repo, id2);
1314 err = got_object_id_str(&idstr, id2);
1317 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1321 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1322 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1323 got_object_commit_get_tree_id(commit2), paths, "", "",
1324 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1325 dsa, repo, outfile, diff_algo);
1328 got_object_commit_close(commit1);
1330 got_object_commit_close(commit2);
1335 const struct got_error *
1336 got_diff_files(struct got_diffreg_result **resultp,
1337 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1338 const char *label2, int diff_context, int ignore_whitespace,
1339 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1341 const struct got_error *err = NULL;
1342 struct got_diffreg_result *diffreg_result = NULL;
1348 fprintf(outfile, "file - %s\n",
1349 f1_exists ? label1 : "/dev/null");
1350 fprintf(outfile, "file + %s\n",
1351 f2_exists ? label2 : "/dev/null");
1354 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1355 ignore_whitespace, force_text_diff);
1360 err = got_diffreg_output(NULL, NULL, diffreg_result,
1361 f1_exists, f2_exists, label1, label2,
1362 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1368 if (resultp && err == NULL)
1369 *resultp = diffreg_result;
1370 else if (diffreg_result) {
1371 const struct got_error *free_err;
1372 free_err = got_diffreg_result_free(diffreg_result);
1373 if (free_err && err == NULL)