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"
31 #include "got_opentemp.h"
33 #include "got_cancel.h"
34 #include "got_worktree.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"
41 static const struct got_error *
42 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
43 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
44 int diff_context, int ignore_whitespace, FILE *outfile,
45 struct got_diff_changes *changes)
47 struct got_diff_state ds;
48 struct got_diff_args args;
49 const struct got_error *err = NULL;
50 FILE *f1 = NULL, *f2 = NULL;
51 char hex1[SHA1_DIGEST_STRING_LENGTH];
52 char hex2[SHA1_DIGEST_STRING_LENGTH];
53 char *idstr1 = NULL, *idstr2 = NULL;
60 return got_error_from_errno("got_opentemp");
67 err = got_error_from_errno("got_opentemp");
76 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
77 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
86 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
94 memset(&ds, 0, sizeof(ds));
95 /* XXX should stat buffers be passed in args instead of ds? */
96 ds.stb1.st_mode = S_IFREG;
98 ds.stb1.st_size = size1;
99 ds.stb1.st_mtime = 0; /* XXX */
101 ds.stb2.st_mode = S_IFREG;
103 ds.stb2.st_size = size2;
104 ds.stb2.st_mtime = 0; /* XXX */
106 memset(&args, 0, sizeof(args));
107 args.diff_format = D_UNIFIED;
108 args.label[0] = label1 ? label1 : idstr1;
109 args.label[1] = label2 ? label2 : idstr2;
110 args.diff_context = diff_context;
111 flags |= D_PROTOTYPE;
112 if (ignore_whitespace)
113 flags |= D_IGNOREBLANKS;
116 char *modestr1 = NULL, *modestr2 = NULL;
118 if (mode1 && mode1 != mode2) {
122 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
123 if (asprintf(&modestr1, " (mode %o)",
124 mode1 & modebits) == -1) {
125 err = got_error_from_errno("asprintf");
129 if (mode2 && mode1 != mode2) {
133 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
134 if (asprintf(&modestr2, " (mode %o)",
135 mode2 & modebits) == -1) {
136 err = got_error_from_errno("asprintf");
140 fprintf(outfile, "blob - %s%s\n", idstr1,
141 modestr1 ? modestr1 : "");
142 fprintf(outfile, "blob + %s%s\n", idstr2,
143 modestr2 ? modestr2 : "");
147 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
148 got_diff_state_free(&ds);
150 if (f1 && fclose(f1) != 0 && err == NULL)
151 err = got_error_from_errno("fclose");
152 if (f2 && fclose(f2) != 0 && err == NULL)
153 err = got_error_from_errno("fclose");
157 const struct got_error *
158 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
159 struct got_blob_object *blob2, struct got_object_id *id1,
160 struct got_object_id *id2, const char *label1, const char *label2,
161 mode_t mode1, mode_t mode2, struct got_repository *repo)
163 struct got_diff_blob_output_unidiff_arg *a = arg;
165 return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
166 a->diff_context, a->ignore_whitespace, a->outfile, NULL);
169 const struct got_error *
170 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
171 const char *label1, const char *label2, int diff_context,
172 int ignore_whitespace, FILE *outfile)
174 return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
175 ignore_whitespace, outfile, NULL);
178 static const struct got_error *
179 alloc_changes(struct got_diff_changes **changes)
181 *changes = calloc(1, sizeof(**changes));
182 if (*changes == NULL)
183 return got_error_from_errno("calloc");
184 SIMPLEQ_INIT(&(*changes)->entries);
188 static const struct got_error *
189 diff_blob_file(struct got_diff_changes **changes,
190 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
191 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
193 struct got_diff_state ds;
194 struct got_diff_args args;
195 const struct got_error *err = NULL;
197 char hex1[SHA1_DIGEST_STRING_LENGTH];
209 return got_error_from_errno("got_opentemp");
210 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
211 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
217 idstr1 = "/dev/null";
223 memset(&ds, 0, sizeof(ds));
224 /* XXX should stat buffers be passed in args instead of ds? */
225 ds.stb1.st_mode = S_IFREG;
227 ds.stb1.st_size = size1;
228 ds.stb1.st_mtime = 0; /* XXX */
230 ds.stb2.st_mode = S_IFREG;
231 ds.stb2.st_size = size2;
232 ds.stb2.st_mtime = 0; /* XXX */
234 memset(&args, 0, sizeof(args));
235 args.diff_format = D_UNIFIED;
236 args.label[0] = label2;
237 args.label[1] = label2;
238 args.diff_context = diff_context;
239 flags |= D_PROTOTYPE;
240 if (ignore_whitespace)
241 flags |= D_IGNOREBLANKS;
244 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
245 fprintf(outfile, "file + %s\n",
246 f2 == NULL ? "/dev/null" : label2);
249 err = alloc_changes(changes);
253 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
254 changes ? *changes : NULL);
255 got_diff_state_free(&ds);
257 if (f1 && fclose(f1) != 0 && err == NULL)
258 err = got_error_from_errno("fclose");
262 const struct got_error *
263 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
264 FILE *f2, size_t size2, const char *label2, int diff_context,
265 int ignore_whitespace, FILE *outfile)
267 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
268 diff_context, ignore_whitespace, outfile);
271 const struct got_error *
272 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
273 struct got_blob_object *blob1, FILE *f2, size_t size2)
275 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
279 const struct got_error *
280 got_diff_blob_lines_changed(struct got_diff_changes **changes,
281 struct got_blob_object *blob1, struct got_blob_object *blob2)
283 const struct got_error *err = NULL;
285 err = alloc_changes(changes);
289 err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
291 got_diff_free_changes(*changes);
298 got_diff_free_changes(struct got_diff_changes *changes)
300 struct got_diff_change *change;
301 while (!SIMPLEQ_EMPTY(&changes->entries)) {
302 change = SIMPLEQ_FIRST(&changes->entries);
303 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
309 static const struct got_error *
310 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
311 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
313 const struct got_error *err;
314 struct got_blob_object *blob = NULL;
315 struct got_object *obj = NULL;
317 err = got_object_open(&obj, repo, id);
321 err = got_object_blob_open(&blob, repo, obj, 8192);
324 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
326 got_object_close(obj);
328 got_object_blob_close(blob);
332 static const struct got_error *
333 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
334 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
335 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
337 const struct got_error *err;
338 struct got_object *obj1 = NULL;
339 struct got_object *obj2 = NULL;
340 struct got_blob_object *blob1 = NULL;
341 struct got_blob_object *blob2 = NULL;
343 err = got_object_open(&obj1, repo, id1);
346 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
347 err = got_error(GOT_ERR_OBJ_TYPE);
351 err = got_object_open(&obj2, repo, id2);
354 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
355 err = got_error(GOT_ERR_BAD_OBJ_DATA);
359 err = got_object_blob_open(&blob1, repo, obj1, 8192);
363 err = got_object_blob_open(&blob2, repo, obj2, 8192);
367 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
371 got_object_close(obj1);
373 got_object_close(obj2);
375 got_object_blob_close(blob1);
377 got_object_blob_close(blob2);
381 static const struct got_error *
382 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
383 struct got_repository *repo, 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);
396 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
398 got_object_close(obj);
400 got_object_blob_close(blob);
404 static const struct got_error *
405 diff_added_tree(struct got_object_id *id, const char *label,
406 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
409 const struct got_error *err = NULL;
410 struct got_object *treeobj = NULL;
411 struct got_tree_object *tree = NULL;
413 err = got_object_open(&treeobj, repo, id);
417 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
418 err = got_error(GOT_ERR_OBJ_TYPE);
422 err = got_object_tree_open(&tree, repo, treeobj);
426 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
430 got_object_tree_close(tree);
432 got_object_close(treeobj);
436 static const struct got_error *
437 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
438 const char *label1, const char *label2, struct got_repository *repo,
439 got_diff_blob_cb cb, void *cb_arg, int diff_content)
441 const struct got_error *err;
442 struct got_object *treeobj1 = NULL;
443 struct got_object *treeobj2 = NULL;
444 struct got_tree_object *tree1 = NULL;
445 struct got_tree_object *tree2 = NULL;
447 err = got_object_open(&treeobj1, repo, id1);
451 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
452 err = got_error(GOT_ERR_OBJ_TYPE);
456 err = got_object_open(&treeobj2, repo, id2);
460 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
461 err = got_error(GOT_ERR_OBJ_TYPE);
465 err = got_object_tree_open(&tree1, repo, treeobj1);
469 err = got_object_tree_open(&tree2, repo, treeobj2);
473 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
478 got_object_tree_close(tree1);
480 got_object_tree_close(tree2);
482 got_object_close(treeobj1);
484 got_object_close(treeobj2);
488 static const struct got_error *
489 diff_deleted_tree(struct got_object_id *id, const char *label,
490 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
493 const struct got_error *err;
494 struct got_object *treeobj = NULL;
495 struct got_tree_object *tree = NULL;
497 err = got_object_open(&treeobj, repo, id);
501 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
502 err = got_error(GOT_ERR_OBJ_TYPE);
506 err = got_object_tree_open(&tree, repo, treeobj);
510 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
514 got_object_tree_close(tree);
516 got_object_close(treeobj);
520 static const struct got_error *
521 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
522 const char *label1, const char *label2, struct got_repository *repo,
523 got_diff_blob_cb cb, void *cb_arg)
529 static const struct got_error *
530 diff_entry_old_new(struct got_tree_entry *te1,
531 struct got_tree_entry *te2, const char *label1, const char *label2,
532 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
535 const struct got_error *err = NULL;
538 if (got_object_tree_entry_is_submodule(te1))
542 if (S_ISDIR(te1->mode))
543 err = diff_deleted_tree(&te1->id, label1, repo,
544 cb, cb_arg, diff_content);
547 err = diff_deleted_blob(&te1->id, label1,
548 te1->mode, repo, cb, cb_arg);
550 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
551 label1, NULL, te1->mode, 0, repo);
554 } else if (got_object_tree_entry_is_submodule(te2))
557 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
558 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
560 return diff_modified_tree(&te1->id, &te2->id,
561 label1, label2, repo, cb, cb_arg, diff_content);
562 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
563 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
565 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
566 (te2->mode & (S_IFLNK | S_IXUSR))) {
568 return diff_modified_blob(&te1->id, &te2->id,
569 label1, label2, te1->mode, te2->mode,
572 return cb(cb_arg, NULL, NULL, &te1->id,
573 &te2->id, label1, label2, te1->mode,
581 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
585 static const struct got_error *
586 diff_entry_new_old(struct got_tree_entry *te2,
587 struct got_tree_entry *te1, const char *label2,
588 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
591 if (te1 != NULL) /* handled by diff_entry_old_new() */
594 if (got_object_tree_entry_is_submodule(te2))
597 if (S_ISDIR(te2->mode))
598 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
602 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
605 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
609 const struct got_error *
610 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
611 struct got_blob_object *blob2, struct got_object_id *id1,
612 struct got_object_id *id2, const char *label1, const char *label2,
613 mode_t mode1, mode_t mode2, struct got_repository *repo)
615 const struct got_error *err = NULL;
616 struct got_pathlist_head *paths = arg;
617 struct got_diff_changed_path *change = NULL;
620 path = strdup(label2 ? label2 : label1);
622 return got_error_from_errno("malloc");
624 change = malloc(sizeof(*change));
625 if (change == NULL) {
626 err = got_error_from_errno("malloc");
630 change->status = GOT_STATUS_NO_CHANGE;
632 change->status = GOT_STATUS_ADD;
633 else if (id2 == NULL)
634 change->status = GOT_STATUS_DELETE;
636 if (got_object_id_cmp(id1, id2) != 0)
637 change->status = GOT_STATUS_MODIFY;
638 else if (mode1 != mode2)
639 change->status = GOT_STATUS_MODE_CHANGE;
642 err = got_pathlist_insert(NULL, paths, path, change);
651 const struct got_error *
652 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
653 const char *label1, const char *label2, struct got_repository *repo,
654 got_diff_blob_cb cb, void *cb_arg, int diff_content)
656 const struct got_error *err = NULL;
657 struct got_tree_entry *te1 = NULL;
658 struct got_tree_entry *te2 = NULL;
659 char *l1 = NULL, *l2 = NULL;
660 int tidx1 = 0, tidx2 = 0;
663 te1 = got_object_tree_get_entry(tree1, 0);
664 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
666 return got_error_from_errno("asprintf");
669 te2 = got_object_tree_get_entry(tree2, 0);
670 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
672 return got_error_from_errno("asprintf");
677 struct got_tree_entry *te = NULL;
679 te = got_object_tree_find_entry(tree2,
684 if (te && asprintf(&l2, "%s%s%s", label2,
685 label2[0] ? "/" : "", te->name) == -1)
687 got_error_from_errno("asprintf");
689 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
690 cb_arg, diff_content);
696 struct got_tree_entry *te = NULL;
698 te = got_object_tree_find_entry(tree1,
702 if (asprintf(&l2, "%s%s%s", label2,
703 label2[0] ? "/" : "", te->name) == -1)
705 got_error_from_errno("asprintf");
707 if (asprintf(&l2, "%s%s%s", label2,
708 label2[0] ? "/" : "", te2->name) == -1)
710 got_error_from_errno("asprintf");
712 err = diff_entry_new_old(te2, te, l2, repo,
713 cb, cb_arg, diff_content);
722 te1 = got_object_tree_get_entry(tree1, tidx1);
724 asprintf(&l1, "%s%s%s", label1,
725 label1[0] ? "/" : "", te1->name) == -1)
726 return got_error_from_errno("asprintf");
732 te2 = got_object_tree_get_entry(tree2, tidx2);
734 asprintf(&l2, "%s%s%s", label2,
735 label2[0] ? "/" : "", te2->name) == -1)
736 return got_error_from_errno("asprintf");
738 } while (te1 || te2);
743 const struct got_error *
744 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
745 const char *label1, const char *label2, int diff_context,
746 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
748 const struct got_error *err;
749 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
751 if (id1 == NULL && id2 == NULL)
752 return got_error(GOT_ERR_NO_OBJ);
755 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
760 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
764 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
765 ignore_whitespace, outfile);
768 got_object_blob_close(blob1);
770 got_object_blob_close(blob2);
774 const struct got_error *
775 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
776 char *label1, char *label2, int diff_context, int ignore_whitespace,
777 struct got_repository *repo, FILE *outfile)
779 const struct got_error *err;
780 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
781 struct got_diff_blob_output_unidiff_arg arg;
783 if (id1 == NULL && id2 == NULL)
784 return got_error(GOT_ERR_NO_OBJ);
787 err = got_object_open_as_tree(&tree1, repo, id1);
792 err = got_object_open_as_tree(&tree2, repo, id2);
796 arg.diff_context = diff_context;
797 arg.ignore_whitespace = ignore_whitespace;
798 arg.outfile = outfile;
799 err = got_diff_tree(tree1, tree2, label1, label2, repo,
800 got_diff_blob_output_unidiff, &arg, 1);
803 got_object_tree_close(tree1);
805 got_object_tree_close(tree2);
809 const struct got_error *
810 got_diff_objects_as_commits(struct got_object_id *id1,
811 struct got_object_id *id2, int diff_context, int ignore_whitespace,
812 struct got_repository *repo, FILE *outfile)
814 const struct got_error *err;
815 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
818 return got_error(GOT_ERR_NO_OBJ);
821 err = got_object_open_as_commit(&commit1, repo, id1);
826 err = got_object_open_as_commit(&commit2, repo, id2);
830 err = got_diff_objects_as_trees(
831 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
832 got_object_commit_get_tree_id(commit2), "", "", diff_context,
833 ignore_whitespace, repo, outfile);
836 got_object_commit_close(commit1);
838 got_object_commit_close(commit2);
842 const struct got_error *
843 got_diff_files(struct got_diff_changes **changes,
844 struct got_diff_state **ds,
845 struct got_diff_args **args,
847 FILE *f1, size_t size1, const char *label1,
848 FILE *f2, size_t size2, const char *label2,
849 int diff_context, FILE *outfile)
851 const struct got_error *err = NULL;
855 *ds = calloc(1, sizeof(**ds));
857 return got_error_from_errno("calloc");
858 *args = calloc(1, sizeof(**args));
860 err = got_error_from_errno("calloc");
873 /* XXX should stat buffers be passed in args instead of ds? */
874 (*ds)->stb1.st_mode = S_IFREG;
875 (*ds)->stb1.st_size = size1;
876 (*ds)->stb1.st_mtime = 0; /* XXX */
878 (*ds)->stb2.st_mode = S_IFREG;
879 (*ds)->stb2.st_size = size2;
880 (*ds)->stb2.st_mtime = 0; /* XXX */
882 (*args)->diff_format = D_UNIFIED;
883 (*args)->label[0] = label1;
884 (*args)->label[1] = label2;
885 (*args)->diff_context = diff_context;
886 *flags |= D_PROTOTYPE;
889 fprintf(outfile, "file - %s\n",
890 f1 == NULL ? "/dev/null" : label1);
891 fprintf(outfile, "file + %s\n",
892 f2 == NULL ? "/dev/null" : label2);
895 err = alloc_changes(changes);
899 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
900 changes ? *changes : NULL);
904 got_diff_state_free(*ds);
914 got_diff_free_changes(*changes);