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/stat.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <sha1.h>
24 #include <zlib.h>
26 #include "got_compat.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, int force_text_diff, 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 off_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];
75 else if (line_offsets) {
76 err = add_line_offset(line_offsets, nlines, 0);
77 if (err)
78 goto done;
79 }
81 if (resultp)
82 *resultp = NULL;
84 if (blob1) {
85 f1 = got_opentemp();
86 if (f1 == NULL)
87 return got_error_from_errno("got_opentemp");
88 }
90 if (blob2) {
91 f2 = got_opentemp();
92 if (f2 == NULL) {
93 err = got_error_from_errno("got_opentemp");
94 if (f1)
95 fclose(f1);
96 return err;
97 }
98 }
100 size1 = 0;
101 if (blob1) {
102 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
103 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
104 blob1);
105 if (err)
106 goto done;
107 } else
108 idstr1 = "/dev/null";
110 size2 = 0;
111 if (blob2) {
112 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
113 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
114 blob2);
115 if (err)
116 goto done;
117 } else
118 idstr2 = "/dev/null";
120 if (outfile) {
121 char *modestr1 = NULL, *modestr2 = NULL;
122 int modebits;
123 if (mode1 && mode1 != mode2) {
124 if (S_ISLNK(mode1))
125 modebits = S_IFLNK;
126 else
127 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
128 if (asprintf(&modestr1, " (mode %o)",
129 mode1 & modebits) == -1) {
130 err = got_error_from_errno("asprintf");
131 goto done;
134 if (mode2 && mode1 != mode2) {
135 if (S_ISLNK(mode2))
136 modebits = S_IFLNK;
137 else
138 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
139 if (asprintf(&modestr2, " (mode %o)",
140 mode2 & modebits) == -1) {
141 err = got_error_from_errno("asprintf");
142 goto done;
145 n = fprintf(outfile, "blob - %s%s\n", idstr1,
146 modestr1 ? modestr1 : "");
147 if (n < 0)
148 goto done;
149 outoff += n;
150 if (line_offsets) {
151 err = add_line_offset(line_offsets, nlines, outoff);
152 if (err)
153 goto done;
156 n = fprintf(outfile, "blob + %s%s\n", idstr2,
157 modestr2 ? modestr2 : "");
158 if (n < 0)
159 goto done;
160 outoff += n;
161 if (line_offsets) {
162 err = add_line_offset(line_offsets, nlines, outoff);
163 if (err)
164 goto done;
167 free(modestr1);
168 free(modestr2);
170 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
171 ignore_whitespace, force_text_diff);
172 if (err)
173 goto done;
175 if (outfile) {
176 err = got_diffreg_output(line_offsets, nlines, result,
177 blob1 != NULL, blob2 != NULL,
178 label1 ? label1 : idstr1,
179 label2 ? label2 : idstr2,
180 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
181 if (err)
182 goto done;
185 if (resultp && err == NULL)
186 *resultp = result;
187 else {
188 free_err = got_diffreg_result_free(result);
189 if (free_err && err == NULL)
190 err = free_err;
192 done:
193 if (f1 && fclose(f1) == EOF && err == NULL)
194 err = got_error_from_errno("fclose");
195 if (f2 && fclose(f2) == EOF && err == NULL)
196 err = got_error_from_errno("fclose");
197 return err;
200 const struct got_error *
201 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
202 struct got_blob_object *blob2, struct got_object_id *id1,
203 struct got_object_id *id2, const char *label1, const char *label2,
204 mode_t mode1, mode_t mode2, struct got_repository *repo)
206 struct got_diff_blob_output_unidiff_arg *a = arg;
208 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
209 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
210 a->ignore_whitespace, a->force_text_diff, a->outfile);
213 const struct got_error *
214 got_diff_blob(off_t **line_offsets, size_t *nlines,
215 struct got_blob_object *blob1, struct got_blob_object *blob2,
216 const char *label1, const char *label2, int diff_context,
217 int ignore_whitespace, int force_text_diff, FILE *outfile)
219 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
220 label1, label2, 0, 0, diff_context, ignore_whitespace,
221 force_text_diff, outfile);
224 static const struct got_error *
225 diff_blob_file(struct got_diffreg_result **resultp,
226 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
227 const char *label2, int diff_context, int ignore_whitespace,
228 int force_text_diff, FILE *outfile)
230 const struct got_error *err = NULL, *free_err;
231 FILE *f1 = NULL;
232 char hex1[SHA1_DIGEST_STRING_LENGTH];
233 char *idstr1 = NULL;
234 off_t size1;
235 struct got_diffreg_result *result = NULL;
237 if (resultp)
238 *resultp = NULL;
240 size1 = 0;
241 if (blob1) {
242 f1 = got_opentemp();
243 if (f1 == NULL)
244 return got_error_from_errno("got_opentemp");
245 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
246 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
247 blob1);
248 if (err)
249 goto done;
250 } else {
251 idstr1 = "/dev/null";
254 if (outfile) {
255 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
256 fprintf(outfile, "file + %s\n",
257 f2 == NULL ? "/dev/null" : label2);
260 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
261 ignore_whitespace, force_text_diff);
262 if (err)
263 goto done;
265 if (outfile) {
266 err = got_diffreg_output(NULL, NULL, result,
267 blob1 != NULL, f2 != NULL,
268 label2, /* show local file's path, not a blob ID */
269 label2, GOT_DIFF_OUTPUT_UNIDIFF,
270 diff_context, outfile);
271 if (err)
272 goto done;
275 if (resultp && err == NULL)
276 *resultp = result;
277 else if (result) {
278 free_err = got_diffreg_result_free(result);
279 if (free_err && err == NULL)
280 err = free_err;
282 done:
283 if (f1 && fclose(f1) == EOF && err == NULL)
284 err = got_error_from_errno("fclose");
285 return err;
288 const struct got_error *
289 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
290 FILE *f2, size_t size2, const char *label2, int diff_context,
291 int ignore_whitespace, int force_text_diff, FILE *outfile)
293 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
294 diff_context, ignore_whitespace, force_text_diff, outfile);
297 static const struct got_error *
298 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
299 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
301 const struct got_error *err;
302 struct got_blob_object *blob = NULL;
303 struct got_object *obj = NULL;
305 err = got_object_open(&obj, repo, id);
306 if (err)
307 return err;
309 err = got_object_blob_open(&blob, repo, obj, 8192);
310 if (err)
311 goto done;
312 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
313 done:
314 got_object_close(obj);
315 if (blob)
316 got_object_blob_close(blob);
317 return err;
320 static const struct got_error *
321 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
322 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
323 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
325 const struct got_error *err;
326 struct got_object *obj1 = NULL;
327 struct got_object *obj2 = NULL;
328 struct got_blob_object *blob1 = NULL;
329 struct got_blob_object *blob2 = NULL;
331 err = got_object_open(&obj1, repo, id1);
332 if (err)
333 return err;
334 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
335 err = got_error(GOT_ERR_OBJ_TYPE);
336 goto done;
339 err = got_object_open(&obj2, repo, id2);
340 if (err)
341 goto done;
342 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
343 err = got_error(GOT_ERR_BAD_OBJ_DATA);
344 goto done;
347 err = got_object_blob_open(&blob1, repo, obj1, 8192);
348 if (err)
349 goto done;
351 err = got_object_blob_open(&blob2, repo, obj2, 8192);
352 if (err)
353 goto done;
355 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
356 repo);
357 done:
358 if (obj1)
359 got_object_close(obj1);
360 if (obj2)
361 got_object_close(obj2);
362 if (blob1)
363 got_object_blob_close(blob1);
364 if (blob2)
365 got_object_blob_close(blob2);
366 return err;
369 static const struct got_error *
370 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
371 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
373 const struct got_error *err;
374 struct got_blob_object *blob = NULL;
375 struct got_object *obj = NULL;
377 err = got_object_open(&obj, repo, id);
378 if (err)
379 return err;
381 err = got_object_blob_open(&blob, repo, obj, 8192);
382 if (err)
383 goto done;
384 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
385 done:
386 got_object_close(obj);
387 if (blob)
388 got_object_blob_close(blob);
389 return err;
392 static const struct got_error *
393 diff_added_tree(struct got_object_id *id, const char *label,
394 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
395 int diff_content)
397 const struct got_error *err = NULL;
398 struct got_object *treeobj = NULL;
399 struct got_tree_object *tree = NULL;
401 err = got_object_open(&treeobj, repo, id);
402 if (err)
403 goto done;
405 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
406 err = got_error(GOT_ERR_OBJ_TYPE);
407 goto done;
410 err = got_object_tree_open(&tree, repo, treeobj);
411 if (err)
412 goto done;
414 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
415 diff_content);
416 done:
417 if (tree)
418 got_object_tree_close(tree);
419 if (treeobj)
420 got_object_close(treeobj);
421 return err;
424 static const struct got_error *
425 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
426 const char *label1, const char *label2, struct got_repository *repo,
427 got_diff_blob_cb cb, void *cb_arg, int diff_content)
429 const struct got_error *err;
430 struct got_object *treeobj1 = NULL;
431 struct got_object *treeobj2 = NULL;
432 struct got_tree_object *tree1 = NULL;
433 struct got_tree_object *tree2 = NULL;
435 err = got_object_open(&treeobj1, repo, id1);
436 if (err)
437 goto done;
439 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
440 err = got_error(GOT_ERR_OBJ_TYPE);
441 goto done;
444 err = got_object_open(&treeobj2, repo, id2);
445 if (err)
446 goto done;
448 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
449 err = got_error(GOT_ERR_OBJ_TYPE);
450 goto done;
453 err = got_object_tree_open(&tree1, repo, treeobj1);
454 if (err)
455 goto done;
457 err = got_object_tree_open(&tree2, repo, treeobj2);
458 if (err)
459 goto done;
461 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
462 diff_content);
464 done:
465 if (tree1)
466 got_object_tree_close(tree1);
467 if (tree2)
468 got_object_tree_close(tree2);
469 if (treeobj1)
470 got_object_close(treeobj1);
471 if (treeobj2)
472 got_object_close(treeobj2);
473 return err;
476 static const struct got_error *
477 diff_deleted_tree(struct got_object_id *id, const char *label,
478 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
479 int diff_content)
481 const struct got_error *err;
482 struct got_object *treeobj = NULL;
483 struct got_tree_object *tree = NULL;
485 err = got_object_open(&treeobj, repo, id);
486 if (err)
487 goto done;
489 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
490 err = got_error(GOT_ERR_OBJ_TYPE);
491 goto done;
494 err = got_object_tree_open(&tree, repo, treeobj);
495 if (err)
496 goto done;
498 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
499 diff_content);
500 done:
501 if (tree)
502 got_object_tree_close(tree);
503 if (treeobj)
504 got_object_close(treeobj);
505 return err;
508 static const struct got_error *
509 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
510 const char *label1, const char *label2, struct got_repository *repo,
511 got_diff_blob_cb cb, void *cb_arg)
513 /* XXX TODO */
514 return NULL;
517 static const struct got_error *
518 diff_entry_old_new(struct got_tree_entry *te1,
519 struct got_tree_entry *te2, const char *label1, const char *label2,
520 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
521 int diff_content)
523 const struct got_error *err = NULL;
524 int id_match;
526 if (got_object_tree_entry_is_submodule(te1))
527 return NULL;
529 if (te2 == NULL) {
530 if (S_ISDIR(te1->mode))
531 err = diff_deleted_tree(&te1->id, label1, repo,
532 cb, cb_arg, diff_content);
533 else {
534 if (diff_content)
535 err = diff_deleted_blob(&te1->id, label1,
536 te1->mode, repo, cb, cb_arg);
537 else
538 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
539 label1, NULL, te1->mode, 0, repo);
541 return err;
542 } else if (got_object_tree_entry_is_submodule(te2))
543 return NULL;
545 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
546 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
547 if (!id_match)
548 return diff_modified_tree(&te1->id, &te2->id,
549 label1, label2, repo, cb, cb_arg, diff_content);
550 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
551 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
552 if (!id_match ||
553 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
554 (te2->mode & (S_IFLNK | S_IXUSR))) {
555 if (diff_content)
556 return diff_modified_blob(&te1->id, &te2->id,
557 label1, label2, te1->mode, te2->mode,
558 repo, cb, cb_arg);
559 else
560 return cb(cb_arg, NULL, NULL, &te1->id,
561 &te2->id, label1, label2, te1->mode,
562 te2->mode, repo);
566 if (id_match)
567 return NULL;
569 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
570 cb, cb_arg);
573 static const struct got_error *
574 diff_entry_new_old(struct got_tree_entry *te2,
575 struct got_tree_entry *te1, const char *label2,
576 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
577 int diff_content)
579 if (te1 != NULL) /* handled by diff_entry_old_new() */
580 return NULL;
582 if (got_object_tree_entry_is_submodule(te2))
583 return NULL;
585 if (S_ISDIR(te2->mode))
586 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
587 diff_content);
589 if (diff_content)
590 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
591 cb_arg);
593 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
594 te2->mode, repo);
597 const struct got_error *
598 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
599 struct got_blob_object *blob2, struct got_object_id *id1,
600 struct got_object_id *id2, const char *label1, const char *label2,
601 mode_t mode1, mode_t mode2, struct got_repository *repo)
603 const struct got_error *err = NULL;
604 struct got_pathlist_head *paths = arg;
605 struct got_diff_changed_path *change = NULL;
606 char *path = NULL;
608 path = strdup(label2 ? label2 : label1);
609 if (path == NULL)
610 return got_error_from_errno("malloc");
612 change = malloc(sizeof(*change));
613 if (change == NULL) {
614 err = got_error_from_errno("malloc");
615 goto done;
618 change->status = GOT_STATUS_NO_CHANGE;
619 if (id1 == NULL)
620 change->status = GOT_STATUS_ADD;
621 else if (id2 == NULL)
622 change->status = GOT_STATUS_DELETE;
623 else {
624 if (got_object_id_cmp(id1, id2) != 0)
625 change->status = GOT_STATUS_MODIFY;
626 else if (mode1 != mode2)
627 change->status = GOT_STATUS_MODE_CHANGE;
630 err = got_pathlist_insert(NULL, paths, path, change);
631 done:
632 if (err) {
633 free(path);
634 free(change);
636 return err;
639 const struct got_error *
640 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
641 const char *label1, const char *label2, struct got_repository *repo,
642 got_diff_blob_cb cb, void *cb_arg, int diff_content)
644 const struct got_error *err = NULL;
645 struct got_tree_entry *te1 = NULL;
646 struct got_tree_entry *te2 = NULL;
647 char *l1 = NULL, *l2 = NULL;
648 int tidx1 = 0, tidx2 = 0;
650 if (tree1) {
651 te1 = got_object_tree_get_entry(tree1, 0);
652 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
653 te1->name) == -1)
654 return got_error_from_errno("asprintf");
656 if (tree2) {
657 te2 = got_object_tree_get_entry(tree2, 0);
658 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
659 te2->name) == -1)
660 return got_error_from_errno("asprintf");
663 do {
664 if (te1) {
665 struct got_tree_entry *te = NULL;
666 if (tree2)
667 te = got_object_tree_find_entry(tree2,
668 te1->name);
669 if (te) {
670 free(l2);
671 l2 = NULL;
672 if (te && asprintf(&l2, "%s%s%s", label2,
673 label2[0] ? "/" : "", te->name) == -1)
674 return
675 got_error_from_errno("asprintf");
677 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
678 cb_arg, diff_content);
679 if (err)
680 break;
683 if (te2) {
684 struct got_tree_entry *te = NULL;
685 if (tree1)
686 te = got_object_tree_find_entry(tree1,
687 te2->name);
688 free(l2);
689 if (te) {
690 if (asprintf(&l2, "%s%s%s", label2,
691 label2[0] ? "/" : "", te->name) == -1)
692 return
693 got_error_from_errno("asprintf");
694 } else {
695 if (asprintf(&l2, "%s%s%s", label2,
696 label2[0] ? "/" : "", te2->name) == -1)
697 return
698 got_error_from_errno("asprintf");
700 err = diff_entry_new_old(te2, te, l2, repo,
701 cb, cb_arg, diff_content);
702 if (err)
703 break;
706 free(l1);
707 l1 = NULL;
708 if (te1) {
709 tidx1++;
710 te1 = got_object_tree_get_entry(tree1, tidx1);
711 if (te1 &&
712 asprintf(&l1, "%s%s%s", label1,
713 label1[0] ? "/" : "", te1->name) == -1)
714 return got_error_from_errno("asprintf");
716 free(l2);
717 l2 = NULL;
718 if (te2) {
719 tidx2++;
720 te2 = got_object_tree_get_entry(tree2, tidx2);
721 if (te2 &&
722 asprintf(&l2, "%s%s%s", label2,
723 label2[0] ? "/" : "", te2->name) == -1)
724 return got_error_from_errno("asprintf");
726 } while (te1 || te2);
728 return err;
731 const struct got_error *
732 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
733 struct got_object_id *id1, struct got_object_id *id2,
734 const char *label1, const char *label2, int diff_context,
735 int ignore_whitespace, int force_text_diff,
736 struct got_repository *repo, FILE *outfile)
738 const struct got_error *err;
739 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
741 if (id1 == NULL && id2 == NULL)
742 return got_error(GOT_ERR_NO_OBJ);
744 if (id1) {
745 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
746 if (err)
747 goto done;
749 if (id2) {
750 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
751 if (err)
752 goto done;
754 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
755 label1, label2, diff_context, ignore_whitespace, force_text_diff,
756 outfile);
757 done:
758 if (blob1)
759 got_object_blob_close(blob1);
760 if (blob2)
761 got_object_blob_close(blob2);
762 return err;
765 const struct got_error *
766 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
767 struct got_object_id *id1, struct got_object_id *id2,
768 char *label1, char *label2, int diff_context, int ignore_whitespace,
769 int force_text_diff, struct got_repository *repo, FILE *outfile)
771 const struct got_error *err;
772 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
773 struct got_diff_blob_output_unidiff_arg arg;
774 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
776 if (id1 == NULL && id2 == NULL)
777 return got_error(GOT_ERR_NO_OBJ);
779 if (id1) {
780 err = got_object_open_as_tree(&tree1, repo, id1);
781 if (err)
782 goto done;
784 if (id2) {
785 err = got_object_open_as_tree(&tree2, repo, id2);
786 if (err)
787 goto done;
789 arg.diff_context = diff_context;
790 arg.ignore_whitespace = ignore_whitespace;
791 arg.force_text_diff = force_text_diff;
792 arg.outfile = outfile;
793 if (want_lineoffsets) {
794 arg.line_offsets = *line_offsets;
795 arg.nlines = *nlines;
796 } else {
797 arg.line_offsets = NULL;
798 arg.nlines = 0;
800 err = got_diff_tree(tree1, tree2, label1, label2, repo,
801 got_diff_blob_output_unidiff, &arg, 1);
803 if (want_lineoffsets) {
804 *line_offsets = arg.line_offsets; /* was likely re-allocated */
805 *nlines = arg.nlines;
807 done:
808 if (tree1)
809 got_object_tree_close(tree1);
810 if (tree2)
811 got_object_tree_close(tree2);
812 return err;
815 const struct got_error *
816 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
817 struct got_object_id *id1, struct got_object_id *id2,
818 int diff_context, int ignore_whitespace, int force_text_diff,
819 struct got_repository *repo, FILE *outfile)
821 const struct got_error *err;
822 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
824 if (id2 == NULL)
825 return got_error(GOT_ERR_NO_OBJ);
827 if (id1) {
828 err = got_object_open_as_commit(&commit1, repo, id1);
829 if (err)
830 goto done;
833 err = got_object_open_as_commit(&commit2, repo, id2);
834 if (err)
835 goto done;
837 err = got_diff_objects_as_trees(line_offsets, nlines,
838 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
839 got_object_commit_get_tree_id(commit2), "", "", diff_context,
840 ignore_whitespace, force_text_diff, repo, outfile);
841 done:
842 if (commit1)
843 got_object_commit_close(commit1);
844 if (commit2)
845 got_object_commit_close(commit2);
846 return err;
849 const struct got_error *
850 got_diff_files(struct got_diffreg_result **resultp,
851 FILE *f1, const char *label1, FILE *f2, const char *label2,
852 int diff_context, int ignore_whitespace, int force_text_diff,
853 FILE *outfile)
855 const struct got_error *err = NULL;
856 struct got_diffreg_result *diffreg_result = NULL;
858 if (resultp)
859 *resultp = NULL;
861 if (outfile) {
862 fprintf(outfile, "file - %s\n",
863 f1 == NULL ? "/dev/null" : label1);
864 fprintf(outfile, "file + %s\n",
865 f2 == NULL ? "/dev/null" : label2);
868 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
869 ignore_whitespace, force_text_diff);
870 if (err)
871 goto done;
873 if (outfile) {
874 err = got_diffreg_output(NULL, NULL, diffreg_result,
875 f1 != NULL, f2 != NULL, label1, label2,
876 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
877 if (err)
878 goto done;
881 done:
882 if (resultp && err == NULL)
883 *resultp = diffreg_result;
884 else if (diffreg_result) {
885 const struct got_error *free_err;
886 free_err = got_diffreg_result_free(diffreg_result);
887 if (free_err && err == NULL)
888 err = free_err;
891 return err;