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>
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
32 #include "got_opentemp.h"
34 #include "got_cancel.h"
35 #include "got_worktree.h"
37 #include "got_lib_diff.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
42 static const struct got_error *
43 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
47 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
49 return got_error_from_errno("reallocarray");
51 (*line_offsets)[*nlines] = off;
56 static const struct got_error *
57 diff_blobs(off_t **line_offsets, size_t *nlines,
58 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
59 struct got_blob_object *blob2,
60 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61 int diff_context, int ignore_whitespace, FILE *outfile)
63 const struct got_error *err = NULL, *free_err;
64 FILE *f1 = NULL, *f2 = NULL;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
67 char *idstr1 = NULL, *idstr2 = NULL;
69 struct got_diffreg_result *result;
73 if (line_offsets && *line_offsets && *nlines > 0)
74 outoff = (*line_offsets)[*nlines - 1];
82 return got_error_from_errno("got_opentemp");
88 err = got_error_from_errno("got_opentemp");
96 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
102 idstr1 = "/dev/null";
106 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
112 idstr2 = "/dev/null";
115 char *modestr1 = NULL, *modestr2 = NULL;
117 if (mode1 && mode1 != mode2) {
121 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122 if (asprintf(&modestr1, " (mode %o)",
123 mode1 & modebits) == -1) {
124 err = got_error_from_errno("asprintf");
128 if (mode2 && mode1 != mode2) {
132 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133 if (asprintf(&modestr2, " (mode %o)",
134 mode2 & modebits) == -1) {
135 err = got_error_from_errno("asprintf");
139 n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 modestr1 ? modestr1 : "");
145 err = add_line_offset(line_offsets, nlines, outoff);
150 n = fprintf(outfile, "blob + %s%s\n", idstr2,
151 modestr2 ? modestr2 : "");
156 err = add_line_offset(line_offsets, nlines, outoff);
164 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
170 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
171 label1 ? label1 : idstr1,
172 label2 ? label2 : idstr2,
173 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
178 if (resultp && err == NULL)
181 free_err = got_diffreg_result_free(result);
182 if (free_err && err == NULL)
186 if (f1 && fclose(f1) != 0 && err == NULL)
187 err = got_error_from_errno("fclose");
188 if (f2 && fclose(f2) != 0 && err == NULL)
189 err = got_error_from_errno("fclose");
193 const struct got_error *
194 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
195 struct got_blob_object *blob2, struct got_object_id *id1,
196 struct got_object_id *id2, const char *label1, const char *label2,
197 mode_t mode1, mode_t mode2, struct got_repository *repo)
199 struct got_diff_blob_output_unidiff_arg *a = arg;
201 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
202 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
203 a->ignore_whitespace, a->outfile);
206 const struct got_error *
207 got_diff_blob(off_t **line_offsets, size_t *nlines,
208 struct got_blob_object *blob1, struct got_blob_object *blob2,
209 const char *label1, const char *label2, int diff_context,
210 int ignore_whitespace, FILE *outfile)
212 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
213 label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
216 static const struct got_error *
217 diff_blob_file(struct got_diffreg_result **resultp,
218 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
219 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
221 const struct got_error *err = NULL, *free_err;
223 char hex1[SHA1_DIGEST_STRING_LENGTH];
226 struct got_diffreg_result *result = NULL;
235 return got_error_from_errno("got_opentemp");
236 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
237 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
242 idstr1 = "/dev/null";
246 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
247 fprintf(outfile, "file + %s\n",
248 f2 == NULL ? "/dev/null" : label2);
251 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
257 err = got_diffreg_output(NULL, NULL, result, f1, f2,
258 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
264 if (resultp && err == NULL)
267 free_err = got_diffreg_result_free(result);
268 if (free_err && err == NULL)
272 if (f1 && fclose(f1) != 0 && err == NULL)
273 err = got_error_from_errno("fclose");
277 const struct got_error *
278 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
279 FILE *f2, size_t size2, const char *label2, int diff_context,
280 int ignore_whitespace, FILE *outfile)
282 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
283 diff_context, ignore_whitespace, outfile);
286 const struct got_error *
287 got_diff_blob_prepared_file(struct got_diffreg_result **resultp,
288 struct diff_data *data1, struct got_blob_object *blob1,
289 struct diff_data *data2, FILE *f2, char *p2, size_t size2,
290 const struct diff_config *cfg, int ignore_whitespace)
292 const struct got_error *err = NULL, *free_err;
294 char hex1[SHA1_DIGEST_STRING_LENGTH];
295 char *idstr1 = NULL, *p1 = NULL;
297 struct got_diffreg_result *result = NULL;
306 return got_error_from_errno("got_opentemp");
307 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
308 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
313 idstr1 = "/dev/null";
316 err = got_diff_prepare_file(&f1, &p1, &f1_created, &size,
317 data1, cfg, ignore_whitespace);
321 err = got_diffreg_prepared_files(&result, cfg, data1, f1,
322 p1, size1, data2, f2, p2, size2);
330 free_err = got_diffreg_result_free_left(result);
332 free_err = got_diffreg_close(f1, p1, size1, NULL,
334 if (free_err && err == NULL)
340 static const struct got_error *
341 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
342 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
344 const struct got_error *err;
345 struct got_blob_object *blob = NULL;
346 struct got_object *obj = NULL;
348 err = got_object_open(&obj, repo, id);
352 err = got_object_blob_open(&blob, repo, obj, 8192);
355 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
357 got_object_close(obj);
359 got_object_blob_close(blob);
363 static const struct got_error *
364 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
365 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
366 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
368 const struct got_error *err;
369 struct got_object *obj1 = NULL;
370 struct got_object *obj2 = NULL;
371 struct got_blob_object *blob1 = NULL;
372 struct got_blob_object *blob2 = NULL;
374 err = got_object_open(&obj1, repo, id1);
377 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
378 err = got_error(GOT_ERR_OBJ_TYPE);
382 err = got_object_open(&obj2, repo, id2);
385 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
386 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 err = got_object_blob_open(&blob1, repo, obj1, 8192);
394 err = got_object_blob_open(&blob2, repo, obj2, 8192);
398 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
402 got_object_close(obj1);
404 got_object_close(obj2);
406 got_object_blob_close(blob1);
408 got_object_blob_close(blob2);
412 static const struct got_error *
413 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
414 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
416 const struct got_error *err;
417 struct got_blob_object *blob = NULL;
418 struct got_object *obj = NULL;
420 err = got_object_open(&obj, repo, id);
424 err = got_object_blob_open(&blob, repo, obj, 8192);
427 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
429 got_object_close(obj);
431 got_object_blob_close(blob);
435 static const struct got_error *
436 diff_added_tree(struct got_object_id *id, const char *label,
437 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
440 const struct got_error *err = NULL;
441 struct got_object *treeobj = NULL;
442 struct got_tree_object *tree = NULL;
444 err = got_object_open(&treeobj, repo, id);
448 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
449 err = got_error(GOT_ERR_OBJ_TYPE);
453 err = got_object_tree_open(&tree, repo, treeobj);
457 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
461 got_object_tree_close(tree);
463 got_object_close(treeobj);
467 static const struct got_error *
468 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
469 const char *label1, const char *label2, struct got_repository *repo,
470 got_diff_blob_cb cb, void *cb_arg, int diff_content)
472 const struct got_error *err;
473 struct got_object *treeobj1 = NULL;
474 struct got_object *treeobj2 = NULL;
475 struct got_tree_object *tree1 = NULL;
476 struct got_tree_object *tree2 = NULL;
478 err = got_object_open(&treeobj1, repo, id1);
482 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
483 err = got_error(GOT_ERR_OBJ_TYPE);
487 err = got_object_open(&treeobj2, repo, id2);
491 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
492 err = got_error(GOT_ERR_OBJ_TYPE);
496 err = got_object_tree_open(&tree1, repo, treeobj1);
500 err = got_object_tree_open(&tree2, repo, treeobj2);
504 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
509 got_object_tree_close(tree1);
511 got_object_tree_close(tree2);
513 got_object_close(treeobj1);
515 got_object_close(treeobj2);
519 static const struct got_error *
520 diff_deleted_tree(struct got_object_id *id, const char *label,
521 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
524 const struct got_error *err;
525 struct got_object *treeobj = NULL;
526 struct got_tree_object *tree = NULL;
528 err = got_object_open(&treeobj, repo, id);
532 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
533 err = got_error(GOT_ERR_OBJ_TYPE);
537 err = got_object_tree_open(&tree, repo, treeobj);
541 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
545 got_object_tree_close(tree);
547 got_object_close(treeobj);
551 static const struct got_error *
552 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
553 const char *label1, const char *label2, struct got_repository *repo,
554 got_diff_blob_cb cb, void *cb_arg)
560 static const struct got_error *
561 diff_entry_old_new(struct got_tree_entry *te1,
562 struct got_tree_entry *te2, const char *label1, const char *label2,
563 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
566 const struct got_error *err = NULL;
569 if (got_object_tree_entry_is_submodule(te1))
573 if (S_ISDIR(te1->mode))
574 err = diff_deleted_tree(&te1->id, label1, repo,
575 cb, cb_arg, diff_content);
578 err = diff_deleted_blob(&te1->id, label1,
579 te1->mode, repo, cb, cb_arg);
581 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
582 label1, NULL, te1->mode, 0, repo);
585 } else if (got_object_tree_entry_is_submodule(te2))
588 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
589 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
591 return diff_modified_tree(&te1->id, &te2->id,
592 label1, label2, repo, cb, cb_arg, diff_content);
593 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
594 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
596 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
597 (te2->mode & (S_IFLNK | S_IXUSR))) {
599 return diff_modified_blob(&te1->id, &te2->id,
600 label1, label2, te1->mode, te2->mode,
603 return cb(cb_arg, NULL, NULL, &te1->id,
604 &te2->id, label1, label2, te1->mode,
612 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
616 static const struct got_error *
617 diff_entry_new_old(struct got_tree_entry *te2,
618 struct got_tree_entry *te1, const char *label2,
619 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
622 if (te1 != NULL) /* handled by diff_entry_old_new() */
625 if (got_object_tree_entry_is_submodule(te2))
628 if (S_ISDIR(te2->mode))
629 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
633 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
636 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
640 const struct got_error *
641 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
642 struct got_blob_object *blob2, struct got_object_id *id1,
643 struct got_object_id *id2, const char *label1, const char *label2,
644 mode_t mode1, mode_t mode2, struct got_repository *repo)
646 const struct got_error *err = NULL;
647 struct got_pathlist_head *paths = arg;
648 struct got_diff_changed_path *change = NULL;
651 path = strdup(label2 ? label2 : label1);
653 return got_error_from_errno("malloc");
655 change = malloc(sizeof(*change));
656 if (change == NULL) {
657 err = got_error_from_errno("malloc");
661 change->status = GOT_STATUS_NO_CHANGE;
663 change->status = GOT_STATUS_ADD;
664 else if (id2 == NULL)
665 change->status = GOT_STATUS_DELETE;
667 if (got_object_id_cmp(id1, id2) != 0)
668 change->status = GOT_STATUS_MODIFY;
669 else if (mode1 != mode2)
670 change->status = GOT_STATUS_MODE_CHANGE;
673 err = got_pathlist_insert(NULL, paths, path, change);
682 const struct got_error *
683 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
684 const char *label1, const char *label2, struct got_repository *repo,
685 got_diff_blob_cb cb, void *cb_arg, int diff_content)
687 const struct got_error *err = NULL;
688 struct got_tree_entry *te1 = NULL;
689 struct got_tree_entry *te2 = NULL;
690 char *l1 = NULL, *l2 = NULL;
691 int tidx1 = 0, tidx2 = 0;
694 te1 = got_object_tree_get_entry(tree1, 0);
695 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
697 return got_error_from_errno("asprintf");
700 te2 = got_object_tree_get_entry(tree2, 0);
701 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
703 return got_error_from_errno("asprintf");
708 struct got_tree_entry *te = NULL;
710 te = got_object_tree_find_entry(tree2,
715 if (te && asprintf(&l2, "%s%s%s", label2,
716 label2[0] ? "/" : "", te->name) == -1)
718 got_error_from_errno("asprintf");
720 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
721 cb_arg, diff_content);
727 struct got_tree_entry *te = NULL;
729 te = got_object_tree_find_entry(tree1,
733 if (asprintf(&l2, "%s%s%s", label2,
734 label2[0] ? "/" : "", te->name) == -1)
736 got_error_from_errno("asprintf");
738 if (asprintf(&l2, "%s%s%s", label2,
739 label2[0] ? "/" : "", te2->name) == -1)
741 got_error_from_errno("asprintf");
743 err = diff_entry_new_old(te2, te, l2, repo,
744 cb, cb_arg, diff_content);
753 te1 = got_object_tree_get_entry(tree1, tidx1);
755 asprintf(&l1, "%s%s%s", label1,
756 label1[0] ? "/" : "", te1->name) == -1)
757 return got_error_from_errno("asprintf");
763 te2 = got_object_tree_get_entry(tree2, tidx2);
765 asprintf(&l2, "%s%s%s", label2,
766 label2[0] ? "/" : "", te2->name) == -1)
767 return got_error_from_errno("asprintf");
769 } while (te1 || te2);
774 const struct got_error *
775 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
776 struct got_object_id *id1, struct got_object_id *id2,
777 const char *label1, const char *label2, int diff_context,
778 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
780 const struct got_error *err;
781 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
783 if (id1 == NULL && id2 == NULL)
784 return got_error(GOT_ERR_NO_OBJ);
787 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
792 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
796 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
797 label1, label2, diff_context, ignore_whitespace, outfile);
800 got_object_blob_close(blob1);
802 got_object_blob_close(blob2);
806 const struct got_error *
807 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
808 struct got_object_id *id1, struct got_object_id *id2,
809 char *label1, char *label2, int diff_context, int ignore_whitespace,
810 struct got_repository *repo, FILE *outfile)
812 const struct got_error *err;
813 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
814 struct got_diff_blob_output_unidiff_arg arg;
815 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
817 if (id1 == NULL && id2 == NULL)
818 return got_error(GOT_ERR_NO_OBJ);
821 err = got_object_open_as_tree(&tree1, repo, id1);
826 err = got_object_open_as_tree(&tree2, repo, id2);
830 arg.diff_context = diff_context;
831 arg.ignore_whitespace = ignore_whitespace;
832 arg.outfile = outfile;
833 if (want_lineoffsets) {
834 arg.line_offsets = *line_offsets;
835 arg.nlines = *nlines;
837 arg.line_offsets = NULL;
840 err = got_diff_tree(tree1, tree2, label1, label2, repo,
841 got_diff_blob_output_unidiff, &arg, 1);
843 if (want_lineoffsets) {
844 *line_offsets = arg.line_offsets; /* was likely re-allocated */
845 *nlines = arg.nlines;
849 got_object_tree_close(tree1);
851 got_object_tree_close(tree2);
855 const struct got_error *
856 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
857 struct got_object_id *id1, struct got_object_id *id2,
858 int diff_context, int ignore_whitespace,
859 struct got_repository *repo, FILE *outfile)
861 const struct got_error *err;
862 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
865 return got_error(GOT_ERR_NO_OBJ);
868 err = got_object_open_as_commit(&commit1, repo, id1);
873 err = got_object_open_as_commit(&commit2, repo, id2);
877 err = got_diff_objects_as_trees(line_offsets, nlines,
878 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
879 got_object_commit_get_tree_id(commit2), "", "", diff_context,
880 ignore_whitespace, repo, outfile);
883 got_object_commit_close(commit1);
885 got_object_commit_close(commit2);
889 const struct got_error *
890 got_diff_files(struct got_diffreg_result **resultp,
891 FILE *f1, const char *label1, FILE *f2, const char *label2,
892 int diff_context, int ignore_whitespace, FILE *outfile)
894 const struct got_error *err = NULL;
895 struct got_diffreg_result *diffreg_result = NULL;
901 fprintf(outfile, "file - %s\n",
902 f1 == NULL ? "/dev/null" : label1);
903 fprintf(outfile, "file + %s\n",
904 f2 == NULL ? "/dev/null" : label2);
907 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
913 err = got_diffreg_output(NULL, NULL, diffreg_result,
914 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
915 diff_context, outfile);
921 if (resultp && err == NULL)
922 *resultp = diffreg_result;
923 else if (diffreg_result) {
924 const struct got_error *free_err;
925 free_err = got_diffreg_result_free(diffreg_result);
926 if (free_err && err == NULL)