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, int diff_context,
42 int ignore_whitespace, FILE *outfile, struct got_diff_changes *changes)
44 struct got_diff_state ds;
45 struct got_diff_args args;
46 const struct got_error *err = NULL;
47 FILE *f1 = NULL, *f2 = NULL;
48 char hex1[SHA1_DIGEST_STRING_LENGTH];
49 char hex2[SHA1_DIGEST_STRING_LENGTH];
50 char *idstr1 = NULL, *idstr2 = NULL;
57 return got_error_from_errno("got_opentemp");
64 err = got_error_from_errno("got_opentemp");
73 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
74 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
83 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
84 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
91 memset(&ds, 0, sizeof(ds));
92 /* XXX should stat buffers be passed in args instead of ds? */
93 ds.stb1.st_mode = S_IFREG;
95 ds.stb1.st_size = size1;
96 ds.stb1.st_mtime = 0; /* XXX */
98 ds.stb2.st_mode = S_IFREG;
100 ds.stb2.st_size = size2;
101 ds.stb2.st_mtime = 0; /* XXX */
103 memset(&args, 0, sizeof(args));
104 args.diff_format = D_UNIFIED;
105 args.label[0] = label1 ? label1 : idstr1;
106 args.label[1] = label2 ? label2 : idstr2;
107 args.diff_context = diff_context;
108 flags |= D_PROTOTYPE;
109 if (ignore_whitespace)
110 flags |= D_IGNOREBLANKS;
113 fprintf(outfile, "blob - %s\n", idstr1);
114 fprintf(outfile, "blob + %s\n", idstr2);
116 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
117 got_diff_state_free(&ds);
119 if (f1 && fclose(f1) != 0 && err == NULL)
120 err = got_error_from_errno("fclose");
121 if (f2 && fclose(f2) != 0 && err == NULL)
122 err = got_error_from_errno("fclose");
126 const struct got_error *
127 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
128 struct got_blob_object *blob2, struct got_object_id *id1,
129 struct got_object_id *id2, const char *label1, const char *label2,
130 struct got_repository *repo)
132 struct got_diff_blob_output_unidiff_arg *a = arg;
134 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
135 a->ignore_whitespace, a->outfile, NULL);
138 const struct got_error *
139 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
140 const char *label1, const char *label2, int diff_context,
141 int ignore_whitespace, FILE *outfile)
143 return diff_blobs(blob1, blob2, label1, label2, diff_context,
144 ignore_whitespace, outfile, NULL);
147 static const struct got_error *
148 alloc_changes(struct got_diff_changes **changes)
150 *changes = calloc(1, sizeof(**changes));
151 if (*changes == NULL)
152 return got_error_from_errno("calloc");
153 SIMPLEQ_INIT(&(*changes)->entries);
157 static const struct got_error *
158 diff_blob_file(struct got_diff_changes **changes,
159 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
160 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
162 struct got_diff_state ds;
163 struct got_diff_args args;
164 const struct got_error *err = NULL;
166 char hex1[SHA1_DIGEST_STRING_LENGTH];
178 return got_error_from_errno("got_opentemp");
179 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
180 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
186 idstr1 = "/dev/null";
192 memset(&ds, 0, sizeof(ds));
193 /* XXX should stat buffers be passed in args instead of ds? */
194 ds.stb1.st_mode = S_IFREG;
196 ds.stb1.st_size = size1;
197 ds.stb1.st_mtime = 0; /* XXX */
199 ds.stb2.st_mode = S_IFREG;
200 ds.stb2.st_size = size2;
201 ds.stb2.st_mtime = 0; /* XXX */
203 memset(&args, 0, sizeof(args));
204 args.diff_format = D_UNIFIED;
205 args.label[0] = label2;
206 args.label[1] = label2;
207 args.diff_context = diff_context;
208 flags |= D_PROTOTYPE;
209 if (ignore_whitespace)
210 flags |= D_IGNOREBLANKS;
213 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
214 fprintf(outfile, "file + %s\n",
215 f2 == NULL ? "/dev/null" : label2);
218 err = alloc_changes(changes);
222 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
223 changes ? *changes : NULL);
224 got_diff_state_free(&ds);
226 if (f1 && fclose(f1) != 0 && err == NULL)
227 err = got_error_from_errno("fclose");
231 const struct got_error *
232 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
233 FILE *f2, size_t size2, const char *label2, int diff_context,
234 int ignore_whitespace, FILE *outfile)
236 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
237 diff_context, ignore_whitespace, outfile);
240 const struct got_error *
241 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
242 struct got_blob_object *blob1, FILE *f2, size_t size2)
244 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
248 const struct got_error *
249 got_diff_blob_lines_changed(struct got_diff_changes **changes,
250 struct got_blob_object *blob1, struct got_blob_object *blob2)
252 const struct got_error *err = NULL;
254 err = alloc_changes(changes);
258 err = diff_blobs(blob1, blob2, NULL, NULL, 3, 0, NULL, *changes);
260 got_diff_free_changes(*changes);
267 got_diff_free_changes(struct got_diff_changes *changes)
269 struct got_diff_change *change;
270 while (!SIMPLEQ_EMPTY(&changes->entries)) {
271 change = SIMPLEQ_FIRST(&changes->entries);
272 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
278 static const struct got_error *
279 diff_added_blob(struct got_object_id *id, const char *label,
280 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
282 const struct got_error *err;
283 struct got_blob_object *blob = NULL;
284 struct got_object *obj = NULL;
286 err = got_object_open(&obj, repo, id);
290 err = got_object_blob_open(&blob, repo, obj, 8192);
293 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
295 got_object_close(obj);
297 got_object_blob_close(blob);
301 static const struct got_error *
302 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
303 const char *label1, const char *label2, struct got_repository *repo,
304 got_diff_blob_cb cb, void *cb_arg)
306 const struct got_error *err;
307 struct got_object *obj1 = NULL;
308 struct got_object *obj2 = NULL;
309 struct got_blob_object *blob1 = NULL;
310 struct got_blob_object *blob2 = NULL;
312 err = got_object_open(&obj1, repo, id1);
315 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
316 err = got_error(GOT_ERR_OBJ_TYPE);
320 err = got_object_open(&obj2, repo, id2);
323 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
324 err = got_error(GOT_ERR_BAD_OBJ_DATA);
328 err = got_object_blob_open(&blob1, repo, obj1, 8192);
332 err = got_object_blob_open(&blob2, repo, obj2, 8192);
336 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
339 got_object_close(obj1);
341 got_object_close(obj2);
343 got_object_blob_close(blob1);
345 got_object_blob_close(blob2);
349 static const struct got_error *
350 diff_deleted_blob(struct got_object_id *id, const char *label,
351 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
353 const struct got_error *err;
354 struct got_blob_object *blob = NULL;
355 struct got_object *obj = NULL;
357 err = got_object_open(&obj, repo, id);
361 err = got_object_blob_open(&blob, repo, obj, 8192);
364 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
366 got_object_close(obj);
368 got_object_blob_close(blob);
372 static const struct got_error *
373 diff_added_tree(struct got_object_id *id, const char *label,
374 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
377 const struct got_error *err = NULL;
378 struct got_object *treeobj = NULL;
379 struct got_tree_object *tree = NULL;
381 err = got_object_open(&treeobj, repo, id);
385 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
386 err = got_error(GOT_ERR_OBJ_TYPE);
390 err = got_object_tree_open(&tree, repo, treeobj);
394 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
398 got_object_tree_close(tree);
400 got_object_close(treeobj);
404 static const struct got_error *
405 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
406 const char *label1, const char *label2, struct got_repository *repo,
407 got_diff_blob_cb cb, void *cb_arg, int diff_content)
409 const struct got_error *err;
410 struct got_object *treeobj1 = NULL;
411 struct got_object *treeobj2 = NULL;
412 struct got_tree_object *tree1 = NULL;
413 struct got_tree_object *tree2 = NULL;
415 err = got_object_open(&treeobj1, repo, id1);
419 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
420 err = got_error(GOT_ERR_OBJ_TYPE);
424 err = got_object_open(&treeobj2, repo, id2);
428 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
429 err = got_error(GOT_ERR_OBJ_TYPE);
433 err = got_object_tree_open(&tree1, repo, treeobj1);
437 err = got_object_tree_open(&tree2, repo, treeobj2);
441 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
446 got_object_tree_close(tree1);
448 got_object_tree_close(tree2);
450 got_object_close(treeobj1);
452 got_object_close(treeobj2);
456 static const struct got_error *
457 diff_deleted_tree(struct got_object_id *id, const char *label,
458 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
461 const struct got_error *err;
462 struct got_object *treeobj = NULL;
463 struct got_tree_object *tree = NULL;
465 err = got_object_open(&treeobj, repo, id);
469 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
470 err = got_error(GOT_ERR_OBJ_TYPE);
474 err = got_object_tree_open(&tree, repo, treeobj);
478 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
482 got_object_tree_close(tree);
484 got_object_close(treeobj);
488 static const struct got_error *
489 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
490 const char *label1, const char *label2, struct got_repository *repo,
491 got_diff_blob_cb cb, void *cb_arg)
497 static const struct got_error *
498 diff_entry_old_new(const struct got_tree_entry *te1,
499 const struct got_tree_entry *te2, const char *label1, const char *label2,
500 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
503 const struct got_error *err = NULL;
506 if (got_object_tree_entry_is_submodule(te1))
510 if (S_ISDIR(te1->mode))
511 err = diff_deleted_tree(te1->id, label1, repo,
512 cb, cb_arg, diff_content);
515 err = diff_deleted_blob(te1->id, label1, repo,
518 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
522 } else if (got_object_tree_entry_is_submodule(te2))
525 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
526 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
528 return diff_modified_tree(te1->id, te2->id,
529 label1, label2, repo, cb, cb_arg, diff_content);
530 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
533 return diff_modified_blob(te1->id, te2->id,
534 label1, label2, repo, cb, cb_arg);
536 return cb(cb_arg, NULL, NULL, te1->id,
537 te2->id, label1, label2, repo);
544 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
548 static const struct got_error *
549 diff_entry_new_old(const struct got_tree_entry *te2,
550 const struct got_tree_entry *te1, const char *label2,
551 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
554 if (te1 != NULL) /* handled by diff_entry_old_new() */
557 if (got_object_tree_entry_is_submodule(te2))
560 if (S_ISDIR(te2->mode))
561 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
565 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
567 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
570 const struct got_error *
571 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
572 const char *label1, const char *label2, struct got_repository *repo,
573 got_diff_blob_cb cb, void *cb_arg, int diff_content)
575 const struct got_error *err = NULL;
576 struct got_tree_entry *te1 = NULL;
577 struct got_tree_entry *te2 = NULL;
578 char *l1 = NULL, *l2 = NULL;
581 const struct got_tree_entries *entries;
582 entries = got_object_tree_get_entries(tree1);
583 te1 = SIMPLEQ_FIRST(&entries->head);
584 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
586 return got_error_from_errno("asprintf");
589 const struct got_tree_entries *entries;
590 entries = got_object_tree_get_entries(tree2);
591 te2 = SIMPLEQ_FIRST(&entries->head);
592 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
594 return got_error_from_errno("asprintf");
599 const struct got_tree_entry *te = NULL;
601 te = got_object_tree_find_entry(tree2,
606 if (te && asprintf(&l2, "%s%s%s", label2,
607 label2[0] ? "/" : "", te->name) == -1)
609 got_error_from_errno("asprintf");
611 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
612 cb_arg, diff_content);
618 const struct got_tree_entry *te = NULL;
620 te = got_object_tree_find_entry(tree1,
624 if (asprintf(&l2, "%s%s%s", label2,
625 label2[0] ? "/" : "", te->name) == -1)
627 got_error_from_errno("asprintf");
629 if (asprintf(&l2, "%s%s%s", label2,
630 label2[0] ? "/" : "", te2->name) == -1)
632 got_error_from_errno("asprintf");
634 err = diff_entry_new_old(te2, te, l2, repo,
635 cb, cb_arg, diff_content);
643 te1 = SIMPLEQ_NEXT(te1, entry);
645 asprintf(&l1, "%s%s%s", label1,
646 label1[0] ? "/" : "", te1->name) == -1)
647 return got_error_from_errno("asprintf");
652 te2 = SIMPLEQ_NEXT(te2, entry);
654 asprintf(&l2, "%s%s%s", label2,
655 label2[0] ? "/" : "", te2->name) == -1)
656 return got_error_from_errno("asprintf");
658 } while (te1 || te2);
663 const struct got_error *
664 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
665 const char *label1, const char *label2, int diff_context,
666 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
668 const struct got_error *err;
669 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
671 if (id1 == NULL && id2 == NULL)
672 return got_error(GOT_ERR_NO_OBJ);
675 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
680 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
684 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
685 ignore_whitespace, outfile);
688 got_object_blob_close(blob1);
690 got_object_blob_close(blob2);
694 const struct got_error *
695 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
696 char *label1, char *label2, int diff_context, int ignore_whitespace,
697 struct got_repository *repo, FILE *outfile)
699 const struct got_error *err;
700 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
701 struct got_diff_blob_output_unidiff_arg arg;
703 if (id1 == NULL && id2 == NULL)
704 return got_error(GOT_ERR_NO_OBJ);
707 err = got_object_open_as_tree(&tree1, repo, id1);
712 err = got_object_open_as_tree(&tree2, repo, id2);
716 arg.diff_context = diff_context;
717 arg.ignore_whitespace = ignore_whitespace;
718 arg.outfile = outfile;
719 err = got_diff_tree(tree1, tree2, label1, label2, repo,
720 got_diff_blob_output_unidiff, &arg, 1);
723 got_object_tree_close(tree1);
725 got_object_tree_close(tree2);
729 const struct got_error *
730 got_diff_objects_as_commits(struct got_object_id *id1,
731 struct got_object_id *id2, int diff_context, int ignore_whitespace,
732 struct got_repository *repo, FILE *outfile)
734 const struct got_error *err;
735 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
738 return got_error(GOT_ERR_NO_OBJ);
741 err = got_object_open_as_commit(&commit1, repo, id1);
746 err = got_object_open_as_commit(&commit2, repo, id2);
750 err = got_diff_objects_as_trees(
751 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
752 got_object_commit_get_tree_id(commit2), "", "", diff_context,
753 ignore_whitespace, repo, outfile);
756 got_object_commit_close(commit1);
758 got_object_commit_close(commit2);
762 const struct got_error *
763 got_diff_files(struct got_diff_changes **changes,
764 struct got_diff_state **ds,
765 struct got_diff_args **args,
767 FILE *f1, size_t size1, const char *label1,
768 FILE *f2, size_t size2, const char *label2,
769 int diff_context, FILE *outfile)
771 const struct got_error *err = NULL;
775 *ds = calloc(1, sizeof(**ds));
777 return got_error_from_errno("calloc");
778 *args = calloc(1, sizeof(**args));
780 err = got_error_from_errno("calloc");
793 /* XXX should stat buffers be passed in args instead of ds? */
794 (*ds)->stb1.st_mode = S_IFREG;
795 (*ds)->stb1.st_size = size1;
796 (*ds)->stb1.st_mtime = 0; /* XXX */
798 (*ds)->stb2.st_mode = S_IFREG;
799 (*ds)->stb2.st_size = size2;
800 (*ds)->stb2.st_mtime = 0; /* XXX */
802 (*args)->diff_format = D_UNIFIED;
803 (*args)->label[0] = label1;
804 (*args)->label[1] = label2;
805 (*args)->diff_context = diff_context;
806 *flags |= D_PROTOTYPE;
809 fprintf(outfile, "file - %s\n",
810 f1 == NULL ? "/dev/null" : label1);
811 fprintf(outfile, "file + %s\n",
812 f2 == NULL ? "/dev/null" : label2);
815 err = alloc_changes(changes);
819 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
820 changes ? *changes : NULL);
824 got_diff_state_free(*ds);
834 got_diff_free_changes(*changes);