Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <zlib.h>
27 #include "got_object.h"
28 #include "got_repository.h"
29 #include "got_error.h"
30 #include "got_diff.h"
31 #include "got_path.h"
32 #include "got_cancel.h"
33 #include "got_worktree.h"
34 #include "got_opentemp.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)
43 {
44 off_t *p;
46 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
47 if (p == NULL)
48 return got_error_from_errno("reallocarray");
49 *line_offsets = p;
50 (*line_offsets)[*nlines] = off;
51 (*nlines)++;
52 return NULL;
53 }
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, FILE *f1, FILE *f2,
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,
61 enum got_diff_algorithm diff_algo)
62 {
63 const struct got_error *err = NULL, *free_err;
64 char hex1[SHA1_DIGEST_STRING_LENGTH];
65 char hex2[SHA1_DIGEST_STRING_LENGTH];
66 const char *idstr1 = NULL, *idstr2 = NULL;
67 off_t size1, size2;
68 struct got_diffreg_result *result;
69 off_t outoff = 0;
70 int n;
72 if (line_offsets && *line_offsets && *nlines > 0)
73 outoff = (*line_offsets)[*nlines - 1];
74 else if (line_offsets) {
75 err = add_line_offset(line_offsets, nlines, 0);
76 if (err)
77 goto done;
78 }
80 if (resultp)
81 *resultp = NULL;
83 if (f1) {
84 err = got_opentemp_truncate(f1);
85 if (err)
86 goto done;
87 }
88 if (f2) {
89 err = got_opentemp_truncate(f2);
90 if (err)
91 goto done;
92 }
94 size1 = 0;
95 if (blob1) {
96 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
98 blob1);
99 if (err)
100 goto done;
101 } else
102 idstr1 = "/dev/null";
104 size2 = 0;
105 if (blob2) {
106 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
108 blob2);
109 if (err)
110 goto done;
111 } else
112 idstr2 = "/dev/null";
114 if (outfile) {
115 char *modestr1 = NULL, *modestr2 = NULL;
116 int modebits;
117 if (mode1 && mode1 != mode2) {
118 if (S_ISLNK(mode1))
119 modebits = S_IFLNK;
120 else
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");
125 goto done;
128 if (mode2 && mode1 != mode2) {
129 if (S_ISLNK(mode2))
130 modebits = S_IFLNK;
131 else
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");
136 goto done;
139 n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 modestr1 ? modestr1 : "");
141 if (n < 0)
142 goto done;
143 outoff += n;
144 if (line_offsets) {
145 err = add_line_offset(line_offsets, nlines, outoff);
146 if (err)
147 goto done;
150 n = fprintf(outfile, "blob + %s%s\n", idstr2,
151 modestr2 ? modestr2 : "");
152 if (n < 0)
153 goto done;
154 outoff += n;
155 if (line_offsets) {
156 err = add_line_offset(line_offsets, nlines, outoff);
157 if (err)
158 goto done;
161 free(modestr1);
162 free(modestr2);
164 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
165 force_text_diff);
166 if (err)
167 goto done;
169 if (outfile) {
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);
175 if (err)
176 goto done;
179 if (resultp && err == NULL)
180 *resultp = result;
181 else {
182 free_err = got_diffreg_result_free(result);
183 if (free_err && err == NULL)
184 err = free_err;
186 done:
187 return err;
190 const struct got_error *
191 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
192 struct got_blob_object *blob2, FILE *f1, FILE *f2,
193 struct got_object_id *id1, struct got_object_id *id2,
194 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
195 struct got_repository *repo)
197 struct got_diff_blob_output_unidiff_arg *a = arg;
199 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
200 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
201 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
204 const struct got_error *
205 got_diff_blob(off_t **line_offsets, size_t *nlines,
206 struct got_blob_object *blob1, struct got_blob_object *blob2,
207 FILE *f1, FILE *f2, const char *label1, const char *label2,
208 enum got_diff_algorithm diff_algo, int diff_context,
209 int ignore_whitespace, int force_text_diff, FILE *outfile)
211 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
212 label1, label2, 0, 0, diff_context, ignore_whitespace,
213 force_text_diff, outfile, diff_algo);
216 static const struct got_error *
217 diff_blob_file(struct got_diffreg_result **resultp,
218 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
219 FILE *f2, int f2_exists, size_t size2, const char *label2,
220 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
221 int force_text_diff, FILE *outfile)
223 const struct got_error *err = NULL, *free_err;
224 char hex1[SHA1_DIGEST_STRING_LENGTH];
225 const char *idstr1 = NULL;
226 struct got_diffreg_result *result = NULL;
228 if (resultp)
229 *resultp = NULL;
231 if (blob1)
232 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
233 else
234 idstr1 = "/dev/null";
236 if (outfile) {
237 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
238 fprintf(outfile, "file + %s\n",
239 f2_exists ? label2 : "/dev/null");
242 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
243 force_text_diff);
244 if (err)
245 goto done;
247 if (outfile) {
248 err = got_diffreg_output(NULL, NULL, result,
249 blob1 != NULL, f2_exists,
250 label2, /* show local file's path, not a blob ID */
251 label2, GOT_DIFF_OUTPUT_UNIDIFF,
252 diff_context, outfile);
253 if (err)
254 goto done;
257 if (resultp && err == NULL)
258 *resultp = result;
259 else if (result) {
260 free_err = got_diffreg_result_free(result);
261 if (free_err && err == NULL)
262 err = free_err;
264 done:
265 return err;
268 const struct got_error *
269 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
270 const char *label1, FILE *f2, int f2_exists, size_t size2,
271 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
272 int ignore_whitespace, int force_text_diff, FILE *outfile)
274 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
275 size2, label2, diff_algo, diff_context, ignore_whitespace,
276 force_text_diff, outfile );
279 static const struct got_error *
280 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
281 const char *label, mode_t mode, struct got_repository *repo,
282 got_diff_blob_cb cb, void *cb_arg)
284 const struct got_error *err;
285 struct got_blob_object *blob = NULL;
286 struct got_object *obj = NULL;
288 err = got_object_open(&obj, repo, id);
289 if (err)
290 return err;
292 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
293 if (err)
294 goto done;
295 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
296 NULL, label, 0, mode, repo);
297 done:
298 got_object_close(obj);
299 if (blob)
300 got_object_blob_close(blob);
301 return err;
304 static const struct got_error *
305 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
306 FILE *f1, FILE *f2, int fd1, int fd2,
307 const char *label1, const char *label2,
308 mode_t mode1, mode_t mode2, struct got_repository *repo,
309 got_diff_blob_cb cb, void *cb_arg)
311 const struct got_error *err;
312 struct got_object *obj1 = NULL;
313 struct got_object *obj2 = NULL;
314 struct got_blob_object *blob1 = NULL;
315 struct got_blob_object *blob2 = NULL;
317 err = got_object_open(&obj1, repo, id1);
318 if (err)
319 return err;
321 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
322 err = got_error(GOT_ERR_OBJ_TYPE);
323 goto done;
326 err = got_object_open(&obj2, repo, id2);
327 if (err)
328 goto done;
329 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
330 err = got_error(GOT_ERR_BAD_OBJ_DATA);
331 goto done;
334 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
335 if (err)
336 goto done;
338 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
339 if (err)
340 goto done;
342 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
343 mode1, mode2, repo);
344 done:
345 if (obj1)
346 got_object_close(obj1);
347 if (obj2)
348 got_object_close(obj2);
349 if (blob1)
350 got_object_blob_close(blob1);
351 if (blob2)
352 got_object_blob_close(blob2);
353 return err;
356 static const struct got_error *
357 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
358 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
359 got_diff_blob_cb cb, void *cb_arg)
361 const struct got_error *err;
362 struct got_blob_object *blob = NULL;
363 struct got_object *obj = NULL;
365 err = got_object_open(&obj, repo, id);
366 if (err)
367 return err;
369 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
370 if (err)
371 goto done;
372 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
373 mode, 0, repo);
374 done:
375 got_object_close(obj);
376 if (blob)
377 got_object_blob_close(blob);
378 return err;
381 static const struct got_error *
382 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
383 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
384 void *cb_arg, int diff_content)
386 const struct got_error *err = NULL;
387 struct got_object *treeobj = NULL;
388 struct got_tree_object *tree = NULL;
390 err = got_object_open(&treeobj, repo, id);
391 if (err)
392 goto done;
394 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
395 err = got_error(GOT_ERR_OBJ_TYPE);
396 goto done;
399 err = got_object_tree_open(&tree, repo, treeobj);
400 if (err)
401 goto done;
403 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
404 repo, cb, cb_arg, diff_content);
405 done:
406 if (tree)
407 got_object_tree_close(tree);
408 if (treeobj)
409 got_object_close(treeobj);
410 return err;
413 static const struct got_error *
414 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
415 FILE *f1, FILE *f2, int fd1, int fd2,
416 const char *label1, const char *label2,
417 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
418 int diff_content)
420 const struct got_error *err;
421 struct got_object *treeobj1 = NULL;
422 struct got_object *treeobj2 = NULL;
423 struct got_tree_object *tree1 = NULL;
424 struct got_tree_object *tree2 = NULL;
426 err = got_object_open(&treeobj1, repo, id1);
427 if (err)
428 goto done;
430 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
431 err = got_error(GOT_ERR_OBJ_TYPE);
432 goto done;
435 err = got_object_open(&treeobj2, repo, id2);
436 if (err)
437 goto done;
439 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
440 err = got_error(GOT_ERR_OBJ_TYPE);
441 goto done;
444 err = got_object_tree_open(&tree1, repo, treeobj1);
445 if (err)
446 goto done;
448 err = got_object_tree_open(&tree2, repo, treeobj2);
449 if (err)
450 goto done;
452 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
453 label1, label2, repo, cb, cb_arg, diff_content);
455 done:
456 if (tree1)
457 got_object_tree_close(tree1);
458 if (tree2)
459 got_object_tree_close(tree2);
460 if (treeobj1)
461 got_object_close(treeobj1);
462 if (treeobj2)
463 got_object_close(treeobj2);
464 return err;
467 static const struct got_error *
468 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
469 FILE *f2, const char *label, 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 *treeobj = NULL;
474 struct got_tree_object *tree = NULL;
476 err = got_object_open(&treeobj, repo, id);
477 if (err)
478 goto done;
480 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
481 err = got_error(GOT_ERR_OBJ_TYPE);
482 goto done;
485 err = got_object_tree_open(&tree, repo, treeobj);
486 if (err)
487 goto done;
489 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
490 repo, cb, cb_arg, diff_content);
491 done:
492 if (tree)
493 got_object_tree_close(tree);
494 if (treeobj)
495 got_object_close(treeobj);
496 return err;
499 static const struct got_error *
500 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
501 const char *label1, const char *label2, struct got_repository *repo,
502 got_diff_blob_cb cb, void *cb_arg)
504 /* XXX TODO */
505 return NULL;
508 static const struct got_error *
509 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
510 FILE *f1, FILE *f2, int fd1, int fd2,
511 const char *label1, const char *label2,
512 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
513 int diff_content)
515 const struct got_error *err = NULL;
516 int id_match;
518 if (got_object_tree_entry_is_submodule(te1))
519 return NULL;
521 if (te2 == NULL) {
522 if (S_ISDIR(te1->mode))
523 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
524 label1, repo, cb, cb_arg, diff_content);
525 else {
526 if (diff_content)
527 err = diff_deleted_blob(&te1->id, f1, fd1,
528 f2, label1, te1->mode, repo, cb, cb_arg);
529 else
530 err = cb(cb_arg, NULL, NULL, NULL, NULL,
531 &te1->id, NULL, label1, NULL,
532 te1->mode, 0, repo);
534 return err;
535 } else if (got_object_tree_entry_is_submodule(te2))
536 return NULL;
538 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
539 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
540 if (!id_match)
541 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
542 fd1, fd2, label1, label2, repo, cb, cb_arg,
543 diff_content);
544 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
545 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
546 if (!id_match ||
547 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
548 (te2->mode & (S_IFLNK | S_IXUSR))) {
549 if (diff_content)
550 return diff_modified_blob(&te1->id, &te2->id,
551 f1, f2, fd1, fd2, label1, label2,
552 te1->mode, te2->mode, repo, cb, cb_arg);
553 else
554 return cb(cb_arg, NULL, NULL, NULL, NULL,
555 &te1->id, &te2->id, label1, label2,
556 te1->mode, te2->mode, repo);
560 if (id_match)
561 return NULL;
563 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
564 cb, cb_arg);
567 static const struct got_error *
568 diff_entry_new_old(struct got_tree_entry *te2,
569 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
570 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
571 int diff_content)
573 if (te1 != NULL) /* handled by diff_entry_old_new() */
574 return NULL;
576 if (got_object_tree_entry_is_submodule(te2))
577 return NULL;
579 if (S_ISDIR(te2->mode))
580 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
581 repo, cb, cb_arg, diff_content);
583 if (diff_content)
584 return diff_added_blob(&te2->id, f1, f2, fd2,
585 label2, te2->mode, repo, cb, cb_arg);
587 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
588 NULL, label2, 0, te2->mode, repo);
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, FILE *f1, FILE *f2,
594 struct got_object_id *id1, struct got_object_id *id2,
595 const char *label1, const char *label2,
596 mode_t mode1, mode_t mode2, struct got_repository *repo)
598 const struct got_error *err = NULL;
599 struct got_pathlist_head *paths = arg;
600 struct got_diff_changed_path *change = NULL;
601 char *path = NULL;
603 path = strdup(label2 ? label2 : label1);
604 if (path == NULL)
605 return got_error_from_errno("malloc");
607 change = malloc(sizeof(*change));
608 if (change == NULL) {
609 err = got_error_from_errno("malloc");
610 goto done;
613 change->status = GOT_STATUS_NO_CHANGE;
614 if (id1 == NULL)
615 change->status = GOT_STATUS_ADD;
616 else if (id2 == NULL)
617 change->status = GOT_STATUS_DELETE;
618 else {
619 if (got_object_id_cmp(id1, id2) != 0)
620 change->status = GOT_STATUS_MODIFY;
621 else if (mode1 != mode2)
622 change->status = GOT_STATUS_MODE_CHANGE;
625 err = got_pathlist_append(paths, path, change);
626 done:
627 if (err) {
628 free(path);
629 free(change);
631 return err;
634 const struct got_error *
635 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
636 FILE *f1, FILE *f2, int fd1, int fd2,
637 const char *label1, const char *label2,
638 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
639 int diff_content)
641 const struct got_error *err = NULL;
642 struct got_tree_entry *te1 = NULL;
643 struct got_tree_entry *te2 = NULL;
644 char *l1 = NULL, *l2 = NULL;
645 int tidx1 = 0, tidx2 = 0;
647 if (tree1) {
648 te1 = got_object_tree_get_entry(tree1, 0);
649 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
650 te1->name) == -1)
651 return got_error_from_errno("asprintf");
653 if (tree2) {
654 te2 = got_object_tree_get_entry(tree2, 0);
655 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
656 te2->name) == -1)
657 return got_error_from_errno("asprintf");
660 do {
661 if (te1) {
662 struct got_tree_entry *te = NULL;
663 if (tree2)
664 te = got_object_tree_find_entry(tree2,
665 te1->name);
666 if (te) {
667 free(l2);
668 l2 = NULL;
669 if (te && asprintf(&l2, "%s%s%s", label2,
670 label2[0] ? "/" : "", te->name) == -1)
671 return
672 got_error_from_errno("asprintf");
674 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
675 l1, l2, repo, cb, cb_arg, diff_content);
676 if (err)
677 break;
680 if (te2) {
681 struct got_tree_entry *te = NULL;
682 if (tree1)
683 te = got_object_tree_find_entry(tree1,
684 te2->name);
685 free(l2);
686 if (te) {
687 if (asprintf(&l2, "%s%s%s", label2,
688 label2[0] ? "/" : "", te->name) == -1)
689 return
690 got_error_from_errno("asprintf");
691 } else {
692 if (asprintf(&l2, "%s%s%s", label2,
693 label2[0] ? "/" : "", te2->name) == -1)
694 return
695 got_error_from_errno("asprintf");
697 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
698 repo, cb, cb_arg, diff_content);
699 if (err)
700 break;
703 free(l1);
704 l1 = NULL;
705 if (te1) {
706 tidx1++;
707 te1 = got_object_tree_get_entry(tree1, tidx1);
708 if (te1 &&
709 asprintf(&l1, "%s%s%s", label1,
710 label1[0] ? "/" : "", te1->name) == -1)
711 return got_error_from_errno("asprintf");
713 free(l2);
714 l2 = NULL;
715 if (te2) {
716 tidx2++;
717 te2 = got_object_tree_get_entry(tree2, tidx2);
718 if (te2 &&
719 asprintf(&l2, "%s%s%s", label2,
720 label2[0] ? "/" : "", te2->name) == -1)
721 return got_error_from_errno("asprintf");
723 } while (te1 || te2);
725 return err;
728 const struct got_error *
729 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
730 FILE *f1, FILE *f2, int fd1, int fd2,
731 struct got_object_id *id1, struct got_object_id *id2,
732 const char *label1, const char *label2,
733 enum got_diff_algorithm diff_algo, int diff_context,
734 int ignore_whitespace, int force_text_diff,
735 struct got_repository *repo, FILE *outfile)
737 const struct got_error *err;
738 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
740 if (id1 == NULL && id2 == NULL)
741 return got_error(GOT_ERR_NO_OBJ);
743 if (id1) {
744 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
745 if (err)
746 goto done;
748 if (id2) {
749 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
750 if (err)
751 goto done;
753 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
754 label1, label2, diff_algo, diff_context, ignore_whitespace,
755 force_text_diff, outfile);
756 done:
757 if (blob1)
758 got_object_blob_close(blob1);
759 if (blob2)
760 got_object_blob_close(blob2);
761 return err;
764 static const struct got_error *
765 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
766 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
767 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
769 const struct got_error *err = NULL;
770 struct got_pathlist_entry *pe;
771 struct got_object_id *id1 = NULL, *id2 = NULL;
772 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
773 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
775 TAILQ_FOREACH(pe, paths, entry) {
776 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
777 mode_t mode1 = 0, mode2 = 0;
779 free(id1);
780 id1 = NULL;
781 free(id2);
782 id2 = NULL;
783 if (subtree1) {
784 got_object_tree_close(subtree1);
785 subtree1 = NULL;
787 if (subtree2) {
788 got_object_tree_close(subtree2);
789 subtree2 = NULL;
791 if (blob1) {
792 got_object_blob_close(blob1);
793 blob1 = NULL;
795 if (blob2) {
796 got_object_blob_close(blob2);
797 blob2 = NULL;
800 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
801 pe->path);
802 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
803 goto done;
804 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
805 pe->path);
806 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
807 goto done;
808 if (id1 == NULL && id2 == NULL) {
809 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
810 goto done;
812 if (id1) {
813 err = got_object_get_type(&type1, repo, id1);
814 if (err)
815 goto done;
817 if (id2) {
818 err = got_object_get_type(&type2, repo, id2);
819 if (err)
820 goto done;
822 if (type1 == GOT_OBJ_TYPE_ANY &&
823 type2 == GOT_OBJ_TYPE_ANY) {
824 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
825 goto done;
826 } else if (type1 != GOT_OBJ_TYPE_ANY &&
827 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
828 err = got_error(GOT_ERR_OBJ_TYPE);
829 goto done;
832 if (type1 == GOT_OBJ_TYPE_BLOB ||
833 type2 == GOT_OBJ_TYPE_BLOB) {
834 if (id1) {
835 err = got_object_open_as_blob(&blob1, repo,
836 id1, 8192, fd1);
837 if (err)
838 goto done;
840 if (id2) {
841 err = got_object_open_as_blob(&blob2, repo,
842 id2, 8192, fd2);
843 if (err)
844 goto done;
846 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
847 id1 ? pe->path : "/dev/null",
848 id2 ? pe->path : "/dev/null",
849 mode1, mode2, repo);
850 if (err)
851 goto done;
852 } else if (type1 == GOT_OBJ_TYPE_TREE ||
853 type2 == GOT_OBJ_TYPE_TREE) {
854 if (id1) {
855 err = got_object_open_as_tree(&subtree1, repo,
856 id1);
857 if (err)
858 goto done;
860 if (id2) {
861 err = got_object_open_as_tree(&subtree2, repo,
862 id2);
863 if (err)
864 goto done;
866 err = got_diff_tree(subtree1, subtree2, f1, f2,
867 fd1, fd2,
868 id1 ? pe->path : "/dev/null",
869 id2 ? pe->path : "/dev/null",
870 repo, cb, cb_arg, 1);
871 if (err)
872 goto done;
873 } else {
874 err = got_error(GOT_ERR_OBJ_TYPE);
875 goto done;
878 done:
879 free(id1);
880 free(id2);
881 if (subtree1)
882 got_object_tree_close(subtree1);
883 if (subtree2)
884 got_object_tree_close(subtree2);
885 if (blob1)
886 got_object_blob_close(blob1);
887 if (blob2)
888 got_object_blob_close(blob2);
889 return err;
892 static const struct got_error *
893 show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
894 int ch, const char *id_str, FILE *outfile)
896 const struct got_error *err;
897 int n;
898 off_t outoff = 0;
900 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
901 if (line_offsets != NULL && *line_offsets != NULL) {
902 if (*nlines == 0) {
903 err = add_line_offset(line_offsets, nlines, 0);
904 if (err)
905 return err;
906 } else
907 outoff = (*line_offsets)[*nlines - 1];
909 outoff += n;
910 err = add_line_offset(line_offsets, nlines, outoff);
911 if (err)
912 return err;
915 return NULL;
918 static const struct got_error *
919 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
920 FILE *f1, FILE *f2, int fd1, int fd2,
921 struct got_object_id *id1, struct got_object_id *id2,
922 struct got_pathlist_head *paths, const char *label1, const char *label2,
923 int diff_context, int ignore_whitespace, int force_text_diff,
924 struct got_repository *repo, FILE *outfile,
925 enum got_diff_algorithm diff_algo)
927 const struct got_error *err;
928 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
929 struct got_diff_blob_output_unidiff_arg arg;
930 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
932 if (id1 == NULL && id2 == NULL)
933 return got_error(GOT_ERR_NO_OBJ);
935 if (id1) {
936 err = got_object_open_as_tree(&tree1, repo, id1);
937 if (err)
938 goto done;
940 if (id2) {
941 err = got_object_open_as_tree(&tree2, repo, id2);
942 if (err)
943 goto done;
946 arg.diff_algo = diff_algo;
947 arg.diff_context = diff_context;
948 arg.ignore_whitespace = ignore_whitespace;
949 arg.force_text_diff = force_text_diff;
950 arg.outfile = outfile;
951 if (want_lineoffsets) {
952 arg.line_offsets = *line_offsets;
953 arg.nlines = *nlines;
954 } else {
955 arg.line_offsets = NULL;
956 arg.nlines = 0;
958 if (paths == NULL || TAILQ_EMPTY(paths)) {
959 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
960 label1, label2, repo,
961 got_diff_blob_output_unidiff, &arg, 1);
962 } else {
963 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
964 got_diff_blob_output_unidiff, &arg);
966 if (want_lineoffsets) {
967 *line_offsets = arg.line_offsets; /* was likely re-allocated */
968 *nlines = arg.nlines;
970 done:
971 if (tree1)
972 got_object_tree_close(tree1);
973 if (tree2)
974 got_object_tree_close(tree2);
975 return err;
978 const struct got_error *
979 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
980 FILE *f1, FILE *f2, int fd1, int fd2,
981 struct got_object_id *id1, struct got_object_id *id2,
982 struct got_pathlist_head *paths, const char *label1, const char *label2,
983 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
984 int force_text_diff, struct got_repository *repo, FILE *outfile)
986 const struct got_error *err;
987 char *idstr = NULL;
989 if (id1 == NULL && id2 == NULL)
990 return got_error(GOT_ERR_NO_OBJ);
992 if (id1) {
993 err = got_object_id_str(&idstr, id1);
994 if (err)
995 goto done;
996 err = show_object_id(line_offsets, nlines, "tree", '-',
997 idstr, outfile);
998 if (err)
999 goto done;
1000 free(idstr);
1001 idstr = NULL;
1002 } else {
1003 err = show_object_id(line_offsets, nlines, "tree", '-',
1004 "/dev/null", outfile);
1005 if (err)
1006 goto done;
1009 if (id2) {
1010 err = got_object_id_str(&idstr, id2);
1011 if (err)
1012 goto done;
1013 err = show_object_id(line_offsets, nlines, "tree", '+',
1014 idstr, outfile);
1015 if (err)
1016 goto done;
1017 free(idstr);
1018 idstr = NULL;
1019 } else {
1020 err = show_object_id(line_offsets, nlines, "tree", '+',
1021 "/dev/null", outfile);
1022 if (err)
1023 goto done;
1026 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1027 id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1028 force_text_diff, repo, outfile, diff_algo);
1029 done:
1030 free(idstr);
1031 return err;
1034 const struct got_error *
1035 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1036 FILE *f1, FILE *f2, int fd1, int fd2,
1037 struct got_object_id *id1, struct got_object_id *id2,
1038 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1039 int diff_context, int ignore_whitespace, int force_text_diff,
1040 struct got_repository *repo, FILE *outfile)
1042 const struct got_error *err;
1043 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1044 char *idstr = NULL;
1046 if (id2 == NULL)
1047 return got_error(GOT_ERR_NO_OBJ);
1049 if (id1) {
1050 err = got_object_open_as_commit(&commit1, repo, id1);
1051 if (err)
1052 goto done;
1053 err = got_object_id_str(&idstr, id1);
1054 if (err)
1055 goto done;
1056 err = show_object_id(line_offsets, nlines, "commit", '-',
1057 idstr, outfile);
1058 if (err)
1059 goto done;
1060 free(idstr);
1061 idstr = NULL;
1062 } else {
1063 err = show_object_id(line_offsets, nlines, "commit", '-',
1064 "/dev/null", outfile);
1065 if (err)
1066 goto done;
1069 err = got_object_open_as_commit(&commit2, repo, id2);
1070 if (err)
1071 goto done;
1073 err = got_object_id_str(&idstr, id2);
1074 if (err)
1075 goto done;
1076 err = show_object_id(line_offsets, nlines, "commit", '+',
1077 idstr, outfile);
1078 if (err)
1079 goto done;
1081 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1082 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1083 got_object_commit_get_tree_id(commit2), paths, "", "",
1084 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1085 diff_algo);
1086 done:
1087 if (commit1)
1088 got_object_commit_close(commit1);
1089 if (commit2)
1090 got_object_commit_close(commit2);
1091 free(idstr);
1092 return err;
1095 const struct got_error *
1096 got_diff_files(struct got_diffreg_result **resultp,
1097 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1098 const char *label2, int diff_context, int ignore_whitespace,
1099 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1101 const struct got_error *err = NULL;
1102 struct got_diffreg_result *diffreg_result = NULL;
1104 if (resultp)
1105 *resultp = NULL;
1107 if (outfile) {
1108 fprintf(outfile, "file - %s\n",
1109 f1_exists ? label1 : "/dev/null");
1110 fprintf(outfile, "file + %s\n",
1111 f2_exists ? label2 : "/dev/null");
1114 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1115 ignore_whitespace, force_text_diff);
1116 if (err)
1117 goto done;
1119 if (outfile) {
1120 err = got_diffreg_output(NULL, NULL, diffreg_result,
1121 f1_exists, f2_exists, label1, label2,
1122 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1123 if (err)
1124 goto done;
1127 done:
1128 if (resultp && err == NULL)
1129 *resultp = diffreg_result;
1130 else if (diffreg_result) {
1131 const struct got_error *free_err;
1132 free_err = got_diffreg_result_free(diffreg_result);
1133 if (free_err && err == NULL)
1134 err = free_err;
1137 return err;