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 {
62 const struct got_error *err = NULL, *free_err;
63 char hex1[SHA1_DIGEST_STRING_LENGTH];
64 char hex2[SHA1_DIGEST_STRING_LENGTH];
65 const char *idstr1 = NULL, *idstr2 = NULL;
66 off_t size1, size2;
67 struct got_diffreg_result *result;
68 off_t outoff = 0;
69 int n;
71 if (line_offsets && *line_offsets && *nlines > 0)
72 outoff = (*line_offsets)[*nlines - 1];
73 else if (line_offsets) {
74 err = add_line_offset(line_offsets, nlines, 0);
75 if (err)
76 goto done;
77 }
79 if (resultp)
80 *resultp = NULL;
82 if (f1) {
83 err = got_opentemp_truncate(f1);
84 if (err)
85 goto done;
86 }
87 if (f2) {
88 err = got_opentemp_truncate(f2);
89 if (err)
90 goto done;
91 }
93 size1 = 0;
94 if (blob1) {
95 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
96 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
97 blob1);
98 if (err)
99 goto done;
100 } else
101 idstr1 = "/dev/null";
103 size2 = 0;
104 if (blob2) {
105 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
106 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
107 blob2);
108 if (err)
109 goto done;
110 } else
111 idstr2 = "/dev/null";
113 if (outfile) {
114 char *modestr1 = NULL, *modestr2 = NULL;
115 int modebits;
116 if (mode1 && mode1 != mode2) {
117 if (S_ISLNK(mode1))
118 modebits = S_IFLNK;
119 else
120 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
121 if (asprintf(&modestr1, " (mode %o)",
122 mode1 & modebits) == -1) {
123 err = got_error_from_errno("asprintf");
124 goto done;
127 if (mode2 && mode1 != mode2) {
128 if (S_ISLNK(mode2))
129 modebits = S_IFLNK;
130 else
131 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
132 if (asprintf(&modestr2, " (mode %o)",
133 mode2 & modebits) == -1) {
134 err = got_error_from_errno("asprintf");
135 goto done;
138 n = fprintf(outfile, "blob - %s%s\n", idstr1,
139 modestr1 ? modestr1 : "");
140 if (n < 0)
141 goto done;
142 outoff += n;
143 if (line_offsets) {
144 err = add_line_offset(line_offsets, nlines, outoff);
145 if (err)
146 goto done;
149 n = fprintf(outfile, "blob + %s%s\n", idstr2,
150 modestr2 ? modestr2 : "");
151 if (n < 0)
152 goto done;
153 outoff += n;
154 if (line_offsets) {
155 err = add_line_offset(line_offsets, nlines, outoff);
156 if (err)
157 goto done;
160 free(modestr1);
161 free(modestr2);
163 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
164 ignore_whitespace, force_text_diff);
165 if (err)
166 goto done;
168 if (outfile) {
169 err = got_diffreg_output(line_offsets, nlines, result,
170 blob1 != NULL, blob2 != NULL,
171 label1 ? label1 : idstr1,
172 label2 ? label2 : idstr2,
173 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
174 if (err)
175 goto done;
178 if (resultp && err == NULL)
179 *resultp = result;
180 else {
181 free_err = got_diffreg_result_free(result);
182 if (free_err && err == NULL)
183 err = free_err;
185 done:
186 return err;
189 const struct got_error *
190 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
191 struct got_blob_object *blob2, FILE *f1, FILE *f2,
192 struct got_object_id *id1, struct got_object_id *id2,
193 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
194 struct got_repository *repo)
196 struct got_diff_blob_output_unidiff_arg *a = arg;
198 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
199 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
200 a->ignore_whitespace, a->force_text_diff, a->outfile);
203 const struct got_error *
204 got_diff_blob(off_t **line_offsets, size_t *nlines,
205 struct got_blob_object *blob1, struct got_blob_object *blob2,
206 FILE *f1, FILE *f2, const char *label1, const char *label2,
207 int diff_context, int ignore_whitespace, int force_text_diff,
208 FILE *outfile)
210 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
211 label1, label2, 0, 0, diff_context, ignore_whitespace,
212 force_text_diff, outfile);
215 static const struct got_error *
216 diff_blob_file(struct got_diffreg_result **resultp,
217 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
218 FILE *f2, size_t size2, const char *label2, int diff_context,
219 int ignore_whitespace, int force_text_diff, FILE *outfile)
221 const struct got_error *err = NULL, *free_err;
222 char hex1[SHA1_DIGEST_STRING_LENGTH];
223 const char *idstr1 = NULL;
224 struct got_diffreg_result *result = NULL;
226 if (resultp)
227 *resultp = NULL;
229 if (blob1)
230 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
231 else
232 idstr1 = "/dev/null";
234 if (outfile) {
235 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 fprintf(outfile, "file + %s\n",
237 f2 == NULL ? "/dev/null" : label2);
240 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
241 ignore_whitespace, force_text_diff);
242 if (err)
243 goto done;
245 if (outfile) {
246 err = got_diffreg_output(NULL, NULL, result,
247 f1 != NULL, f2 != NULL,
248 label2, /* show local file's path, not a blob ID */
249 label2, GOT_DIFF_OUTPUT_UNIDIFF,
250 diff_context, outfile);
251 if (err)
252 goto done;
255 if (resultp && err == NULL)
256 *resultp = result;
257 else if (result) {
258 free_err = got_diffreg_result_free(result);
259 if (free_err && err == NULL)
260 err = free_err;
262 done:
263 return err;
266 const struct got_error *
267 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
268 const char *label1, FILE *f2, size_t size2, const char *label2,
269 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
271 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
272 diff_context, ignore_whitespace, force_text_diff, outfile);
275 static const struct got_error *
276 diff_added_blob(struct got_object_id *id, FILE *f, int fd,
277 const char *label, mode_t mode, struct got_repository *repo,
278 got_diff_blob_cb cb, void *cb_arg)
280 const struct got_error *err;
281 struct got_blob_object *blob = NULL;
282 struct got_object *obj = NULL;
284 err = got_object_open(&obj, repo, id);
285 if (err)
286 return err;
288 err = got_object_blob_open(&blob, repo, obj, 8192, fd);
289 if (err)
290 goto done;
291 err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
292 NULL, label, 0, mode, repo);
293 done:
294 got_object_close(obj);
295 if (blob)
296 got_object_blob_close(blob);
297 return err;
300 static const struct got_error *
301 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
302 FILE *f1, FILE *f2, int fd1, int fd2,
303 const char *label1, const char *label2,
304 mode_t mode1, mode_t mode2, struct got_repository *repo,
305 got_diff_blob_cb cb, void *cb_arg)
307 const struct got_error *err;
308 struct got_object *obj1 = NULL;
309 struct got_object *obj2 = NULL;
310 struct got_blob_object *blob1 = NULL;
311 struct got_blob_object *blob2 = NULL;
313 err = got_object_open(&obj1, repo, id1);
314 if (err)
315 return err;
317 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
319 goto done;
322 err = got_object_open(&obj2, repo, id2);
323 if (err)
324 goto done;
325 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
326 err = got_error(GOT_ERR_BAD_OBJ_DATA);
327 goto done;
330 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
331 if (err)
332 goto done;
334 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
335 if (err)
336 goto done;
338 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
339 mode1, mode2, repo);
340 done:
341 if (obj1)
342 got_object_close(obj1);
343 if (obj2)
344 got_object_close(obj2);
345 if (blob1)
346 got_object_blob_close(blob1);
347 if (blob2)
348 got_object_blob_close(blob2);
349 return err;
352 static const struct got_error *
353 diff_deleted_blob(struct got_object_id *id, FILE *f, int fd,
354 const char *label, mode_t mode, struct got_repository *repo,
355 got_diff_blob_cb cb, void *cb_arg)
357 const struct got_error *err;
358 struct got_blob_object *blob = NULL;
359 struct got_object *obj = NULL;
361 err = got_object_open(&obj, repo, id);
362 if (err)
363 return err;
365 err = got_object_blob_open(&blob, repo, obj, 8192, fd);
366 if (err)
367 goto done;
368 err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
369 mode, 0, repo);
370 done:
371 got_object_close(obj);
372 if (blob)
373 got_object_blob_close(blob);
374 return err;
377 static const struct got_error *
378 diff_added_tree(struct got_object_id *id, FILE *f, int fd, const char *label,
379 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
380 int diff_content)
382 const struct got_error *err = NULL;
383 struct got_object *treeobj = NULL;
384 struct got_tree_object *tree = NULL;
386 err = got_object_open(&treeobj, repo, id);
387 if (err)
388 goto done;
390 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
391 err = got_error(GOT_ERR_OBJ_TYPE);
392 goto done;
395 err = got_object_tree_open(&tree, repo, treeobj);
396 if (err)
397 goto done;
399 err = got_diff_tree(NULL, tree, NULL, f, -1, fd, NULL, label,
400 repo, cb, cb_arg, diff_content);
401 done:
402 if (tree)
403 got_object_tree_close(tree);
404 if (treeobj)
405 got_object_close(treeobj);
406 return err;
409 static const struct got_error *
410 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
411 FILE *f1, FILE *f2, int fd1, int fd2,
412 const char *label1, const char *label2,
413 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
414 int diff_content)
416 const struct got_error *err;
417 struct got_object *treeobj1 = NULL;
418 struct got_object *treeobj2 = NULL;
419 struct got_tree_object *tree1 = NULL;
420 struct got_tree_object *tree2 = NULL;
422 err = got_object_open(&treeobj1, repo, id1);
423 if (err)
424 goto done;
426 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
427 err = got_error(GOT_ERR_OBJ_TYPE);
428 goto done;
431 err = got_object_open(&treeobj2, repo, id2);
432 if (err)
433 goto done;
435 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
436 err = got_error(GOT_ERR_OBJ_TYPE);
437 goto done;
440 err = got_object_tree_open(&tree1, repo, treeobj1);
441 if (err)
442 goto done;
444 err = got_object_tree_open(&tree2, repo, treeobj2);
445 if (err)
446 goto done;
448 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
449 label1, label2, repo, cb, cb_arg, diff_content);
451 done:
452 if (tree1)
453 got_object_tree_close(tree1);
454 if (tree2)
455 got_object_tree_close(tree2);
456 if (treeobj1)
457 got_object_close(treeobj1);
458 if (treeobj2)
459 got_object_close(treeobj2);
460 return err;
463 static const struct got_error *
464 diff_deleted_tree(struct got_object_id *id, FILE *f, int fd,
465 const char *label, struct got_repository *repo,
466 got_diff_blob_cb cb, void *cb_arg, int diff_content)
468 const struct got_error *err;
469 struct got_object *treeobj = NULL;
470 struct got_tree_object *tree = NULL;
472 err = got_object_open(&treeobj, repo, id);
473 if (err)
474 goto done;
476 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
477 err = got_error(GOT_ERR_OBJ_TYPE);
478 goto done;
481 err = got_object_tree_open(&tree, repo, treeobj);
482 if (err)
483 goto done;
485 err = got_diff_tree(tree, NULL, f, NULL, fd, -1, label, NULL,
486 repo, cb, cb_arg, diff_content);
487 done:
488 if (tree)
489 got_object_tree_close(tree);
490 if (treeobj)
491 got_object_close(treeobj);
492 return err;
495 static const struct got_error *
496 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
497 const char *label1, const char *label2, struct got_repository *repo,
498 got_diff_blob_cb cb, void *cb_arg)
500 /* XXX TODO */
501 return NULL;
504 static const struct got_error *
505 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
506 FILE *f1, FILE *f2, int fd1, int fd2,
507 const char *label1, const char *label2,
508 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
509 int diff_content)
511 const struct got_error *err = NULL;
512 int id_match;
514 if (got_object_tree_entry_is_submodule(te1))
515 return NULL;
517 if (te2 == NULL) {
518 if (S_ISDIR(te1->mode))
519 err = diff_deleted_tree(&te1->id, f1, fd1, label1,
520 repo, cb, cb_arg, diff_content);
521 else {
522 if (diff_content)
523 err = diff_deleted_blob(&te1->id, f1, fd1,
524 label1, te1->mode, repo, cb, cb_arg);
525 else
526 err = cb(cb_arg, NULL, NULL, NULL, NULL,
527 &te1->id, NULL, label1, NULL,
528 te1->mode, 0, repo);
530 return err;
531 } else if (got_object_tree_entry_is_submodule(te2))
532 return NULL;
534 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
535 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
536 if (!id_match)
537 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
538 fd1, fd2, label1, label2, repo, cb, cb_arg,
539 diff_content);
540 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
541 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
542 if (!id_match ||
543 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
544 (te2->mode & (S_IFLNK | S_IXUSR))) {
545 if (diff_content)
546 return diff_modified_blob(&te1->id, &te2->id,
547 f1, f2, fd1, fd2, label1, label2,
548 te1->mode, te2->mode, repo, cb, cb_arg);
549 else
550 return cb(cb_arg, NULL, NULL, NULL, NULL,
551 &te1->id, &te2->id, label1, label2,
552 te1->mode, te2->mode, repo);
556 if (id_match)
557 return NULL;
559 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
560 cb, cb_arg);
563 static const struct got_error *
564 diff_entry_new_old(struct got_tree_entry *te2,
565 struct got_tree_entry *te1, FILE *f2, int fd2, const char *label2,
566 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
567 int diff_content)
569 if (te1 != NULL) /* handled by diff_entry_old_new() */
570 return NULL;
572 if (got_object_tree_entry_is_submodule(te2))
573 return NULL;
575 if (S_ISDIR(te2->mode))
576 return diff_added_tree(&te2->id, f2, fd2, label2,
577 repo, cb, cb_arg, diff_content);
579 if (diff_content)
580 return diff_added_blob(&te2->id, f2, fd2,
581 label2, te2->mode, repo, cb, cb_arg);
583 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
584 NULL, label2, 0, te2->mode, repo);
587 const struct got_error *
588 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
589 struct got_blob_object *blob2, FILE *f1, FILE *f2,
590 struct got_object_id *id1, struct got_object_id *id2,
591 const char *label1, const char *label2,
592 mode_t mode1, mode_t mode2, struct got_repository *repo)
594 const struct got_error *err = NULL;
595 struct got_pathlist_head *paths = arg;
596 struct got_diff_changed_path *change = NULL;
597 char *path = NULL;
599 path = strdup(label2 ? label2 : label1);
600 if (path == NULL)
601 return got_error_from_errno("malloc");
603 change = malloc(sizeof(*change));
604 if (change == NULL) {
605 err = got_error_from_errno("malloc");
606 goto done;
609 change->status = GOT_STATUS_NO_CHANGE;
610 if (id1 == NULL)
611 change->status = GOT_STATUS_ADD;
612 else if (id2 == NULL)
613 change->status = GOT_STATUS_DELETE;
614 else {
615 if (got_object_id_cmp(id1, id2) != 0)
616 change->status = GOT_STATUS_MODIFY;
617 else if (mode1 != mode2)
618 change->status = GOT_STATUS_MODE_CHANGE;
621 err = got_pathlist_append(paths, path, change);
622 done:
623 if (err) {
624 free(path);
625 free(change);
627 return err;
630 const struct got_error *
631 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
632 FILE *f1, FILE *f2, int fd1, int fd2,
633 const char *label1, const char *label2,
634 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
635 int diff_content)
637 const struct got_error *err = NULL;
638 struct got_tree_entry *te1 = NULL;
639 struct got_tree_entry *te2 = NULL;
640 char *l1 = NULL, *l2 = NULL;
641 int tidx1 = 0, tidx2 = 0;
643 if (tree1) {
644 te1 = got_object_tree_get_entry(tree1, 0);
645 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
646 te1->name) == -1)
647 return got_error_from_errno("asprintf");
649 if (tree2) {
650 te2 = got_object_tree_get_entry(tree2, 0);
651 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
652 te2->name) == -1)
653 return got_error_from_errno("asprintf");
656 do {
657 if (te1) {
658 struct got_tree_entry *te = NULL;
659 if (tree2)
660 te = got_object_tree_find_entry(tree2,
661 te1->name);
662 if (te) {
663 free(l2);
664 l2 = NULL;
665 if (te && asprintf(&l2, "%s%s%s", label2,
666 label2[0] ? "/" : "", te->name) == -1)
667 return
668 got_error_from_errno("asprintf");
670 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
671 l1, l2, repo, cb, cb_arg, diff_content);
672 if (err)
673 break;
676 if (te2) {
677 struct got_tree_entry *te = NULL;
678 if (tree1)
679 te = got_object_tree_find_entry(tree1,
680 te2->name);
681 free(l2);
682 if (te) {
683 if (asprintf(&l2, "%s%s%s", label2,
684 label2[0] ? "/" : "", te->name) == -1)
685 return
686 got_error_from_errno("asprintf");
687 } else {
688 if (asprintf(&l2, "%s%s%s", label2,
689 label2[0] ? "/" : "", te2->name) == -1)
690 return
691 got_error_from_errno("asprintf");
693 err = diff_entry_new_old(te2, te, f2, fd2, l2, repo,
694 cb, cb_arg, diff_content);
695 if (err)
696 break;
699 free(l1);
700 l1 = NULL;
701 if (te1) {
702 tidx1++;
703 te1 = got_object_tree_get_entry(tree1, tidx1);
704 if (te1 &&
705 asprintf(&l1, "%s%s%s", label1,
706 label1[0] ? "/" : "", te1->name) == -1)
707 return got_error_from_errno("asprintf");
709 free(l2);
710 l2 = NULL;
711 if (te2) {
712 tidx2++;
713 te2 = got_object_tree_get_entry(tree2, tidx2);
714 if (te2 &&
715 asprintf(&l2, "%s%s%s", label2,
716 label2[0] ? "/" : "", te2->name) == -1)
717 return got_error_from_errno("asprintf");
719 } while (te1 || te2);
721 return err;
724 const struct got_error *
725 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
726 FILE *f1, FILE *f2, int fd1, int fd2,
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);
738 if (id1) {
739 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
740 if (err)
741 goto done;
743 if (id2) {
744 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
745 if (err)
746 goto done;
748 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
749 label1, label2, diff_context, ignore_whitespace, force_text_diff,
750 outfile);
751 done:
752 if (blob1)
753 got_object_blob_close(blob1);
754 if (blob2)
755 got_object_blob_close(blob2);
756 return err;
759 static const struct got_error *
760 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
761 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
762 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
764 const struct got_error *err = NULL;
765 struct got_pathlist_entry *pe;
766 struct got_object_id *id1 = NULL, *id2 = NULL;
767 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
768 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
770 TAILQ_FOREACH(pe, paths, entry) {
771 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
772 mode_t mode1 = 0, mode2 = 0;
774 free(id1);
775 id1 = NULL;
776 free(id2);
777 id2 = NULL;
778 if (subtree1) {
779 got_object_tree_close(subtree1);
780 subtree1 = NULL;
782 if (subtree2) {
783 got_object_tree_close(subtree2);
784 subtree2 = NULL;
786 if (blob1) {
787 got_object_blob_close(blob1);
788 blob1 = NULL;
790 if (blob2) {
791 got_object_blob_close(blob2);
792 blob2 = NULL;
795 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
796 pe->path);
797 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
798 goto done;
799 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
800 pe->path);
801 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
802 goto done;
803 if (id1 == NULL && id2 == NULL) {
804 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
805 goto done;
807 if (id1) {
808 err = got_object_get_type(&type1, repo, id1);
809 if (err)
810 goto done;
812 if (id2) {
813 err = got_object_get_type(&type2, repo, id2);
814 if (err)
815 goto done;
817 if (type1 == GOT_OBJ_TYPE_ANY &&
818 type2 == GOT_OBJ_TYPE_ANY) {
819 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
820 goto done;
821 } else if (type1 != GOT_OBJ_TYPE_ANY &&
822 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
823 err = got_error(GOT_ERR_OBJ_TYPE);
824 goto done;
827 if (type1 == GOT_OBJ_TYPE_BLOB ||
828 type2 == GOT_OBJ_TYPE_BLOB) {
829 if (id1) {
830 err = got_object_open_as_blob(&blob1, repo,
831 id1, 8192, fd1);
832 if (err)
833 goto done;
835 if (id2) {
836 err = got_object_open_as_blob(&blob2, repo,
837 id2, 8192, fd2);
838 if (err)
839 goto done;
841 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
842 id1 ? pe->path : "/dev/null",
843 id2 ? pe->path : "/dev/null",
844 mode1, mode2, repo);
845 if (err)
846 goto done;
847 } else if (type1 == GOT_OBJ_TYPE_TREE ||
848 type2 == GOT_OBJ_TYPE_TREE) {
849 if (id1) {
850 err = got_object_open_as_tree(&subtree1, repo,
851 id1);
852 if (err)
853 goto done;
855 if (id2) {
856 err = got_object_open_as_tree(&subtree2, repo,
857 id2);
858 if (err)
859 goto done;
861 err = got_diff_tree(subtree1, subtree2, f1, f2,
862 fd1, fd2,
863 id1 ? pe->path : "/dev/null",
864 id2 ? pe->path : "/dev/null",
865 repo, cb, cb_arg, 1);
866 if (err)
867 goto done;
868 } else {
869 err = got_error(GOT_ERR_OBJ_TYPE);
870 goto done;
873 done:
874 free(id1);
875 free(id2);
876 if (subtree1)
877 got_object_tree_close(subtree1);
878 if (subtree2)
879 got_object_tree_close(subtree2);
880 if (blob1)
881 got_object_blob_close(blob1);
882 if (blob2)
883 got_object_blob_close(blob2);
884 return err;
887 static const struct got_error *
888 show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
889 int ch, const char *id_str, FILE *outfile)
891 const struct got_error *err;
892 int n;
893 off_t outoff = 0;
895 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
896 if (line_offsets != NULL && *line_offsets != NULL) {
897 if (*nlines == 0) {
898 err = add_line_offset(line_offsets, nlines, 0);
899 if (err)
900 return err;
901 } else
902 outoff = (*line_offsets)[*nlines - 1];
904 outoff += n;
905 err = add_line_offset(line_offsets, nlines, outoff);
906 if (err)
907 return err;
910 return NULL;
913 static const struct got_error *
914 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
915 FILE *f1, FILE *f2, int fd1, int fd2,
916 struct got_object_id *id1, struct got_object_id *id2,
917 struct got_pathlist_head *paths, const char *label1, const char *label2,
918 int diff_context, int ignore_whitespace, int force_text_diff,
919 struct got_repository *repo, FILE *outfile)
921 const struct got_error *err;
922 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
923 struct got_diff_blob_output_unidiff_arg arg;
924 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
926 if (id1 == NULL && id2 == NULL)
927 return got_error(GOT_ERR_NO_OBJ);
929 if (id1) {
930 err = got_object_open_as_tree(&tree1, repo, id1);
931 if (err)
932 goto done;
934 if (id2) {
935 err = got_object_open_as_tree(&tree2, repo, id2);
936 if (err)
937 goto done;
940 arg.diff_context = diff_context;
941 arg.ignore_whitespace = ignore_whitespace;
942 arg.force_text_diff = force_text_diff;
943 arg.outfile = outfile;
944 if (want_lineoffsets) {
945 arg.line_offsets = *line_offsets;
946 arg.nlines = *nlines;
947 } else {
948 arg.line_offsets = NULL;
949 arg.nlines = 0;
951 if (paths == NULL || TAILQ_EMPTY(paths)) {
952 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
953 label1, label2, repo,
954 got_diff_blob_output_unidiff, &arg, 1);
955 } else {
956 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
957 got_diff_blob_output_unidiff, &arg);
959 if (want_lineoffsets) {
960 *line_offsets = arg.line_offsets; /* was likely re-allocated */
961 *nlines = arg.nlines;
963 done:
964 if (tree1)
965 got_object_tree_close(tree1);
966 if (tree2)
967 got_object_tree_close(tree2);
968 return err;
971 const struct got_error *
972 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
973 FILE *f1, FILE *f2, int fd1, int fd2,
974 struct got_object_id *id1, struct got_object_id *id2,
975 struct got_pathlist_head *paths, const char *label1, const char *label2,
976 int diff_context, int ignore_whitespace, int force_text_diff,
977 struct got_repository *repo, FILE *outfile)
979 const struct got_error *err;
980 char *idstr = NULL;
982 if (id1 == NULL && id2 == NULL)
983 return got_error(GOT_ERR_NO_OBJ);
985 if (id1) {
986 err = got_object_id_str(&idstr, id1);
987 if (err)
988 goto done;
989 err = show_object_id(line_offsets, nlines, "tree", '-',
990 idstr, outfile);
991 if (err)
992 goto done;
993 free(idstr);
994 idstr = NULL;
995 } else {
996 err = show_object_id(line_offsets, nlines, "tree", '-',
997 "/dev/null", outfile);
998 if (err)
999 goto done;
1002 if (id2) {
1003 err = got_object_id_str(&idstr, id2);
1004 if (err)
1005 goto done;
1006 err = show_object_id(line_offsets, nlines, "tree", '+',
1007 idstr, outfile);
1008 if (err)
1009 goto done;
1010 free(idstr);
1011 idstr = NULL;
1012 } else {
1013 err = show_object_id(line_offsets, nlines, "tree", '+',
1014 "/dev/null", outfile);
1015 if (err)
1016 goto done;
1019 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1020 id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1021 force_text_diff, repo, outfile);
1022 done:
1023 free(idstr);
1024 return err;
1027 const struct got_error *
1028 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1029 FILE *f1, FILE *f2, int fd1, int fd2,
1030 struct got_object_id *id1, struct got_object_id *id2,
1031 struct got_pathlist_head *paths,
1032 int diff_context, int ignore_whitespace, int force_text_diff,
1033 struct got_repository *repo, FILE *outfile)
1035 const struct got_error *err;
1036 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1037 char *idstr = NULL;
1039 if (id2 == NULL)
1040 return got_error(GOT_ERR_NO_OBJ);
1042 if (id1) {
1043 err = got_object_open_as_commit(&commit1, repo, id1);
1044 if (err)
1045 goto done;
1046 err = got_object_id_str(&idstr, id1);
1047 if (err)
1048 goto done;
1049 err = show_object_id(line_offsets, nlines, "commit", '-',
1050 idstr, outfile);
1051 if (err)
1052 goto done;
1053 free(idstr);
1054 idstr = NULL;
1055 } else {
1056 err = show_object_id(line_offsets, nlines, "commit", '-',
1057 "/dev/null", outfile);
1058 if (err)
1059 goto done;
1062 err = got_object_open_as_commit(&commit2, repo, id2);
1063 if (err)
1064 goto done;
1066 err = got_object_id_str(&idstr, id2);
1067 if (err)
1068 goto done;
1069 err = show_object_id(line_offsets, nlines, "commit", '+',
1070 idstr, outfile);
1071 if (err)
1072 goto done;
1074 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1075 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1076 got_object_commit_get_tree_id(commit2), paths, "", "",
1077 diff_context, ignore_whitespace, force_text_diff, repo, outfile);
1078 done:
1079 if (commit1)
1080 got_object_commit_close(commit1);
1081 if (commit2)
1082 got_object_commit_close(commit2);
1083 free(idstr);
1084 return err;
1087 const struct got_error *
1088 got_diff_files(struct got_diffreg_result **resultp,
1089 FILE *f1, const char *label1, FILE *f2, const char *label2,
1090 int diff_context, int ignore_whitespace, int force_text_diff,
1091 FILE *outfile)
1093 const struct got_error *err = NULL;
1094 struct got_diffreg_result *diffreg_result = NULL;
1096 if (resultp)
1097 *resultp = NULL;
1099 if (outfile) {
1100 fprintf(outfile, "file - %s\n",
1101 f1 == NULL ? "/dev/null" : label1);
1102 fprintf(outfile, "file + %s\n",
1103 f2 == NULL ? "/dev/null" : label2);
1106 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1107 ignore_whitespace, force_text_diff);
1108 if (err)
1109 goto done;
1111 if (outfile) {
1112 err = got_diffreg_output(NULL, NULL, diffreg_result,
1113 f1 != NULL, f2 != NULL, label1, label2,
1114 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1115 if (err)
1116 goto done;
1119 done:
1120 if (resultp && err == NULL)
1121 *resultp = diffreg_result;
1122 else if (diffreg_result) {
1123 const struct got_error *free_err;
1124 free_err = got_diffreg_result_free(diffreg_result);
1125 if (free_err && err == NULL)
1126 err = free_err;
1129 return err;