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 diff_blobs(struct got_diffreg_result **resultp,
44 struct got_blob_object *blob1, struct got_blob_object *blob2,
45 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
46 int diff_context, int ignore_whitespace, FILE *outfile)
48 const struct got_error *err = NULL, *free_err;
49 FILE *f1 = NULL, *f2 = NULL;
50 char hex1[SHA1_DIGEST_STRING_LENGTH];
51 char hex2[SHA1_DIGEST_STRING_LENGTH];
52 char *idstr1 = NULL, *idstr2 = NULL;
54 struct got_diffreg_result *result;
62 return got_error_from_errno("got_opentemp");
68 err = got_error_from_errno("got_opentemp");
76 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
77 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
86 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
95 char *modestr1 = NULL, *modestr2 = NULL;
97 if (mode1 && mode1 != mode2) {
101 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
102 if (asprintf(&modestr1, " (mode %o)",
103 mode1 & modebits) == -1) {
104 err = got_error_from_errno("asprintf");
108 if (mode2 && mode1 != mode2) {
112 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
113 if (asprintf(&modestr2, " (mode %o)",
114 mode2 & modebits) == -1) {
115 err = got_error_from_errno("asprintf");
119 fprintf(outfile, "blob - %s%s\n", idstr1,
120 modestr1 ? modestr1 : "");
121 fprintf(outfile, "blob + %s%s\n", idstr2,
122 modestr2 ? modestr2 : "");
126 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
132 err = got_diffreg_output(NULL, NULL, result, f1, f2,
133 label1 ? label1 : idstr1,
134 label2 ? label2 : idstr2,
135 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
140 if (resultp && err == NULL)
143 free_err = got_diffreg_result_free(result);
144 if (free_err && err == NULL)
148 if (f1 && fclose(f1) != 0 && err == NULL)
149 err = got_error_from_errno("fclose");
150 if (f2 && fclose(f2) != 0 && err == NULL)
151 err = got_error_from_errno("fclose");
155 const struct got_error *
156 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
157 struct got_blob_object *blob2, struct got_object_id *id1,
158 struct got_object_id *id2, const char *label1, const char *label2,
159 mode_t mode1, mode_t mode2, struct got_repository *repo)
161 const struct got_error *err;
162 struct got_diff_blob_output_unidiff_arg *a = arg;
164 err = diff_blobs(NULL, blob1, blob2, label1, label2, mode1, mode2,
165 a->diff_context, a->ignore_whitespace, a->outfile);
169 const struct got_error *
170 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
171 const char *label1, const char *label2, int diff_context,
172 int ignore_whitespace, FILE *outfile)
174 return diff_blobs(NULL, blob1, blob2, label1, label2, 0, 0, diff_context,
175 ignore_whitespace, outfile);
178 static const struct got_error *
179 diff_blob_file(struct got_diffreg_result **resultp,
180 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
181 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
183 const struct got_error *err = NULL, *free_err;
185 char hex1[SHA1_DIGEST_STRING_LENGTH];
188 struct got_diffreg_result *result = NULL;
197 return got_error_from_errno("got_opentemp");
198 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
199 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
204 idstr1 = "/dev/null";
208 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
209 fprintf(outfile, "file + %s\n",
210 f2 == NULL ? "/dev/null" : label2);
213 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
219 err = got_diffreg_output(NULL, NULL, result, f1, f2,
220 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
226 if (resultp && err == NULL)
229 free_err = got_diffreg_result_free(result);
230 if (free_err && err == NULL)
234 if (f1 && fclose(f1) != 0 && err == NULL)
235 err = got_error_from_errno("fclose");
239 const struct got_error *
240 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
241 FILE *f2, size_t size2, const char *label2, int diff_context,
242 int ignore_whitespace, FILE *outfile)
244 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
245 diff_context, ignore_whitespace, outfile);
248 const struct got_error *
249 got_diff_blob_file_lines_changed(struct got_diffreg_result **result,
250 struct got_blob_object *blob1, FILE *f2, size_t size2)
252 return diff_blob_file(result, blob1, NULL, f2, size2, NULL,
256 const struct got_error *
257 got_diff_blob_lines_changed(struct got_diffreg_result **result,
258 struct got_blob_object *blob1, struct got_blob_object *blob2)
260 const struct got_error *err = NULL;
262 err = diff_blobs(result, blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL);
264 got_diffreg_result_free(*result);
270 static const struct got_error *
271 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
272 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
274 const struct got_error *err;
275 struct got_blob_object *blob = NULL;
276 struct got_object *obj = NULL;
278 err = got_object_open(&obj, repo, id);
282 err = got_object_blob_open(&blob, repo, obj, 8192);
285 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
287 got_object_close(obj);
289 got_object_blob_close(blob);
293 static const struct got_error *
294 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
295 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
296 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
298 const struct got_error *err;
299 struct got_object *obj1 = NULL;
300 struct got_object *obj2 = NULL;
301 struct got_blob_object *blob1 = NULL;
302 struct got_blob_object *blob2 = NULL;
304 err = got_object_open(&obj1, repo, id1);
307 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
308 err = got_error(GOT_ERR_OBJ_TYPE);
312 err = got_object_open(&obj2, repo, id2);
315 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
316 err = got_error(GOT_ERR_BAD_OBJ_DATA);
320 err = got_object_blob_open(&blob1, repo, obj1, 8192);
324 err = got_object_blob_open(&blob2, repo, obj2, 8192);
328 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
332 got_object_close(obj1);
334 got_object_close(obj2);
336 got_object_blob_close(blob1);
338 got_object_blob_close(blob2);
342 static const struct got_error *
343 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
344 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
346 const struct got_error *err;
347 struct got_blob_object *blob = NULL;
348 struct got_object *obj = NULL;
350 err = got_object_open(&obj, repo, id);
354 err = got_object_blob_open(&blob, repo, obj, 8192);
357 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
359 got_object_close(obj);
361 got_object_blob_close(blob);
365 static const struct got_error *
366 diff_added_tree(struct got_object_id *id, const char *label,
367 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
370 const struct got_error *err = NULL;
371 struct got_object *treeobj = NULL;
372 struct got_tree_object *tree = NULL;
374 err = got_object_open(&treeobj, repo, id);
378 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
379 err = got_error(GOT_ERR_OBJ_TYPE);
383 err = got_object_tree_open(&tree, repo, treeobj);
387 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
391 got_object_tree_close(tree);
393 got_object_close(treeobj);
397 static const struct got_error *
398 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
399 const char *label1, const char *label2, struct got_repository *repo,
400 got_diff_blob_cb cb, void *cb_arg, int diff_content)
402 const struct got_error *err;
403 struct got_object *treeobj1 = NULL;
404 struct got_object *treeobj2 = NULL;
405 struct got_tree_object *tree1 = NULL;
406 struct got_tree_object *tree2 = NULL;
408 err = got_object_open(&treeobj1, repo, id1);
412 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
413 err = got_error(GOT_ERR_OBJ_TYPE);
417 err = got_object_open(&treeobj2, repo, id2);
421 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
422 err = got_error(GOT_ERR_OBJ_TYPE);
426 err = got_object_tree_open(&tree1, repo, treeobj1);
430 err = got_object_tree_open(&tree2, repo, treeobj2);
434 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
439 got_object_tree_close(tree1);
441 got_object_tree_close(tree2);
443 got_object_close(treeobj1);
445 got_object_close(treeobj2);
449 static const struct got_error *
450 diff_deleted_tree(struct got_object_id *id, const char *label,
451 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
454 const struct got_error *err;
455 struct got_object *treeobj = NULL;
456 struct got_tree_object *tree = NULL;
458 err = got_object_open(&treeobj, repo, id);
462 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
463 err = got_error(GOT_ERR_OBJ_TYPE);
467 err = got_object_tree_open(&tree, repo, treeobj);
471 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
475 got_object_tree_close(tree);
477 got_object_close(treeobj);
481 static const struct got_error *
482 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
483 const char *label1, const char *label2, struct got_repository *repo,
484 got_diff_blob_cb cb, void *cb_arg)
490 static const struct got_error *
491 diff_entry_old_new(struct got_tree_entry *te1,
492 struct got_tree_entry *te2, const char *label1, const char *label2,
493 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
496 const struct got_error *err = NULL;
499 if (got_object_tree_entry_is_submodule(te1))
503 if (S_ISDIR(te1->mode))
504 err = diff_deleted_tree(&te1->id, label1, repo,
505 cb, cb_arg, diff_content);
508 err = diff_deleted_blob(&te1->id, label1,
509 te1->mode, repo, cb, cb_arg);
511 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
512 label1, NULL, te1->mode, 0, repo);
515 } else if (got_object_tree_entry_is_submodule(te2))
518 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
519 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
521 return diff_modified_tree(&te1->id, &te2->id,
522 label1, label2, repo, cb, cb_arg, diff_content);
523 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
524 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
526 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
527 (te2->mode & (S_IFLNK | S_IXUSR))) {
529 return diff_modified_blob(&te1->id, &te2->id,
530 label1, label2, te1->mode, te2->mode,
533 return cb(cb_arg, NULL, NULL, &te1->id,
534 &te2->id, label1, label2, te1->mode,
542 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
546 static const struct got_error *
547 diff_entry_new_old(struct got_tree_entry *te2,
548 struct got_tree_entry *te1, const char *label2,
549 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
552 if (te1 != NULL) /* handled by diff_entry_old_new() */
555 if (got_object_tree_entry_is_submodule(te2))
558 if (S_ISDIR(te2->mode))
559 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
563 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
566 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
570 const struct got_error *
571 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
572 struct got_blob_object *blob2, struct got_object_id *id1,
573 struct got_object_id *id2, const char *label1, const char *label2,
574 mode_t mode1, mode_t mode2, struct got_repository *repo)
576 const struct got_error *err = NULL;
577 struct got_pathlist_head *paths = arg;
578 struct got_diff_changed_path *change = NULL;
581 path = strdup(label2 ? label2 : label1);
583 return got_error_from_errno("malloc");
585 change = malloc(sizeof(*change));
586 if (change == NULL) {
587 err = got_error_from_errno("malloc");
591 change->status = GOT_STATUS_NO_CHANGE;
593 change->status = GOT_STATUS_ADD;
594 else if (id2 == NULL)
595 change->status = GOT_STATUS_DELETE;
597 if (got_object_id_cmp(id1, id2) != 0)
598 change->status = GOT_STATUS_MODIFY;
599 else if (mode1 != mode2)
600 change->status = GOT_STATUS_MODE_CHANGE;
603 err = got_pathlist_insert(NULL, paths, path, change);
612 const struct got_error *
613 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
614 const char *label1, const char *label2, struct got_repository *repo,
615 got_diff_blob_cb cb, void *cb_arg, int diff_content)
617 const struct got_error *err = NULL;
618 struct got_tree_entry *te1 = NULL;
619 struct got_tree_entry *te2 = NULL;
620 char *l1 = NULL, *l2 = NULL;
621 int tidx1 = 0, tidx2 = 0;
624 te1 = got_object_tree_get_entry(tree1, 0);
625 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
627 return got_error_from_errno("asprintf");
630 te2 = got_object_tree_get_entry(tree2, 0);
631 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
633 return got_error_from_errno("asprintf");
638 struct got_tree_entry *te = NULL;
640 te = got_object_tree_find_entry(tree2,
645 if (te && asprintf(&l2, "%s%s%s", label2,
646 label2[0] ? "/" : "", te->name) == -1)
648 got_error_from_errno("asprintf");
650 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
651 cb_arg, diff_content);
657 struct got_tree_entry *te = NULL;
659 te = got_object_tree_find_entry(tree1,
663 if (asprintf(&l2, "%s%s%s", label2,
664 label2[0] ? "/" : "", te->name) == -1)
666 got_error_from_errno("asprintf");
668 if (asprintf(&l2, "%s%s%s", label2,
669 label2[0] ? "/" : "", te2->name) == -1)
671 got_error_from_errno("asprintf");
673 err = diff_entry_new_old(te2, te, l2, repo,
674 cb, cb_arg, diff_content);
683 te1 = got_object_tree_get_entry(tree1, tidx1);
685 asprintf(&l1, "%s%s%s", label1,
686 label1[0] ? "/" : "", te1->name) == -1)
687 return got_error_from_errno("asprintf");
693 te2 = got_object_tree_get_entry(tree2, tidx2);
695 asprintf(&l2, "%s%s%s", label2,
696 label2[0] ? "/" : "", te2->name) == -1)
697 return got_error_from_errno("asprintf");
699 } while (te1 || te2);
704 const struct got_error *
705 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
706 const char *label1, const char *label2, int diff_context,
707 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
709 const struct got_error *err;
710 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
712 if (id1 == NULL && id2 == NULL)
713 return got_error(GOT_ERR_NO_OBJ);
716 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
721 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
725 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
726 ignore_whitespace, outfile);
729 got_object_blob_close(blob1);
731 got_object_blob_close(blob2);
735 const struct got_error *
736 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
737 char *label1, char *label2, int diff_context, int ignore_whitespace,
738 struct got_repository *repo, FILE *outfile)
740 const struct got_error *err;
741 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
742 struct got_diff_blob_output_unidiff_arg arg;
744 if (id1 == NULL && id2 == NULL)
745 return got_error(GOT_ERR_NO_OBJ);
748 err = got_object_open_as_tree(&tree1, repo, id1);
753 err = got_object_open_as_tree(&tree2, repo, id2);
757 arg.diff_context = diff_context;
758 arg.ignore_whitespace = ignore_whitespace;
759 arg.outfile = outfile;
760 err = got_diff_tree(tree1, tree2, label1, label2, repo,
761 got_diff_blob_output_unidiff, &arg, 1);
764 got_object_tree_close(tree1);
766 got_object_tree_close(tree2);
770 const struct got_error *
771 got_diff_objects_as_commits(struct got_object_id *id1,
772 struct got_object_id *id2, int diff_context, int ignore_whitespace,
773 struct got_repository *repo, FILE *outfile)
775 const struct got_error *err;
776 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
779 return got_error(GOT_ERR_NO_OBJ);
782 err = got_object_open_as_commit(&commit1, repo, id1);
787 err = got_object_open_as_commit(&commit2, repo, id2);
791 err = got_diff_objects_as_trees(
792 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
793 got_object_commit_get_tree_id(commit2), "", "", diff_context,
794 ignore_whitespace, repo, outfile);
797 got_object_commit_close(commit1);
799 got_object_commit_close(commit2);
803 const struct got_error *
804 got_diff_files(struct got_diffreg_result **resultp,
805 FILE *f1, const char *label1, FILE *f2, const char *label2,
806 int diff_context, int ignore_whitespace, FILE *outfile)
808 const struct got_error *err = NULL;
809 struct got_diffreg_result *diffreg_result = NULL;
815 fprintf(outfile, "file - %s\n",
816 f1 == NULL ? "/dev/null" : label1);
817 fprintf(outfile, "file + %s\n",
818 f2 == NULL ? "/dev/null" : label2);
821 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
827 err = got_diffreg_output(NULL, NULL, diffreg_result,
828 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
829 diff_context, outfile);
835 if (resultp && err == NULL)
836 *resultp = diffreg_result;
837 else if (diffreg_result) {
838 const struct got_error *free_err;
839 free_err = got_diffreg_result_free(diffreg_result);
840 if (free_err && err == NULL)