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, FILE *outfile,
42 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;
111 fprintf(outfile, "blob - %s\n", idstr1);
112 fprintf(outfile, "blob + %s\n", idstr2);
114 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
115 got_diff_state_free(&ds);
117 if (f1 && fclose(f1) != 0 && err == NULL)
118 err = got_error_from_errno("fclose");
119 if (f2 && fclose(f2) != 0 && err == NULL)
120 err = got_error_from_errno("fclose");
124 const struct got_error *
125 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
126 struct got_blob_object *blob2, struct got_object_id *id1,
127 struct got_object_id *id2, const char *label1, const char *label2,
128 struct got_repository *repo)
130 struct got_diff_blob_output_unidiff_arg *a = arg;
132 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
136 const struct got_error *
137 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
138 const char *label1, const char *label2, int diff_context, FILE *outfile)
140 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
144 static const struct got_error *
145 alloc_changes(struct got_diff_changes **changes)
147 *changes = calloc(1, sizeof(**changes));
148 if (*changes == NULL)
149 return got_error_from_errno("calloc");
150 SIMPLEQ_INIT(&(*changes)->entries);
154 static const struct got_error *
155 diff_blob_file(struct got_diff_changes **changes,
156 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
157 const char *label2, int diff_context, FILE *outfile)
159 struct got_diff_state ds;
160 struct got_diff_args args;
161 const struct got_error *err = NULL;
163 char hex1[SHA1_DIGEST_STRING_LENGTH];
175 return got_error_from_errno("got_opentemp");
176 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
177 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
183 idstr1 = "/dev/null";
189 memset(&ds, 0, sizeof(ds));
190 /* XXX should stat buffers be passed in args instead of ds? */
191 ds.stb1.st_mode = S_IFREG;
193 ds.stb1.st_size = size1;
194 ds.stb1.st_mtime = 0; /* XXX */
196 ds.stb2.st_mode = S_IFREG;
197 ds.stb2.st_size = size2;
198 ds.stb2.st_mtime = 0; /* XXX */
200 memset(&args, 0, sizeof(args));
201 args.diff_format = D_UNIFIED;
202 args.label[0] = label2;
203 args.label[1] = label2;
204 args.diff_context = diff_context;
205 flags |= D_PROTOTYPE;
208 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
209 fprintf(outfile, "file + %s\n",
210 f2 == NULL ? "/dev/null" : label2);
213 err = alloc_changes(changes);
217 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
218 changes ? *changes : NULL);
219 got_diff_state_free(&ds);
221 if (f1 && fclose(f1) != 0 && err == NULL)
222 err = got_error_from_errno("fclose");
226 const struct got_error *
227 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
228 FILE *f2, size_t size2, const char *label2, int diff_context,
231 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
232 diff_context, outfile);
235 const struct got_error *
236 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
237 struct got_blob_object *blob1, FILE *f2, size_t size2)
239 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
243 const struct got_error *
244 got_diff_blob_lines_changed(struct got_diff_changes **changes,
245 struct got_blob_object *blob1, struct got_blob_object *blob2)
247 const struct got_error *err = NULL;
249 err = alloc_changes(changes);
253 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
255 got_diff_free_changes(*changes);
262 got_diff_free_changes(struct got_diff_changes *changes)
264 struct got_diff_change *change;
265 while (!SIMPLEQ_EMPTY(&changes->entries)) {
266 change = SIMPLEQ_FIRST(&changes->entries);
267 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
273 static const struct got_error *
274 diff_added_blob(struct got_object_id *id, const char *label,
275 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
277 const struct got_error *err;
278 struct got_blob_object *blob = NULL;
279 struct got_object *obj = NULL;
281 err = got_object_open(&obj, repo, id);
285 err = got_object_blob_open(&blob, repo, obj, 8192);
288 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
290 got_object_close(obj);
292 got_object_blob_close(blob);
296 static const struct got_error *
297 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
298 const char *label1, const char *label2, struct got_repository *repo,
299 got_diff_blob_cb cb, void *cb_arg)
301 const struct got_error *err;
302 struct got_object *obj1 = NULL;
303 struct got_object *obj2 = NULL;
304 struct got_blob_object *blob1 = NULL;
305 struct got_blob_object *blob2 = NULL;
307 err = got_object_open(&obj1, repo, id1);
310 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
311 err = got_error(GOT_ERR_OBJ_TYPE);
315 err = got_object_open(&obj2, repo, id2);
318 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
323 err = got_object_blob_open(&blob1, repo, obj1, 8192);
327 err = got_object_blob_open(&blob2, repo, obj2, 8192);
331 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
334 got_object_close(obj1);
336 got_object_close(obj2);
338 got_object_blob_close(blob1);
340 got_object_blob_close(blob2);
344 static const struct got_error *
345 diff_deleted_blob(struct got_object_id *id, const char *label,
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, blob, NULL, id, NULL, label, NULL, repo);
361 got_object_close(obj);
363 got_object_blob_close(blob);
367 static const struct got_error *
368 diff_added_tree(struct got_object_id *id, const char *label,
369 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
372 const struct got_error *err = NULL;
373 struct got_object *treeobj = NULL;
374 struct got_tree_object *tree = NULL;
376 err = got_object_open(&treeobj, repo, id);
380 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
381 err = got_error(GOT_ERR_OBJ_TYPE);
385 err = got_object_tree_open(&tree, repo, treeobj);
389 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
393 got_object_tree_close(tree);
395 got_object_close(treeobj);
399 static const struct got_error *
400 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
401 const char *label1, const char *label2, struct got_repository *repo,
402 got_diff_blob_cb cb, void *cb_arg, int diff_content)
404 const struct got_error *err;
405 struct got_object *treeobj1 = NULL;
406 struct got_object *treeobj2 = NULL;
407 struct got_tree_object *tree1 = NULL;
408 struct got_tree_object *tree2 = NULL;
410 err = got_object_open(&treeobj1, repo, id1);
414 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
415 err = got_error(GOT_ERR_OBJ_TYPE);
419 err = got_object_open(&treeobj2, repo, id2);
423 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
424 err = got_error(GOT_ERR_OBJ_TYPE);
428 err = got_object_tree_open(&tree1, repo, treeobj1);
432 err = got_object_tree_open(&tree2, repo, treeobj2);
436 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
441 got_object_tree_close(tree1);
443 got_object_tree_close(tree2);
445 got_object_close(treeobj1);
447 got_object_close(treeobj2);
451 static const struct got_error *
452 diff_deleted_tree(struct got_object_id *id, const char *label,
453 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
456 const struct got_error *err;
457 struct got_object *treeobj = NULL;
458 struct got_tree_object *tree = NULL;
460 err = got_object_open(&treeobj, repo, id);
464 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
465 err = got_error(GOT_ERR_OBJ_TYPE);
469 err = got_object_tree_open(&tree, repo, treeobj);
473 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
477 got_object_tree_close(tree);
479 got_object_close(treeobj);
483 static const struct got_error *
484 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
485 const char *label1, const char *label2, struct got_repository *repo,
486 got_diff_blob_cb cb, void *cb_arg)
492 static const struct got_error *
493 diff_entry_old_new(const struct got_tree_entry *te1,
494 const struct got_tree_entry *te2, const char *label1, const char *label2,
495 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
498 const struct got_error *err = NULL;
502 if (S_ISDIR(te1->mode))
503 err = diff_deleted_tree(te1->id, label1, repo,
504 cb, cb_arg, diff_content);
507 err = diff_deleted_blob(te1->id, label1, repo,
510 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
516 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
517 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
519 return diff_modified_tree(te1->id, te2->id,
520 label1, label2, repo, cb, cb_arg, diff_content);
521 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
524 return diff_modified_blob(te1->id, te2->id,
525 label1, label2, repo, cb, cb_arg);
527 return cb(cb_arg, NULL, NULL, te1->id,
528 te2->id, label1, label2, repo);
535 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
539 static const struct got_error *
540 diff_entry_new_old(const struct got_tree_entry *te2,
541 const struct got_tree_entry *te1, const char *label2,
542 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
545 if (te1 != NULL) /* handled by diff_entry_old_new() */
548 if (S_ISDIR(te2->mode))
549 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
553 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
555 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
558 const struct got_error *
559 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
560 const char *label1, const char *label2, struct got_repository *repo,
561 got_diff_blob_cb cb, void *cb_arg, int diff_content)
563 const struct got_error *err = NULL;
564 struct got_tree_entry *te1 = NULL;
565 struct got_tree_entry *te2 = NULL;
566 char *l1 = NULL, *l2 = NULL;
569 const struct got_tree_entries *entries;
570 entries = got_object_tree_get_entries(tree1);
571 te1 = SIMPLEQ_FIRST(&entries->head);
572 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
574 return got_error_from_errno("asprintf");
577 const struct got_tree_entries *entries;
578 entries = got_object_tree_get_entries(tree2);
579 te2 = SIMPLEQ_FIRST(&entries->head);
580 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
582 return got_error_from_errno("asprintf");
587 const struct got_tree_entry *te = NULL;
589 te = got_object_tree_find_entry(tree2,
594 if (te && asprintf(&l2, "%s%s%s", label2,
595 label2[0] ? "/" : "", te->name) == -1)
597 got_error_from_errno("asprintf");
599 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
600 cb_arg, diff_content);
606 const struct got_tree_entry *te = NULL;
608 te = got_object_tree_find_entry(tree1,
612 if (asprintf(&l2, "%s%s%s", label2,
613 label2[0] ? "/" : "", te->name) == -1)
615 got_error_from_errno("asprintf");
617 if (asprintf(&l2, "%s%s%s", label2,
618 label2[0] ? "/" : "", te2->name) == -1)
620 got_error_from_errno("asprintf");
622 err = diff_entry_new_old(te2, te, l2, repo,
623 cb, cb_arg, diff_content);
631 te1 = SIMPLEQ_NEXT(te1, entry);
633 asprintf(&l1, "%s%s%s", label1,
634 label1[0] ? "/" : "", te1->name) == -1)
635 return got_error_from_errno("asprintf");
640 te2 = SIMPLEQ_NEXT(te2, entry);
642 asprintf(&l2, "%s%s%s", label2,
643 label2[0] ? "/" : "", te2->name) == -1)
644 return got_error_from_errno("asprintf");
646 } while (te1 || te2);
651 const struct got_error *
652 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
653 const char *label1, const char *label2, int diff_context,
654 struct got_repository *repo, FILE *outfile)
656 const struct got_error *err;
657 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
659 if (id1 == NULL && id2 == NULL)
660 return got_error(GOT_ERR_NO_OBJ);
663 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
668 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
672 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
676 got_object_blob_close(blob1);
678 got_object_blob_close(blob2);
682 const struct got_error *
683 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
684 char *label1, char *label2, int diff_context, struct got_repository *repo,
687 const struct got_error *err;
688 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
689 struct got_diff_blob_output_unidiff_arg arg;
691 if (id1 == NULL && id2 == NULL)
692 return got_error(GOT_ERR_NO_OBJ);
695 err = got_object_open_as_tree(&tree1, repo, id1);
700 err = got_object_open_as_tree(&tree2, repo, id2);
704 arg.diff_context = diff_context;
705 arg.outfile = outfile;
706 err = got_diff_tree(tree1, tree2, label1, label2, repo,
707 got_diff_blob_output_unidiff, &arg, 1);
710 got_object_tree_close(tree1);
712 got_object_tree_close(tree2);
716 const struct got_error *
717 got_diff_objects_as_commits(struct got_object_id *id1,
718 struct got_object_id *id2, int diff_context,
719 struct got_repository *repo, FILE *outfile)
721 const struct got_error *err;
722 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
725 return got_error(GOT_ERR_NO_OBJ);
728 err = got_object_open_as_commit(&commit1, repo, id1);
733 err = got_object_open_as_commit(&commit2, repo, id2);
737 err = got_diff_objects_as_trees(
738 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
739 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
743 got_object_commit_close(commit1);
745 got_object_commit_close(commit2);
749 const struct got_error *
750 got_diff_files(struct got_diff_changes **changes,
751 struct got_diff_state **ds,
752 struct got_diff_args **args,
754 FILE *f1, size_t size1, const char *label1,
755 FILE *f2, size_t size2, const char *label2,
756 int diff_context, FILE *outfile)
758 const struct got_error *err = NULL;
762 *ds = calloc(1, sizeof(**ds));
764 return got_error_from_errno("calloc");
765 *args = calloc(1, sizeof(**args));
767 err = got_error_from_errno("calloc");
780 /* XXX should stat buffers be passed in args instead of ds? */
781 (*ds)->stb1.st_mode = S_IFREG;
782 (*ds)->stb1.st_size = size1;
783 (*ds)->stb1.st_mtime = 0; /* XXX */
785 (*ds)->stb2.st_mode = S_IFREG;
786 (*ds)->stb2.st_size = size2;
787 (*ds)->stb2.st_mtime = 0; /* XXX */
789 (*args)->diff_format = D_UNIFIED;
790 (*args)->label[0] = label1;
791 (*args)->label[1] = label2;
792 (*args)->diff_context = diff_context;
793 *flags |= D_PROTOTYPE;
796 fprintf(outfile, "file - %s\n",
797 f1 == NULL ? "/dev/null" : label1);
798 fprintf(outfile, "file + %s\n",
799 f2 == NULL ? "/dev/null" : label2);
802 err = alloc_changes(changes);
806 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
807 changes ? *changes : NULL);
811 got_diff_state_free(*ds);
821 got_diff_free_changes(*changes);