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, const char *label,
277 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);
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, const char *label1, const char *label2,
303 mode_t mode1, mode_t mode2, struct got_repository *repo,
304 got_diff_blob_cb cb, void *cb_arg)
306 const struct got_error *err;
307 struct got_object *obj1 = NULL;
308 struct got_object *obj2 = NULL;
309 struct got_blob_object *blob1 = NULL;
310 struct got_blob_object *blob2 = NULL;
312 err = got_object_open(&obj1, repo, id1);
313 if (err)
314 return err;
315 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
316 err = got_error(GOT_ERR_OBJ_TYPE);
317 goto done;
320 err = got_object_open(&obj2, repo, id2);
321 if (err)
322 goto done;
323 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
324 err = got_error(GOT_ERR_BAD_OBJ_DATA);
325 goto done;
328 err = got_object_blob_open(&blob1, repo, obj1, 8192);
329 if (err)
330 goto done;
332 err = got_object_blob_open(&blob2, repo, obj2, 8192);
333 if (err)
334 goto done;
336 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
337 mode1, mode2, repo);
338 done:
339 if (obj1)
340 got_object_close(obj1);
341 if (obj2)
342 got_object_close(obj2);
343 if (blob1)
344 got_object_blob_close(blob1);
345 if (blob2)
346 got_object_blob_close(blob2);
347 return err;
350 static const struct got_error *
351 diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
352 mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
354 const struct got_error *err;
355 struct got_blob_object *blob = NULL;
356 struct got_object *obj = NULL;
358 err = got_object_open(&obj, repo, id);
359 if (err)
360 return err;
362 err = got_object_blob_open(&blob, repo, obj, 8192);
363 if (err)
364 goto done;
365 err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
366 mode, 0, repo);
367 done:
368 got_object_close(obj);
369 if (blob)
370 got_object_blob_close(blob);
371 return err;
374 static const struct got_error *
375 diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
376 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
377 int diff_content)
379 const struct got_error *err = NULL;
380 struct got_object *treeobj = NULL;
381 struct got_tree_object *tree = NULL;
383 err = got_object_open(&treeobj, repo, id);
384 if (err)
385 goto done;
387 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
388 err = got_error(GOT_ERR_OBJ_TYPE);
389 goto done;
392 err = got_object_tree_open(&tree, repo, treeobj);
393 if (err)
394 goto done;
396 err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
397 repo, cb, cb_arg, diff_content);
398 done:
399 if (tree)
400 got_object_tree_close(tree);
401 if (treeobj)
402 got_object_close(treeobj);
403 return err;
406 static const struct got_error *
407 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
408 FILE *f1, FILE *f2, const char *label1, const char *label2,
409 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
410 int diff_content)
412 const struct got_error *err;
413 struct got_object *treeobj1 = NULL;
414 struct got_object *treeobj2 = NULL;
415 struct got_tree_object *tree1 = NULL;
416 struct got_tree_object *tree2 = NULL;
418 err = got_object_open(&treeobj1, repo, id1);
419 if (err)
420 goto done;
422 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
423 err = got_error(GOT_ERR_OBJ_TYPE);
424 goto done;
427 err = got_object_open(&treeobj2, repo, id2);
428 if (err)
429 goto done;
431 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
432 err = got_error(GOT_ERR_OBJ_TYPE);
433 goto done;
436 err = got_object_tree_open(&tree1, repo, treeobj1);
437 if (err)
438 goto done;
440 err = got_object_tree_open(&tree2, repo, treeobj2);
441 if (err)
442 goto done;
444 err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
445 repo, cb, cb_arg, diff_content);
447 done:
448 if (tree1)
449 got_object_tree_close(tree1);
450 if (tree2)
451 got_object_tree_close(tree2);
452 if (treeobj1)
453 got_object_close(treeobj1);
454 if (treeobj2)
455 got_object_close(treeobj2);
456 return err;
459 static const struct got_error *
460 diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
461 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
462 int diff_content)
464 const struct got_error *err;
465 struct got_object *treeobj = NULL;
466 struct got_tree_object *tree = NULL;
468 err = got_object_open(&treeobj, repo, id);
469 if (err)
470 goto done;
472 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
473 err = got_error(GOT_ERR_OBJ_TYPE);
474 goto done;
477 err = got_object_tree_open(&tree, repo, treeobj);
478 if (err)
479 goto done;
481 err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
482 repo, cb, cb_arg, diff_content);
483 done:
484 if (tree)
485 got_object_tree_close(tree);
486 if (treeobj)
487 got_object_close(treeobj);
488 return err;
491 static const struct got_error *
492 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
493 const char *label1, const char *label2, struct got_repository *repo,
494 got_diff_blob_cb cb, void *cb_arg)
496 /* XXX TODO */
497 return NULL;
500 static const struct got_error *
501 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
502 FILE *f1, FILE *f2, const char *label1, const char *label2,
503 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
504 int diff_content)
506 const struct got_error *err = NULL;
507 int id_match;
509 if (got_object_tree_entry_is_submodule(te1))
510 return NULL;
512 if (te2 == NULL) {
513 if (S_ISDIR(te1->mode))
514 err = diff_deleted_tree(&te1->id, f1, label1, repo,
515 cb, cb_arg, diff_content);
516 else {
517 if (diff_content)
518 err = diff_deleted_blob(&te1->id, f1, label1,
519 te1->mode, repo, cb, cb_arg);
520 else
521 err = cb(cb_arg, NULL, NULL, NULL, NULL,
522 &te1->id, NULL, label1, NULL,
523 te1->mode, 0, repo);
525 return err;
526 } else if (got_object_tree_entry_is_submodule(te2))
527 return NULL;
529 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
530 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
531 if (!id_match)
532 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
533 label1, label2, repo, cb, cb_arg, diff_content);
534 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
535 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
536 if (!id_match ||
537 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
538 (te2->mode & (S_IFLNK | S_IXUSR))) {
539 if (diff_content)
540 return diff_modified_blob(&te1->id, &te2->id,
541 f1, f2, label1, label2, te1->mode,
542 te2->mode, repo, cb, cb_arg);
543 else
544 return cb(cb_arg, NULL, NULL, NULL, NULL,
545 &te1->id, &te2->id, label1, label2,
546 te1->mode, te2->mode, repo);
550 if (id_match)
551 return NULL;
553 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
554 cb, cb_arg);
557 static const struct got_error *
558 diff_entry_new_old(struct got_tree_entry *te2,
559 struct got_tree_entry *te1, FILE *f2, const char *label2,
560 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
561 int diff_content)
563 if (te1 != NULL) /* handled by diff_entry_old_new() */
564 return NULL;
566 if (got_object_tree_entry_is_submodule(te2))
567 return NULL;
569 if (S_ISDIR(te2->mode))
570 return diff_added_tree(&te2->id, f2, label2,
571 repo, cb, cb_arg, diff_content);
573 if (diff_content)
574 return diff_added_blob(&te2->id, f2, label2, te2->mode,
575 repo, cb, cb_arg);
577 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
578 NULL, label2, 0, te2->mode, repo);
581 const struct got_error *
582 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
583 struct got_blob_object *blob2, FILE *f1, FILE *f2,
584 struct got_object_id *id1, struct got_object_id *id2,
585 const char *label1, const char *label2,
586 mode_t mode1, mode_t mode2, struct got_repository *repo)
588 const struct got_error *err = NULL;
589 struct got_pathlist_head *paths = arg;
590 struct got_diff_changed_path *change = NULL;
591 char *path = NULL;
593 path = strdup(label2 ? label2 : label1);
594 if (path == NULL)
595 return got_error_from_errno("malloc");
597 change = malloc(sizeof(*change));
598 if (change == NULL) {
599 err = got_error_from_errno("malloc");
600 goto done;
603 change->status = GOT_STATUS_NO_CHANGE;
604 if (id1 == NULL)
605 change->status = GOT_STATUS_ADD;
606 else if (id2 == NULL)
607 change->status = GOT_STATUS_DELETE;
608 else {
609 if (got_object_id_cmp(id1, id2) != 0)
610 change->status = GOT_STATUS_MODIFY;
611 else if (mode1 != mode2)
612 change->status = GOT_STATUS_MODE_CHANGE;
615 err = got_pathlist_append(paths, path, change);
616 done:
617 if (err) {
618 free(path);
619 free(change);
621 return err;
624 const struct got_error *
625 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
626 FILE *f1, FILE *f2, const char *label1, const char *label2,
627 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
628 int diff_content)
630 const struct got_error *err = NULL;
631 struct got_tree_entry *te1 = NULL;
632 struct got_tree_entry *te2 = NULL;
633 char *l1 = NULL, *l2 = NULL;
634 int tidx1 = 0, tidx2 = 0;
636 if (tree1) {
637 te1 = got_object_tree_get_entry(tree1, 0);
638 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
639 te1->name) == -1)
640 return got_error_from_errno("asprintf");
642 if (tree2) {
643 te2 = got_object_tree_get_entry(tree2, 0);
644 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
645 te2->name) == -1)
646 return got_error_from_errno("asprintf");
649 do {
650 if (te1) {
651 struct got_tree_entry *te = NULL;
652 if (tree2)
653 te = got_object_tree_find_entry(tree2,
654 te1->name);
655 if (te) {
656 free(l2);
657 l2 = NULL;
658 if (te && asprintf(&l2, "%s%s%s", label2,
659 label2[0] ? "/" : "", te->name) == -1)
660 return
661 got_error_from_errno("asprintf");
663 err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
664 repo, cb, cb_arg, diff_content);
665 if (err)
666 break;
669 if (te2) {
670 struct got_tree_entry *te = NULL;
671 if (tree1)
672 te = got_object_tree_find_entry(tree1,
673 te2->name);
674 free(l2);
675 if (te) {
676 if (asprintf(&l2, "%s%s%s", label2,
677 label2[0] ? "/" : "", te->name) == -1)
678 return
679 got_error_from_errno("asprintf");
680 } else {
681 if (asprintf(&l2, "%s%s%s", label2,
682 label2[0] ? "/" : "", te2->name) == -1)
683 return
684 got_error_from_errno("asprintf");
686 err = diff_entry_new_old(te2, te, f2, l2, repo,
687 cb, cb_arg, diff_content);
688 if (err)
689 break;
692 free(l1);
693 l1 = NULL;
694 if (te1) {
695 tidx1++;
696 te1 = got_object_tree_get_entry(tree1, tidx1);
697 if (te1 &&
698 asprintf(&l1, "%s%s%s", label1,
699 label1[0] ? "/" : "", te1->name) == -1)
700 return got_error_from_errno("asprintf");
702 free(l2);
703 l2 = NULL;
704 if (te2) {
705 tidx2++;
706 te2 = got_object_tree_get_entry(tree2, tidx2);
707 if (te2 &&
708 asprintf(&l2, "%s%s%s", label2,
709 label2[0] ? "/" : "", te2->name) == -1)
710 return got_error_from_errno("asprintf");
712 } while (te1 || te2);
714 return err;
717 const struct got_error *
718 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
719 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
720 const char *label1, const char *label2, int diff_context,
721 int ignore_whitespace, int force_text_diff,
722 struct got_repository *repo, FILE *outfile)
724 const struct got_error *err;
725 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
727 if (id1 == NULL && id2 == NULL)
728 return got_error(GOT_ERR_NO_OBJ);
730 if (id1) {
731 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
732 if (err)
733 goto done;
735 if (id2) {
736 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
737 if (err)
738 goto done;
740 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
741 label1, label2, diff_context, ignore_whitespace, force_text_diff,
742 outfile);
743 done:
744 if (blob1)
745 got_object_blob_close(blob1);
746 if (blob2)
747 got_object_blob_close(blob2);
748 return err;
751 static const struct got_error *
752 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
753 FILE *f1, FILE *f2, struct got_pathlist_head *paths,
754 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
756 const struct got_error *err = NULL;
757 struct got_pathlist_entry *pe;
758 struct got_object_id *id1 = NULL, *id2 = NULL;
759 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
760 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
762 TAILQ_FOREACH(pe, paths, entry) {
763 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
764 mode_t mode1 = 0, mode2 = 0;
766 free(id1);
767 id1 = NULL;
768 free(id2);
769 id2 = NULL;
770 if (subtree1) {
771 got_object_tree_close(subtree1);
772 subtree1 = NULL;
774 if (subtree2) {
775 got_object_tree_close(subtree2);
776 subtree2 = NULL;
778 if (blob1) {
779 got_object_blob_close(blob1);
780 blob1 = NULL;
782 if (blob2) {
783 got_object_blob_close(blob2);
784 blob2 = NULL;
787 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
788 pe->path);
789 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
790 goto done;
791 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
792 pe->path);
793 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
794 goto done;
795 if (id1 == NULL && id2 == NULL) {
796 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
797 goto done;
799 if (id1) {
800 err = got_object_get_type(&type1, repo, id1);
801 if (err)
802 goto done;
804 if (id2) {
805 err = got_object_get_type(&type2, repo, id2);
806 if (err)
807 goto done;
809 if (type1 == GOT_OBJ_TYPE_ANY &&
810 type2 == GOT_OBJ_TYPE_ANY) {
811 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
812 goto done;
813 } else if (type1 != GOT_OBJ_TYPE_ANY &&
814 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
815 err = got_error(GOT_ERR_OBJ_TYPE);
816 goto done;
819 if (type1 == GOT_OBJ_TYPE_BLOB ||
820 type2 == GOT_OBJ_TYPE_BLOB) {
821 if (id1) {
822 err = got_object_open_as_blob(&blob1, repo,
823 id1, 8192);
824 if (err)
825 goto done;
827 if (id2) {
828 err = got_object_open_as_blob(&blob2, repo,
829 id2, 8192);
830 if (err)
831 goto done;
833 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
834 id1 ? pe->path : "/dev/null",
835 id2 ? pe->path : "/dev/null",
836 mode1, mode2, repo);
837 if (err)
838 goto done;
839 } else if (type1 == GOT_OBJ_TYPE_TREE ||
840 type2 == GOT_OBJ_TYPE_TREE) {
841 if (id1) {
842 err = got_object_open_as_tree(&subtree1, repo,
843 id1);
844 if (err)
845 goto done;
847 if (id2) {
848 err = got_object_open_as_tree(&subtree2, repo,
849 id2);
850 if (err)
851 goto done;
853 err = got_diff_tree(subtree1, subtree2, f1, f2,
854 id1 ? pe->path : "/dev/null",
855 id2 ? pe->path : "/dev/null",
856 repo, cb, cb_arg, 1);
857 if (err)
858 goto done;
859 } else {
860 err = got_error(GOT_ERR_OBJ_TYPE);
861 goto done;
864 done:
865 free(id1);
866 free(id2);
867 if (subtree1)
868 got_object_tree_close(subtree1);
869 if (subtree2)
870 got_object_tree_close(subtree2);
871 if (blob1)
872 got_object_blob_close(blob1);
873 if (blob2)
874 got_object_blob_close(blob2);
875 return err;
878 static const struct got_error *
879 show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
880 int ch, const char *id_str, FILE *outfile)
882 const struct got_error *err;
883 int n;
884 off_t outoff = 0;
886 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
887 if (line_offsets != NULL && *line_offsets != NULL) {
888 if (*nlines == 0) {
889 err = add_line_offset(line_offsets, nlines, 0);
890 if (err)
891 return err;
892 } else
893 outoff = (*line_offsets)[*nlines - 1];
895 outoff += n;
896 err = add_line_offset(line_offsets, nlines, outoff);
897 if (err)
898 return err;
901 return NULL;
904 static const struct got_error *
905 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
906 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
907 struct got_pathlist_head *paths, const char *label1, const char *label2,
908 int diff_context, int ignore_whitespace, int force_text_diff,
909 struct got_repository *repo, FILE *outfile)
911 const struct got_error *err;
912 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
913 struct got_diff_blob_output_unidiff_arg arg;
914 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
916 if (id1 == NULL && id2 == NULL)
917 return got_error(GOT_ERR_NO_OBJ);
919 if (id1) {
920 err = got_object_open_as_tree(&tree1, repo, id1);
921 if (err)
922 goto done;
924 if (id2) {
925 err = got_object_open_as_tree(&tree2, repo, id2);
926 if (err)
927 goto done;
930 arg.diff_context = diff_context;
931 arg.ignore_whitespace = ignore_whitespace;
932 arg.force_text_diff = force_text_diff;
933 arg.outfile = outfile;
934 if (want_lineoffsets) {
935 arg.line_offsets = *line_offsets;
936 arg.nlines = *nlines;
937 } else {
938 arg.line_offsets = NULL;
939 arg.nlines = 0;
941 if (paths == NULL || TAILQ_EMPTY(paths)) {
942 err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
943 repo, got_diff_blob_output_unidiff, &arg, 1);
944 } else {
945 err = diff_paths(tree1, tree2, f1, f2, paths, repo,
946 got_diff_blob_output_unidiff, &arg);
948 if (want_lineoffsets) {
949 *line_offsets = arg.line_offsets; /* was likely re-allocated */
950 *nlines = arg.nlines;
952 done:
953 if (tree1)
954 got_object_tree_close(tree1);
955 if (tree2)
956 got_object_tree_close(tree2);
957 return err;
960 const struct got_error *
961 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
962 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
963 struct got_pathlist_head *paths, const char *label1, const char *label2,
964 int diff_context, int ignore_whitespace, int force_text_diff,
965 struct got_repository *repo, FILE *outfile)
967 const struct got_error *err;
968 char *idstr = NULL;
970 if (id1 == NULL && id2 == NULL)
971 return got_error(GOT_ERR_NO_OBJ);
973 if (id1) {
974 err = got_object_id_str(&idstr, id1);
975 if (err)
976 goto done;
977 err = show_object_id(line_offsets, nlines, "tree", '-',
978 idstr, outfile);
979 if (err)
980 goto done;
981 free(idstr);
982 idstr = NULL;
983 } else {
984 err = show_object_id(line_offsets, nlines, "tree", '-',
985 "/dev/null", outfile);
986 if (err)
987 goto done;
990 if (id2) {
991 err = got_object_id_str(&idstr, id2);
992 if (err)
993 goto done;
994 err = show_object_id(line_offsets, nlines, "tree", '+',
995 idstr, outfile);
996 if (err)
997 goto done;
998 free(idstr);
999 idstr = NULL;
1000 } else {
1001 err = show_object_id(line_offsets, nlines, "tree", '+',
1002 "/dev/null", outfile);
1003 if (err)
1004 goto done;
1007 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
1008 paths, label1, label2, diff_context, ignore_whitespace,
1009 force_text_diff, repo, outfile);
1010 done:
1011 free(idstr);
1012 return err;
1015 const struct got_error *
1016 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1017 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1018 struct got_pathlist_head *paths,
1019 int diff_context, int ignore_whitespace, int force_text_diff,
1020 struct got_repository *repo, FILE *outfile)
1022 const struct got_error *err;
1023 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1024 char *idstr = NULL;
1026 if (id2 == NULL)
1027 return got_error(GOT_ERR_NO_OBJ);
1029 if (id1) {
1030 err = got_object_open_as_commit(&commit1, repo, id1);
1031 if (err)
1032 goto done;
1033 err = got_object_id_str(&idstr, id1);
1034 if (err)
1035 goto done;
1036 err = show_object_id(line_offsets, nlines, "commit", '-',
1037 idstr, outfile);
1038 if (err)
1039 goto done;
1040 free(idstr);
1041 idstr = NULL;
1042 } else {
1043 err = show_object_id(line_offsets, nlines, "commit", '-',
1044 "/dev/null", outfile);
1045 if (err)
1046 goto done;
1049 err = got_object_open_as_commit(&commit2, repo, id2);
1050 if (err)
1051 goto done;
1053 err = got_object_id_str(&idstr, id2);
1054 if (err)
1055 goto done;
1056 err = show_object_id(line_offsets, nlines, "commit", '+',
1057 idstr, outfile);
1058 if (err)
1059 goto done;
1061 err = diff_objects_as_trees(line_offsets, nlines, f1, f2,
1062 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1063 got_object_commit_get_tree_id(commit2), paths, "", "",
1064 diff_context, ignore_whitespace, force_text_diff, repo, outfile);
1065 done:
1066 if (commit1)
1067 got_object_commit_close(commit1);
1068 if (commit2)
1069 got_object_commit_close(commit2);
1070 free(idstr);
1071 return err;
1074 const struct got_error *
1075 got_diff_files(struct got_diffreg_result **resultp,
1076 FILE *f1, const char *label1, FILE *f2, const char *label2,
1077 int diff_context, int ignore_whitespace, int force_text_diff,
1078 FILE *outfile)
1080 const struct got_error *err = NULL;
1081 struct got_diffreg_result *diffreg_result = NULL;
1083 if (resultp)
1084 *resultp = NULL;
1086 if (outfile) {
1087 fprintf(outfile, "file - %s\n",
1088 f1 == NULL ? "/dev/null" : label1);
1089 fprintf(outfile, "file + %s\n",
1090 f2 == NULL ? "/dev/null" : label2);
1093 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1094 ignore_whitespace, force_text_diff);
1095 if (err)
1096 goto done;
1098 if (outfile) {
1099 err = got_diffreg_output(NULL, NULL, diffreg_result,
1100 f1 != NULL, f2 != NULL, label1, label2,
1101 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1102 if (err)
1103 goto done;
1106 done:
1107 if (resultp && err == NULL)
1108 *resultp = diffreg_result;
1109 else if (diffreg_result) {
1110 const struct got_error *free_err;
1111 free_err = got_diffreg_result_free(diffreg_result);
1112 if (free_err && err == NULL)
1113 err = free_err;
1116 return err;