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_opentemp.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_worktree.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,
59 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
60 int diff_context, int ignore_whitespace, FILE *outfile)
61 {
62 const struct got_error *err = NULL, *free_err;
63 FILE *f1 = NULL, *f2 = NULL;
64 char hex1[SHA1_DIGEST_STRING_LENGTH];
65 char hex2[SHA1_DIGEST_STRING_LENGTH];
66 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];
75 if (resultp)
76 *resultp = NULL;
78 if (blob1) {
79 f1 = got_opentemp();
80 if (f1 == NULL)
81 return got_error_from_errno("got_opentemp");
82 }
84 if (blob2) {
85 f2 = got_opentemp();
86 if (f2 == NULL) {
87 err = got_error_from_errno("got_opentemp");
88 fclose(f1);
89 return err;
90 }
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);
165 if (err)
166 goto done;
168 if (outfile) {
169 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
170 label1 ? label1 : idstr1,
171 label2 ? label2 : idstr2,
172 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
173 if (err)
174 goto done;
177 if (resultp && err == NULL)
178 *resultp = result;
179 else {
180 free_err = got_diffreg_result_free(result);
181 if (free_err && err == NULL)
182 err = free_err;
184 done:
185 if (f1 && fclose(f1) != 0 && err == NULL)
186 err = got_error_from_errno("fclose");
187 if (f2 && fclose(f2) != 0 && err == NULL)
188 err = got_error_from_errno("fclose");
189 return err;
192 const struct got_error *
193 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
194 struct got_blob_object *blob2, struct got_object_id *id1,
195 struct got_object_id *id2, const char *label1, const char *label2,
196 mode_t mode1, mode_t mode2, struct got_repository *repo)
198 struct got_diff_blob_output_unidiff_arg *a = arg;
200 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
201 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
202 a->ignore_whitespace, a->outfile);
205 const struct got_error *
206 got_diff_blob(off_t **line_offsets, size_t *nlines,
207 struct got_blob_object *blob1, struct got_blob_object *blob2,
208 const char *label1, const char *label2, int diff_context,
209 int ignore_whitespace, FILE *outfile)
211 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
212 label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
215 static const struct got_error *
216 diff_blob_file(struct got_diffreg_result **resultp,
217 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
218 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
220 const struct got_error *err = NULL, *free_err;
221 FILE *f1 = NULL;
222 char hex1[SHA1_DIGEST_STRING_LENGTH];
223 char *idstr1 = NULL;
224 off_t size1;
225 struct got_diffreg_result *result = NULL;
227 if (resultp)
228 *resultp = NULL;
230 size1 = 0;
231 if (blob1) {
232 f1 = got_opentemp();
233 if (f1 == NULL)
234 return got_error_from_errno("got_opentemp");
235 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
236 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
237 blob1);
238 if (err)
239 goto done;
240 } else {
241 idstr1 = "/dev/null";
244 if (outfile) {
245 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
246 fprintf(outfile, "file + %s\n",
247 f2 == NULL ? "/dev/null" : label2);
250 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
251 ignore_whitespace);
252 if (err)
253 goto done;
255 if (outfile) {
256 err = got_diffreg_output(NULL, NULL, result, f1, f2,
257 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
258 outfile);
259 if (err)
260 goto done;
263 if (resultp && err == NULL)
264 *resultp = result;
265 else if (result) {
266 free_err = got_diffreg_result_free(result);
267 if (free_err && err == NULL)
268 err = free_err;
270 done:
271 if (f1 && fclose(f1) != 0 && err == NULL)
272 err = got_error_from_errno("fclose");
273 return err;
276 const struct got_error *
277 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
278 FILE *f2, size_t size2, const char *label2, int diff_context,
279 int ignore_whitespace, FILE *outfile)
281 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
282 diff_context, ignore_whitespace, outfile);
285 static const struct got_error *
286 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
287 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
289 const struct got_error *err;
290 struct got_blob_object *blob = NULL;
291 struct got_object *obj = NULL;
293 err = got_object_open(&obj, repo, id);
294 if (err)
295 return err;
297 err = got_object_blob_open(&blob, repo, obj, 8192);
298 if (err)
299 goto done;
300 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
301 done:
302 got_object_close(obj);
303 if (blob)
304 got_object_blob_close(blob);
305 return err;
308 static const struct got_error *
309 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
310 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
311 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
313 const struct got_error *err;
314 struct got_object *obj1 = NULL;
315 struct got_object *obj2 = NULL;
316 struct got_blob_object *blob1 = NULL;
317 struct got_blob_object *blob2 = NULL;
319 err = got_object_open(&obj1, repo, id1);
320 if (err)
321 return err;
322 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
323 err = got_error(GOT_ERR_OBJ_TYPE);
324 goto done;
327 err = got_object_open(&obj2, repo, id2);
328 if (err)
329 goto done;
330 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
331 err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 goto done;
335 err = got_object_blob_open(&blob1, repo, obj1, 8192);
336 if (err)
337 goto done;
339 err = got_object_blob_open(&blob2, repo, obj2, 8192);
340 if (err)
341 goto done;
343 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
344 repo);
345 done:
346 if (obj1)
347 got_object_close(obj1);
348 if (obj2)
349 got_object_close(obj2);
350 if (blob1)
351 got_object_blob_close(blob1);
352 if (blob2)
353 got_object_blob_close(blob2);
354 return err;
357 static const struct got_error *
358 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
359 struct got_repository *repo, 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);
370 if (err)
371 goto done;
372 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
373 done:
374 got_object_close(obj);
375 if (blob)
376 got_object_blob_close(blob);
377 return err;
380 static const struct got_error *
381 diff_added_tree(struct got_object_id *id, const char *label,
382 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
383 int diff_content)
385 const struct got_error *err = NULL;
386 struct got_object *treeobj = NULL;
387 struct got_tree_object *tree = NULL;
389 err = got_object_open(&treeobj, repo, id);
390 if (err)
391 goto done;
393 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
394 err = got_error(GOT_ERR_OBJ_TYPE);
395 goto done;
398 err = got_object_tree_open(&tree, repo, treeobj);
399 if (err)
400 goto done;
402 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
403 diff_content);
404 done:
405 if (tree)
406 got_object_tree_close(tree);
407 if (treeobj)
408 got_object_close(treeobj);
409 return err;
412 static const struct got_error *
413 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
414 const char *label1, const char *label2, struct got_repository *repo,
415 got_diff_blob_cb cb, void *cb_arg, int diff_content)
417 const struct got_error *err;
418 struct got_object *treeobj1 = NULL;
419 struct got_object *treeobj2 = NULL;
420 struct got_tree_object *tree1 = NULL;
421 struct got_tree_object *tree2 = NULL;
423 err = got_object_open(&treeobj1, repo, id1);
424 if (err)
425 goto done;
427 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
428 err = got_error(GOT_ERR_OBJ_TYPE);
429 goto done;
432 err = got_object_open(&treeobj2, repo, id2);
433 if (err)
434 goto done;
436 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
437 err = got_error(GOT_ERR_OBJ_TYPE);
438 goto done;
441 err = got_object_tree_open(&tree1, repo, treeobj1);
442 if (err)
443 goto done;
445 err = got_object_tree_open(&tree2, repo, treeobj2);
446 if (err)
447 goto done;
449 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
450 diff_content);
452 done:
453 if (tree1)
454 got_object_tree_close(tree1);
455 if (tree2)
456 got_object_tree_close(tree2);
457 if (treeobj1)
458 got_object_close(treeobj1);
459 if (treeobj2)
460 got_object_close(treeobj2);
461 return err;
464 static const struct got_error *
465 diff_deleted_tree(struct got_object_id *id, const char *label,
466 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
467 int diff_content)
469 const struct got_error *err;
470 struct got_object *treeobj = NULL;
471 struct got_tree_object *tree = NULL;
473 err = got_object_open(&treeobj, repo, id);
474 if (err)
475 goto done;
477 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
478 err = got_error(GOT_ERR_OBJ_TYPE);
479 goto done;
482 err = got_object_tree_open(&tree, repo, treeobj);
483 if (err)
484 goto done;
486 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
487 diff_content);
488 done:
489 if (tree)
490 got_object_tree_close(tree);
491 if (treeobj)
492 got_object_close(treeobj);
493 return err;
496 static const struct got_error *
497 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
498 const char *label1, const char *label2, struct got_repository *repo,
499 got_diff_blob_cb cb, void *cb_arg)
501 /* XXX TODO */
502 return NULL;
505 static const struct got_error *
506 diff_entry_old_new(struct got_tree_entry *te1,
507 struct got_tree_entry *te2, 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, label1, repo,
520 cb, cb_arg, diff_content);
521 else {
522 if (diff_content)
523 err = diff_deleted_blob(&te1->id, label1,
524 te1->mode, repo, cb, cb_arg);
525 else
526 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
527 label1, NULL, te1->mode, 0, repo);
529 return err;
530 } else if (got_object_tree_entry_is_submodule(te2))
531 return NULL;
533 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
534 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
535 if (!id_match)
536 return diff_modified_tree(&te1->id, &te2->id,
537 label1, label2, repo, cb, cb_arg, diff_content);
538 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
539 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
540 if (!id_match ||
541 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
542 (te2->mode & (S_IFLNK | S_IXUSR))) {
543 if (diff_content)
544 return diff_modified_blob(&te1->id, &te2->id,
545 label1, label2, te1->mode, te2->mode,
546 repo, cb, cb_arg);
547 else
548 return cb(cb_arg, NULL, NULL, &te1->id,
549 &te2->id, label1, label2, te1->mode,
550 te2->mode, repo);
554 if (id_match)
555 return NULL;
557 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
558 cb, cb_arg);
561 static const struct got_error *
562 diff_entry_new_old(struct got_tree_entry *te2,
563 struct got_tree_entry *te1, const char *label2,
564 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
565 int diff_content)
567 if (te1 != NULL) /* handled by diff_entry_old_new() */
568 return NULL;
570 if (got_object_tree_entry_is_submodule(te2))
571 return NULL;
573 if (S_ISDIR(te2->mode))
574 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
575 diff_content);
577 if (diff_content)
578 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
579 cb_arg);
581 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
582 te2->mode, repo);
585 const struct got_error *
586 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
587 struct got_blob_object *blob2, struct got_object_id *id1,
588 struct got_object_id *id2, const char *label1, const char *label2,
589 mode_t mode1, mode_t mode2, struct got_repository *repo)
591 const struct got_error *err = NULL;
592 struct got_pathlist_head *paths = arg;
593 struct got_diff_changed_path *change = NULL;
594 char *path = NULL;
596 path = strdup(label2 ? label2 : label1);
597 if (path == NULL)
598 return got_error_from_errno("malloc");
600 change = malloc(sizeof(*change));
601 if (change == NULL) {
602 err = got_error_from_errno("malloc");
603 goto done;
606 change->status = GOT_STATUS_NO_CHANGE;
607 if (id1 == NULL)
608 change->status = GOT_STATUS_ADD;
609 else if (id2 == NULL)
610 change->status = GOT_STATUS_DELETE;
611 else {
612 if (got_object_id_cmp(id1, id2) != 0)
613 change->status = GOT_STATUS_MODIFY;
614 else if (mode1 != mode2)
615 change->status = GOT_STATUS_MODE_CHANGE;
618 err = got_pathlist_insert(NULL, paths, path, change);
619 done:
620 if (err) {
621 free(path);
622 free(change);
624 return err;
627 const struct got_error *
628 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
629 const char *label1, const char *label2, struct got_repository *repo,
630 got_diff_blob_cb cb, void *cb_arg, int diff_content)
632 const struct got_error *err = NULL;
633 struct got_tree_entry *te1 = NULL;
634 struct got_tree_entry *te2 = NULL;
635 char *l1 = NULL, *l2 = NULL;
636 int tidx1 = 0, tidx2 = 0;
638 if (tree1) {
639 te1 = got_object_tree_get_entry(tree1, 0);
640 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
641 te1->name) == -1)
642 return got_error_from_errno("asprintf");
644 if (tree2) {
645 te2 = got_object_tree_get_entry(tree2, 0);
646 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
647 te2->name) == -1)
648 return got_error_from_errno("asprintf");
651 do {
652 if (te1) {
653 struct got_tree_entry *te = NULL;
654 if (tree2)
655 te = got_object_tree_find_entry(tree2,
656 te1->name);
657 if (te) {
658 free(l2);
659 l2 = NULL;
660 if (te && asprintf(&l2, "%s%s%s", label2,
661 label2[0] ? "/" : "", te->name) == -1)
662 return
663 got_error_from_errno("asprintf");
665 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
666 cb_arg, diff_content);
667 if (err)
668 break;
671 if (te2) {
672 struct got_tree_entry *te = NULL;
673 if (tree1)
674 te = got_object_tree_find_entry(tree1,
675 te2->name);
676 free(l2);
677 if (te) {
678 if (asprintf(&l2, "%s%s%s", label2,
679 label2[0] ? "/" : "", te->name) == -1)
680 return
681 got_error_from_errno("asprintf");
682 } else {
683 if (asprintf(&l2, "%s%s%s", label2,
684 label2[0] ? "/" : "", te2->name) == -1)
685 return
686 got_error_from_errno("asprintf");
688 err = diff_entry_new_old(te2, te, l2, repo,
689 cb, cb_arg, diff_content);
690 if (err)
691 break;
694 free(l1);
695 l1 = NULL;
696 if (te1) {
697 tidx1++;
698 te1 = got_object_tree_get_entry(tree1, tidx1);
699 if (te1 &&
700 asprintf(&l1, "%s%s%s", label1,
701 label1[0] ? "/" : "", te1->name) == -1)
702 return got_error_from_errno("asprintf");
704 free(l2);
705 l2 = NULL;
706 if (te2) {
707 tidx2++;
708 te2 = got_object_tree_get_entry(tree2, tidx2);
709 if (te2 &&
710 asprintf(&l2, "%s%s%s", label2,
711 label2[0] ? "/" : "", te2->name) == -1)
712 return got_error_from_errno("asprintf");
714 } while (te1 || te2);
716 return err;
719 const struct got_error *
720 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
721 struct got_object_id *id1, struct got_object_id *id2,
722 const char *label1, const char *label2, int diff_context,
723 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
725 const struct got_error *err;
726 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
728 if (id1 == NULL && id2 == NULL)
729 return got_error(GOT_ERR_NO_OBJ);
731 if (id1) {
732 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
733 if (err)
734 goto done;
736 if (id2) {
737 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
738 if (err)
739 goto done;
741 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
742 label1, label2, diff_context, ignore_whitespace, 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 const struct got_error *
752 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
753 struct got_object_id *id1, struct got_object_id *id2,
754 char *label1, char *label2, int diff_context, int ignore_whitespace,
755 struct got_repository *repo, FILE *outfile)
757 const struct got_error *err;
758 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
759 struct got_diff_blob_output_unidiff_arg arg;
760 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
762 if (id1 == NULL && id2 == NULL)
763 return got_error(GOT_ERR_NO_OBJ);
765 if (id1) {
766 err = got_object_open_as_tree(&tree1, repo, id1);
767 if (err)
768 goto done;
770 if (id2) {
771 err = got_object_open_as_tree(&tree2, repo, id2);
772 if (err)
773 goto done;
775 arg.diff_context = diff_context;
776 arg.ignore_whitespace = ignore_whitespace;
777 arg.outfile = outfile;
778 if (want_lineoffsets) {
779 arg.line_offsets = *line_offsets;
780 arg.nlines = *nlines;
781 } else {
782 arg.line_offsets = NULL;
783 arg.nlines = 0;
785 err = got_diff_tree(tree1, tree2, label1, label2, repo,
786 got_diff_blob_output_unidiff, &arg, 1);
788 if (want_lineoffsets) {
789 *line_offsets = arg.line_offsets; /* was likely re-allocated */
790 *nlines = arg.nlines;
792 done:
793 if (tree1)
794 got_object_tree_close(tree1);
795 if (tree2)
796 got_object_tree_close(tree2);
797 return err;
800 const struct got_error *
801 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
802 struct got_object_id *id1, struct got_object_id *id2,
803 int diff_context, int ignore_whitespace,
804 struct got_repository *repo, FILE *outfile)
806 const struct got_error *err;
807 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
809 if (id2 == NULL)
810 return got_error(GOT_ERR_NO_OBJ);
812 if (id1) {
813 err = got_object_open_as_commit(&commit1, repo, id1);
814 if (err)
815 goto done;
818 err = got_object_open_as_commit(&commit2, repo, id2);
819 if (err)
820 goto done;
822 err = got_diff_objects_as_trees(line_offsets, nlines,
823 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
824 got_object_commit_get_tree_id(commit2), "", "", diff_context,
825 ignore_whitespace, repo, outfile);
826 done:
827 if (commit1)
828 got_object_commit_close(commit1);
829 if (commit2)
830 got_object_commit_close(commit2);
831 return err;
834 const struct got_error *
835 got_diff_files(struct got_diffreg_result **resultp,
836 FILE *f1, const char *label1, FILE *f2, const char *label2,
837 int diff_context, int ignore_whitespace, FILE *outfile)
839 const struct got_error *err = NULL;
840 struct got_diffreg_result *diffreg_result = NULL;
842 if (resultp)
843 *resultp = NULL;
845 if (outfile) {
846 fprintf(outfile, "file - %s\n",
847 f1 == NULL ? "/dev/null" : label1);
848 fprintf(outfile, "file + %s\n",
849 f2 == NULL ? "/dev/null" : label2);
852 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
853 ignore_whitespace);
854 if (err)
855 goto done;
857 if (outfile) {
858 err = got_diffreg_output(NULL, NULL, diffreg_result,
859 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
860 diff_context, outfile);
861 if (err)
862 goto done;
865 done:
866 if (resultp && err == NULL)
867 *resultp = diffreg_result;
868 else if (diffreg_result) {
869 const struct got_error *free_err;
870 free_err = got_diffreg_result_free(diffreg_result);
871 if (free_err && err == NULL)
872 err = free_err;
875 return err;