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 <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <zlib.h>
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
31 #include "got_diff.h"
32 #include "got_opentemp.h"
33 #include "got_path.h"
34 #include "got_cancel.h"
35 #include "got_worktree.h"
37 #include "got_lib_diff.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
42 static const struct got_error *
43 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
44 {
45 off_t *p;
47 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
48 if (p == NULL)
49 return got_error_from_errno("reallocarray");
50 *line_offsets = p;
51 (*line_offsets)[*nlines] = off;
52 (*nlines)++;
53 return NULL;
54 }
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,
60 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61 int diff_context, int ignore_whitespace, FILE *outfile)
62 {
63 const struct got_error *err = NULL, *free_err;
64 FILE *f1 = NULL, *f2 = NULL;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
67 char *idstr1 = NULL, *idstr2 = NULL;
68 size_t size1, size2;
69 struct got_diffreg_result *result;
70 off_t outoff = 0;
71 int n;
73 if (line_offsets && *line_offsets && *nlines > 0)
74 outoff = (*line_offsets)[*nlines - 1];
76 if (resultp)
77 *resultp = NULL;
79 if (blob1) {
80 f1 = got_opentemp();
81 if (f1 == NULL)
82 return got_error_from_errno("got_opentemp");
83 }
85 if (blob2) {
86 f2 = got_opentemp();
87 if (f2 == NULL) {
88 err = got_error_from_errno("got_opentemp");
89 fclose(f1);
90 return err;
91 }
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, GOT_DIFF_ALGORITHM_PATIENCE,
165 ignore_whitespace);
166 if (err)
167 goto done;
169 if (outfile) {
170 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
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 if (f1 && fclose(f1) != 0 && err == NULL)
187 err = got_error_from_errno("fclose");
188 if (f2 && fclose(f2) != 0 && err == NULL)
189 err = got_error_from_errno("fclose");
190 return err;
193 const struct got_error *
194 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
195 struct got_blob_object *blob2, struct got_object_id *id1,
196 struct got_object_id *id2, const char *label1, const char *label2,
197 mode_t mode1, mode_t mode2, struct got_repository *repo)
199 struct got_diff_blob_output_unidiff_arg *a = arg;
201 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
202 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
203 a->ignore_whitespace, a->outfile);
206 const struct got_error *
207 got_diff_blob(off_t **line_offsets, size_t *nlines,
208 struct got_blob_object *blob1, struct got_blob_object *blob2,
209 const char *label1, const char *label2, int diff_context,
210 int ignore_whitespace, FILE *outfile)
212 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
213 label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
216 static const struct got_error *
217 diff_blob_file(struct got_diffreg_result **resultp,
218 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
219 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
221 const struct got_error *err = NULL, *free_err;
222 FILE *f1 = NULL;
223 char hex1[SHA1_DIGEST_STRING_LENGTH];
224 char *idstr1 = NULL;
225 size_t size1;
226 struct got_diffreg_result *result = NULL;
228 if (resultp)
229 *resultp = NULL;
231 size1 = 0;
232 if (blob1) {
233 f1 = got_opentemp();
234 if (f1 == NULL)
235 return got_error_from_errno("got_opentemp");
236 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
237 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
238 blob1);
239 if (err)
240 goto done;
241 } else {
242 idstr1 = "/dev/null";
245 if (outfile) {
246 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
247 fprintf(outfile, "file + %s\n",
248 f2 == NULL ? "/dev/null" : label2);
251 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
252 ignore_whitespace);
253 if (err)
254 goto done;
256 if (outfile) {
257 err = got_diffreg_output(NULL, NULL, result, f1, f2,
258 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
259 outfile);
260 if (err)
261 goto done;
264 if (resultp && err == NULL)
265 *resultp = result;
266 else if (result) {
267 free_err = got_diffreg_result_free(result);
268 if (free_err && err == NULL)
269 err = free_err;
271 done:
272 if (f1 && fclose(f1) != 0 && err == NULL)
273 err = got_error_from_errno("fclose");
274 return err;
277 const struct got_error *
278 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
279 FILE *f2, size_t size2, const char *label2, int diff_context,
280 int ignore_whitespace, FILE *outfile)
282 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
283 diff_context, ignore_whitespace, outfile);
286 const struct got_error *
287 got_diff_blob_prepared_file(struct got_diffreg_result **resultp,
288 struct diff_data *data1, struct got_blob_object *blob1,
289 struct diff_data *data2, FILE *f2, char *p2, size_t size2,
290 const struct diff_config *cfg, int ignore_whitespace)
292 const struct got_error *err = NULL, *free_err;
293 FILE *f1 = NULL;
294 char hex1[SHA1_DIGEST_STRING_LENGTH];
295 char *idstr1 = NULL, *p1 = NULL;
296 size_t size1, size;
297 struct got_diffreg_result *result = NULL;
298 int f1_created = 0;
300 *resultp = NULL;
302 size1 = 0;
303 if (blob1) {
304 f1 = got_opentemp();
305 if (f1 == NULL)
306 return got_error_from_errno("got_opentemp");
307 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
308 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
309 blob1);
310 if (err)
311 goto done;
312 } else {
313 idstr1 = "/dev/null";
316 err = got_diff_prepare_file(&f1, &p1, &f1_created, &size,
317 data1, cfg, ignore_whitespace);
318 if (err)
319 goto done;
321 err = got_diffreg_prepared_files(&result, cfg, data1, f1,
322 p1, size1, data2, f2, p2, size2);
323 if (err)
324 goto done;
326 *resultp = result;
327 done:
328 if (err) {
329 if (result)
330 free_err = got_diffreg_result_free_left(result);
331 else
332 free_err = got_diffreg_close(f1, p1, size1, NULL,
333 NULL, 0);
334 if (free_err && err == NULL)
335 err = free_err;
337 return err;
340 static const struct got_error *
341 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
342 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
344 const struct got_error *err;
345 struct got_blob_object *blob = NULL;
346 struct got_object *obj = NULL;
348 err = got_object_open(&obj, repo, id);
349 if (err)
350 return err;
352 err = got_object_blob_open(&blob, repo, obj, 8192);
353 if (err)
354 goto done;
355 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
356 done:
357 got_object_close(obj);
358 if (blob)
359 got_object_blob_close(blob);
360 return err;
363 static const struct got_error *
364 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
365 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
366 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
368 const struct got_error *err;
369 struct got_object *obj1 = NULL;
370 struct got_object *obj2 = NULL;
371 struct got_blob_object *blob1 = NULL;
372 struct got_blob_object *blob2 = NULL;
374 err = got_object_open(&obj1, repo, id1);
375 if (err)
376 return err;
377 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
378 err = got_error(GOT_ERR_OBJ_TYPE);
379 goto done;
382 err = got_object_open(&obj2, repo, id2);
383 if (err)
384 goto done;
385 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
386 err = got_error(GOT_ERR_BAD_OBJ_DATA);
387 goto done;
390 err = got_object_blob_open(&blob1, repo, obj1, 8192);
391 if (err)
392 goto done;
394 err = got_object_blob_open(&blob2, repo, obj2, 8192);
395 if (err)
396 goto done;
398 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
399 repo);
400 done:
401 if (obj1)
402 got_object_close(obj1);
403 if (obj2)
404 got_object_close(obj2);
405 if (blob1)
406 got_object_blob_close(blob1);
407 if (blob2)
408 got_object_blob_close(blob2);
409 return err;
412 static const struct got_error *
413 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
414 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
416 const struct got_error *err;
417 struct got_blob_object *blob = NULL;
418 struct got_object *obj = NULL;
420 err = got_object_open(&obj, repo, id);
421 if (err)
422 return err;
424 err = got_object_blob_open(&blob, repo, obj, 8192);
425 if (err)
426 goto done;
427 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
428 done:
429 got_object_close(obj);
430 if (blob)
431 got_object_blob_close(blob);
432 return err;
435 static const struct got_error *
436 diff_added_tree(struct got_object_id *id, const char *label,
437 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
438 int diff_content)
440 const struct got_error *err = NULL;
441 struct got_object *treeobj = NULL;
442 struct got_tree_object *tree = NULL;
444 err = got_object_open(&treeobj, repo, id);
445 if (err)
446 goto done;
448 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
449 err = got_error(GOT_ERR_OBJ_TYPE);
450 goto done;
453 err = got_object_tree_open(&tree, repo, treeobj);
454 if (err)
455 goto done;
457 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
458 diff_content);
459 done:
460 if (tree)
461 got_object_tree_close(tree);
462 if (treeobj)
463 got_object_close(treeobj);
464 return err;
467 static const struct got_error *
468 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
469 const char *label1, const char *label2, 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 *treeobj1 = NULL;
474 struct got_object *treeobj2 = NULL;
475 struct got_tree_object *tree1 = NULL;
476 struct got_tree_object *tree2 = NULL;
478 err = got_object_open(&treeobj1, repo, id1);
479 if (err)
480 goto done;
482 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
483 err = got_error(GOT_ERR_OBJ_TYPE);
484 goto done;
487 err = got_object_open(&treeobj2, repo, id2);
488 if (err)
489 goto done;
491 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
492 err = got_error(GOT_ERR_OBJ_TYPE);
493 goto done;
496 err = got_object_tree_open(&tree1, repo, treeobj1);
497 if (err)
498 goto done;
500 err = got_object_tree_open(&tree2, repo, treeobj2);
501 if (err)
502 goto done;
504 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
505 diff_content);
507 done:
508 if (tree1)
509 got_object_tree_close(tree1);
510 if (tree2)
511 got_object_tree_close(tree2);
512 if (treeobj1)
513 got_object_close(treeobj1);
514 if (treeobj2)
515 got_object_close(treeobj2);
516 return err;
519 static const struct got_error *
520 diff_deleted_tree(struct got_object_id *id, const char *label,
521 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
522 int diff_content)
524 const struct got_error *err;
525 struct got_object *treeobj = NULL;
526 struct got_tree_object *tree = NULL;
528 err = got_object_open(&treeobj, repo, id);
529 if (err)
530 goto done;
532 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
533 err = got_error(GOT_ERR_OBJ_TYPE);
534 goto done;
537 err = got_object_tree_open(&tree, repo, treeobj);
538 if (err)
539 goto done;
541 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
542 diff_content);
543 done:
544 if (tree)
545 got_object_tree_close(tree);
546 if (treeobj)
547 got_object_close(treeobj);
548 return err;
551 static const struct got_error *
552 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
553 const char *label1, const char *label2, struct got_repository *repo,
554 got_diff_blob_cb cb, void *cb_arg)
556 /* XXX TODO */
557 return NULL;
560 static const struct got_error *
561 diff_entry_old_new(struct got_tree_entry *te1,
562 struct got_tree_entry *te2, const char *label1, const char *label2,
563 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
564 int diff_content)
566 const struct got_error *err = NULL;
567 int id_match;
569 if (got_object_tree_entry_is_submodule(te1))
570 return NULL;
572 if (te2 == NULL) {
573 if (S_ISDIR(te1->mode))
574 err = diff_deleted_tree(&te1->id, label1, repo,
575 cb, cb_arg, diff_content);
576 else {
577 if (diff_content)
578 err = diff_deleted_blob(&te1->id, label1,
579 te1->mode, repo, cb, cb_arg);
580 else
581 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
582 label1, NULL, te1->mode, 0, repo);
584 return err;
585 } else if (got_object_tree_entry_is_submodule(te2))
586 return NULL;
588 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
589 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
590 if (!id_match)
591 return diff_modified_tree(&te1->id, &te2->id,
592 label1, label2, repo, cb, cb_arg, diff_content);
593 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
594 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
595 if (!id_match ||
596 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
597 (te2->mode & (S_IFLNK | S_IXUSR))) {
598 if (diff_content)
599 return diff_modified_blob(&te1->id, &te2->id,
600 label1, label2, te1->mode, te2->mode,
601 repo, cb, cb_arg);
602 else
603 return cb(cb_arg, NULL, NULL, &te1->id,
604 &te2->id, label1, label2, te1->mode,
605 te2->mode, repo);
609 if (id_match)
610 return NULL;
612 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
613 cb, cb_arg);
616 static const struct got_error *
617 diff_entry_new_old(struct got_tree_entry *te2,
618 struct got_tree_entry *te1, const char *label2,
619 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
620 int diff_content)
622 if (te1 != NULL) /* handled by diff_entry_old_new() */
623 return NULL;
625 if (got_object_tree_entry_is_submodule(te2))
626 return NULL;
628 if (S_ISDIR(te2->mode))
629 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
630 diff_content);
632 if (diff_content)
633 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
634 cb_arg);
636 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
637 te2->mode, repo);
640 const struct got_error *
641 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
642 struct got_blob_object *blob2, struct got_object_id *id1,
643 struct got_object_id *id2, const char *label1, const char *label2,
644 mode_t mode1, mode_t mode2, struct got_repository *repo)
646 const struct got_error *err = NULL;
647 struct got_pathlist_head *paths = arg;
648 struct got_diff_changed_path *change = NULL;
649 char *path = NULL;
651 path = strdup(label2 ? label2 : label1);
652 if (path == NULL)
653 return got_error_from_errno("malloc");
655 change = malloc(sizeof(*change));
656 if (change == NULL) {
657 err = got_error_from_errno("malloc");
658 goto done;
661 change->status = GOT_STATUS_NO_CHANGE;
662 if (id1 == NULL)
663 change->status = GOT_STATUS_ADD;
664 else if (id2 == NULL)
665 change->status = GOT_STATUS_DELETE;
666 else {
667 if (got_object_id_cmp(id1, id2) != 0)
668 change->status = GOT_STATUS_MODIFY;
669 else if (mode1 != mode2)
670 change->status = GOT_STATUS_MODE_CHANGE;
673 err = got_pathlist_insert(NULL, paths, path, change);
674 done:
675 if (err) {
676 free(path);
677 free(change);
679 return err;
682 const struct got_error *
683 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
684 const char *label1, const char *label2, struct got_repository *repo,
685 got_diff_blob_cb cb, void *cb_arg, int diff_content)
687 const struct got_error *err = NULL;
688 struct got_tree_entry *te1 = NULL;
689 struct got_tree_entry *te2 = NULL;
690 char *l1 = NULL, *l2 = NULL;
691 int tidx1 = 0, tidx2 = 0;
693 if (tree1) {
694 te1 = got_object_tree_get_entry(tree1, 0);
695 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
696 te1->name) == -1)
697 return got_error_from_errno("asprintf");
699 if (tree2) {
700 te2 = got_object_tree_get_entry(tree2, 0);
701 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
702 te2->name) == -1)
703 return got_error_from_errno("asprintf");
706 do {
707 if (te1) {
708 struct got_tree_entry *te = NULL;
709 if (tree2)
710 te = got_object_tree_find_entry(tree2,
711 te1->name);
712 if (te) {
713 free(l2);
714 l2 = NULL;
715 if (te && asprintf(&l2, "%s%s%s", label2,
716 label2[0] ? "/" : "", te->name) == -1)
717 return
718 got_error_from_errno("asprintf");
720 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
721 cb_arg, diff_content);
722 if (err)
723 break;
726 if (te2) {
727 struct got_tree_entry *te = NULL;
728 if (tree1)
729 te = got_object_tree_find_entry(tree1,
730 te2->name);
731 free(l2);
732 if (te) {
733 if (asprintf(&l2, "%s%s%s", label2,
734 label2[0] ? "/" : "", te->name) == -1)
735 return
736 got_error_from_errno("asprintf");
737 } else {
738 if (asprintf(&l2, "%s%s%s", label2,
739 label2[0] ? "/" : "", te2->name) == -1)
740 return
741 got_error_from_errno("asprintf");
743 err = diff_entry_new_old(te2, te, l2, repo,
744 cb, cb_arg, diff_content);
745 if (err)
746 break;
749 free(l1);
750 l1 = NULL;
751 if (te1) {
752 tidx1++;
753 te1 = got_object_tree_get_entry(tree1, tidx1);
754 if (te1 &&
755 asprintf(&l1, "%s%s%s", label1,
756 label1[0] ? "/" : "", te1->name) == -1)
757 return got_error_from_errno("asprintf");
759 free(l2);
760 l2 = NULL;
761 if (te2) {
762 tidx2++;
763 te2 = got_object_tree_get_entry(tree2, tidx2);
764 if (te2 &&
765 asprintf(&l2, "%s%s%s", label2,
766 label2[0] ? "/" : "", te2->name) == -1)
767 return got_error_from_errno("asprintf");
769 } while (te1 || te2);
771 return err;
774 const struct got_error *
775 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
776 struct got_object_id *id1, struct got_object_id *id2,
777 const char *label1, const char *label2, int diff_context,
778 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
780 const struct got_error *err;
781 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
783 if (id1 == NULL && id2 == NULL)
784 return got_error(GOT_ERR_NO_OBJ);
786 if (id1) {
787 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
788 if (err)
789 goto done;
791 if (id2) {
792 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
793 if (err)
794 goto done;
796 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
797 label1, label2, diff_context, ignore_whitespace, outfile);
798 done:
799 if (blob1)
800 got_object_blob_close(blob1);
801 if (blob2)
802 got_object_blob_close(blob2);
803 return err;
806 const struct got_error *
807 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
808 struct got_object_id *id1, struct got_object_id *id2,
809 char *label1, char *label2, int diff_context, int ignore_whitespace,
810 struct got_repository *repo, FILE *outfile)
812 const struct got_error *err;
813 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
814 struct got_diff_blob_output_unidiff_arg arg;
815 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
817 if (id1 == NULL && id2 == NULL)
818 return got_error(GOT_ERR_NO_OBJ);
820 if (id1) {
821 err = got_object_open_as_tree(&tree1, repo, id1);
822 if (err)
823 goto done;
825 if (id2) {
826 err = got_object_open_as_tree(&tree2, repo, id2);
827 if (err)
828 goto done;
830 arg.diff_context = diff_context;
831 arg.ignore_whitespace = ignore_whitespace;
832 arg.outfile = outfile;
833 if (want_lineoffsets) {
834 arg.line_offsets = *line_offsets;
835 arg.nlines = *nlines;
836 } else {
837 arg.line_offsets = NULL;
838 arg.nlines = 0;
840 err = got_diff_tree(tree1, tree2, label1, label2, repo,
841 got_diff_blob_output_unidiff, &arg, 1);
843 if (want_lineoffsets) {
844 *line_offsets = arg.line_offsets; /* was likely re-allocated */
845 *nlines = arg.nlines;
847 done:
848 if (tree1)
849 got_object_tree_close(tree1);
850 if (tree2)
851 got_object_tree_close(tree2);
852 return err;
855 const struct got_error *
856 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
857 struct got_object_id *id1, struct got_object_id *id2,
858 int diff_context, int ignore_whitespace,
859 struct got_repository *repo, FILE *outfile)
861 const struct got_error *err;
862 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
864 if (id2 == NULL)
865 return got_error(GOT_ERR_NO_OBJ);
867 if (id1) {
868 err = got_object_open_as_commit(&commit1, repo, id1);
869 if (err)
870 goto done;
873 err = got_object_open_as_commit(&commit2, repo, id2);
874 if (err)
875 goto done;
877 err = got_diff_objects_as_trees(line_offsets, nlines,
878 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
879 got_object_commit_get_tree_id(commit2), "", "", diff_context,
880 ignore_whitespace, repo, outfile);
881 done:
882 if (commit1)
883 got_object_commit_close(commit1);
884 if (commit2)
885 got_object_commit_close(commit2);
886 return err;
889 const struct got_error *
890 got_diff_files(struct got_diffreg_result **resultp,
891 FILE *f1, const char *label1, FILE *f2, const char *label2,
892 int diff_context, int ignore_whitespace, FILE *outfile)
894 const struct got_error *err = NULL;
895 struct got_diffreg_result *diffreg_result = NULL;
897 if (resultp)
898 *resultp = NULL;
900 if (outfile) {
901 fprintf(outfile, "file - %s\n",
902 f1 == NULL ? "/dev/null" : label1);
903 fprintf(outfile, "file + %s\n",
904 f2 == NULL ? "/dev/null" : label2);
907 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
908 ignore_whitespace);
909 if (err)
910 goto done;
912 if (outfile) {
913 err = got_diffreg_output(NULL, NULL, diffreg_result,
914 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
915 diff_context, outfile);
916 if (err)
917 goto done;
920 done:
921 if (resultp && err == NULL)
922 *resultp = diffreg_result;
923 else if (diffreg_result) {
924 const struct got_error *free_err;
925 free_err = got_diffreg_result_free(diffreg_result);
926 if (free_err && err == NULL)
927 err = free_err;
930 return err;