2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
32 #include "got_opentemp.h"
34 #include "got_cancel.h"
35 #include "got_worktree.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"
42 static const struct got_error *
43 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
47 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
49 return got_error_from_errno("reallocarray");
51 (*line_offsets)[*nlines] = off;
56 static const struct got_error *
57 diff_blobs(off_t **line_offsets, size_t *nlines,
58 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
59 struct got_blob_object *blob2,
60 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61 int diff_context, int ignore_whitespace, FILE *outfile)
63 const struct got_error *err = NULL, *free_err;
64 FILE *f1 = NULL, *f2 = NULL;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
67 char *idstr1 = NULL, *idstr2 = NULL;
69 struct got_diffreg_result *result;
73 if (line_offsets && *line_offsets && *nlines > 0)
74 outoff = (*line_offsets)[*nlines - 1];
82 return got_error_from_errno("got_opentemp");
88 err = got_error_from_errno("got_opentemp");
96 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
102 idstr1 = "/dev/null";
106 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
112 idstr2 = "/dev/null";
115 char *modestr1 = NULL, *modestr2 = NULL;
117 if (mode1 && mode1 != mode2) {
121 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122 if (asprintf(&modestr1, " (mode %o)",
123 mode1 & modebits) == -1) {
124 err = got_error_from_errno("asprintf");
128 if (mode2 && mode1 != mode2) {
132 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133 if (asprintf(&modestr2, " (mode %o)",
134 mode2 & modebits) == -1) {
135 err = got_error_from_errno("asprintf");
139 n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 modestr1 ? modestr1 : "");
142 err = got_error_from_errno("fprintf");
147 err = add_line_offset(line_offsets, nlines, outoff);
152 n = fprintf(outfile, "blob + %s%s\n", idstr2,
153 modestr2 ? modestr2 : "");
155 err = got_error_from_errno("fprintf");
160 err = add_line_offset(line_offsets, nlines, outoff);
168 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
174 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
175 label1 ? label1 : idstr1,
176 label2 ? label2 : idstr2,
177 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
182 if (resultp && err == NULL)
185 free_err = got_diffreg_result_free(result);
186 if (free_err && err == NULL)
190 if (f1 && fclose(f1) != 0 && err == NULL)
191 err = got_error_from_errno("fclose");
192 if (f2 && fclose(f2) != 0 && err == NULL)
193 err = got_error_from_errno("fclose");
197 const struct got_error *
198 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
199 struct got_blob_object *blob2, struct got_object_id *id1,
200 struct got_object_id *id2, const char *label1, const char *label2,
201 mode_t mode1, mode_t mode2, struct got_repository *repo)
203 struct got_diff_blob_output_unidiff_arg *a = arg;
205 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
206 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
207 a->ignore_whitespace, a->outfile);
210 const struct got_error *
211 got_diff_blob(off_t **line_offsets, size_t *nlines,
212 struct got_blob_object *blob1, struct got_blob_object *blob2,
213 const char *label1, const char *label2, int diff_context,
214 int ignore_whitespace, FILE *outfile)
216 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
217 label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
220 static const struct got_error *
221 diff_blob_file(struct got_diffreg_result **resultp,
222 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
223 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
225 const struct got_error *err = NULL, *free_err;
227 char hex1[SHA1_DIGEST_STRING_LENGTH];
230 struct got_diffreg_result *result = NULL;
239 return got_error_from_errno("got_opentemp");
240 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
241 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
246 idstr1 = "/dev/null";
250 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
251 fprintf(outfile, "file + %s\n",
252 f2 == NULL ? "/dev/null" : label2);
255 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
261 err = got_diffreg_output(NULL, NULL, result, f1, f2,
262 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
268 if (resultp && err == NULL)
271 free_err = got_diffreg_result_free(result);
272 if (free_err && err == NULL)
276 if (f1 && fclose(f1) != 0 && err == NULL)
277 err = got_error_from_errno("fclose");
281 const struct got_error *
282 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
283 FILE *f2, size_t size2, const char *label2, int diff_context,
284 int ignore_whitespace, FILE *outfile)
286 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
287 diff_context, ignore_whitespace, outfile);
290 const struct got_error *
291 got_diff_blob_prepared_file(struct got_diffreg_result **resultp,
292 struct diff_data *data1, struct got_blob_object *blob1,
293 struct diff_data *data2, FILE *f2, char *p2, size_t size2,
294 const struct diff_config *cfg, int ignore_whitespace)
296 const struct got_error *err = NULL, *free_err;
298 char hex1[SHA1_DIGEST_STRING_LENGTH];
299 char *idstr1 = NULL, *p1 = NULL;
301 struct got_diffreg_result *result = NULL;
310 return got_error_from_errno("got_opentemp");
311 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
312 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
317 idstr1 = "/dev/null";
320 err = got_diff_prepare_file(&f1, &p1, &f1_created, &size,
321 data1, cfg, ignore_whitespace);
325 err = got_diffreg_prepared_files(&result, cfg, data1, f1,
326 p1, size1, data2, f2, p2, size2);
334 free_err = got_diffreg_result_free_left(result);
336 free_err = got_diffreg_close(f1, p1, size1, NULL,
338 if (free_err && err == NULL)
344 static const struct got_error *
345 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
346 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
348 const struct got_error *err;
349 struct got_blob_object *blob = NULL;
350 struct got_object *obj = NULL;
352 err = got_object_open(&obj, repo, id);
356 err = got_object_blob_open(&blob, repo, obj, 8192);
359 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
361 got_object_close(obj);
363 got_object_blob_close(blob);
367 static const struct got_error *
368 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
369 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
370 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
372 const struct got_error *err;
373 struct got_object *obj1 = NULL;
374 struct got_object *obj2 = NULL;
375 struct got_blob_object *blob1 = NULL;
376 struct got_blob_object *blob2 = NULL;
378 err = got_object_open(&obj1, repo, id1);
381 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
382 err = got_error(GOT_ERR_OBJ_TYPE);
386 err = got_object_open(&obj2, repo, id2);
389 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
390 err = got_error(GOT_ERR_BAD_OBJ_DATA);
394 err = got_object_blob_open(&blob1, repo, obj1, 8192);
398 err = got_object_blob_open(&blob2, repo, obj2, 8192);
402 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
406 got_object_close(obj1);
408 got_object_close(obj2);
410 got_object_blob_close(blob1);
412 got_object_blob_close(blob2);
416 static const struct got_error *
417 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
418 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
420 const struct got_error *err;
421 struct got_blob_object *blob = NULL;
422 struct got_object *obj = NULL;
424 err = got_object_open(&obj, repo, id);
428 err = got_object_blob_open(&blob, repo, obj, 8192);
431 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
433 got_object_close(obj);
435 got_object_blob_close(blob);
439 static const struct got_error *
440 diff_added_tree(struct got_object_id *id, const char *label,
441 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
444 const struct got_error *err = NULL;
445 struct got_object *treeobj = NULL;
446 struct got_tree_object *tree = NULL;
448 err = got_object_open(&treeobj, repo, id);
452 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
453 err = got_error(GOT_ERR_OBJ_TYPE);
457 err = got_object_tree_open(&tree, repo, treeobj);
461 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
465 got_object_tree_close(tree);
467 got_object_close(treeobj);
471 static const struct got_error *
472 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
473 const char *label1, const char *label2, struct got_repository *repo,
474 got_diff_blob_cb cb, void *cb_arg, int diff_content)
476 const struct got_error *err;
477 struct got_object *treeobj1 = NULL;
478 struct got_object *treeobj2 = NULL;
479 struct got_tree_object *tree1 = NULL;
480 struct got_tree_object *tree2 = NULL;
482 err = got_object_open(&treeobj1, repo, id1);
486 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
487 err = got_error(GOT_ERR_OBJ_TYPE);
491 err = got_object_open(&treeobj2, repo, id2);
495 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
496 err = got_error(GOT_ERR_OBJ_TYPE);
500 err = got_object_tree_open(&tree1, repo, treeobj1);
504 err = got_object_tree_open(&tree2, repo, treeobj2);
508 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
513 got_object_tree_close(tree1);
515 got_object_tree_close(tree2);
517 got_object_close(treeobj1);
519 got_object_close(treeobj2);
523 static const struct got_error *
524 diff_deleted_tree(struct got_object_id *id, const char *label,
525 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
528 const struct got_error *err;
529 struct got_object *treeobj = NULL;
530 struct got_tree_object *tree = NULL;
532 err = got_object_open(&treeobj, repo, id);
536 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
537 err = got_error(GOT_ERR_OBJ_TYPE);
541 err = got_object_tree_open(&tree, repo, treeobj);
545 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
549 got_object_tree_close(tree);
551 got_object_close(treeobj);
555 static const struct got_error *
556 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
557 const char *label1, const char *label2, struct got_repository *repo,
558 got_diff_blob_cb cb, void *cb_arg)
564 static const struct got_error *
565 diff_entry_old_new(struct got_tree_entry *te1,
566 struct got_tree_entry *te2, const char *label1, const char *label2,
567 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
570 const struct got_error *err = NULL;
573 if (got_object_tree_entry_is_submodule(te1))
577 if (S_ISDIR(te1->mode))
578 err = diff_deleted_tree(&te1->id, label1, repo,
579 cb, cb_arg, diff_content);
582 err = diff_deleted_blob(&te1->id, label1,
583 te1->mode, repo, cb, cb_arg);
585 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
586 label1, NULL, te1->mode, 0, repo);
589 } else if (got_object_tree_entry_is_submodule(te2))
592 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
593 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
595 return diff_modified_tree(&te1->id, &te2->id,
596 label1, label2, repo, cb, cb_arg, diff_content);
597 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
598 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
600 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
601 (te2->mode & (S_IFLNK | S_IXUSR))) {
603 return diff_modified_blob(&te1->id, &te2->id,
604 label1, label2, te1->mode, te2->mode,
607 return cb(cb_arg, NULL, NULL, &te1->id,
608 &te2->id, label1, label2, te1->mode,
616 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
620 static const struct got_error *
621 diff_entry_new_old(struct got_tree_entry *te2,
622 struct got_tree_entry *te1, const char *label2,
623 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
626 if (te1 != NULL) /* handled by diff_entry_old_new() */
629 if (got_object_tree_entry_is_submodule(te2))
632 if (S_ISDIR(te2->mode))
633 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
637 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
640 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
644 const struct got_error *
645 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
646 struct got_blob_object *blob2, struct got_object_id *id1,
647 struct got_object_id *id2, const char *label1, const char *label2,
648 mode_t mode1, mode_t mode2, struct got_repository *repo)
650 const struct got_error *err = NULL;
651 struct got_pathlist_head *paths = arg;
652 struct got_diff_changed_path *change = NULL;
655 path = strdup(label2 ? label2 : label1);
657 return got_error_from_errno("malloc");
659 change = malloc(sizeof(*change));
660 if (change == NULL) {
661 err = got_error_from_errno("malloc");
665 change->status = GOT_STATUS_NO_CHANGE;
667 change->status = GOT_STATUS_ADD;
668 else if (id2 == NULL)
669 change->status = GOT_STATUS_DELETE;
671 if (got_object_id_cmp(id1, id2) != 0)
672 change->status = GOT_STATUS_MODIFY;
673 else if (mode1 != mode2)
674 change->status = GOT_STATUS_MODE_CHANGE;
677 err = got_pathlist_insert(NULL, paths, path, change);
686 const struct got_error *
687 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
688 const char *label1, const char *label2, struct got_repository *repo,
689 got_diff_blob_cb cb, void *cb_arg, int diff_content)
691 const struct got_error *err = NULL;
692 struct got_tree_entry *te1 = NULL;
693 struct got_tree_entry *te2 = NULL;
694 char *l1 = NULL, *l2 = NULL;
695 int tidx1 = 0, tidx2 = 0;
698 te1 = got_object_tree_get_entry(tree1, 0);
699 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
701 return got_error_from_errno("asprintf");
704 te2 = got_object_tree_get_entry(tree2, 0);
705 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
707 return got_error_from_errno("asprintf");
712 struct got_tree_entry *te = NULL;
714 te = got_object_tree_find_entry(tree2,
719 if (te && asprintf(&l2, "%s%s%s", label2,
720 label2[0] ? "/" : "", te->name) == -1)
722 got_error_from_errno("asprintf");
724 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
725 cb_arg, diff_content);
731 struct got_tree_entry *te = NULL;
733 te = got_object_tree_find_entry(tree1,
737 if (asprintf(&l2, "%s%s%s", label2,
738 label2[0] ? "/" : "", te->name) == -1)
740 got_error_from_errno("asprintf");
742 if (asprintf(&l2, "%s%s%s", label2,
743 label2[0] ? "/" : "", te2->name) == -1)
745 got_error_from_errno("asprintf");
747 err = diff_entry_new_old(te2, te, l2, repo,
748 cb, cb_arg, diff_content);
757 te1 = got_object_tree_get_entry(tree1, tidx1);
759 asprintf(&l1, "%s%s%s", label1,
760 label1[0] ? "/" : "", te1->name) == -1)
761 return got_error_from_errno("asprintf");
767 te2 = got_object_tree_get_entry(tree2, tidx2);
769 asprintf(&l2, "%s%s%s", label2,
770 label2[0] ? "/" : "", te2->name) == -1)
771 return got_error_from_errno("asprintf");
773 } while (te1 || te2);
778 const struct got_error *
779 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
780 struct got_object_id *id1, struct got_object_id *id2,
781 const char *label1, const char *label2, int diff_context,
782 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
784 const struct got_error *err;
785 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
787 if (id1 == NULL && id2 == NULL)
788 return got_error(GOT_ERR_NO_OBJ);
791 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
796 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
800 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
801 label1, label2, diff_context, ignore_whitespace, outfile);
804 got_object_blob_close(blob1);
806 got_object_blob_close(blob2);
810 const struct got_error *
811 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
812 struct got_object_id *id1, struct got_object_id *id2,
813 char *label1, char *label2, int diff_context, int ignore_whitespace,
814 struct got_repository *repo, FILE *outfile)
816 const struct got_error *err;
817 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
818 struct got_diff_blob_output_unidiff_arg arg;
819 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
821 if (id1 == NULL && id2 == NULL)
822 return got_error(GOT_ERR_NO_OBJ);
825 err = got_object_open_as_tree(&tree1, repo, id1);
830 err = got_object_open_as_tree(&tree2, repo, id2);
834 arg.diff_context = diff_context;
835 arg.ignore_whitespace = ignore_whitespace;
836 arg.outfile = outfile;
837 if (want_lineoffsets) {
838 arg.line_offsets = *line_offsets;
839 arg.nlines = *nlines;
841 arg.line_offsets = NULL;
844 err = got_diff_tree(tree1, tree2, label1, label2, repo,
845 got_diff_blob_output_unidiff, &arg, 1);
847 if (want_lineoffsets) {
848 *line_offsets = arg.line_offsets; /* was likely re-allocated */
849 *nlines = arg.nlines;
853 got_object_tree_close(tree1);
855 got_object_tree_close(tree2);
859 const struct got_error *
860 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
861 struct got_object_id *id1, struct got_object_id *id2,
862 int diff_context, int ignore_whitespace,
863 struct got_repository *repo, FILE *outfile)
865 const struct got_error *err;
866 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
869 return got_error(GOT_ERR_NO_OBJ);
872 err = got_object_open_as_commit(&commit1, repo, id1);
877 err = got_object_open_as_commit(&commit2, repo, id2);
881 err = got_diff_objects_as_trees(line_offsets, nlines,
882 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
883 got_object_commit_get_tree_id(commit2), "", "", diff_context,
884 ignore_whitespace, repo, outfile);
887 got_object_commit_close(commit1);
889 got_object_commit_close(commit2);
893 const struct got_error *
894 got_diff_files(struct got_diffreg_result **resultp,
895 FILE *f1, const char *label1, FILE *f2, const char *label2,
896 int diff_context, int ignore_whitespace, FILE *outfile)
898 const struct got_error *err = NULL;
899 struct got_diffreg_result *diffreg_result = NULL;
905 fprintf(outfile, "file - %s\n",
906 f1 == NULL ? "/dev/null" : label1);
907 fprintf(outfile, "file + %s\n",
908 f2 == NULL ? "/dev/null" : label2);
911 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
917 err = got_diffreg_output(NULL, NULL, diffreg_result,
918 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
919 diff_context, outfile);
925 if (resultp && err == NULL)
926 *resultp = diffreg_result;
927 else if (diffreg_result) {
928 const struct got_error *free_err;
929 free_err = got_diffreg_result_free(diffreg_result);
930 if (free_err && err == NULL)