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.
26 #include "got_compat.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, int force_text_diff, 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];
75 else if (line_offsets) {
76 err = add_line_offset(line_offsets, nlines, 0);
87 return got_error_from_errno("got_opentemp");
93 err = got_error_from_errno("got_opentemp");
102 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
103 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
108 idstr1 = "/dev/null";
112 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
113 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
118 idstr2 = "/dev/null";
121 char *modestr1 = NULL, *modestr2 = NULL;
123 if (mode1 && mode1 != mode2) {
127 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
128 if (asprintf(&modestr1, " (mode %o)",
129 mode1 & modebits) == -1) {
130 err = got_error_from_errno("asprintf");
134 if (mode2 && mode1 != mode2) {
138 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
139 if (asprintf(&modestr2, " (mode %o)",
140 mode2 & modebits) == -1) {
141 err = got_error_from_errno("asprintf");
145 n = fprintf(outfile, "blob - %s%s\n", idstr1,
146 modestr1 ? modestr1 : "");
151 err = add_line_offset(line_offsets, nlines, outoff);
156 n = fprintf(outfile, "blob + %s%s\n", idstr2,
157 modestr2 ? modestr2 : "");
162 err = add_line_offset(line_offsets, nlines, outoff);
170 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
171 ignore_whitespace, force_text_diff);
176 err = got_diffreg_output(line_offsets, nlines, result,
177 blob1 != NULL, blob2 != NULL,
178 label1 ? label1 : idstr1,
179 label2 ? label2 : idstr2,
180 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
185 if (resultp && err == NULL)
188 free_err = got_diffreg_result_free(result);
189 if (free_err && err == NULL)
193 if (f1 && fclose(f1) == EOF && err == NULL)
194 err = got_error_from_errno("fclose");
195 if (f2 && fclose(f2) == EOF && err == NULL)
196 err = got_error_from_errno("fclose");
200 const struct got_error *
201 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
202 struct got_blob_object *blob2, struct got_object_id *id1,
203 struct got_object_id *id2, const char *label1, const char *label2,
204 mode_t mode1, mode_t mode2, struct got_repository *repo)
206 struct got_diff_blob_output_unidiff_arg *a = arg;
208 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
209 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
210 a->ignore_whitespace, a->force_text_diff, a->outfile);
213 const struct got_error *
214 got_diff_blob(off_t **line_offsets, size_t *nlines,
215 struct got_blob_object *blob1, struct got_blob_object *blob2,
216 const char *label1, const char *label2, int diff_context,
217 int ignore_whitespace, int force_text_diff, FILE *outfile)
219 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
220 label1, label2, 0, 0, diff_context, ignore_whitespace,
221 force_text_diff, outfile);
224 static const struct got_error *
225 diff_blob_file(struct got_diffreg_result **resultp,
226 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
227 const char *label2, int diff_context, int ignore_whitespace,
228 int force_text_diff, FILE *outfile)
230 const struct got_error *err = NULL, *free_err;
232 char hex1[SHA1_DIGEST_STRING_LENGTH];
235 struct got_diffreg_result *result = NULL;
244 return got_error_from_errno("got_opentemp");
245 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
246 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
251 idstr1 = "/dev/null";
255 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
256 fprintf(outfile, "file + %s\n",
257 f2 == NULL ? "/dev/null" : label2);
260 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
261 ignore_whitespace, force_text_diff);
266 err = got_diffreg_output(NULL, NULL, result,
267 blob1 != NULL, f2 != NULL,
268 label2, /* show local file's path, not a blob ID */
269 label2, GOT_DIFF_OUTPUT_UNIDIFF,
270 diff_context, outfile);
275 if (resultp && err == NULL)
278 free_err = got_diffreg_result_free(result);
279 if (free_err && err == NULL)
283 if (f1 && fclose(f1) == EOF && err == NULL)
284 err = got_error_from_errno("fclose");
288 const struct got_error *
289 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
290 FILE *f2, size_t size2, const char *label2, int diff_context,
291 int ignore_whitespace, int force_text_diff, FILE *outfile)
293 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
294 diff_context, ignore_whitespace, force_text_diff, outfile);
297 static const struct got_error *
298 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
299 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
301 const struct got_error *err;
302 struct got_blob_object *blob = NULL;
303 struct got_object *obj = NULL;
305 err = got_object_open(&obj, repo, id);
309 err = got_object_blob_open(&blob, repo, obj, 8192);
312 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
314 got_object_close(obj);
316 got_object_blob_close(blob);
320 static const struct got_error *
321 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
322 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
323 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
325 const struct got_error *err;
326 struct got_object *obj1 = NULL;
327 struct got_object *obj2 = NULL;
328 struct got_blob_object *blob1 = NULL;
329 struct got_blob_object *blob2 = NULL;
331 err = got_object_open(&obj1, repo, id1);
334 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
335 err = got_error(GOT_ERR_OBJ_TYPE);
339 err = got_object_open(&obj2, repo, id2);
342 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
343 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 err = got_object_blob_open(&blob1, repo, obj1, 8192);
351 err = got_object_blob_open(&blob2, repo, obj2, 8192);
355 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
359 got_object_close(obj1);
361 got_object_close(obj2);
363 got_object_blob_close(blob1);
365 got_object_blob_close(blob2);
369 static const struct got_error *
370 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
371 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
373 const struct got_error *err;
374 struct got_blob_object *blob = NULL;
375 struct got_object *obj = NULL;
377 err = got_object_open(&obj, repo, id);
381 err = got_object_blob_open(&blob, repo, obj, 8192);
384 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
386 got_object_close(obj);
388 got_object_blob_close(blob);
392 static const struct got_error *
393 diff_added_tree(struct got_object_id *id, const char *label,
394 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
397 const struct got_error *err = NULL;
398 struct got_object *treeobj = NULL;
399 struct got_tree_object *tree = NULL;
401 err = got_object_open(&treeobj, repo, id);
405 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
406 err = got_error(GOT_ERR_OBJ_TYPE);
410 err = got_object_tree_open(&tree, repo, treeobj);
414 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
418 got_object_tree_close(tree);
420 got_object_close(treeobj);
424 static const struct got_error *
425 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
426 const char *label1, const char *label2, struct got_repository *repo,
427 got_diff_blob_cb cb, void *cb_arg, int diff_content)
429 const struct got_error *err;
430 struct got_object *treeobj1 = NULL;
431 struct got_object *treeobj2 = NULL;
432 struct got_tree_object *tree1 = NULL;
433 struct got_tree_object *tree2 = NULL;
435 err = got_object_open(&treeobj1, repo, id1);
439 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
440 err = got_error(GOT_ERR_OBJ_TYPE);
444 err = got_object_open(&treeobj2, repo, id2);
448 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
449 err = got_error(GOT_ERR_OBJ_TYPE);
453 err = got_object_tree_open(&tree1, repo, treeobj1);
457 err = got_object_tree_open(&tree2, repo, treeobj2);
461 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
466 got_object_tree_close(tree1);
468 got_object_tree_close(tree2);
470 got_object_close(treeobj1);
472 got_object_close(treeobj2);
476 static const struct got_error *
477 diff_deleted_tree(struct got_object_id *id, const char *label,
478 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
481 const struct got_error *err;
482 struct got_object *treeobj = NULL;
483 struct got_tree_object *tree = NULL;
485 err = got_object_open(&treeobj, repo, id);
489 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
490 err = got_error(GOT_ERR_OBJ_TYPE);
494 err = got_object_tree_open(&tree, repo, treeobj);
498 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
502 got_object_tree_close(tree);
504 got_object_close(treeobj);
508 static const struct got_error *
509 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
510 const char *label1, const char *label2, struct got_repository *repo,
511 got_diff_blob_cb cb, void *cb_arg)
517 static const struct got_error *
518 diff_entry_old_new(struct got_tree_entry *te1,
519 struct got_tree_entry *te2, const char *label1, const char *label2,
520 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
523 const struct got_error *err = NULL;
526 if (got_object_tree_entry_is_submodule(te1))
530 if (S_ISDIR(te1->mode))
531 err = diff_deleted_tree(&te1->id, label1, repo,
532 cb, cb_arg, diff_content);
535 err = diff_deleted_blob(&te1->id, label1,
536 te1->mode, repo, cb, cb_arg);
538 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
539 label1, NULL, te1->mode, 0, repo);
542 } else if (got_object_tree_entry_is_submodule(te2))
545 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
546 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
548 return diff_modified_tree(&te1->id, &te2->id,
549 label1, label2, repo, cb, cb_arg, diff_content);
550 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
551 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
553 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
554 (te2->mode & (S_IFLNK | S_IXUSR))) {
556 return diff_modified_blob(&te1->id, &te2->id,
557 label1, label2, te1->mode, te2->mode,
560 return cb(cb_arg, NULL, NULL, &te1->id,
561 &te2->id, label1, label2, te1->mode,
569 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
573 static const struct got_error *
574 diff_entry_new_old(struct got_tree_entry *te2,
575 struct got_tree_entry *te1, const char *label2,
576 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
579 if (te1 != NULL) /* handled by diff_entry_old_new() */
582 if (got_object_tree_entry_is_submodule(te2))
585 if (S_ISDIR(te2->mode))
586 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
590 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
593 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
597 const struct got_error *
598 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
599 struct got_blob_object *blob2, struct got_object_id *id1,
600 struct got_object_id *id2, const char *label1, const char *label2,
601 mode_t mode1, mode_t mode2, struct got_repository *repo)
603 const struct got_error *err = NULL;
604 struct got_pathlist_head *paths = arg;
605 struct got_diff_changed_path *change = NULL;
608 path = strdup(label2 ? label2 : label1);
610 return got_error_from_errno("malloc");
612 change = malloc(sizeof(*change));
613 if (change == NULL) {
614 err = got_error_from_errno("malloc");
618 change->status = GOT_STATUS_NO_CHANGE;
620 change->status = GOT_STATUS_ADD;
621 else if (id2 == NULL)
622 change->status = GOT_STATUS_DELETE;
624 if (got_object_id_cmp(id1, id2) != 0)
625 change->status = GOT_STATUS_MODIFY;
626 else if (mode1 != mode2)
627 change->status = GOT_STATUS_MODE_CHANGE;
630 err = got_pathlist_insert(NULL, paths, path, change);
639 const struct got_error *
640 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
641 const char *label1, const char *label2, struct got_repository *repo,
642 got_diff_blob_cb cb, void *cb_arg, int diff_content)
644 const struct got_error *err = NULL;
645 struct got_tree_entry *te1 = NULL;
646 struct got_tree_entry *te2 = NULL;
647 char *l1 = NULL, *l2 = NULL;
648 int tidx1 = 0, tidx2 = 0;
651 te1 = got_object_tree_get_entry(tree1, 0);
652 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
654 return got_error_from_errno("asprintf");
657 te2 = got_object_tree_get_entry(tree2, 0);
658 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
660 return got_error_from_errno("asprintf");
665 struct got_tree_entry *te = NULL;
667 te = got_object_tree_find_entry(tree2,
672 if (te && asprintf(&l2, "%s%s%s", label2,
673 label2[0] ? "/" : "", te->name) == -1)
675 got_error_from_errno("asprintf");
677 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
678 cb_arg, diff_content);
684 struct got_tree_entry *te = NULL;
686 te = got_object_tree_find_entry(tree1,
690 if (asprintf(&l2, "%s%s%s", label2,
691 label2[0] ? "/" : "", te->name) == -1)
693 got_error_from_errno("asprintf");
695 if (asprintf(&l2, "%s%s%s", label2,
696 label2[0] ? "/" : "", te2->name) == -1)
698 got_error_from_errno("asprintf");
700 err = diff_entry_new_old(te2, te, l2, repo,
701 cb, cb_arg, diff_content);
710 te1 = got_object_tree_get_entry(tree1, tidx1);
712 asprintf(&l1, "%s%s%s", label1,
713 label1[0] ? "/" : "", te1->name) == -1)
714 return got_error_from_errno("asprintf");
720 te2 = got_object_tree_get_entry(tree2, tidx2);
722 asprintf(&l2, "%s%s%s", label2,
723 label2[0] ? "/" : "", te2->name) == -1)
724 return got_error_from_errno("asprintf");
726 } while (te1 || te2);
731 const struct got_error *
732 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
733 struct got_object_id *id1, struct got_object_id *id2,
734 const char *label1, const char *label2, int diff_context,
735 int ignore_whitespace, int force_text_diff,
736 struct got_repository *repo, FILE *outfile)
738 const struct got_error *err;
739 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
741 if (id1 == NULL && id2 == NULL)
742 return got_error(GOT_ERR_NO_OBJ);
745 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
750 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
754 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
755 label1, label2, diff_context, ignore_whitespace, force_text_diff,
759 got_object_blob_close(blob1);
761 got_object_blob_close(blob2);
765 const struct got_error *
766 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
767 struct got_object_id *id1, struct got_object_id *id2,
768 char *label1, char *label2, int diff_context, int ignore_whitespace,
769 int force_text_diff, struct got_repository *repo, FILE *outfile)
771 const struct got_error *err;
772 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
773 struct got_diff_blob_output_unidiff_arg arg;
774 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
776 if (id1 == NULL && id2 == NULL)
777 return got_error(GOT_ERR_NO_OBJ);
780 err = got_object_open_as_tree(&tree1, repo, id1);
785 err = got_object_open_as_tree(&tree2, repo, id2);
789 arg.diff_context = diff_context;
790 arg.ignore_whitespace = ignore_whitespace;
791 arg.force_text_diff = force_text_diff;
792 arg.outfile = outfile;
793 if (want_lineoffsets) {
794 arg.line_offsets = *line_offsets;
795 arg.nlines = *nlines;
797 arg.line_offsets = NULL;
800 err = got_diff_tree(tree1, tree2, label1, label2, repo,
801 got_diff_blob_output_unidiff, &arg, 1);
803 if (want_lineoffsets) {
804 *line_offsets = arg.line_offsets; /* was likely re-allocated */
805 *nlines = arg.nlines;
809 got_object_tree_close(tree1);
811 got_object_tree_close(tree2);
815 const struct got_error *
816 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
817 struct got_object_id *id1, struct got_object_id *id2,
818 int diff_context, int ignore_whitespace, int force_text_diff,
819 struct got_repository *repo, FILE *outfile)
821 const struct got_error *err;
822 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
825 return got_error(GOT_ERR_NO_OBJ);
828 err = got_object_open_as_commit(&commit1, repo, id1);
833 err = got_object_open_as_commit(&commit2, repo, id2);
837 err = got_diff_objects_as_trees(line_offsets, nlines,
838 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
839 got_object_commit_get_tree_id(commit2), "", "", diff_context,
840 ignore_whitespace, force_text_diff, repo, outfile);
843 got_object_commit_close(commit1);
845 got_object_commit_close(commit2);
849 const struct got_error *
850 got_diff_files(struct got_diffreg_result **resultp,
851 FILE *f1, const char *label1, FILE *f2, const char *label2,
852 int diff_context, int ignore_whitespace, int force_text_diff,
855 const struct got_error *err = NULL;
856 struct got_diffreg_result *diffreg_result = NULL;
862 fprintf(outfile, "file - %s\n",
863 f1 == NULL ? "/dev/null" : label1);
864 fprintf(outfile, "file + %s\n",
865 f2 == NULL ? "/dev/null" : label2);
868 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
869 ignore_whitespace, force_text_diff);
874 err = got_diffreg_output(NULL, NULL, diffreg_result,
875 f1 != NULL, f2 != NULL, label1, label2,
876 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
882 if (resultp && err == NULL)
883 *resultp = diffreg_result;
884 else if (diffreg_result) {
885 const struct got_error *free_err;
886 free_err = got_diffreg_result_free(diffreg_result);
887 if (free_err && err == NULL)