2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
26 #include "got_compat.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_worktree.h"
35 #include "got_opentemp.h"
37 #include "got_lib_diff.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
43 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 static const struct got_error *
47 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 off_t off, uint8_t type)
50 struct got_diff_line *p;
52 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
54 return got_error_from_errno("reallocarray");
56 (*lines)[*nlines].offset = off;
57 (*lines)[*nlines].type = type;
63 static const struct got_error *
64 diff_blobs(struct got_diff_line **lines, size_t *nlines,
65 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
66 struct got_blob_object *blob2, FILE *f1, FILE *f2,
67 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
68 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
69 enum got_diff_algorithm diff_algo)
71 const struct got_error *err = NULL, *free_err;
72 char hex1[SHA1_DIGEST_STRING_LENGTH];
73 char hex2[SHA1_DIGEST_STRING_LENGTH];
74 const char *idstr1 = NULL, *idstr2 = NULL;
76 struct got_diffreg_result *result = NULL;
80 if (lines && *lines && *nlines > 0)
81 outoff = (*lines)[*nlines - 1].offset;
83 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
92 err = got_opentemp_truncate(f1);
97 err = got_opentemp_truncate(f2);
104 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
105 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
110 idstr1 = "/dev/null";
114 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
115 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
120 idstr2 = "/dev/null";
123 char *modestr1 = NULL, *modestr2 = NULL;
125 if (mode1 && mode1 != mode2) {
129 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
130 if (asprintf(&modestr1, " (mode %o)",
131 mode1 & modebits) == -1) {
132 err = got_error_from_errno("asprintf");
136 if (mode2 && mode1 != mode2) {
140 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
141 if (asprintf(&modestr2, " (mode %o)",
142 mode2 & modebits) == -1) {
143 err = got_error_from_errno("asprintf");
147 n = fprintf(outfile, "blob - %s%s\n", idstr1,
148 modestr1 ? modestr1 : "");
153 err = add_line_metadata(lines, nlines, outoff,
154 GOT_DIFF_LINE_BLOB_MIN);
159 n = fprintf(outfile, "blob + %s%s\n", idstr2,
160 modestr2 ? modestr2 : "");
165 err = add_line_metadata(lines, nlines, outoff,
166 GOT_DIFF_LINE_BLOB_PLUS);
174 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
180 err = got_diffreg_output(lines, nlines, result,
181 blob1 != NULL, blob2 != NULL,
182 label1 ? label1 : idstr1,
183 label2 ? label2 : idstr2,
184 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
190 if (resultp && err == NULL)
193 free_err = got_diffreg_result_free(result);
194 if (free_err && err == NULL)
201 const struct got_error *
202 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
203 struct got_blob_object *blob2, FILE *f1, FILE *f2,
204 struct got_object_id *id1, struct got_object_id *id2,
205 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
206 struct got_repository *repo)
208 struct got_diff_blob_output_unidiff_arg *a = arg;
210 return diff_blobs(&a->lines, &a->nlines, NULL,
211 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
212 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
215 const struct got_error *
216 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
217 struct got_blob_object *blob1, struct got_blob_object *blob2,
218 FILE *f1, FILE *f2, const char *label1, const char *label2,
219 enum got_diff_algorithm diff_algo, int diff_context,
220 int ignore_whitespace, int force_text_diff, FILE *outfile)
222 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
223 label1, label2, 0, 0, diff_context, ignore_whitespace,
224 force_text_diff, outfile, diff_algo);
227 static const struct got_error *
228 diff_blob_file(struct got_diffreg_result **resultp,
229 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
230 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
231 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
232 int force_text_diff, FILE *outfile)
234 const struct got_error *err = NULL, *free_err;
235 char hex1[SHA1_DIGEST_STRING_LENGTH];
236 const char *idstr1 = NULL;
237 struct got_diffreg_result *result = NULL;
243 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
245 idstr1 = "/dev/null";
250 /* display file mode for new added files only */
251 if (f2_exists && blob1 == NULL) {
252 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
254 if (S_ISLNK(sb2->st_mode))
256 if (asprintf(&mode, " (mode %o)",
257 sb2->st_mode & mmask) == -1)
258 return got_error_from_errno("asprintf");
260 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
261 fprintf(outfile, "file + %s%s\n",
262 f2_exists ? label2 : "/dev/null", mode ? mode : "");
266 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
272 err = got_diffreg_output(NULL, NULL, result,
273 blob1 != NULL, f2_exists,
274 label2, /* show local file's path, not a blob ID */
275 label2, GOT_DIFF_OUTPUT_UNIDIFF,
276 diff_context, outfile);
282 if (resultp && err == NULL)
285 free_err = got_diffreg_result_free(result);
286 if (free_err && err == NULL)
292 const struct got_error *
293 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
294 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
295 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
296 int ignore_whitespace, int force_text_diff, FILE *outfile)
298 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
299 sb2, label2, diff_algo, diff_context, ignore_whitespace,
300 force_text_diff, outfile);
303 static const struct got_error *
304 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
305 const char *label, mode_t mode, struct got_repository *repo,
306 got_diff_blob_cb cb, void *cb_arg)
308 const struct got_error *err;
309 struct got_blob_object *blob = NULL;
310 struct got_object *obj = NULL;
312 err = got_object_open(&obj, repo, id);
316 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
319 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
320 NULL, label, 0, mode, repo);
322 got_object_close(obj);
324 got_object_blob_close(blob);
328 static const struct got_error *
329 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
330 FILE *f1, FILE *f2, int fd1, int fd2,
331 const char *label1, const char *label2,
332 mode_t mode1, mode_t mode2, struct got_repository *repo,
333 got_diff_blob_cb cb, void *cb_arg)
335 const struct got_error *err;
336 struct got_object *obj1 = NULL;
337 struct got_object *obj2 = NULL;
338 struct got_blob_object *blob1 = NULL;
339 struct got_blob_object *blob2 = NULL;
341 err = got_object_open(&obj1, repo, id1);
345 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
346 err = got_error(GOT_ERR_OBJ_TYPE);
350 err = got_object_open(&obj2, repo, id2);
353 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
354 err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
362 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
366 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
370 got_object_close(obj1);
372 got_object_close(obj2);
374 got_object_blob_close(blob1);
376 got_object_blob_close(blob2);
380 static const struct got_error *
381 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
382 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
383 got_diff_blob_cb cb, void *cb_arg)
385 const struct got_error *err;
386 struct got_blob_object *blob = NULL;
387 struct got_object *obj = NULL;
389 err = got_object_open(&obj, repo, id);
393 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
396 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
399 got_object_close(obj);
401 got_object_blob_close(blob);
405 static const struct got_error *
406 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
407 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
408 void *cb_arg, int diff_content)
410 const struct got_error *err = NULL;
411 struct got_object *treeobj = NULL;
412 struct got_tree_object *tree = NULL;
414 err = got_object_open(&treeobj, repo, id);
418 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
419 err = got_error(GOT_ERR_OBJ_TYPE);
423 err = got_object_tree_open(&tree, repo, treeobj);
427 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
428 repo, cb, cb_arg, diff_content);
431 got_object_tree_close(tree);
433 got_object_close(treeobj);
437 static const struct got_error *
438 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
439 FILE *f1, FILE *f2, int fd1, int fd2,
440 const char *label1, const char *label2,
441 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
444 const struct got_error *err;
445 struct got_object *treeobj1 = NULL;
446 struct got_object *treeobj2 = NULL;
447 struct got_tree_object *tree1 = NULL;
448 struct got_tree_object *tree2 = NULL;
450 err = got_object_open(&treeobj1, repo, id1);
454 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
455 err = got_error(GOT_ERR_OBJ_TYPE);
459 err = got_object_open(&treeobj2, repo, id2);
463 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
464 err = got_error(GOT_ERR_OBJ_TYPE);
468 err = got_object_tree_open(&tree1, repo, treeobj1);
472 err = got_object_tree_open(&tree2, repo, treeobj2);
476 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
477 label1, label2, repo, cb, cb_arg, diff_content);
481 got_object_tree_close(tree1);
483 got_object_tree_close(tree2);
485 got_object_close(treeobj1);
487 got_object_close(treeobj2);
491 static const struct got_error *
492 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
493 FILE *f2, const char *label, struct got_repository *repo,
494 got_diff_blob_cb cb, void *cb_arg, int diff_content)
496 const struct got_error *err;
497 struct got_object *treeobj = NULL;
498 struct got_tree_object *tree = NULL;
500 err = got_object_open(&treeobj, repo, id);
504 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
505 err = got_error(GOT_ERR_OBJ_TYPE);
509 err = got_object_tree_open(&tree, repo, treeobj);
513 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
514 repo, cb, cb_arg, diff_content);
517 got_object_tree_close(tree);
519 got_object_close(treeobj);
523 static const struct got_error *
524 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
525 const char *label1, const char *label2, struct got_repository *repo,
526 got_diff_blob_cb cb, void *cb_arg)
532 static const struct got_error *
533 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
534 FILE *f1, FILE *f2, int fd1, int fd2,
535 const char *label1, const char *label2,
536 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
539 const struct got_error *err = NULL;
542 if (got_object_tree_entry_is_submodule(te1))
546 if (S_ISDIR(te1->mode))
547 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
548 label1, repo, cb, cb_arg, diff_content);
551 err = diff_deleted_blob(&te1->id, f1, fd1,
552 f2, label1, te1->mode, repo, cb, cb_arg);
554 err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 &te1->id, NULL, label1, NULL,
559 } else if (got_object_tree_entry_is_submodule(te2))
562 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
563 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
565 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
566 fd1, fd2, label1, label2, repo, cb, cb_arg,
568 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
569 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
571 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
572 (te2->mode & (S_IFLNK | S_IXUSR))) {
574 return diff_modified_blob(&te1->id, &te2->id,
575 f1, f2, fd1, fd2, label1, label2,
576 te1->mode, te2->mode, repo, cb, cb_arg);
578 return cb(cb_arg, NULL, NULL, NULL, NULL,
579 &te1->id, &te2->id, label1, label2,
580 te1->mode, te2->mode, repo);
587 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
591 static const struct got_error *
592 diff_entry_new_old(struct got_tree_entry *te2,
593 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
594 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
597 if (te1 != NULL) /* handled by diff_entry_old_new() */
600 if (got_object_tree_entry_is_submodule(te2))
603 if (S_ISDIR(te2->mode))
604 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
605 repo, cb, cb_arg, diff_content);
608 return diff_added_blob(&te2->id, f1, f2, fd2,
609 label2, te2->mode, repo, cb, cb_arg);
611 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
612 NULL, label2, 0, te2->mode, repo);
616 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
617 uint32_t add, uint32_t rm)
621 *maxlen = MAX(*maxlen, len);
625 *add_cols = MAX(*add_cols, d1);
629 *rm_cols = MAX(*rm_cols, d2);
632 const struct got_error *
633 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
634 struct got_blob_object *blob2, FILE *f1, FILE *f2,
635 struct got_object_id *id1, struct got_object_id *id2,
636 const char *label1, const char *label2,
637 mode_t mode1, mode_t mode2, struct got_repository *repo)
639 const struct got_error *err = NULL;
640 struct got_diffreg_result *result = NULL;
641 struct diff_result *r;
642 struct got_diff_changed_path *change = NULL;
643 struct got_diffstat_cb_arg *a = arg;
644 struct got_pathlist_entry *pe;
648 path = strdup(label2 ? label2 : label1);
650 return got_error_from_errno("malloc");
652 change = malloc(sizeof(*change));
653 if (change == NULL) {
654 err = got_error_from_errno("malloc");
660 change->status = GOT_STATUS_NO_CHANGE;
662 change->status = GOT_STATUS_ADD;
663 else if (id2 == NULL)
664 change->status = GOT_STATUS_DELETE;
666 if (got_object_id_cmp(id1, id2) != 0)
667 change->status = GOT_STATUS_MODIFY;
668 else if (mode1 != mode2)
669 change->status = GOT_STATUS_MODE_CHANGE;
673 err = got_opentemp_truncate(f1);
678 err = got_opentemp_truncate(f2);
684 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
690 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
696 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
701 for (i = 0, r = result->result; i < r->chunks.len; ++i) {
702 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
703 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
705 if (!isbin || a->force_text) {
706 struct diff_chunk *c;
709 c = diff_chunk_get(r, i);
710 clc = diff_chunk_get_left_count(c);
711 crc = diff_chunk_get_right_count(c);
715 else if (crc && !clc)
720 err = got_pathlist_append(a->paths, path, change);
724 pe = TAILQ_LAST(a->paths, got_pathlist_head);
725 diffstat_field_width(&a->max_path_len, &a->add_cols, &a->rm_cols,
726 pe->path_len, change->add, change->rm);
727 a->ins += change->add;
728 a->del += change->rm;
733 const struct got_error *free_err;
735 free_err = got_diffreg_result_free(result);
736 if (free_err && err == NULL)
746 const struct got_error *
747 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
748 struct got_blob_object *blob2, FILE *f1, FILE *f2,
749 struct got_object_id *id1, struct got_object_id *id2,
750 const char *label1, const char *label2,
751 mode_t mode1, mode_t mode2, struct got_repository *repo)
753 const struct got_error *err = NULL;
754 struct got_pathlist_head *paths = arg;
755 struct got_diff_changed_path *change = NULL;
758 path = strdup(label2 ? label2 : label1);
760 return got_error_from_errno("malloc");
762 change = malloc(sizeof(*change));
763 if (change == NULL) {
764 err = got_error_from_errno("malloc");
768 change->status = GOT_STATUS_NO_CHANGE;
770 change->status = GOT_STATUS_ADD;
771 else if (id2 == NULL)
772 change->status = GOT_STATUS_DELETE;
774 if (got_object_id_cmp(id1, id2) != 0)
775 change->status = GOT_STATUS_MODIFY;
776 else if (mode1 != mode2)
777 change->status = GOT_STATUS_MODE_CHANGE;
780 err = got_pathlist_append(paths, path, change);
789 const struct got_error *
790 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
791 FILE *f1, FILE *f2, int fd1, int fd2,
792 const char *label1, const char *label2,
793 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
796 const struct got_error *err = NULL;
797 struct got_tree_entry *te1 = NULL;
798 struct got_tree_entry *te2 = NULL;
799 char *l1 = NULL, *l2 = NULL;
800 int tidx1 = 0, tidx2 = 0;
803 te1 = got_object_tree_get_entry(tree1, 0);
804 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
806 return got_error_from_errno("asprintf");
809 te2 = got_object_tree_get_entry(tree2, 0);
810 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
812 return got_error_from_errno("asprintf");
817 struct got_tree_entry *te = NULL;
819 te = got_object_tree_find_entry(tree2,
824 if (te && asprintf(&l2, "%s%s%s", label2,
825 label2[0] ? "/" : "", te->name) == -1)
827 got_error_from_errno("asprintf");
829 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
830 l1, l2, repo, cb, cb_arg, diff_content);
836 struct got_tree_entry *te = NULL;
838 te = got_object_tree_find_entry(tree1,
842 if (asprintf(&l2, "%s%s%s", label2,
843 label2[0] ? "/" : "", te->name) == -1)
845 got_error_from_errno("asprintf");
847 if (asprintf(&l2, "%s%s%s", label2,
848 label2[0] ? "/" : "", te2->name) == -1)
850 got_error_from_errno("asprintf");
852 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
853 repo, cb, cb_arg, diff_content);
862 te1 = got_object_tree_get_entry(tree1, tidx1);
864 asprintf(&l1, "%s%s%s", label1,
865 label1[0] ? "/" : "", te1->name) == -1)
866 return got_error_from_errno("asprintf");
872 te2 = got_object_tree_get_entry(tree2, tidx2);
874 asprintf(&l2, "%s%s%s", label2,
875 label2[0] ? "/" : "", te2->name) == -1)
876 return got_error_from_errno("asprintf");
878 } while (te1 || te2);
883 const struct got_error *
884 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
885 FILE *f1, FILE *f2, int fd1, int fd2,
886 struct got_object_id *id1, struct got_object_id *id2,
887 const char *label1, const char *label2,
888 enum got_diff_algorithm diff_algo, int diff_context,
889 int ignore_whitespace, int force_text_diff,
890 struct got_repository *repo, FILE *outfile)
892 const struct got_error *err;
893 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
895 if (id1 == NULL && id2 == NULL)
896 return got_error(GOT_ERR_NO_OBJ);
899 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
904 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
908 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
909 diff_algo, diff_context, ignore_whitespace, force_text_diff,
913 got_object_blob_close(blob1);
915 got_object_blob_close(blob2);
919 static const struct got_error *
920 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
921 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
922 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
924 const struct got_error *err = NULL;
925 struct got_pathlist_entry *pe;
926 struct got_object_id *id1 = NULL, *id2 = NULL;
927 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
928 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
930 TAILQ_FOREACH(pe, paths, entry) {
931 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
932 mode_t mode1 = 0, mode2 = 0;
939 got_object_tree_close(subtree1);
943 got_object_tree_close(subtree2);
947 got_object_blob_close(blob1);
951 got_object_blob_close(blob2);
955 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
957 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
959 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
961 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
963 if (id1 == NULL && id2 == NULL) {
964 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
968 err = got_object_get_type(&type1, repo, id1);
973 err = got_object_get_type(&type2, repo, id2);
977 if (type1 == GOT_OBJ_TYPE_ANY &&
978 type2 == GOT_OBJ_TYPE_ANY) {
979 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
981 } else if (type1 != GOT_OBJ_TYPE_ANY &&
982 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
983 err = got_error(GOT_ERR_OBJ_TYPE);
987 if (type1 == GOT_OBJ_TYPE_BLOB ||
988 type2 == GOT_OBJ_TYPE_BLOB) {
990 err = got_object_open_as_blob(&blob1, repo,
996 err = got_object_open_as_blob(&blob2, repo,
1001 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1002 id1 ? pe->path : "/dev/null",
1003 id2 ? pe->path : "/dev/null",
1004 mode1, mode2, repo);
1007 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1008 type2 == GOT_OBJ_TYPE_TREE) {
1010 err = got_object_open_as_tree(&subtree1, repo,
1016 err = got_object_open_as_tree(&subtree2, repo,
1021 err = got_diff_tree(subtree1, subtree2, f1, f2,
1023 id1 ? pe->path : "/dev/null",
1024 id2 ? pe->path : "/dev/null",
1025 repo, cb, cb_arg, 1);
1029 err = got_error(GOT_ERR_OBJ_TYPE);
1037 got_object_tree_close(subtree1);
1039 got_object_tree_close(subtree2);
1041 got_object_blob_close(blob1);
1043 got_object_blob_close(blob2);
1047 static const struct got_error *
1048 show_object_id(struct got_diff_line **lines, size_t *nlines,
1049 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1051 const struct got_error *err;
1055 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1057 return got_error_from_errno("fprintf");
1059 if (lines != NULL && *lines != NULL) {
1061 err = add_line_metadata(lines, nlines, 0,
1062 GOT_DIFF_LINE_META);
1066 outoff = (*lines)[*nlines - 1].offset;
1069 err = add_line_metadata(lines, nlines, outoff,
1070 GOT_DIFF_LINE_META);
1078 static const struct got_error *
1079 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1080 FILE *f1, FILE *f2, int fd1, int fd2,
1081 struct got_object_id *id1, struct got_object_id *id2,
1082 struct got_pathlist_head *paths, const char *label1, const char *label2,
1083 int diff_context, int ignore_whitespace, int force_text_diff,
1084 struct got_repository *repo, FILE *outfile,
1085 enum got_diff_algorithm diff_algo)
1087 const struct got_error *err;
1088 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1089 struct got_diff_blob_output_unidiff_arg arg;
1090 int want_linemeta = (lines != NULL && *lines != NULL);
1092 if (id1 == NULL && id2 == NULL)
1093 return got_error(GOT_ERR_NO_OBJ);
1096 err = got_object_open_as_tree(&tree1, repo, id1);
1101 err = got_object_open_as_tree(&tree2, repo, id2);
1106 arg.diff_algo = diff_algo;
1107 arg.diff_context = diff_context;
1108 arg.ignore_whitespace = ignore_whitespace;
1109 arg.force_text_diff = force_text_diff;
1110 arg.outfile = outfile;
1111 if (want_linemeta) {
1113 arg.nlines = *nlines;
1118 if (paths == NULL || TAILQ_EMPTY(paths)) {
1119 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
1120 label1, label2, repo,
1121 got_diff_blob_output_unidiff, &arg, 1);
1123 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1124 got_diff_blob_output_unidiff, &arg);
1126 if (want_linemeta) {
1127 *lines = arg.lines; /* was likely re-allocated */
1128 *nlines = arg.nlines;
1132 got_object_tree_close(tree1);
1134 got_object_tree_close(tree2);
1138 const struct got_error *
1139 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1140 FILE *f1, FILE *f2, int fd1, int fd2,
1141 struct got_object_id *id1, struct got_object_id *id2,
1142 struct got_pathlist_head *paths, const char *label1, const char *label2,
1143 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1144 int force_text_diff, struct got_repository *repo, FILE *outfile)
1146 const struct got_error *err;
1149 if (id1 == NULL && id2 == NULL)
1150 return got_error(GOT_ERR_NO_OBJ);
1153 err = got_object_id_str(&idstr, id1);
1156 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1162 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1169 err = got_object_id_str(&idstr, id2);
1172 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1178 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1184 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1185 paths, label1, label2, diff_context, ignore_whitespace,
1186 force_text_diff, repo, outfile, diff_algo);
1192 const struct got_error *
1193 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1194 FILE *f1, FILE *f2, int fd1, int fd2,
1195 struct got_object_id *id1, struct got_object_id *id2,
1196 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1197 int diff_context, int ignore_whitespace, int force_text_diff,
1198 struct got_repository *repo, FILE *outfile)
1200 const struct got_error *err;
1201 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1205 return got_error(GOT_ERR_NO_OBJ);
1208 err = got_object_open_as_commit(&commit1, repo, id1);
1211 err = got_object_id_str(&idstr, id1);
1214 err = show_object_id(lines, nlines, "commit", '-', idstr,
1221 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1227 err = got_object_open_as_commit(&commit2, repo, id2);
1231 err = got_object_id_str(&idstr, id2);
1234 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1238 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1239 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1240 got_object_commit_get_tree_id(commit2), paths, "", "",
1241 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1245 got_object_commit_close(commit1);
1247 got_object_commit_close(commit2);
1252 const struct got_error *
1253 got_diff_files(struct got_diffreg_result **resultp,
1254 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1255 const char *label2, int diff_context, int ignore_whitespace,
1256 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1258 const struct got_error *err = NULL;
1259 struct got_diffreg_result *diffreg_result = NULL;
1265 fprintf(outfile, "file - %s\n",
1266 f1_exists ? label1 : "/dev/null");
1267 fprintf(outfile, "file + %s\n",
1268 f2_exists ? label2 : "/dev/null");
1271 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1272 ignore_whitespace, force_text_diff);
1277 err = got_diffreg_output(NULL, NULL, diffreg_result,
1278 f1_exists, f2_exists, label1, label2,
1279 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1285 if (resultp && err == NULL)
1286 *resultp = diffreg_result;
1287 else if (diffreg_result) {
1288 const struct got_error *free_err;
1289 free_err = got_diffreg_result_free(diffreg_result);
1290 if (free_err && err == NULL)