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, int force_text_diff, 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 if (f1)
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, force_text_diff);
166 if (err)
167 goto done;
169 if (outfile) {
170 err = got_diffreg_output(line_offsets, nlines, result,
171 blob1 != NULL, blob2 != NULL,
172 label1 ? label1 : idstr1,
173 label2 ? label2 : idstr2,
174 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
175 if (err)
176 goto done;
179 if (resultp && err == NULL)
180 *resultp = result;
181 else {
182 free_err = got_diffreg_result_free(result);
183 if (free_err && err == NULL)
184 err = free_err;
186 done:
187 if (f1 && fclose(f1) != 0 && err == NULL)
188 err = got_error_from_errno("fclose");
189 if (f2 && fclose(f2) != 0 && err == NULL)
190 err = got_error_from_errno("fclose");
191 return err;
194 const struct got_error *
195 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
196 struct got_blob_object *blob2, struct got_object_id *id1,
197 struct got_object_id *id2, const char *label1, const char *label2,
198 mode_t mode1, mode_t mode2, struct got_repository *repo)
200 struct got_diff_blob_output_unidiff_arg *a = arg;
202 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
203 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
204 a->ignore_whitespace, a->force_text_diff, a->outfile);
207 const struct got_error *
208 got_diff_blob(off_t **line_offsets, size_t *nlines,
209 struct got_blob_object *blob1, struct got_blob_object *blob2,
210 const char *label1, const char *label2, int diff_context,
211 int ignore_whitespace, int force_text_diff, FILE *outfile)
213 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
214 label1, label2, 0, 0, diff_context, ignore_whitespace,
215 force_text_diff, outfile);
218 static const struct got_error *
219 diff_blob_file(struct got_diffreg_result **resultp,
220 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
221 const char *label2, int diff_context, int ignore_whitespace,
222 int force_text_diff, FILE *outfile)
224 const struct got_error *err = NULL, *free_err;
225 FILE *f1 = NULL;
226 char hex1[SHA1_DIGEST_STRING_LENGTH];
227 char *idstr1 = NULL;
228 off_t size1;
229 struct got_diffreg_result *result = NULL;
231 if (resultp)
232 *resultp = NULL;
234 size1 = 0;
235 if (blob1) {
236 f1 = got_opentemp();
237 if (f1 == NULL)
238 return got_error_from_errno("got_opentemp");
239 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
240 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
241 blob1);
242 if (err)
243 goto done;
244 } else {
245 idstr1 = "/dev/null";
248 if (outfile) {
249 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
250 fprintf(outfile, "file + %s\n",
251 f2 == NULL ? "/dev/null" : label2);
254 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
255 ignore_whitespace, force_text_diff);
256 if (err)
257 goto done;
259 if (outfile) {
260 err = got_diffreg_output(NULL, NULL, result,
261 blob1 != NULL, f2 != NULL,
262 label2, /* show local file's path, not a blob ID */
263 label2, GOT_DIFF_OUTPUT_UNIDIFF,
264 diff_context, outfile);
265 if (err)
266 goto done;
269 if (resultp && err == NULL)
270 *resultp = result;
271 else if (result) {
272 free_err = got_diffreg_result_free(result);
273 if (free_err && err == NULL)
274 err = free_err;
276 done:
277 if (f1 && fclose(f1) != 0 && err == NULL)
278 err = got_error_from_errno("fclose");
279 return err;
282 const struct got_error *
283 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
284 FILE *f2, size_t size2, const char *label2, int diff_context,
285 int ignore_whitespace, int force_text_diff, FILE *outfile)
287 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
288 diff_context, ignore_whitespace, force_text_diff, outfile);
291 static const struct got_error *
292 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
293 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
295 const struct got_error *err;
296 struct got_blob_object *blob = NULL;
297 struct got_object *obj = NULL;
299 err = got_object_open(&obj, repo, id);
300 if (err)
301 return err;
303 err = got_object_blob_open(&blob, repo, obj, 8192);
304 if (err)
305 goto done;
306 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
307 done:
308 got_object_close(obj);
309 if (blob)
310 got_object_blob_close(blob);
311 return err;
314 static const struct got_error *
315 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
316 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
319 const struct got_error *err;
320 struct got_object *obj1 = NULL;
321 struct got_object *obj2 = NULL;
322 struct got_blob_object *blob1 = NULL;
323 struct got_blob_object *blob2 = NULL;
325 err = got_object_open(&obj1, repo, id1);
326 if (err)
327 return err;
328 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
329 err = got_error(GOT_ERR_OBJ_TYPE);
330 goto done;
333 err = got_object_open(&obj2, repo, id2);
334 if (err)
335 goto done;
336 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
337 err = got_error(GOT_ERR_BAD_OBJ_DATA);
338 goto done;
341 err = got_object_blob_open(&blob1, repo, obj1, 8192);
342 if (err)
343 goto done;
345 err = got_object_blob_open(&blob2, repo, obj2, 8192);
346 if (err)
347 goto done;
349 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
350 repo);
351 done:
352 if (obj1)
353 got_object_close(obj1);
354 if (obj2)
355 got_object_close(obj2);
356 if (blob1)
357 got_object_blob_close(blob1);
358 if (blob2)
359 got_object_blob_close(blob2);
360 return err;
363 static const struct got_error *
364 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
365 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
367 const struct got_error *err;
368 struct got_blob_object *blob = NULL;
369 struct got_object *obj = NULL;
371 err = got_object_open(&obj, repo, id);
372 if (err)
373 return err;
375 err = got_object_blob_open(&blob, repo, obj, 8192);
376 if (err)
377 goto done;
378 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
379 done:
380 got_object_close(obj);
381 if (blob)
382 got_object_blob_close(blob);
383 return err;
386 static const struct got_error *
387 diff_added_tree(struct got_object_id *id, const char *label,
388 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
389 int diff_content)
391 const struct got_error *err = NULL;
392 struct got_object *treeobj = NULL;
393 struct got_tree_object *tree = NULL;
395 err = got_object_open(&treeobj, repo, id);
396 if (err)
397 goto done;
399 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
400 err = got_error(GOT_ERR_OBJ_TYPE);
401 goto done;
404 err = got_object_tree_open(&tree, repo, treeobj);
405 if (err)
406 goto done;
408 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
409 diff_content);
410 done:
411 if (tree)
412 got_object_tree_close(tree);
413 if (treeobj)
414 got_object_close(treeobj);
415 return err;
418 static const struct got_error *
419 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
420 const char *label1, const char *label2, struct got_repository *repo,
421 got_diff_blob_cb cb, void *cb_arg, int diff_content)
423 const struct got_error *err;
424 struct got_object *treeobj1 = NULL;
425 struct got_object *treeobj2 = NULL;
426 struct got_tree_object *tree1 = NULL;
427 struct got_tree_object *tree2 = NULL;
429 err = got_object_open(&treeobj1, repo, id1);
430 if (err)
431 goto done;
433 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
434 err = got_error(GOT_ERR_OBJ_TYPE);
435 goto done;
438 err = got_object_open(&treeobj2, repo, id2);
439 if (err)
440 goto done;
442 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
443 err = got_error(GOT_ERR_OBJ_TYPE);
444 goto done;
447 err = got_object_tree_open(&tree1, repo, treeobj1);
448 if (err)
449 goto done;
451 err = got_object_tree_open(&tree2, repo, treeobj2);
452 if (err)
453 goto done;
455 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
456 diff_content);
458 done:
459 if (tree1)
460 got_object_tree_close(tree1);
461 if (tree2)
462 got_object_tree_close(tree2);
463 if (treeobj1)
464 got_object_close(treeobj1);
465 if (treeobj2)
466 got_object_close(treeobj2);
467 return err;
470 static const struct got_error *
471 diff_deleted_tree(struct got_object_id *id, const char *label,
472 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
473 int diff_content)
475 const struct got_error *err;
476 struct got_object *treeobj = NULL;
477 struct got_tree_object *tree = NULL;
479 err = got_object_open(&treeobj, repo, id);
480 if (err)
481 goto done;
483 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
484 err = got_error(GOT_ERR_OBJ_TYPE);
485 goto done;
488 err = got_object_tree_open(&tree, repo, treeobj);
489 if (err)
490 goto done;
492 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
493 diff_content);
494 done:
495 if (tree)
496 got_object_tree_close(tree);
497 if (treeobj)
498 got_object_close(treeobj);
499 return err;
502 static const struct got_error *
503 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
504 const char *label1, const char *label2, struct got_repository *repo,
505 got_diff_blob_cb cb, void *cb_arg)
507 /* XXX TODO */
508 return NULL;
511 static const struct got_error *
512 diff_entry_old_new(struct got_tree_entry *te1,
513 struct got_tree_entry *te2, const char *label1, const char *label2,
514 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
515 int diff_content)
517 const struct got_error *err = NULL;
518 int id_match;
520 if (got_object_tree_entry_is_submodule(te1))
521 return NULL;
523 if (te2 == NULL) {
524 if (S_ISDIR(te1->mode))
525 err = diff_deleted_tree(&te1->id, label1, repo,
526 cb, cb_arg, diff_content);
527 else {
528 if (diff_content)
529 err = diff_deleted_blob(&te1->id, label1,
530 te1->mode, repo, cb, cb_arg);
531 else
532 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
533 label1, NULL, te1->mode, 0, repo);
535 return err;
536 } else if (got_object_tree_entry_is_submodule(te2))
537 return NULL;
539 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
540 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
541 if (!id_match)
542 return diff_modified_tree(&te1->id, &te2->id,
543 label1, label2, repo, cb, cb_arg, diff_content);
544 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
545 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
546 if (!id_match ||
547 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
548 (te2->mode & (S_IFLNK | S_IXUSR))) {
549 if (diff_content)
550 return diff_modified_blob(&te1->id, &te2->id,
551 label1, label2, te1->mode, te2->mode,
552 repo, cb, cb_arg);
553 else
554 return cb(cb_arg, NULL, NULL, &te1->id,
555 &te2->id, label1, label2, te1->mode,
556 te2->mode, repo);
560 if (id_match)
561 return NULL;
563 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
564 cb, cb_arg);
567 static const struct got_error *
568 diff_entry_new_old(struct got_tree_entry *te2,
569 struct got_tree_entry *te1, const char *label2,
570 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
571 int diff_content)
573 if (te1 != NULL) /* handled by diff_entry_old_new() */
574 return NULL;
576 if (got_object_tree_entry_is_submodule(te2))
577 return NULL;
579 if (S_ISDIR(te2->mode))
580 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
581 diff_content);
583 if (diff_content)
584 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
585 cb_arg);
587 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
588 te2->mode, repo);
591 const struct got_error *
592 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
593 struct got_blob_object *blob2, struct got_object_id *id1,
594 struct got_object_id *id2, const char *label1, const char *label2,
595 mode_t mode1, mode_t mode2, struct got_repository *repo)
597 const struct got_error *err = NULL;
598 struct got_pathlist_head *paths = arg;
599 struct got_diff_changed_path *change = NULL;
600 char *path = NULL;
602 path = strdup(label2 ? label2 : label1);
603 if (path == NULL)
604 return got_error_from_errno("malloc");
606 change = malloc(sizeof(*change));
607 if (change == NULL) {
608 err = got_error_from_errno("malloc");
609 goto done;
612 change->status = GOT_STATUS_NO_CHANGE;
613 if (id1 == NULL)
614 change->status = GOT_STATUS_ADD;
615 else if (id2 == NULL)
616 change->status = GOT_STATUS_DELETE;
617 else {
618 if (got_object_id_cmp(id1, id2) != 0)
619 change->status = GOT_STATUS_MODIFY;
620 else if (mode1 != mode2)
621 change->status = GOT_STATUS_MODE_CHANGE;
624 err = got_pathlist_insert(NULL, paths, path, change);
625 done:
626 if (err) {
627 free(path);
628 free(change);
630 return err;
633 const struct got_error *
634 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
635 const char *label1, const char *label2, struct got_repository *repo,
636 got_diff_blob_cb cb, void *cb_arg, int diff_content)
638 const struct got_error *err = NULL;
639 struct got_tree_entry *te1 = NULL;
640 struct got_tree_entry *te2 = NULL;
641 char *l1 = NULL, *l2 = NULL;
642 int tidx1 = 0, tidx2 = 0;
644 if (tree1) {
645 te1 = got_object_tree_get_entry(tree1, 0);
646 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
647 te1->name) == -1)
648 return got_error_from_errno("asprintf");
650 if (tree2) {
651 te2 = got_object_tree_get_entry(tree2, 0);
652 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
653 te2->name) == -1)
654 return got_error_from_errno("asprintf");
657 do {
658 if (te1) {
659 struct got_tree_entry *te = NULL;
660 if (tree2)
661 te = got_object_tree_find_entry(tree2,
662 te1->name);
663 if (te) {
664 free(l2);
665 l2 = NULL;
666 if (te && asprintf(&l2, "%s%s%s", label2,
667 label2[0] ? "/" : "", te->name) == -1)
668 return
669 got_error_from_errno("asprintf");
671 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
672 cb_arg, diff_content);
673 if (err)
674 break;
677 if (te2) {
678 struct got_tree_entry *te = NULL;
679 if (tree1)
680 te = got_object_tree_find_entry(tree1,
681 te2->name);
682 free(l2);
683 if (te) {
684 if (asprintf(&l2, "%s%s%s", label2,
685 label2[0] ? "/" : "", te->name) == -1)
686 return
687 got_error_from_errno("asprintf");
688 } else {
689 if (asprintf(&l2, "%s%s%s", label2,
690 label2[0] ? "/" : "", te2->name) == -1)
691 return
692 got_error_from_errno("asprintf");
694 err = diff_entry_new_old(te2, te, l2, repo,
695 cb, cb_arg, diff_content);
696 if (err)
697 break;
700 free(l1);
701 l1 = NULL;
702 if (te1) {
703 tidx1++;
704 te1 = got_object_tree_get_entry(tree1, tidx1);
705 if (te1 &&
706 asprintf(&l1, "%s%s%s", label1,
707 label1[0] ? "/" : "", te1->name) == -1)
708 return got_error_from_errno("asprintf");
710 free(l2);
711 l2 = NULL;
712 if (te2) {
713 tidx2++;
714 te2 = got_object_tree_get_entry(tree2, tidx2);
715 if (te2 &&
716 asprintf(&l2, "%s%s%s", label2,
717 label2[0] ? "/" : "", te2->name) == -1)
718 return got_error_from_errno("asprintf");
720 } while (te1 || te2);
722 return err;
725 const struct got_error *
726 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
727 struct got_object_id *id1, struct got_object_id *id2,
728 const char *label1, const char *label2, int diff_context,
729 int ignore_whitespace, int force_text_diff,
730 struct got_repository *repo, FILE *outfile)
732 const struct got_error *err;
733 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
735 if (id1 == NULL && id2 == NULL)
736 return got_error(GOT_ERR_NO_OBJ);
738 if (id1) {
739 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
740 if (err)
741 goto done;
743 if (id2) {
744 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
745 if (err)
746 goto done;
748 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
749 label1, label2, diff_context, ignore_whitespace, force_text_diff,
750 outfile);
751 done:
752 if (blob1)
753 got_object_blob_close(blob1);
754 if (blob2)
755 got_object_blob_close(blob2);
756 return err;
759 const struct got_error *
760 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
761 struct got_object_id *id1, struct got_object_id *id2,
762 char *label1, char *label2, int diff_context, int ignore_whitespace,
763 int force_text_diff, struct got_repository *repo, FILE *outfile)
765 const struct got_error *err;
766 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
767 struct got_diff_blob_output_unidiff_arg arg;
768 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
770 if (id1 == NULL && id2 == NULL)
771 return got_error(GOT_ERR_NO_OBJ);
773 if (id1) {
774 err = got_object_open_as_tree(&tree1, repo, id1);
775 if (err)
776 goto done;
778 if (id2) {
779 err = got_object_open_as_tree(&tree2, repo, id2);
780 if (err)
781 goto done;
783 arg.diff_context = diff_context;
784 arg.ignore_whitespace = ignore_whitespace;
785 arg.force_text_diff = force_text_diff;
786 arg.outfile = outfile;
787 if (want_lineoffsets) {
788 arg.line_offsets = *line_offsets;
789 arg.nlines = *nlines;
790 } else {
791 arg.line_offsets = NULL;
792 arg.nlines = 0;
794 err = got_diff_tree(tree1, tree2, label1, label2, repo,
795 got_diff_blob_output_unidiff, &arg, 1);
797 if (want_lineoffsets) {
798 *line_offsets = arg.line_offsets; /* was likely re-allocated */
799 *nlines = arg.nlines;
801 done:
802 if (tree1)
803 got_object_tree_close(tree1);
804 if (tree2)
805 got_object_tree_close(tree2);
806 return err;
809 const struct got_error *
810 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
811 struct got_object_id *id1, struct got_object_id *id2,
812 int diff_context, int ignore_whitespace, int force_text_diff,
813 struct got_repository *repo, FILE *outfile)
815 const struct got_error *err;
816 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
818 if (id2 == NULL)
819 return got_error(GOT_ERR_NO_OBJ);
821 if (id1) {
822 err = got_object_open_as_commit(&commit1, repo, id1);
823 if (err)
824 goto done;
827 err = got_object_open_as_commit(&commit2, repo, id2);
828 if (err)
829 goto done;
831 err = got_diff_objects_as_trees(line_offsets, nlines,
832 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
833 got_object_commit_get_tree_id(commit2), "", "", diff_context,
834 ignore_whitespace, force_text_diff, repo, outfile);
835 done:
836 if (commit1)
837 got_object_commit_close(commit1);
838 if (commit2)
839 got_object_commit_close(commit2);
840 return err;
843 const struct got_error *
844 got_diff_files(struct got_diffreg_result **resultp,
845 FILE *f1, const char *label1, FILE *f2, const char *label2,
846 int diff_context, int ignore_whitespace, int force_text_diff,
847 FILE *outfile)
849 const struct got_error *err = NULL;
850 struct got_diffreg_result *diffreg_result = NULL;
852 if (resultp)
853 *resultp = NULL;
855 if (outfile) {
856 fprintf(outfile, "file - %s\n",
857 f1 == NULL ? "/dev/null" : label1);
858 fprintf(outfile, "file + %s\n",
859 f2 == NULL ? "/dev/null" : label2);
862 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
863 ignore_whitespace, force_text_diff);
864 if (err)
865 goto done;
867 if (outfile) {
868 err = got_diffreg_output(NULL, NULL, diffreg_result,
869 f1 != NULL, f2 != NULL, label1, label2,
870 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
871 if (err)
872 goto done;
875 done:
876 if (resultp && err == NULL)
877 *resultp = diffreg_result;
878 else if (diffreg_result) {
879 const struct got_error *free_err;
880 free_err = got_diffreg_result_free(diffreg_result);
881 if (free_err && err == NULL)
882 err = free_err;
885 return err;