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;
117 if (mode1 && mode1 != mode2) {
118 if (asprintf(&modestr1, " (mode %o)",
119 mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
120 err = got_error_from_errno("asprintf");
124 if (mode2 && mode1 != mode2) {
125 if (asprintf(&modestr2, " (mode %o)",
126 mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
127 err = got_error_from_errno("asprintf");
131 fprintf(outfile, "blob - %s%s\n", idstr1,
132 modestr1 ? modestr1 : "");
133 fprintf(outfile, "blob + %s%s\n", idstr2,
134 modestr2 ? modestr2 : "");
138 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
139 got_diff_state_free(&ds);
141 if (f1 && fclose(f1) != 0 && err == NULL)
142 err = got_error_from_errno("fclose");
143 if (f2 && fclose(f2) != 0 && err == NULL)
144 err = got_error_from_errno("fclose");
148 const struct got_error *
149 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
150 struct got_blob_object *blob2, struct got_object_id *id1,
151 struct got_object_id *id2, const char *label1, const char *label2,
152 mode_t mode1, mode_t mode2, struct got_repository *repo)
154 struct got_diff_blob_output_unidiff_arg *a = arg;
156 return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
157 a->diff_context, a->ignore_whitespace, a->outfile, NULL);
160 const struct got_error *
161 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
162 const char *label1, const char *label2, int diff_context,
163 int ignore_whitespace, FILE *outfile)
165 return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
166 ignore_whitespace, outfile, NULL);
169 static const struct got_error *
170 alloc_changes(struct got_diff_changes **changes)
172 *changes = calloc(1, sizeof(**changes));
173 if (*changes == NULL)
174 return got_error_from_errno("calloc");
175 SIMPLEQ_INIT(&(*changes)->entries);
179 static const struct got_error *
180 diff_blob_file(struct got_diff_changes **changes,
181 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
182 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
184 struct got_diff_state ds;
185 struct got_diff_args args;
186 const struct got_error *err = NULL;
188 char hex1[SHA1_DIGEST_STRING_LENGTH];
200 return got_error_from_errno("got_opentemp");
201 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
202 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
208 idstr1 = "/dev/null";
214 memset(&ds, 0, sizeof(ds));
215 /* XXX should stat buffers be passed in args instead of ds? */
216 ds.stb1.st_mode = S_IFREG;
218 ds.stb1.st_size = size1;
219 ds.stb1.st_mtime = 0; /* XXX */
221 ds.stb2.st_mode = S_IFREG;
222 ds.stb2.st_size = size2;
223 ds.stb2.st_mtime = 0; /* XXX */
225 memset(&args, 0, sizeof(args));
226 args.diff_format = D_UNIFIED;
227 args.label[0] = label2;
228 args.label[1] = label2;
229 args.diff_context = diff_context;
230 flags |= D_PROTOTYPE;
231 if (ignore_whitespace)
232 flags |= D_IGNOREBLANKS;
235 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 fprintf(outfile, "file + %s\n",
237 f2 == NULL ? "/dev/null" : label2);
240 err = alloc_changes(changes);
244 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
245 changes ? *changes : NULL);
246 got_diff_state_free(&ds);
248 if (f1 && fclose(f1) != 0 && err == NULL)
249 err = got_error_from_errno("fclose");
253 const struct got_error *
254 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
255 FILE *f2, size_t size2, const char *label2, int diff_context,
256 int ignore_whitespace, FILE *outfile)
258 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
259 diff_context, ignore_whitespace, outfile);
262 const struct got_error *
263 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
264 struct got_blob_object *blob1, FILE *f2, size_t size2)
266 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
270 const struct got_error *
271 got_diff_blob_lines_changed(struct got_diff_changes **changes,
272 struct got_blob_object *blob1, struct got_blob_object *blob2)
274 const struct got_error *err = NULL;
276 err = alloc_changes(changes);
280 err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
282 got_diff_free_changes(*changes);
289 got_diff_free_changes(struct got_diff_changes *changes)
291 struct got_diff_change *change;
292 while (!SIMPLEQ_EMPTY(&changes->entries)) {
293 change = SIMPLEQ_FIRST(&changes->entries);
294 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
300 static const struct got_error *
301 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
302 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
304 const struct got_error *err;
305 struct got_blob_object *blob = NULL;
306 struct got_object *obj = NULL;
308 err = got_object_open(&obj, repo, id);
312 err = got_object_blob_open(&blob, repo, obj, 8192);
315 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
317 got_object_close(obj);
319 got_object_blob_close(blob);
323 static const struct got_error *
324 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
325 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
326 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
328 const struct got_error *err;
329 struct got_object *obj1 = NULL;
330 struct got_object *obj2 = NULL;
331 struct got_blob_object *blob1 = NULL;
332 struct got_blob_object *blob2 = NULL;
334 err = got_object_open(&obj1, repo, id1);
337 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
342 err = got_object_open(&obj2, repo, id2);
345 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 err = got_object_blob_open(&blob1, repo, obj1, 8192);
354 err = got_object_blob_open(&blob2, repo, obj2, 8192);
358 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
362 got_object_close(obj1);
364 got_object_close(obj2);
366 got_object_blob_close(blob1);
368 got_object_blob_close(blob2);
372 static const struct got_error *
373 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
374 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
376 const struct got_error *err;
377 struct got_blob_object *blob = NULL;
378 struct got_object *obj = NULL;
380 err = got_object_open(&obj, repo, id);
384 err = got_object_blob_open(&blob, repo, obj, 8192);
387 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
389 got_object_close(obj);
391 got_object_blob_close(blob);
395 static const struct got_error *
396 diff_added_tree(struct got_object_id *id, const char *label,
397 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
400 const struct got_error *err = NULL;
401 struct got_object *treeobj = NULL;
402 struct got_tree_object *tree = NULL;
404 err = got_object_open(&treeobj, repo, id);
408 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
409 err = got_error(GOT_ERR_OBJ_TYPE);
413 err = got_object_tree_open(&tree, repo, treeobj);
417 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
421 got_object_tree_close(tree);
423 got_object_close(treeobj);
427 static const struct got_error *
428 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
429 const char *label1, const char *label2, struct got_repository *repo,
430 got_diff_blob_cb cb, void *cb_arg, int diff_content)
432 const struct got_error *err;
433 struct got_object *treeobj1 = NULL;
434 struct got_object *treeobj2 = NULL;
435 struct got_tree_object *tree1 = NULL;
436 struct got_tree_object *tree2 = NULL;
438 err = got_object_open(&treeobj1, repo, id1);
442 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
443 err = got_error(GOT_ERR_OBJ_TYPE);
447 err = got_object_open(&treeobj2, repo, id2);
451 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
452 err = got_error(GOT_ERR_OBJ_TYPE);
456 err = got_object_tree_open(&tree1, repo, treeobj1);
460 err = got_object_tree_open(&tree2, repo, treeobj2);
464 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
469 got_object_tree_close(tree1);
471 got_object_tree_close(tree2);
473 got_object_close(treeobj1);
475 got_object_close(treeobj2);
479 static const struct got_error *
480 diff_deleted_tree(struct got_object_id *id, const char *label,
481 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
484 const struct got_error *err;
485 struct got_object *treeobj = NULL;
486 struct got_tree_object *tree = NULL;
488 err = got_object_open(&treeobj, repo, id);
492 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
493 err = got_error(GOT_ERR_OBJ_TYPE);
497 err = got_object_tree_open(&tree, repo, treeobj);
501 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
505 got_object_tree_close(tree);
507 got_object_close(treeobj);
511 static const struct got_error *
512 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
513 const char *label1, const char *label2, struct got_repository *repo,
514 got_diff_blob_cb cb, void *cb_arg)
520 static const struct got_error *
521 diff_entry_old_new(struct got_tree_entry *te1,
522 struct got_tree_entry *te2, const char *label1, const char *label2,
523 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
526 const struct got_error *err = NULL;
529 if (got_object_tree_entry_is_submodule(te1))
533 if (S_ISDIR(te1->mode))
534 err = diff_deleted_tree(&te1->id, label1, repo,
535 cb, cb_arg, diff_content);
538 err = diff_deleted_blob(&te1->id, label1,
539 te1->mode, repo, cb, cb_arg);
541 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
542 label1, NULL, te1->mode, 0, repo);
545 } else if (got_object_tree_entry_is_submodule(te2))
548 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
549 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
551 return diff_modified_tree(&te1->id, &te2->id,
552 label1, label2, repo, cb, cb_arg, diff_content);
553 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
555 (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
557 return diff_modified_blob(&te1->id, &te2->id,
558 label1, label2, te1->mode, te2->mode,
561 return cb(cb_arg, NULL, NULL, &te1->id,
562 &te2->id, label1, label2, te1->mode,
570 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
574 static const struct got_error *
575 diff_entry_new_old(struct got_tree_entry *te2,
576 struct got_tree_entry *te1, const char *label2,
577 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
580 if (te1 != NULL) /* handled by diff_entry_old_new() */
583 if (got_object_tree_entry_is_submodule(te2))
586 if (S_ISDIR(te2->mode))
587 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
591 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
594 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
598 const struct got_error *
599 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
600 struct got_blob_object *blob2, struct got_object_id *id1,
601 struct got_object_id *id2, const char *label1, const char *label2,
602 mode_t mode1, mode_t mode2, struct got_repository *repo)
604 const struct got_error *err = NULL;
605 struct got_pathlist_head *paths = arg;
606 struct got_diff_changed_path *change = NULL;
609 path = strdup(label2 ? label2 : label1);
611 return got_error_from_errno("malloc");
613 change = malloc(sizeof(*change));
614 if (change == NULL) {
615 err = got_error_from_errno("malloc");
619 change->status = GOT_STATUS_NO_CHANGE;
621 change->status = GOT_STATUS_ADD;
622 else if (id2 == NULL)
623 change->status = GOT_STATUS_DELETE;
625 if (got_object_id_cmp(id1, id2) != 0)
626 change->status = GOT_STATUS_MODIFY;
627 else if (mode1 != mode2)
628 change->status = GOT_STATUS_MODE_CHANGE;
631 err = got_pathlist_insert(NULL, paths, path, change);
640 const struct got_error *
641 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
642 const char *label1, const char *label2, struct got_repository *repo,
643 got_diff_blob_cb cb, void *cb_arg, int diff_content)
645 const struct got_error *err = NULL;
646 struct got_tree_entry *te1 = NULL;
647 struct got_tree_entry *te2 = NULL;
648 char *l1 = NULL, *l2 = NULL;
649 int tidx1 = 0, tidx2 = 0;
652 te1 = got_object_tree_get_entry(tree1, 0);
653 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
655 return got_error_from_errno("asprintf");
658 te2 = got_object_tree_get_entry(tree2, 0);
659 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
661 return got_error_from_errno("asprintf");
666 struct got_tree_entry *te = NULL;
668 te = got_object_tree_find_entry(tree2,
673 if (te && asprintf(&l2, "%s%s%s", label2,
674 label2[0] ? "/" : "", te->name) == -1)
676 got_error_from_errno("asprintf");
678 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
679 cb_arg, diff_content);
685 struct got_tree_entry *te = NULL;
687 te = got_object_tree_find_entry(tree1,
691 if (asprintf(&l2, "%s%s%s", label2,
692 label2[0] ? "/" : "", te->name) == -1)
694 got_error_from_errno("asprintf");
696 if (asprintf(&l2, "%s%s%s", label2,
697 label2[0] ? "/" : "", te2->name) == -1)
699 got_error_from_errno("asprintf");
701 err = diff_entry_new_old(te2, te, l2, repo,
702 cb, cb_arg, diff_content);
711 te1 = got_object_tree_get_entry(tree1, tidx1);
713 asprintf(&l1, "%s%s%s", label1,
714 label1[0] ? "/" : "", te1->name) == -1)
715 return got_error_from_errno("asprintf");
721 te2 = got_object_tree_get_entry(tree2, tidx2);
723 asprintf(&l2, "%s%s%s", label2,
724 label2[0] ? "/" : "", te2->name) == -1)
725 return got_error_from_errno("asprintf");
727 } while (te1 || te2);
732 const struct got_error *
733 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
734 const char *label1, const char *label2, int diff_context,
735 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
737 const struct got_error *err;
738 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
740 if (id1 == NULL && id2 == NULL)
741 return got_error(GOT_ERR_NO_OBJ);
744 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
749 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
753 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
754 ignore_whitespace, outfile);
757 got_object_blob_close(blob1);
759 got_object_blob_close(blob2);
763 const struct got_error *
764 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
765 char *label1, char *label2, int diff_context, int ignore_whitespace,
766 struct got_repository *repo, FILE *outfile)
768 const struct got_error *err;
769 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
770 struct got_diff_blob_output_unidiff_arg arg;
772 if (id1 == NULL && id2 == NULL)
773 return got_error(GOT_ERR_NO_OBJ);
776 err = got_object_open_as_tree(&tree1, repo, id1);
781 err = got_object_open_as_tree(&tree2, repo, id2);
785 arg.diff_context = diff_context;
786 arg.ignore_whitespace = ignore_whitespace;
787 arg.outfile = outfile;
788 err = got_diff_tree(tree1, tree2, label1, label2, repo,
789 got_diff_blob_output_unidiff, &arg, 1);
792 got_object_tree_close(tree1);
794 got_object_tree_close(tree2);
798 const struct got_error *
799 got_diff_objects_as_commits(struct got_object_id *id1,
800 struct got_object_id *id2, int diff_context, int ignore_whitespace,
801 struct got_repository *repo, FILE *outfile)
803 const struct got_error *err;
804 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
807 return got_error(GOT_ERR_NO_OBJ);
810 err = got_object_open_as_commit(&commit1, repo, id1);
815 err = got_object_open_as_commit(&commit2, repo, id2);
819 err = got_diff_objects_as_trees(
820 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
821 got_object_commit_get_tree_id(commit2), "", "", diff_context,
822 ignore_whitespace, repo, outfile);
825 got_object_commit_close(commit1);
827 got_object_commit_close(commit2);
831 const struct got_error *
832 got_diff_files(struct got_diff_changes **changes,
833 struct got_diff_state **ds,
834 struct got_diff_args **args,
836 FILE *f1, size_t size1, const char *label1,
837 FILE *f2, size_t size2, const char *label2,
838 int diff_context, FILE *outfile)
840 const struct got_error *err = NULL;
844 *ds = calloc(1, sizeof(**ds));
846 return got_error_from_errno("calloc");
847 *args = calloc(1, sizeof(**args));
849 err = got_error_from_errno("calloc");
862 /* XXX should stat buffers be passed in args instead of ds? */
863 (*ds)->stb1.st_mode = S_IFREG;
864 (*ds)->stb1.st_size = size1;
865 (*ds)->stb1.st_mtime = 0; /* XXX */
867 (*ds)->stb2.st_mode = S_IFREG;
868 (*ds)->stb2.st_size = size2;
869 (*ds)->stb2.st_mtime = 0; /* XXX */
871 (*args)->diff_format = D_UNIFIED;
872 (*args)->label[0] = label1;
873 (*args)->label[1] = label2;
874 (*args)->diff_context = diff_context;
875 *flags |= D_PROTOTYPE;
878 fprintf(outfile, "file - %s\n",
879 f1 == NULL ? "/dev/null" : label1);
880 fprintf(outfile, "file + %s\n",
881 f2 == NULL ? "/dev/null" : label2);
884 err = alloc_changes(changes);
888 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
889 changes ? *changes : NULL);
893 got_diff_state_free(*ds);
903 got_diff_free_changes(*changes);