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_compat.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_worktree.h"
36 #include "got_opentemp.h"
38 #include "got_lib_diff.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_inflate.h"
41 #include "got_lib_object.h"
44 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
47 static const struct got_error *
48 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
49 off_t off, uint8_t type)
51 struct got_diff_line *p;
53 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
55 return got_error_from_errno("reallocarray");
57 (*lines)[*nlines].offset = off;
58 (*lines)[*nlines].type = type;
65 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
66 uint32_t add, uint32_t rm)
71 *maxlen = MAX(*maxlen, len);
75 *add_cols = MAX(*add_cols, d1);
79 *rm_cols = MAX(*rm_cols, d2);
82 static const struct got_error *
83 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
84 struct diff_result *r, int force_text, int status)
86 const struct got_error *err;
87 struct got_pathlist_entry *pe;
88 struct got_diff_changed_path *change = NULL;
89 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
90 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
93 change = calloc(1, sizeof(*change));
95 return got_error_from_errno("calloc");
97 if (!isbin || force_text) {
98 for (i = 0; i < r->chunks.len; ++i) {
102 c = diff_chunk_get(r, i);
103 clc = diff_chunk_get_left_count(c);
104 crc = diff_chunk_get_right_count(c);
113 change->status = status;
114 ds->ins += change->add;
115 ds->del += change->rm;
118 err = got_pathlist_append(ds->paths, path, change);
124 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
125 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
126 pe->path_len, change->add, change->rm);
131 static const struct got_error *
132 diff_blobs(struct got_diff_line **lines, size_t *nlines,
133 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
134 struct got_blob_object *blob2, FILE *f1, FILE *f2,
135 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
136 int diff_context, int ignore_whitespace, int force_text_diff,
137 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
138 enum got_diff_algorithm diff_algo)
140 const struct got_error *err = NULL, *free_err;
141 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
142 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
143 const char *idstr1 = NULL, *idstr2 = NULL;
144 char *modestr1 = NULL, *modestr2 = NULL;
146 struct got_diffreg_result *result = NULL;
150 if (lines && *lines && *nlines > 0)
151 outoff = (*lines)[*nlines - 1].offset;
153 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
162 err = got_opentemp_truncate(f1);
167 err = got_opentemp_truncate(f2);
174 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
175 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
180 idstr1 = "/dev/null";
184 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
185 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
190 idstr2 = "/dev/null";
195 if (mode1 && mode1 != mode2) {
199 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
200 if (asprintf(&modestr1, " (mode %o)",
201 mode1 & modebits) == -1) {
202 err = got_error_from_errno("asprintf");
206 if (mode2 && mode1 != mode2) {
210 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
211 if (asprintf(&modestr2, " (mode %o)",
212 mode2 & modebits) == -1) {
213 err = got_error_from_errno("asprintf");
217 n = fprintf(outfile, "blob - %s%s\n", idstr1,
218 modestr1 ? modestr1 : "");
223 err = add_line_metadata(lines, nlines, outoff,
224 GOT_DIFF_LINE_BLOB_MIN);
229 n = fprintf(outfile, "blob + %s%s\n", idstr2,
230 modestr2 ? modestr2 : "");
235 err = add_line_metadata(lines, nlines, outoff,
236 GOT_DIFF_LINE_BLOB_PLUS);
242 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
249 int status = GOT_STATUS_NO_CHANGE;
252 status = GOT_STATUS_ADD;
253 else if (blob2 == NULL)
254 status = GOT_STATUS_DELETE;
256 if (strcmp(idstr1, idstr2) != 0)
257 status = GOT_STATUS_MODIFY;
258 else if (mode1 != mode2)
259 status = GOT_STATUS_MODE_CHANGE;
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(diffstat, path, result->result,
282 force_text_diff, status);
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->diffstat, a->outfile,
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,
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, 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, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
348 const struct got_error *err = NULL, *free_err;
349 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
350 const char *idstr1 = NULL;
351 struct got_diffreg_result *result = NULL;
357 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
359 idstr1 = "/dev/null";
364 /* display file mode for new added files only */
365 if (f2_exists && blob1 == NULL) {
366 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
368 if (S_ISLNK(sb2->st_mode))
370 if (asprintf(&mode, " (mode %o)",
371 sb2->st_mode & mmask) == -1)
372 return got_error_from_errno("asprintf");
374 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
375 fprintf(outfile, "file + %s%s\n",
376 f2_exists ? label2 : "/dev/null", mode ? mode : "");
380 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
386 err = got_diffreg_output(NULL, NULL, result,
387 blob1 != NULL, f2_exists,
388 label2, /* show local file's path, not a blob ID */
389 label2, GOT_DIFF_OUTPUT_UNIDIFF,
390 diff_context, outfile);
397 int status = GOT_STATUS_NO_CHANGE;
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 if (label2 != NULL &&
412 (status != GOT_STATUS_DELETE || label1 == NULL))
413 path = strdup(label2);
415 path = strdup(label1);
417 err = got_error_from_errno("strdup");
421 err = get_diffstat(diffstat, path, result->result,
422 force_text_diff, status);
430 if (resultp && err == NULL)
433 free_err = got_diffreg_result_free(result);
434 if (free_err && err == NULL)
440 const struct got_error *
441 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
442 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
443 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
444 int ignore_whitespace, int force_text_diff,
445 struct got_diffstat_cb_arg *ds, FILE *outfile)
447 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
448 sb2, label2, diff_algo, diff_context, ignore_whitespace,
449 force_text_diff, ds, outfile);
452 static const struct got_error *
453 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
454 const char *label, mode_t mode, struct got_repository *repo,
455 got_diff_blob_cb cb, void *cb_arg)
457 const struct got_error *err;
458 struct got_blob_object *blob = NULL;
459 struct got_object *obj = NULL;
461 err = got_object_open(&obj, repo, id);
465 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
468 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
469 NULL, label, 0, mode, repo);
471 got_object_close(obj);
473 got_object_blob_close(blob);
477 static const struct got_error *
478 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
479 FILE *f1, FILE *f2, int fd1, int fd2,
480 const char *label1, const char *label2,
481 mode_t mode1, mode_t mode2, struct got_repository *repo,
482 got_diff_blob_cb cb, void *cb_arg)
484 const struct got_error *err;
485 struct got_object *obj1 = NULL;
486 struct got_object *obj2 = NULL;
487 struct got_blob_object *blob1 = NULL;
488 struct got_blob_object *blob2 = NULL;
490 err = got_object_open(&obj1, repo, id1);
494 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
495 err = got_error(GOT_ERR_OBJ_TYPE);
499 err = got_object_open(&obj2, repo, id2);
502 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
511 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
515 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
519 got_object_close(obj1);
521 got_object_close(obj2);
523 got_object_blob_close(blob1);
525 got_object_blob_close(blob2);
529 static const struct got_error *
530 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
531 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
532 got_diff_blob_cb cb, void *cb_arg)
534 const struct got_error *err;
535 struct got_blob_object *blob = NULL;
536 struct got_object *obj = NULL;
538 err = got_object_open(&obj, repo, id);
542 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
545 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
548 got_object_close(obj);
550 got_object_blob_close(blob);
554 static const struct got_error *
555 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
556 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
557 void *cb_arg, int diff_content)
559 const struct got_error *err = NULL;
560 struct got_object *treeobj = NULL;
561 struct got_tree_object *tree = NULL;
563 err = got_object_open(&treeobj, repo, id);
567 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
568 err = got_error(GOT_ERR_OBJ_TYPE);
572 err = got_object_tree_open(&tree, repo, treeobj);
576 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
577 repo, cb, cb_arg, diff_content);
580 got_object_tree_close(tree);
582 got_object_close(treeobj);
586 static const struct got_error *
587 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
588 FILE *f1, FILE *f2, int fd1, int fd2,
589 const char *label1, const char *label2,
590 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
593 const struct got_error *err;
594 struct got_object *treeobj1 = NULL;
595 struct got_object *treeobj2 = NULL;
596 struct got_tree_object *tree1 = NULL;
597 struct got_tree_object *tree2 = NULL;
599 err = got_object_open(&treeobj1, repo, id1);
603 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
604 err = got_error(GOT_ERR_OBJ_TYPE);
608 err = got_object_open(&treeobj2, repo, id2);
612 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
613 err = got_error(GOT_ERR_OBJ_TYPE);
617 err = got_object_tree_open(&tree1, repo, treeobj1);
621 err = got_object_tree_open(&tree2, repo, treeobj2);
625 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
626 label1, label2, repo, cb, cb_arg, diff_content);
630 got_object_tree_close(tree1);
632 got_object_tree_close(tree2);
634 got_object_close(treeobj1);
636 got_object_close(treeobj2);
640 static const struct got_error *
641 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
642 FILE *f2, const char *label, struct got_repository *repo,
643 got_diff_blob_cb cb, void *cb_arg, int diff_content)
645 const struct got_error *err;
646 struct got_object *treeobj = NULL;
647 struct got_tree_object *tree = NULL;
649 err = got_object_open(&treeobj, repo, id);
653 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
654 err = got_error(GOT_ERR_OBJ_TYPE);
658 err = got_object_tree_open(&tree, repo, treeobj);
662 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
663 repo, cb, cb_arg, diff_content);
666 got_object_tree_close(tree);
668 got_object_close(treeobj);
672 static const struct got_error *
673 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
674 const char *label1, const char *label2, struct got_repository *repo,
675 got_diff_blob_cb cb, void *cb_arg)
681 static const struct got_error *
682 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
683 FILE *f1, FILE *f2, int fd1, int fd2,
684 const char *label1, const char *label2,
685 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
688 const struct got_error *err = NULL;
691 if (got_object_tree_entry_is_submodule(te1))
695 if (S_ISDIR(te1->mode))
696 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
697 label1, repo, cb, cb_arg, diff_content);
700 err = diff_deleted_blob(&te1->id, f1, fd1,
701 f2, label1, te1->mode, repo, cb, cb_arg);
703 err = cb(cb_arg, NULL, NULL, NULL, NULL,
704 &te1->id, NULL, label1, NULL,
708 } else if (got_object_tree_entry_is_submodule(te2))
711 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
712 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
714 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
715 fd1, fd2, label1, label2, repo, cb, cb_arg,
717 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
718 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
720 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
721 (te2->mode & (S_IFLNK | S_IXUSR))) {
723 return diff_modified_blob(&te1->id, &te2->id,
724 f1, f2, fd1, fd2, label1, label2,
725 te1->mode, te2->mode, repo, cb, cb_arg);
727 return cb(cb_arg, NULL, NULL, NULL, NULL,
728 &te1->id, &te2->id, label1, label2,
729 te1->mode, te2->mode, repo);
736 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
740 static const struct got_error *
741 diff_entry_new_old(struct got_tree_entry *te2,
742 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
743 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
746 if (te1 != NULL) /* handled by diff_entry_old_new() */
749 if (got_object_tree_entry_is_submodule(te2))
752 if (S_ISDIR(te2->mode))
753 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
754 repo, cb, cb_arg, diff_content);
757 return diff_added_blob(&te2->id, f1, f2, fd2,
758 label2, te2->mode, repo, cb, cb_arg);
760 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
761 NULL, label2, 0, te2->mode, repo);
764 const struct got_error *
765 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
766 struct got_blob_object *blob2, FILE *f1, FILE *f2,
767 struct got_object_id *id1, struct got_object_id *id2,
768 const char *label1, const char *label2,
769 mode_t mode1, mode_t mode2, struct got_repository *repo)
771 const struct got_error *err = NULL;
772 struct got_diffreg_result *result = NULL;
773 struct got_diffstat_cb_arg *a = arg;
775 int status = GOT_STATUS_NO_CHANGE;
777 path = strdup(label2 ? label2 : label1);
779 return got_error_from_errno("strdup");
782 status = GOT_STATUS_ADD;
783 else if (id2 == NULL)
784 status = GOT_STATUS_DELETE;
786 if (got_object_id_cmp(id1, id2) != 0)
787 status = GOT_STATUS_MODIFY;
788 else if (mode1 != mode2)
789 status = GOT_STATUS_MODE_CHANGE;
793 err = got_opentemp_truncate(f1);
798 err = got_opentemp_truncate(f2);
804 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
810 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
816 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
821 err = get_diffstat(a, path, result->result, a->force_text, status);
825 const struct got_error *free_err;
827 free_err = got_diffreg_result_free(result);
828 if (free_err && err == NULL)
836 const struct got_error *
837 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
838 struct got_blob_object *blob2, FILE *f1, FILE *f2,
839 struct got_object_id *id1, struct got_object_id *id2,
840 const char *label1, const char *label2,
841 mode_t mode1, mode_t mode2, struct got_repository *repo)
843 const struct got_error *err = NULL;
844 struct got_pathlist_head *paths = arg;
845 struct got_diff_changed_path *change = NULL;
848 path = strdup(label2 ? label2 : label1);
850 return got_error_from_errno("strdup");
852 change = malloc(sizeof(*change));
853 if (change == NULL) {
854 err = got_error_from_errno("malloc");
858 change->status = GOT_STATUS_NO_CHANGE;
860 change->status = GOT_STATUS_ADD;
861 else if (id2 == NULL)
862 change->status = GOT_STATUS_DELETE;
864 if (got_object_id_cmp(id1, id2) != 0)
865 change->status = GOT_STATUS_MODIFY;
866 else if (mode1 != mode2)
867 change->status = GOT_STATUS_MODE_CHANGE;
870 err = got_pathlist_append(paths, path, change);
879 const struct got_error *
880 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
881 FILE *f1, FILE *f2, int fd1, int fd2,
882 const char *label1, const char *label2,
883 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
886 const struct got_error *err = NULL;
887 struct got_tree_entry *te1 = NULL;
888 struct got_tree_entry *te2 = NULL;
889 char *l1 = NULL, *l2 = NULL;
890 int tidx1 = 0, tidx2 = 0;
893 te1 = got_object_tree_get_entry(tree1, 0);
894 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
896 return got_error_from_errno("asprintf");
899 te2 = got_object_tree_get_entry(tree2, 0);
900 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
902 err = got_error_from_errno("asprintf");
909 struct got_tree_entry *te = NULL;
912 te = got_object_tree_find_entry(tree2,
917 if (te && asprintf(&l2, "%s%s%s", label2,
918 label2[0] ? "/" : "", te->name) == -1) {
919 err = got_error_from_errno("asprintf");
924 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
925 l1, l2, repo, cb, cb_arg, diff_content);
931 struct got_tree_entry *te = NULL;
934 te = got_object_tree_find_entry(tree1,
940 if (asprintf(&l2, "%s%s%s", label2,
941 label2[0] ? "/" : "", te->name) == -1) {
942 err = got_error_from_errno("asprintf");
946 if (asprintf(&l2, "%s%s%s", label2,
947 label2[0] ? "/" : "", te2->name) == -1) {
948 err = got_error_from_errno("asprintf");
953 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
954 repo, cb, cb_arg, diff_content);
963 te1 = got_object_tree_get_entry(tree1, tidx1);
965 asprintf(&l1, "%s%s%s", label1,
966 label1[0] ? "/" : "", te1->name) == -1) {
967 err = got_error_from_errno("asprintf");
976 te2 = got_object_tree_get_entry(tree2, tidx2);
978 asprintf(&l2, "%s%s%s", label2,
979 label2[0] ? "/" : "", te2->name) == -1) {
980 err = got_error_from_errno("asprintf");
984 } while (te1 || te2);
992 const struct got_error *
993 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
994 FILE *f1, FILE *f2, int fd1, int fd2,
995 struct got_object_id *id1, struct got_object_id *id2,
996 const char *label1, const char *label2,
997 enum got_diff_algorithm diff_algo, int diff_context,
998 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
999 struct got_repository *repo, FILE *outfile)
1001 const struct got_error *err;
1002 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1004 if (id1 == NULL && id2 == NULL)
1005 return got_error(GOT_ERR_NO_OBJ);
1008 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1013 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1017 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1018 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1022 got_object_blob_close(blob1);
1024 got_object_blob_close(blob2);
1028 static const struct got_error *
1029 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1030 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1031 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1033 const struct got_error *err = NULL;
1034 struct got_pathlist_entry *pe;
1035 struct got_object_id *id1 = NULL, *id2 = NULL;
1036 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1037 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1039 TAILQ_FOREACH(pe, paths, entry) {
1040 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1041 mode_t mode1 = 0, mode2 = 0;
1048 got_object_tree_close(subtree1);
1052 got_object_tree_close(subtree2);
1056 got_object_blob_close(blob1);
1060 got_object_blob_close(blob2);
1064 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1066 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1068 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1070 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1072 if (id1 == NULL && id2 == NULL) {
1073 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1077 err = got_object_get_type(&type1, repo, id1);
1082 err = got_object_get_type(&type2, repo, id2);
1086 if (type1 == GOT_OBJ_TYPE_ANY &&
1087 type2 == GOT_OBJ_TYPE_ANY) {
1088 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1090 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1091 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1092 err = got_error(GOT_ERR_OBJ_TYPE);
1096 if (type1 == GOT_OBJ_TYPE_BLOB ||
1097 type2 == GOT_OBJ_TYPE_BLOB) {
1099 err = got_object_open_as_blob(&blob1, repo,
1105 err = got_object_open_as_blob(&blob2, repo,
1110 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1111 id1 ? pe->path : "/dev/null",
1112 id2 ? pe->path : "/dev/null",
1113 mode1, mode2, repo);
1116 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1117 type2 == GOT_OBJ_TYPE_TREE) {
1119 err = got_object_open_as_tree(&subtree1, repo,
1125 err = got_object_open_as_tree(&subtree2, repo,
1130 err = got_diff_tree(subtree1, subtree2, f1, f2,
1132 id1 ? pe->path : "/dev/null",
1133 id2 ? pe->path : "/dev/null",
1134 repo, cb, cb_arg, 1);
1138 err = got_error(GOT_ERR_OBJ_TYPE);
1146 got_object_tree_close(subtree1);
1148 got_object_tree_close(subtree2);
1150 got_object_blob_close(blob1);
1152 got_object_blob_close(blob2);
1156 static const struct got_error *
1157 show_object_id(struct got_diff_line **lines, size_t *nlines,
1158 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1160 const struct got_error *err;
1164 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1166 return got_error_from_errno("fprintf");
1168 if (lines != NULL && *lines != NULL) {
1170 err = add_line_metadata(lines, nlines, 0,
1171 GOT_DIFF_LINE_META);
1175 outoff = (*lines)[*nlines - 1].offset;
1178 err = add_line_metadata(lines, nlines, outoff,
1179 GOT_DIFF_LINE_META);
1187 static const struct got_error *
1188 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1189 FILE *f1, FILE *f2, int fd1, int fd2,
1190 struct got_object_id *id1, struct got_object_id *id2,
1191 struct got_pathlist_head *paths, const char *label1, const char *label2,
1192 int diff_context, int ignore_whitespace, int force_text_diff,
1193 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1194 FILE *outfile, enum got_diff_algorithm diff_algo)
1196 const struct got_error *err;
1197 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1198 struct got_diff_blob_output_unidiff_arg arg;
1199 int want_linemeta = (lines != NULL && *lines != NULL);
1201 if (id1 == NULL && id2 == NULL)
1202 return got_error(GOT_ERR_NO_OBJ);
1205 err = got_object_open_as_tree(&tree1, repo, id1);
1210 err = got_object_open_as_tree(&tree2, repo, id2);
1215 arg.diff_algo = diff_algo;
1216 arg.diff_context = diff_context;
1217 arg.ignore_whitespace = ignore_whitespace;
1218 arg.force_text_diff = force_text_diff;
1220 arg.outfile = outfile;
1221 if (want_linemeta) {
1223 arg.nlines = *nlines;
1228 if (paths == NULL || TAILQ_EMPTY(paths))
1229 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1230 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1232 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1233 got_diff_blob_output_unidiff, &arg);
1234 if (want_linemeta) {
1235 *lines = arg.lines; /* was likely re-allocated */
1236 *nlines = arg.nlines;
1240 got_object_tree_close(tree1);
1242 got_object_tree_close(tree2);
1246 const struct got_error *
1247 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1248 FILE *f1, FILE *f2, int fd1, int fd2,
1249 struct got_object_id *id1, struct got_object_id *id2,
1250 struct got_pathlist_head *paths, const char *label1, const char *label2,
1251 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1252 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1253 struct got_repository *repo, FILE *outfile)
1255 const struct got_error *err;
1258 if (id1 == NULL && id2 == NULL)
1259 return got_error(GOT_ERR_NO_OBJ);
1262 err = got_object_id_str(&idstr, id1);
1265 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1271 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1278 err = got_object_id_str(&idstr, id2);
1281 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1287 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1293 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1294 paths, label1, label2, diff_context, ignore_whitespace,
1295 force_text_diff, dsa, repo, outfile, diff_algo);
1301 const struct got_error *
1302 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1303 FILE *f1, FILE *f2, int fd1, int fd2,
1304 struct got_object_id *id1, struct got_object_id *id2,
1305 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1306 int diff_context, int ignore_whitespace, int force_text_diff,
1307 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1309 const struct got_error *err;
1310 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1314 return got_error(GOT_ERR_NO_OBJ);
1317 err = got_object_open_as_commit(&commit1, repo, id1);
1320 err = got_object_id_str(&idstr, id1);
1323 err = show_object_id(lines, nlines, "commit", '-', idstr,
1330 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1336 err = got_object_open_as_commit(&commit2, repo, id2);
1340 err = got_object_id_str(&idstr, id2);
1343 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1347 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1348 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1349 got_object_commit_get_tree_id(commit2), paths, "", "",
1350 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1351 outfile, diff_algo);
1354 got_object_commit_close(commit1);
1356 got_object_commit_close(commit2);
1361 const struct got_error *
1362 got_diff_files(struct got_diffreg_result **resultp,
1363 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1364 const char *label2, int diff_context, int ignore_whitespace,
1365 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1367 const struct got_error *err = NULL;
1368 struct got_diffreg_result *diffreg_result = NULL;
1374 fprintf(outfile, "file - %s\n",
1375 f1_exists ? label1 : "/dev/null");
1376 fprintf(outfile, "file + %s\n",
1377 f2_exists ? label2 : "/dev/null");
1380 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1381 ignore_whitespace, force_text_diff);
1386 err = got_diffreg_output(NULL, NULL, diffreg_result,
1387 f1_exists, f2_exists, label1, label2,
1388 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1394 if (resultp && err == NULL)
1395 *resultp = diffreg_result;
1396 else if (diffreg_result) {
1397 const struct got_error *free_err;
1399 free_err = got_diffreg_result_free(diffreg_result);
1400 if (free_err && err == NULL)