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"
34 #include "got_lib_diff.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 static const struct got_error *
40 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
41 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
42 int diff_context, int ignore_whitespace, FILE *outfile,
43 struct got_diff_changes *changes)
45 struct got_diff_state ds;
46 struct got_diff_args args;
47 const struct got_error *err = NULL;
48 FILE *f1 = NULL, *f2 = NULL;
49 char hex1[SHA1_DIGEST_STRING_LENGTH];
50 char hex2[SHA1_DIGEST_STRING_LENGTH];
51 char *idstr1 = NULL, *idstr2 = NULL;
58 return got_error_from_errno("got_opentemp");
65 err = got_error_from_errno("got_opentemp");
74 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
75 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
84 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
85 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
92 memset(&ds, 0, sizeof(ds));
93 /* XXX should stat buffers be passed in args instead of ds? */
94 ds.stb1.st_mode = S_IFREG;
96 ds.stb1.st_size = size1;
97 ds.stb1.st_mtime = 0; /* XXX */
99 ds.stb2.st_mode = S_IFREG;
101 ds.stb2.st_size = size2;
102 ds.stb2.st_mtime = 0; /* XXX */
104 memset(&args, 0, sizeof(args));
105 args.diff_format = D_UNIFIED;
106 args.label[0] = label1 ? label1 : idstr1;
107 args.label[1] = label2 ? label2 : idstr2;
108 args.diff_context = diff_context;
109 flags |= D_PROTOTYPE;
110 if (ignore_whitespace)
111 flags |= D_IGNOREBLANKS;
114 char *modestr1 = NULL, *modestr2 = NULL;
115 if (mode1 && mode1 != mode2) {
116 if (asprintf(&modestr1, " (mode %o)",
117 mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
118 err = got_error_from_errno("asprintf");
122 if (mode2 && mode1 != mode2) {
123 if (asprintf(&modestr2, " (mode %o)",
124 mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
125 err = got_error_from_errno("asprintf");
129 fprintf(outfile, "blob - %s%s\n", idstr1,
130 modestr1 ? modestr1 : "");
131 fprintf(outfile, "blob + %s%s\n", idstr2,
132 modestr2 ? modestr2 : "");
136 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
137 got_diff_state_free(&ds);
139 if (f1 && fclose(f1) != 0 && err == NULL)
140 err = got_error_from_errno("fclose");
141 if (f2 && fclose(f2) != 0 && err == NULL)
142 err = got_error_from_errno("fclose");
146 const struct got_error *
147 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
148 struct got_blob_object *blob2, struct got_object_id *id1,
149 struct got_object_id *id2, const char *label1, const char *label2,
150 mode_t mode1, mode_t mode2, struct got_repository *repo)
152 struct got_diff_blob_output_unidiff_arg *a = arg;
154 return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
155 a->diff_context, a->ignore_whitespace, a->outfile, NULL);
158 const struct got_error *
159 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
160 const char *label1, const char *label2, int diff_context,
161 int ignore_whitespace, FILE *outfile)
163 return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
164 ignore_whitespace, outfile, NULL);
167 static const struct got_error *
168 alloc_changes(struct got_diff_changes **changes)
170 *changes = calloc(1, sizeof(**changes));
171 if (*changes == NULL)
172 return got_error_from_errno("calloc");
173 SIMPLEQ_INIT(&(*changes)->entries);
177 static const struct got_error *
178 diff_blob_file(struct got_diff_changes **changes,
179 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
180 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
182 struct got_diff_state ds;
183 struct got_diff_args args;
184 const struct got_error *err = NULL;
186 char hex1[SHA1_DIGEST_STRING_LENGTH];
198 return got_error_from_errno("got_opentemp");
199 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
200 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
206 idstr1 = "/dev/null";
212 memset(&ds, 0, sizeof(ds));
213 /* XXX should stat buffers be passed in args instead of ds? */
214 ds.stb1.st_mode = S_IFREG;
216 ds.stb1.st_size = size1;
217 ds.stb1.st_mtime = 0; /* XXX */
219 ds.stb2.st_mode = S_IFREG;
220 ds.stb2.st_size = size2;
221 ds.stb2.st_mtime = 0; /* XXX */
223 memset(&args, 0, sizeof(args));
224 args.diff_format = D_UNIFIED;
225 args.label[0] = label2;
226 args.label[1] = label2;
227 args.diff_context = diff_context;
228 flags |= D_PROTOTYPE;
229 if (ignore_whitespace)
230 flags |= D_IGNOREBLANKS;
233 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
234 fprintf(outfile, "file + %s\n",
235 f2 == NULL ? "/dev/null" : label2);
238 err = alloc_changes(changes);
242 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
243 changes ? *changes : NULL);
244 got_diff_state_free(&ds);
246 if (f1 && fclose(f1) != 0 && err == NULL)
247 err = got_error_from_errno("fclose");
251 const struct got_error *
252 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
253 FILE *f2, size_t size2, const char *label2, int diff_context,
254 int ignore_whitespace, FILE *outfile)
256 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
257 diff_context, ignore_whitespace, outfile);
260 const struct got_error *
261 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
262 struct got_blob_object *blob1, FILE *f2, size_t size2)
264 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
268 const struct got_error *
269 got_diff_blob_lines_changed(struct got_diff_changes **changes,
270 struct got_blob_object *blob1, struct got_blob_object *blob2)
272 const struct got_error *err = NULL;
274 err = alloc_changes(changes);
278 err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
280 got_diff_free_changes(*changes);
287 got_diff_free_changes(struct got_diff_changes *changes)
289 struct got_diff_change *change;
290 while (!SIMPLEQ_EMPTY(&changes->entries)) {
291 change = SIMPLEQ_FIRST(&changes->entries);
292 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
298 static const struct got_error *
299 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
300 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
302 const struct got_error *err;
303 struct got_blob_object *blob = NULL;
304 struct got_object *obj = NULL;
306 err = got_object_open(&obj, repo, id);
310 err = got_object_blob_open(&blob, repo, obj, 8192);
313 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
315 got_object_close(obj);
317 got_object_blob_close(blob);
321 static const struct got_error *
322 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
323 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
324 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
326 const struct got_error *err;
327 struct got_object *obj1 = NULL;
328 struct got_object *obj2 = NULL;
329 struct got_blob_object *blob1 = NULL;
330 struct got_blob_object *blob2 = NULL;
332 err = got_object_open(&obj1, repo, id1);
335 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
336 err = got_error(GOT_ERR_OBJ_TYPE);
340 err = got_object_open(&obj2, repo, id2);
343 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
344 err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 err = got_object_blob_open(&blob1, repo, obj1, 8192);
352 err = got_object_blob_open(&blob2, repo, obj2, 8192);
356 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
360 got_object_close(obj1);
362 got_object_close(obj2);
364 got_object_blob_close(blob1);
366 got_object_blob_close(blob2);
370 static const struct got_error *
371 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
372 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
374 const struct got_error *err;
375 struct got_blob_object *blob = NULL;
376 struct got_object *obj = NULL;
378 err = got_object_open(&obj, repo, id);
382 err = got_object_blob_open(&blob, repo, obj, 8192);
385 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
387 got_object_close(obj);
389 got_object_blob_close(blob);
393 static const struct got_error *
394 diff_added_tree(struct got_object_id *id, const char *label,
395 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
398 const struct got_error *err = NULL;
399 struct got_object *treeobj = NULL;
400 struct got_tree_object *tree = NULL;
402 err = got_object_open(&treeobj, repo, id);
406 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
407 err = got_error(GOT_ERR_OBJ_TYPE);
411 err = got_object_tree_open(&tree, repo, treeobj);
415 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
419 got_object_tree_close(tree);
421 got_object_close(treeobj);
425 static const struct got_error *
426 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
427 const char *label1, const char *label2, struct got_repository *repo,
428 got_diff_blob_cb cb, void *cb_arg, int diff_content)
430 const struct got_error *err;
431 struct got_object *treeobj1 = NULL;
432 struct got_object *treeobj2 = NULL;
433 struct got_tree_object *tree1 = NULL;
434 struct got_tree_object *tree2 = NULL;
436 err = got_object_open(&treeobj1, repo, id1);
440 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
441 err = got_error(GOT_ERR_OBJ_TYPE);
445 err = got_object_open(&treeobj2, repo, id2);
449 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
450 err = got_error(GOT_ERR_OBJ_TYPE);
454 err = got_object_tree_open(&tree1, repo, treeobj1);
458 err = got_object_tree_open(&tree2, repo, treeobj2);
462 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
467 got_object_tree_close(tree1);
469 got_object_tree_close(tree2);
471 got_object_close(treeobj1);
473 got_object_close(treeobj2);
477 static const struct got_error *
478 diff_deleted_tree(struct got_object_id *id, const char *label,
479 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
482 const struct got_error *err;
483 struct got_object *treeobj = NULL;
484 struct got_tree_object *tree = NULL;
486 err = got_object_open(&treeobj, repo, id);
490 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
491 err = got_error(GOT_ERR_OBJ_TYPE);
495 err = got_object_tree_open(&tree, repo, treeobj);
499 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
503 got_object_tree_close(tree);
505 got_object_close(treeobj);
509 static const struct got_error *
510 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
511 const char *label1, const char *label2, struct got_repository *repo,
512 got_diff_blob_cb cb, void *cb_arg)
518 static const struct got_error *
519 diff_entry_old_new(struct got_tree_entry *te1,
520 struct got_tree_entry *te2, const char *label1, const char *label2,
521 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
524 const struct got_error *err = NULL;
527 if (got_object_tree_entry_is_submodule(te1))
531 if (S_ISDIR(te1->mode))
532 err = diff_deleted_tree(&te1->id, label1, repo,
533 cb, cb_arg, diff_content);
536 err = diff_deleted_blob(&te1->id, label1,
537 te1->mode, repo, cb, cb_arg);
539 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
540 label1, NULL, te1->mode, 0, repo);
543 } else if (got_object_tree_entry_is_submodule(te2))
546 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
547 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
549 return diff_modified_tree(&te1->id, &te2->id,
550 label1, label2, repo, cb, cb_arg, diff_content);
551 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
553 (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
555 return diff_modified_blob(&te1->id, &te2->id,
556 label1, label2, te1->mode, te2->mode,
559 return cb(cb_arg, NULL, NULL, &te1->id,
560 &te2->id, label1, label2, te1->mode,
568 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
572 static const struct got_error *
573 diff_entry_new_old(struct got_tree_entry *te2,
574 struct got_tree_entry *te1, const char *label2,
575 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
578 if (te1 != NULL) /* handled by diff_entry_old_new() */
581 if (got_object_tree_entry_is_submodule(te2))
584 if (S_ISDIR(te2->mode))
585 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
589 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
592 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
596 const struct got_error *
597 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
598 const char *label1, const char *label2, struct got_repository *repo,
599 got_diff_blob_cb cb, void *cb_arg, int diff_content)
601 const struct got_error *err = NULL;
602 struct got_tree_entry *te1 = NULL;
603 struct got_tree_entry *te2 = NULL;
604 char *l1 = NULL, *l2 = NULL;
605 int tidx1 = 0, tidx2 = 0;
608 te1 = got_object_tree_get_entry(tree1, 0);
609 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
611 return got_error_from_errno("asprintf");
614 te2 = got_object_tree_get_entry(tree2, 0);
615 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
617 return got_error_from_errno("asprintf");
622 struct got_tree_entry *te = NULL;
624 te = got_object_tree_find_entry(tree2,
629 if (te && asprintf(&l2, "%s%s%s", label2,
630 label2[0] ? "/" : "", te->name) == -1)
632 got_error_from_errno("asprintf");
634 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
635 cb_arg, diff_content);
641 struct got_tree_entry *te = NULL;
643 te = got_object_tree_find_entry(tree1,
647 if (asprintf(&l2, "%s%s%s", label2,
648 label2[0] ? "/" : "", te->name) == -1)
650 got_error_from_errno("asprintf");
652 if (asprintf(&l2, "%s%s%s", label2,
653 label2[0] ? "/" : "", te2->name) == -1)
655 got_error_from_errno("asprintf");
657 err = diff_entry_new_old(te2, te, l2, repo,
658 cb, cb_arg, diff_content);
667 te1 = got_object_tree_get_entry(tree1, tidx1);
669 asprintf(&l1, "%s%s%s", label1,
670 label1[0] ? "/" : "", te1->name) == -1)
671 return got_error_from_errno("asprintf");
677 te2 = got_object_tree_get_entry(tree2, tidx2);
679 asprintf(&l2, "%s%s%s", label2,
680 label2[0] ? "/" : "", te2->name) == -1)
681 return got_error_from_errno("asprintf");
683 } while (te1 || te2);
688 const struct got_error *
689 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
690 const char *label1, const char *label2, int diff_context,
691 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
693 const struct got_error *err;
694 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
696 if (id1 == NULL && id2 == NULL)
697 return got_error(GOT_ERR_NO_OBJ);
700 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
705 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
709 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
710 ignore_whitespace, outfile);
713 got_object_blob_close(blob1);
715 got_object_blob_close(blob2);
719 const struct got_error *
720 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
721 char *label1, char *label2, int diff_context, int ignore_whitespace,
722 struct got_repository *repo, FILE *outfile)
724 const struct got_error *err;
725 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
726 struct got_diff_blob_output_unidiff_arg arg;
728 if (id1 == NULL && id2 == NULL)
729 return got_error(GOT_ERR_NO_OBJ);
732 err = got_object_open_as_tree(&tree1, repo, id1);
737 err = got_object_open_as_tree(&tree2, repo, id2);
741 arg.diff_context = diff_context;
742 arg.ignore_whitespace = ignore_whitespace;
743 arg.outfile = outfile;
744 err = got_diff_tree(tree1, tree2, label1, label2, repo,
745 got_diff_blob_output_unidiff, &arg, 1);
748 got_object_tree_close(tree1);
750 got_object_tree_close(tree2);
754 const struct got_error *
755 got_diff_objects_as_commits(struct got_object_id *id1,
756 struct got_object_id *id2, int diff_context, int ignore_whitespace,
757 struct got_repository *repo, FILE *outfile)
759 const struct got_error *err;
760 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
763 return got_error(GOT_ERR_NO_OBJ);
766 err = got_object_open_as_commit(&commit1, repo, id1);
771 err = got_object_open_as_commit(&commit2, repo, id2);
775 err = got_diff_objects_as_trees(
776 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
777 got_object_commit_get_tree_id(commit2), "", "", diff_context,
778 ignore_whitespace, repo, outfile);
781 got_object_commit_close(commit1);
783 got_object_commit_close(commit2);
787 const struct got_error *
788 got_diff_files(struct got_diff_changes **changes,
789 struct got_diff_state **ds,
790 struct got_diff_args **args,
792 FILE *f1, size_t size1, const char *label1,
793 FILE *f2, size_t size2, const char *label2,
794 int diff_context, FILE *outfile)
796 const struct got_error *err = NULL;
800 *ds = calloc(1, sizeof(**ds));
802 return got_error_from_errno("calloc");
803 *args = calloc(1, sizeof(**args));
805 err = got_error_from_errno("calloc");
818 /* XXX should stat buffers be passed in args instead of ds? */
819 (*ds)->stb1.st_mode = S_IFREG;
820 (*ds)->stb1.st_size = size1;
821 (*ds)->stb1.st_mtime = 0; /* XXX */
823 (*ds)->stb2.st_mode = S_IFREG;
824 (*ds)->stb2.st_size = size2;
825 (*ds)->stb2.st_mtime = 0; /* XXX */
827 (*args)->diff_format = D_UNIFIED;
828 (*args)->label[0] = label1;
829 (*args)->label[1] = label2;
830 (*args)->diff_context = diff_context;
831 *flags |= D_PROTOTYPE;
834 fprintf(outfile, "file - %s\n",
835 f1 == NULL ? "/dev/null" : label1);
836 fprintf(outfile, "file + %s\n",
837 f2 == NULL ? "/dev/null" : label2);
840 err = alloc_changes(changes);
844 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
845 changes ? *changes : NULL);
849 got_diff_state_free(*ds);
859 got_diff_free_changes(*changes);