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"
33 #include "got_cancel.h"
34 #include "got_worktree.h"
36 #include "got_lib_diff.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
41 static const struct got_error *
42 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
46 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
48 return got_error_from_errno("reallocarray");
50 (*line_offsets)[*nlines] = off;
55 static const struct got_error *
56 diff_blobs(off_t **line_offsets, size_t *nlines,
57 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
58 struct got_blob_object *blob2,
59 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
60 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
62 const struct got_error *err = NULL, *free_err;
63 FILE *f1 = NULL, *f2 = NULL;
64 char hex1[SHA1_DIGEST_STRING_LENGTH];
65 char hex2[SHA1_DIGEST_STRING_LENGTH];
66 char *idstr1 = NULL, *idstr2 = NULL;
68 struct got_diffreg_result *result;
72 if (line_offsets && *line_offsets && *nlines > 0)
73 outoff = (*line_offsets)[*nlines - 1];
81 return got_error_from_errno("got_opentemp");
87 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,
165 ignore_whitespace, force_text_diff);
170 err = got_diffreg_output(line_offsets, nlines, result,
171 blob1 != NULL, blob2 != NULL,
172 label1 ? label1 : idstr1,
173 label2 ? label2 : idstr2,
174 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
179 if (resultp && err == NULL)
182 free_err = got_diffreg_result_free(result);
183 if (free_err && err == NULL)
187 if (f1 && fclose(f1) != 0 && err == NULL)
188 err = got_error_from_errno("fclose");
189 if (f2 && fclose(f2) != 0 && err == NULL)
190 err = got_error_from_errno("fclose");
194 const struct got_error *
195 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
196 struct got_blob_object *blob2, struct got_object_id *id1,
197 struct got_object_id *id2, const char *label1, const char *label2,
198 mode_t mode1, mode_t mode2, struct got_repository *repo)
200 struct got_diff_blob_output_unidiff_arg *a = arg;
202 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
203 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
204 a->ignore_whitespace, a->force_text_diff, a->outfile);
207 const struct got_error *
208 got_diff_blob(off_t **line_offsets, size_t *nlines,
209 struct got_blob_object *blob1, struct got_blob_object *blob2,
210 const char *label1, const char *label2, int diff_context,
211 int ignore_whitespace, int force_text_diff, FILE *outfile)
213 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
214 label1, label2, 0, 0, diff_context, ignore_whitespace,
215 force_text_diff, outfile);
218 static const struct got_error *
219 diff_blob_file(struct got_diffreg_result **resultp,
220 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
221 const char *label2, int diff_context, int ignore_whitespace,
222 int force_text_diff, FILE *outfile)
224 const struct got_error *err = NULL, *free_err;
226 char hex1[SHA1_DIGEST_STRING_LENGTH];
229 struct got_diffreg_result *result = NULL;
238 return got_error_from_errno("got_opentemp");
239 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
240 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
245 idstr1 = "/dev/null";
249 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
250 fprintf(outfile, "file + %s\n",
251 f2 == NULL ? "/dev/null" : label2);
254 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
255 ignore_whitespace, force_text_diff);
260 err = got_diffreg_output(NULL, NULL, result,
261 blob1 != NULL, f2 != NULL,
262 label2, /* show local file's path, not a blob ID */
263 label2, GOT_DIFF_OUTPUT_UNIDIFF,
264 diff_context, outfile);
269 if (resultp && err == NULL)
272 free_err = got_diffreg_result_free(result);
273 if (free_err && err == NULL)
277 if (f1 && fclose(f1) != 0 && err == NULL)
278 err = got_error_from_errno("fclose");
282 const struct got_error *
283 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
284 FILE *f2, size_t size2, const char *label2, int diff_context,
285 int ignore_whitespace, int force_text_diff, FILE *outfile)
287 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
288 diff_context, ignore_whitespace, force_text_diff, outfile);
291 static const struct got_error *
292 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
293 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
295 const struct got_error *err;
296 struct got_blob_object *blob = NULL;
297 struct got_object *obj = NULL;
299 err = got_object_open(&obj, repo, id);
303 err = got_object_blob_open(&blob, repo, obj, 8192);
306 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
308 got_object_close(obj);
310 got_object_blob_close(blob);
314 static const struct got_error *
315 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
316 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
319 const struct got_error *err;
320 struct got_object *obj1 = NULL;
321 struct got_object *obj2 = NULL;
322 struct got_blob_object *blob1 = NULL;
323 struct got_blob_object *blob2 = NULL;
325 err = got_object_open(&obj1, repo, id1);
328 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
329 err = got_error(GOT_ERR_OBJ_TYPE);
333 err = got_object_open(&obj2, repo, id2);
336 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
337 err = got_error(GOT_ERR_BAD_OBJ_DATA);
341 err = got_object_blob_open(&blob1, repo, obj1, 8192);
345 err = got_object_blob_open(&blob2, repo, obj2, 8192);
349 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
353 got_object_close(obj1);
355 got_object_close(obj2);
357 got_object_blob_close(blob1);
359 got_object_blob_close(blob2);
363 static const struct got_error *
364 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
365 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
367 const struct got_error *err;
368 struct got_blob_object *blob = NULL;
369 struct got_object *obj = NULL;
371 err = got_object_open(&obj, repo, id);
375 err = got_object_blob_open(&blob, repo, obj, 8192);
378 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
380 got_object_close(obj);
382 got_object_blob_close(blob);
386 static const struct got_error *
387 diff_added_tree(struct got_object_id *id, const char *label,
388 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
391 const struct got_error *err = NULL;
392 struct got_object *treeobj = NULL;
393 struct got_tree_object *tree = NULL;
395 err = got_object_open(&treeobj, repo, id);
399 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
400 err = got_error(GOT_ERR_OBJ_TYPE);
404 err = got_object_tree_open(&tree, repo, treeobj);
408 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
412 got_object_tree_close(tree);
414 got_object_close(treeobj);
418 static const struct got_error *
419 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
420 const char *label1, const char *label2, struct got_repository *repo,
421 got_diff_blob_cb cb, void *cb_arg, int diff_content)
423 const struct got_error *err;
424 struct got_object *treeobj1 = NULL;
425 struct got_object *treeobj2 = NULL;
426 struct got_tree_object *tree1 = NULL;
427 struct got_tree_object *tree2 = NULL;
429 err = got_object_open(&treeobj1, repo, id1);
433 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
434 err = got_error(GOT_ERR_OBJ_TYPE);
438 err = got_object_open(&treeobj2, repo, id2);
442 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
443 err = got_error(GOT_ERR_OBJ_TYPE);
447 err = got_object_tree_open(&tree1, repo, treeobj1);
451 err = got_object_tree_open(&tree2, repo, treeobj2);
455 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
460 got_object_tree_close(tree1);
462 got_object_tree_close(tree2);
464 got_object_close(treeobj1);
466 got_object_close(treeobj2);
470 static const struct got_error *
471 diff_deleted_tree(struct got_object_id *id, const char *label,
472 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
475 const struct got_error *err;
476 struct got_object *treeobj = NULL;
477 struct got_tree_object *tree = NULL;
479 err = got_object_open(&treeobj, repo, id);
483 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
484 err = got_error(GOT_ERR_OBJ_TYPE);
488 err = got_object_tree_open(&tree, repo, treeobj);
492 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
496 got_object_tree_close(tree);
498 got_object_close(treeobj);
502 static const struct got_error *
503 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
504 const char *label1, const char *label2, struct got_repository *repo,
505 got_diff_blob_cb cb, void *cb_arg)
511 static const struct got_error *
512 diff_entry_old_new(struct got_tree_entry *te1,
513 struct got_tree_entry *te2, const char *label1, const char *label2,
514 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
517 const struct got_error *err = NULL;
520 if (got_object_tree_entry_is_submodule(te1))
524 if (S_ISDIR(te1->mode))
525 err = diff_deleted_tree(&te1->id, label1, repo,
526 cb, cb_arg, diff_content);
529 err = diff_deleted_blob(&te1->id, label1,
530 te1->mode, repo, cb, cb_arg);
532 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
533 label1, NULL, te1->mode, 0, repo);
536 } else if (got_object_tree_entry_is_submodule(te2))
539 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
540 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
542 return diff_modified_tree(&te1->id, &te2->id,
543 label1, label2, repo, cb, cb_arg, diff_content);
544 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
545 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
547 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
548 (te2->mode & (S_IFLNK | S_IXUSR))) {
550 return diff_modified_blob(&te1->id, &te2->id,
551 label1, label2, te1->mode, te2->mode,
554 return cb(cb_arg, NULL, NULL, &te1->id,
555 &te2->id, label1, label2, te1->mode,
563 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
567 static const struct got_error *
568 diff_entry_new_old(struct got_tree_entry *te2,
569 struct got_tree_entry *te1, const char *label2,
570 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
573 if (te1 != NULL) /* handled by diff_entry_old_new() */
576 if (got_object_tree_entry_is_submodule(te2))
579 if (S_ISDIR(te2->mode))
580 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
584 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
587 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
591 const struct got_error *
592 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
593 struct got_blob_object *blob2, struct got_object_id *id1,
594 struct got_object_id *id2, const char *label1, const char *label2,
595 mode_t mode1, mode_t mode2, struct got_repository *repo)
597 const struct got_error *err = NULL;
598 struct got_pathlist_head *paths = arg;
599 struct got_diff_changed_path *change = NULL;
602 path = strdup(label2 ? label2 : label1);
604 return got_error_from_errno("malloc");
606 change = malloc(sizeof(*change));
607 if (change == NULL) {
608 err = got_error_from_errno("malloc");
612 change->status = GOT_STATUS_NO_CHANGE;
614 change->status = GOT_STATUS_ADD;
615 else if (id2 == NULL)
616 change->status = GOT_STATUS_DELETE;
618 if (got_object_id_cmp(id1, id2) != 0)
619 change->status = GOT_STATUS_MODIFY;
620 else if (mode1 != mode2)
621 change->status = GOT_STATUS_MODE_CHANGE;
624 err = got_pathlist_insert(NULL, paths, path, change);
633 const struct got_error *
634 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
635 const char *label1, const char *label2, struct got_repository *repo,
636 got_diff_blob_cb cb, void *cb_arg, int diff_content)
638 const struct got_error *err = NULL;
639 struct got_tree_entry *te1 = NULL;
640 struct got_tree_entry *te2 = NULL;
641 char *l1 = NULL, *l2 = NULL;
642 int tidx1 = 0, tidx2 = 0;
645 te1 = got_object_tree_get_entry(tree1, 0);
646 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
648 return got_error_from_errno("asprintf");
651 te2 = got_object_tree_get_entry(tree2, 0);
652 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
654 return got_error_from_errno("asprintf");
659 struct got_tree_entry *te = NULL;
661 te = got_object_tree_find_entry(tree2,
666 if (te && asprintf(&l2, "%s%s%s", label2,
667 label2[0] ? "/" : "", te->name) == -1)
669 got_error_from_errno("asprintf");
671 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
672 cb_arg, diff_content);
678 struct got_tree_entry *te = NULL;
680 te = got_object_tree_find_entry(tree1,
684 if (asprintf(&l2, "%s%s%s", label2,
685 label2[0] ? "/" : "", te->name) == -1)
687 got_error_from_errno("asprintf");
689 if (asprintf(&l2, "%s%s%s", label2,
690 label2[0] ? "/" : "", te2->name) == -1)
692 got_error_from_errno("asprintf");
694 err = diff_entry_new_old(te2, te, l2, repo,
695 cb, cb_arg, diff_content);
704 te1 = got_object_tree_get_entry(tree1, tidx1);
706 asprintf(&l1, "%s%s%s", label1,
707 label1[0] ? "/" : "", te1->name) == -1)
708 return got_error_from_errno("asprintf");
714 te2 = got_object_tree_get_entry(tree2, tidx2);
716 asprintf(&l2, "%s%s%s", label2,
717 label2[0] ? "/" : "", te2->name) == -1)
718 return got_error_from_errno("asprintf");
720 } while (te1 || te2);
725 const struct got_error *
726 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
727 struct got_object_id *id1, struct got_object_id *id2,
728 const char *label1, const char *label2, int diff_context,
729 int ignore_whitespace, int force_text_diff,
730 struct got_repository *repo, FILE *outfile)
732 const struct got_error *err;
733 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
735 if (id1 == NULL && id2 == NULL)
736 return got_error(GOT_ERR_NO_OBJ);
739 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
744 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
748 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
749 label1, label2, diff_context, ignore_whitespace, force_text_diff,
753 got_object_blob_close(blob1);
755 got_object_blob_close(blob2);
759 const struct got_error *
760 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
761 struct got_object_id *id1, struct got_object_id *id2,
762 char *label1, char *label2, int diff_context, int ignore_whitespace,
763 int force_text_diff, struct got_repository *repo, FILE *outfile)
765 const struct got_error *err;
766 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
767 struct got_diff_blob_output_unidiff_arg arg;
768 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
770 if (id1 == NULL && id2 == NULL)
771 return got_error(GOT_ERR_NO_OBJ);
774 err = got_object_open_as_tree(&tree1, repo, id1);
779 err = got_object_open_as_tree(&tree2, repo, id2);
783 arg.diff_context = diff_context;
784 arg.ignore_whitespace = ignore_whitespace;
785 arg.force_text_diff = force_text_diff;
786 arg.outfile = outfile;
787 if (want_lineoffsets) {
788 arg.line_offsets = *line_offsets;
789 arg.nlines = *nlines;
791 arg.line_offsets = NULL;
794 err = got_diff_tree(tree1, tree2, label1, label2, repo,
795 got_diff_blob_output_unidiff, &arg, 1);
797 if (want_lineoffsets) {
798 *line_offsets = arg.line_offsets; /* was likely re-allocated */
799 *nlines = arg.nlines;
803 got_object_tree_close(tree1);
805 got_object_tree_close(tree2);
809 const struct got_error *
810 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
811 struct got_object_id *id1, struct got_object_id *id2,
812 int diff_context, int ignore_whitespace, int force_text_diff,
813 struct got_repository *repo, FILE *outfile)
815 const struct got_error *err;
816 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
819 return got_error(GOT_ERR_NO_OBJ);
822 err = got_object_open_as_commit(&commit1, repo, id1);
827 err = got_object_open_as_commit(&commit2, repo, id2);
831 err = got_diff_objects_as_trees(line_offsets, nlines,
832 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
833 got_object_commit_get_tree_id(commit2), "", "", diff_context,
834 ignore_whitespace, force_text_diff, repo, outfile);
837 got_object_commit_close(commit1);
839 got_object_commit_close(commit2);
843 const struct got_error *
844 got_diff_files(struct got_diffreg_result **resultp,
845 FILE *f1, const char *label1, FILE *f2, const char *label2,
846 int diff_context, int ignore_whitespace, int force_text_diff,
849 const struct got_error *err = NULL;
850 struct got_diffreg_result *diffreg_result = NULL;
856 fprintf(outfile, "file - %s\n",
857 f1 == NULL ? "/dev/null" : label1);
858 fprintf(outfile, "file + %s\n",
859 f2 == NULL ? "/dev/null" : label2);
862 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
863 ignore_whitespace, force_text_diff);
868 err = got_diffreg_output(NULL, NULL, diffreg_result,
869 f1 != NULL, f2 != NULL, label1, label2,
870 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
876 if (resultp && err == NULL)
877 *resultp = diffreg_result;
878 else if (diffreg_result) {
879 const struct got_error *free_err;
880 free_err = got_diffreg_result_free(diffreg_result);
881 if (free_err && err == NULL)