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>
26 #include "got_compat.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_worktree.h"
35 #include "got_opentemp.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, FILE *f1, FILE *f2,
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,
62 enum got_diff_algorithm diff_algo)
64 const struct got_error *err = NULL, *free_err;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
67 const 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);
85 err = got_opentemp_truncate(f1);
90 err = got_opentemp_truncate(f2);
97 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
98 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
103 idstr1 = "/dev/null";
107 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
108 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
113 idstr2 = "/dev/null";
116 char *modestr1 = NULL, *modestr2 = NULL;
118 if (mode1 && mode1 != mode2) {
122 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
123 if (asprintf(&modestr1, " (mode %o)",
124 mode1 & modebits) == -1) {
125 err = got_error_from_errno("asprintf");
129 if (mode2 && mode1 != mode2) {
133 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
134 if (asprintf(&modestr2, " (mode %o)",
135 mode2 & modebits) == -1) {
136 err = got_error_from_errno("asprintf");
140 n = fprintf(outfile, "blob - %s%s\n", idstr1,
141 modestr1 ? modestr1 : "");
146 err = add_line_offset(line_offsets, nlines, outoff);
151 n = fprintf(outfile, "blob + %s%s\n", idstr2,
152 modestr2 ? modestr2 : "");
157 err = add_line_offset(line_offsets, nlines, outoff);
165 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
171 err = got_diffreg_output(line_offsets, nlines, result,
172 blob1 != NULL, blob2 != NULL,
173 label1 ? label1 : idstr1,
174 label2 ? label2 : idstr2,
175 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
180 if (resultp && err == NULL)
183 free_err = got_diffreg_result_free(result);
184 if (free_err && err == NULL)
191 const struct got_error *
192 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
193 struct got_blob_object *blob2, FILE *f1, FILE *f2,
194 struct got_object_id *id1, struct got_object_id *id2,
195 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
196 struct got_repository *repo)
198 struct got_diff_blob_output_unidiff_arg *a = arg;
200 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
201 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
202 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
205 const struct got_error *
206 got_diff_blob(off_t **line_offsets, size_t *nlines,
207 struct got_blob_object *blob1, struct got_blob_object *blob2,
208 FILE *f1, FILE *f2, const char *label1, const char *label2,
209 enum got_diff_algorithm diff_algo, int diff_context,
210 int ignore_whitespace, int force_text_diff, FILE *outfile)
212 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
213 label1, label2, 0, 0, diff_context, ignore_whitespace,
214 force_text_diff, outfile, diff_algo);
217 static const struct got_error *
218 diff_blob_file(struct got_diffreg_result **resultp,
219 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
220 FILE *f2, int f2_exists, size_t size2, const char *label2,
221 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
222 int force_text_diff, FILE *outfile)
224 const struct got_error *err = NULL, *free_err;
225 char hex1[SHA1_DIGEST_STRING_LENGTH];
226 const char *idstr1 = NULL;
227 struct got_diffreg_result *result = NULL;
233 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
235 idstr1 = "/dev/null";
238 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
239 fprintf(outfile, "file + %s\n",
240 f2_exists ? label2 : "/dev/null");
243 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
249 err = got_diffreg_output(NULL, NULL, result,
250 blob1 != NULL, f2_exists,
251 label2, /* show local file's path, not a blob ID */
252 label2, GOT_DIFF_OUTPUT_UNIDIFF,
253 diff_context, outfile);
258 if (resultp && err == NULL)
261 free_err = got_diffreg_result_free(result);
262 if (free_err && err == NULL)
269 const struct got_error *
270 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
271 const char *label1, FILE *f2, int f2_exists, size_t size2,
272 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
273 int ignore_whitespace, int force_text_diff, FILE *outfile)
275 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
276 size2, label2, diff_algo, diff_context, ignore_whitespace,
277 force_text_diff, outfile );
280 static const struct got_error *
281 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
282 const char *label, mode_t mode, struct got_repository *repo,
283 got_diff_blob_cb cb, void *cb_arg)
285 const struct got_error *err;
286 struct got_blob_object *blob = NULL;
287 struct got_object *obj = NULL;
289 err = got_object_open(&obj, repo, id);
293 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
296 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
297 NULL, label, 0, mode, repo);
299 got_object_close(obj);
301 got_object_blob_close(blob);
305 static const struct got_error *
306 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
307 FILE *f1, FILE *f2, int fd1, int fd2,
308 const char *label1, const char *label2,
309 mode_t mode1, mode_t mode2, struct got_repository *repo,
310 got_diff_blob_cb cb, void *cb_arg)
312 const struct got_error *err;
313 struct got_object *obj1 = NULL;
314 struct got_object *obj2 = NULL;
315 struct got_blob_object *blob1 = NULL;
316 struct got_blob_object *blob2 = NULL;
318 err = got_object_open(&obj1, repo, id1);
322 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
323 err = got_error(GOT_ERR_OBJ_TYPE);
327 err = got_object_open(&obj2, repo, id2);
330 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
331 err = got_error(GOT_ERR_BAD_OBJ_DATA);
335 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
339 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
343 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
347 got_object_close(obj1);
349 got_object_close(obj2);
351 got_object_blob_close(blob1);
353 got_object_blob_close(blob2);
357 static const struct got_error *
358 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
359 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
360 got_diff_blob_cb cb, void *cb_arg)
362 const struct got_error *err;
363 struct got_blob_object *blob = NULL;
364 struct got_object *obj = NULL;
366 err = got_object_open(&obj, repo, id);
370 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
373 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
376 got_object_close(obj);
378 got_object_blob_close(blob);
382 static const struct got_error *
383 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
384 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
385 void *cb_arg, int diff_content)
387 const struct got_error *err = NULL;
388 struct got_object *treeobj = NULL;
389 struct got_tree_object *tree = NULL;
391 err = got_object_open(&treeobj, repo, id);
395 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
396 err = got_error(GOT_ERR_OBJ_TYPE);
400 err = got_object_tree_open(&tree, repo, treeobj);
404 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
405 repo, cb, cb_arg, diff_content);
408 got_object_tree_close(tree);
410 got_object_close(treeobj);
414 static const struct got_error *
415 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
416 FILE *f1, FILE *f2, int fd1, int fd2,
417 const char *label1, const char *label2,
418 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
421 const struct got_error *err;
422 struct got_object *treeobj1 = NULL;
423 struct got_object *treeobj2 = NULL;
424 struct got_tree_object *tree1 = NULL;
425 struct got_tree_object *tree2 = NULL;
427 err = got_object_open(&treeobj1, repo, id1);
431 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
432 err = got_error(GOT_ERR_OBJ_TYPE);
436 err = got_object_open(&treeobj2, repo, id2);
440 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
441 err = got_error(GOT_ERR_OBJ_TYPE);
445 err = got_object_tree_open(&tree1, repo, treeobj1);
449 err = got_object_tree_open(&tree2, repo, treeobj2);
453 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
454 label1, label2, repo, cb, cb_arg, diff_content);
458 got_object_tree_close(tree1);
460 got_object_tree_close(tree2);
462 got_object_close(treeobj1);
464 got_object_close(treeobj2);
468 static const struct got_error *
469 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
470 FILE *f2, const char *label, struct got_repository *repo,
471 got_diff_blob_cb cb, void *cb_arg, int diff_content)
473 const struct got_error *err;
474 struct got_object *treeobj = NULL;
475 struct got_tree_object *tree = NULL;
477 err = got_object_open(&treeobj, repo, id);
481 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
482 err = got_error(GOT_ERR_OBJ_TYPE);
486 err = got_object_tree_open(&tree, repo, treeobj);
490 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
491 repo, cb, cb_arg, diff_content);
494 got_object_tree_close(tree);
496 got_object_close(treeobj);
500 static const struct got_error *
501 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
502 const char *label1, const char *label2, struct got_repository *repo,
503 got_diff_blob_cb cb, void *cb_arg)
509 static const struct got_error *
510 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
511 FILE *f1, FILE *f2, int fd1, int fd2,
512 const char *label1, const char *label2,
513 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
516 const struct got_error *err = NULL;
519 if (got_object_tree_entry_is_submodule(te1))
523 if (S_ISDIR(te1->mode))
524 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
525 label1, repo, cb, cb_arg, diff_content);
528 err = diff_deleted_blob(&te1->id, f1, fd1,
529 f2, label1, te1->mode, repo, cb, cb_arg);
531 err = cb(cb_arg, NULL, NULL, NULL, NULL,
532 &te1->id, NULL, label1, NULL,
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, f1, f2,
543 fd1, fd2, label1, label2, repo, cb, cb_arg,
545 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
546 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
548 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
549 (te2->mode & (S_IFLNK | S_IXUSR))) {
551 return diff_modified_blob(&te1->id, &te2->id,
552 f1, f2, fd1, fd2, label1, label2,
553 te1->mode, te2->mode, repo, cb, cb_arg);
555 return cb(cb_arg, NULL, NULL, NULL, NULL,
556 &te1->id, &te2->id, label1, label2,
557 te1->mode, te2->mode, repo);
564 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
568 static const struct got_error *
569 diff_entry_new_old(struct got_tree_entry *te2,
570 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
571 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
574 if (te1 != NULL) /* handled by diff_entry_old_new() */
577 if (got_object_tree_entry_is_submodule(te2))
580 if (S_ISDIR(te2->mode))
581 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
582 repo, cb, cb_arg, diff_content);
585 return diff_added_blob(&te2->id, f1, f2, fd2,
586 label2, te2->mode, repo, cb, cb_arg);
588 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
589 NULL, label2, 0, te2->mode, repo);
592 const struct got_error *
593 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
594 struct got_blob_object *blob2, FILE *f1, FILE *f2,
595 struct got_object_id *id1, struct got_object_id *id2,
596 const char *label1, const char *label2,
597 mode_t mode1, mode_t mode2, struct got_repository *repo)
599 const struct got_error *err = NULL;
600 struct got_pathlist_head *paths = arg;
601 struct got_diff_changed_path *change = NULL;
604 path = strdup(label2 ? label2 : label1);
606 return got_error_from_errno("malloc");
608 change = malloc(sizeof(*change));
609 if (change == NULL) {
610 err = got_error_from_errno("malloc");
614 change->status = GOT_STATUS_NO_CHANGE;
616 change->status = GOT_STATUS_ADD;
617 else if (id2 == NULL)
618 change->status = GOT_STATUS_DELETE;
620 if (got_object_id_cmp(id1, id2) != 0)
621 change->status = GOT_STATUS_MODIFY;
622 else if (mode1 != mode2)
623 change->status = GOT_STATUS_MODE_CHANGE;
626 err = got_pathlist_append(paths, path, change);
635 const struct got_error *
636 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
637 FILE *f1, FILE *f2, int fd1, int fd2,
638 const char *label1, const char *label2,
639 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
642 const struct got_error *err = NULL;
643 struct got_tree_entry *te1 = NULL;
644 struct got_tree_entry *te2 = NULL;
645 char *l1 = NULL, *l2 = NULL;
646 int tidx1 = 0, tidx2 = 0;
649 te1 = got_object_tree_get_entry(tree1, 0);
650 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
652 return got_error_from_errno("asprintf");
655 te2 = got_object_tree_get_entry(tree2, 0);
656 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
658 return got_error_from_errno("asprintf");
663 struct got_tree_entry *te = NULL;
665 te = got_object_tree_find_entry(tree2,
670 if (te && asprintf(&l2, "%s%s%s", label2,
671 label2[0] ? "/" : "", te->name) == -1)
673 got_error_from_errno("asprintf");
675 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
676 l1, l2, repo, cb, cb_arg, diff_content);
682 struct got_tree_entry *te = NULL;
684 te = got_object_tree_find_entry(tree1,
688 if (asprintf(&l2, "%s%s%s", label2,
689 label2[0] ? "/" : "", te->name) == -1)
691 got_error_from_errno("asprintf");
693 if (asprintf(&l2, "%s%s%s", label2,
694 label2[0] ? "/" : "", te2->name) == -1)
696 got_error_from_errno("asprintf");
698 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
699 repo, cb, cb_arg, diff_content);
708 te1 = got_object_tree_get_entry(tree1, tidx1);
710 asprintf(&l1, "%s%s%s", label1,
711 label1[0] ? "/" : "", te1->name) == -1)
712 return got_error_from_errno("asprintf");
718 te2 = got_object_tree_get_entry(tree2, tidx2);
720 asprintf(&l2, "%s%s%s", label2,
721 label2[0] ? "/" : "", te2->name) == -1)
722 return got_error_from_errno("asprintf");
724 } while (te1 || te2);
729 const struct got_error *
730 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
731 FILE *f1, FILE *f2, int fd1, int fd2,
732 struct got_object_id *id1, struct got_object_id *id2,
733 const char *label1, const char *label2,
734 enum got_diff_algorithm diff_algo, 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, fd1);
750 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
754 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
755 label1, label2, diff_algo, diff_context, ignore_whitespace,
756 force_text_diff, outfile);
759 got_object_blob_close(blob1);
761 got_object_blob_close(blob2);
765 static const struct got_error *
766 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
767 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
768 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
770 const struct got_error *err = NULL;
771 struct got_pathlist_entry *pe;
772 struct got_object_id *id1 = NULL, *id2 = NULL;
773 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
774 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
776 TAILQ_FOREACH(pe, paths, entry) {
777 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
778 mode_t mode1 = 0, mode2 = 0;
785 got_object_tree_close(subtree1);
789 got_object_tree_close(subtree2);
793 got_object_blob_close(blob1);
797 got_object_blob_close(blob2);
801 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
803 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
805 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
807 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
809 if (id1 == NULL && id2 == NULL) {
810 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
814 err = got_object_get_type(&type1, repo, id1);
819 err = got_object_get_type(&type2, repo, id2);
823 if (type1 == GOT_OBJ_TYPE_ANY &&
824 type2 == GOT_OBJ_TYPE_ANY) {
825 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
827 } else if (type1 != GOT_OBJ_TYPE_ANY &&
828 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
829 err = got_error(GOT_ERR_OBJ_TYPE);
833 if (type1 == GOT_OBJ_TYPE_BLOB ||
834 type2 == GOT_OBJ_TYPE_BLOB) {
836 err = got_object_open_as_blob(&blob1, repo,
842 err = got_object_open_as_blob(&blob2, repo,
847 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
848 id1 ? pe->path : "/dev/null",
849 id2 ? pe->path : "/dev/null",
853 } else if (type1 == GOT_OBJ_TYPE_TREE ||
854 type2 == GOT_OBJ_TYPE_TREE) {
856 err = got_object_open_as_tree(&subtree1, repo,
862 err = got_object_open_as_tree(&subtree2, repo,
867 err = got_diff_tree(subtree1, subtree2, f1, f2,
869 id1 ? pe->path : "/dev/null",
870 id2 ? pe->path : "/dev/null",
871 repo, cb, cb_arg, 1);
875 err = got_error(GOT_ERR_OBJ_TYPE);
883 got_object_tree_close(subtree1);
885 got_object_tree_close(subtree2);
887 got_object_blob_close(blob1);
889 got_object_blob_close(blob2);
893 static const struct got_error *
894 show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
895 int ch, const char *id_str, FILE *outfile)
897 const struct got_error *err;
901 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
902 if (line_offsets != NULL && *line_offsets != NULL) {
904 err = add_line_offset(line_offsets, nlines, 0);
908 outoff = (*line_offsets)[*nlines - 1];
911 err = add_line_offset(line_offsets, nlines, outoff);
919 static const struct got_error *
920 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
921 FILE *f1, FILE *f2, int fd1, int fd2,
922 struct got_object_id *id1, struct got_object_id *id2,
923 struct got_pathlist_head *paths, const char *label1, const char *label2,
924 int diff_context, int ignore_whitespace, int force_text_diff,
925 struct got_repository *repo, FILE *outfile,
926 enum got_diff_algorithm diff_algo)
928 const struct got_error *err;
929 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
930 struct got_diff_blob_output_unidiff_arg arg;
931 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
933 if (id1 == NULL && id2 == NULL)
934 return got_error(GOT_ERR_NO_OBJ);
937 err = got_object_open_as_tree(&tree1, repo, id1);
942 err = got_object_open_as_tree(&tree2, repo, id2);
947 arg.diff_algo = diff_algo;
948 arg.diff_context = diff_context;
949 arg.ignore_whitespace = ignore_whitespace;
950 arg.force_text_diff = force_text_diff;
951 arg.outfile = outfile;
952 if (want_lineoffsets) {
953 arg.line_offsets = *line_offsets;
954 arg.nlines = *nlines;
956 arg.line_offsets = NULL;
959 if (paths == NULL || TAILQ_EMPTY(paths)) {
960 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
961 label1, label2, repo,
962 got_diff_blob_output_unidiff, &arg, 1);
964 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
965 got_diff_blob_output_unidiff, &arg);
967 if (want_lineoffsets) {
968 *line_offsets = arg.line_offsets; /* was likely re-allocated */
969 *nlines = arg.nlines;
973 got_object_tree_close(tree1);
975 got_object_tree_close(tree2);
979 const struct got_error *
980 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
981 FILE *f1, FILE *f2, int fd1, int fd2,
982 struct got_object_id *id1, struct got_object_id *id2,
983 struct got_pathlist_head *paths, const char *label1, const char *label2,
984 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
985 int force_text_diff, struct got_repository *repo, FILE *outfile)
987 const struct got_error *err;
990 if (id1 == NULL && id2 == NULL)
991 return got_error(GOT_ERR_NO_OBJ);
994 err = got_object_id_str(&idstr, id1);
997 err = show_object_id(line_offsets, nlines, "tree", '-',
1004 err = show_object_id(line_offsets, nlines, "tree", '-',
1005 "/dev/null", outfile);
1011 err = got_object_id_str(&idstr, id2);
1014 err = show_object_id(line_offsets, nlines, "tree", '+',
1021 err = show_object_id(line_offsets, nlines, "tree", '+',
1022 "/dev/null", outfile);
1027 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1028 id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1029 force_text_diff, repo, outfile, diff_algo);
1035 const struct got_error *
1036 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1037 FILE *f1, FILE *f2, int fd1, int fd2,
1038 struct got_object_id *id1, struct got_object_id *id2,
1039 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1040 int diff_context, int ignore_whitespace, int force_text_diff,
1041 struct got_repository *repo, FILE *outfile)
1043 const struct got_error *err;
1044 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1048 return got_error(GOT_ERR_NO_OBJ);
1051 err = got_object_open_as_commit(&commit1, repo, id1);
1054 err = got_object_id_str(&idstr, id1);
1057 err = show_object_id(line_offsets, nlines, "commit", '-',
1064 err = show_object_id(line_offsets, nlines, "commit", '-',
1065 "/dev/null", outfile);
1070 err = got_object_open_as_commit(&commit2, repo, id2);
1074 err = got_object_id_str(&idstr, id2);
1077 err = show_object_id(line_offsets, nlines, "commit", '+',
1082 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1083 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1084 got_object_commit_get_tree_id(commit2), paths, "", "",
1085 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1089 got_object_commit_close(commit1);
1091 got_object_commit_close(commit2);
1096 const struct got_error *
1097 got_diff_files(struct got_diffreg_result **resultp,
1098 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1099 const char *label2, int diff_context, int ignore_whitespace,
1100 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1102 const struct got_error *err = NULL;
1103 struct got_diffreg_result *diffreg_result = NULL;
1109 fprintf(outfile, "file - %s\n",
1110 f1_exists ? label1 : "/dev/null");
1111 fprintf(outfile, "file + %s\n",
1112 f2_exists ? label2 : "/dev/null");
1115 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1116 ignore_whitespace, force_text_diff);
1121 err = got_diffreg_output(NULL, NULL, diffreg_result,
1122 f1_exists, f2_exists, label1, label2,
1123 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1129 if (resultp && err == NULL)
1130 *resultp = diffreg_result;
1131 else if (diffreg_result) {
1132 const struct got_error *free_err;
1133 free_err = got_diffreg_result_free(diffreg_result);
1134 if (free_err && err == NULL)