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 err = got_error_from_errno("fprintf");
143 goto done;
145 outoff += n;
146 if (line_offsets) {
147 err = add_line_offset(line_offsets, nlines, outoff);
148 if (err)
149 goto done;
152 n = fprintf(outfile, "blob + %s%s\n", idstr2,
153 modestr2 ? modestr2 : "");
154 if (n < 0) {
155 err = got_error_from_errno("fprintf");
156 goto done;
158 outoff += n;
159 if (line_offsets) {
160 err = add_line_offset(line_offsets, nlines, outoff);
161 if (err)
162 goto done;
165 free(modestr1);
166 free(modestr2);
168 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
169 ignore_whitespace);
170 if (err)
171 goto done;
173 if (outfile) {
174 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
175 label1 ? label1 : idstr1,
176 label2 ? label2 : idstr2,
177 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
178 if (err)
179 goto done;
182 if (resultp && err == NULL)
183 *resultp = result;
184 else {
185 free_err = got_diffreg_result_free(result);
186 if (free_err && err == NULL)
187 err = free_err;
189 done:
190 if (f1 && fclose(f1) != 0 && err == NULL)
191 err = got_error_from_errno("fclose");
192 if (f2 && fclose(f2) != 0 && err == NULL)
193 err = got_error_from_errno("fclose");
194 return err;
197 const struct got_error *
198 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
199 struct got_blob_object *blob2, struct got_object_id *id1,
200 struct got_object_id *id2, const char *label1, const char *label2,
201 mode_t mode1, mode_t mode2, struct got_repository *repo)
203 struct got_diff_blob_output_unidiff_arg *a = arg;
205 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
206 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
207 a->ignore_whitespace, a->outfile);
210 const struct got_error *
211 got_diff_blob(off_t **line_offsets, size_t *nlines,
212 struct got_blob_object *blob1, struct got_blob_object *blob2,
213 const char *label1, const char *label2, int diff_context,
214 int ignore_whitespace, FILE *outfile)
216 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
217 label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
220 static const struct got_error *
221 diff_blob_file(struct got_diffreg_result **resultp,
222 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
223 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
225 const struct got_error *err = NULL, *free_err;
226 FILE *f1 = NULL;
227 char hex1[SHA1_DIGEST_STRING_LENGTH];
228 char *idstr1 = NULL;
229 size_t size1;
230 struct got_diffreg_result *result = NULL;
232 if (resultp)
233 *resultp = NULL;
235 size1 = 0;
236 if (blob1) {
237 f1 = got_opentemp();
238 if (f1 == NULL)
239 return got_error_from_errno("got_opentemp");
240 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
241 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
242 blob1);
243 if (err)
244 goto done;
245 } else {
246 idstr1 = "/dev/null";
249 if (outfile) {
250 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
251 fprintf(outfile, "file + %s\n",
252 f2 == NULL ? "/dev/null" : label2);
255 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
256 ignore_whitespace);
257 if (err)
258 goto done;
260 if (outfile) {
261 err = got_diffreg_output(NULL, NULL, result, f1, f2,
262 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
263 outfile);
264 if (err)
265 goto done;
268 if (resultp && err == NULL)
269 *resultp = result;
270 else if (result) {
271 free_err = got_diffreg_result_free(result);
272 if (free_err && err == NULL)
273 err = free_err;
275 done:
276 if (f1 && fclose(f1) != 0 && err == NULL)
277 err = got_error_from_errno("fclose");
278 return err;
281 const struct got_error *
282 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
283 FILE *f2, size_t size2, const char *label2, int diff_context,
284 int ignore_whitespace, FILE *outfile)
286 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
287 diff_context, ignore_whitespace, outfile);
290 const struct got_error *
291 got_diff_blob_file_lines_changed(struct got_diffreg_result **result,
292 struct got_blob_object *blob1, FILE *f2, size_t size2)
294 return diff_blob_file(result, blob1, NULL, f2, size2, NULL,
295 0, 0, NULL);
298 const struct got_error *
299 got_diff_blob_lines_changed(struct got_diffreg_result **result,
300 struct got_blob_object *blob1, struct got_blob_object *blob2)
302 const struct got_error *err = NULL;
304 err = diff_blobs(NULL, NULL, result, blob1, blob2,
305 NULL, NULL, 0, 0, 3, 0, NULL);
306 if (err) {
307 got_diffreg_result_free(*result);
308 *result = NULL;
310 return err;
313 static const struct got_error *
314 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
315 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
317 const struct got_error *err;
318 struct got_blob_object *blob = NULL;
319 struct got_object *obj = NULL;
321 err = got_object_open(&obj, repo, id);
322 if (err)
323 return err;
325 err = got_object_blob_open(&blob, repo, obj, 8192);
326 if (err)
327 goto done;
328 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
329 done:
330 got_object_close(obj);
331 if (blob)
332 got_object_blob_close(blob);
333 return err;
336 static const struct got_error *
337 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
338 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
339 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
341 const struct got_error *err;
342 struct got_object *obj1 = NULL;
343 struct got_object *obj2 = NULL;
344 struct got_blob_object *blob1 = NULL;
345 struct got_blob_object *blob2 = NULL;
347 err = got_object_open(&obj1, repo, id1);
348 if (err)
349 return err;
350 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
351 err = got_error(GOT_ERR_OBJ_TYPE);
352 goto done;
355 err = got_object_open(&obj2, repo, id2);
356 if (err)
357 goto done;
358 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
359 err = got_error(GOT_ERR_BAD_OBJ_DATA);
360 goto done;
363 err = got_object_blob_open(&blob1, repo, obj1, 8192);
364 if (err)
365 goto done;
367 err = got_object_blob_open(&blob2, repo, obj2, 8192);
368 if (err)
369 goto done;
371 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
372 repo);
373 done:
374 if (obj1)
375 got_object_close(obj1);
376 if (obj2)
377 got_object_close(obj2);
378 if (blob1)
379 got_object_blob_close(blob1);
380 if (blob2)
381 got_object_blob_close(blob2);
382 return err;
385 static const struct got_error *
386 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
387 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
389 const struct got_error *err;
390 struct got_blob_object *blob = NULL;
391 struct got_object *obj = NULL;
393 err = got_object_open(&obj, repo, id);
394 if (err)
395 return err;
397 err = got_object_blob_open(&blob, repo, obj, 8192);
398 if (err)
399 goto done;
400 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
401 done:
402 got_object_close(obj);
403 if (blob)
404 got_object_blob_close(blob);
405 return err;
408 static const struct got_error *
409 diff_added_tree(struct got_object_id *id, const char *label,
410 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
411 int diff_content)
413 const struct got_error *err = NULL;
414 struct got_object *treeobj = NULL;
415 struct got_tree_object *tree = NULL;
417 err = got_object_open(&treeobj, repo, id);
418 if (err)
419 goto done;
421 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
422 err = got_error(GOT_ERR_OBJ_TYPE);
423 goto done;
426 err = got_object_tree_open(&tree, repo, treeobj);
427 if (err)
428 goto done;
430 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
431 diff_content);
432 done:
433 if (tree)
434 got_object_tree_close(tree);
435 if (treeobj)
436 got_object_close(treeobj);
437 return err;
440 static const struct got_error *
441 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
442 const char *label1, const char *label2, struct got_repository *repo,
443 got_diff_blob_cb cb, void *cb_arg, int diff_content)
445 const struct got_error *err;
446 struct got_object *treeobj1 = NULL;
447 struct got_object *treeobj2 = NULL;
448 struct got_tree_object *tree1 = NULL;
449 struct got_tree_object *tree2 = NULL;
451 err = got_object_open(&treeobj1, repo, id1);
452 if (err)
453 goto done;
455 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
456 err = got_error(GOT_ERR_OBJ_TYPE);
457 goto done;
460 err = got_object_open(&treeobj2, repo, id2);
461 if (err)
462 goto done;
464 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
465 err = got_error(GOT_ERR_OBJ_TYPE);
466 goto done;
469 err = got_object_tree_open(&tree1, repo, treeobj1);
470 if (err)
471 goto done;
473 err = got_object_tree_open(&tree2, repo, treeobj2);
474 if (err)
475 goto done;
477 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
478 diff_content);
480 done:
481 if (tree1)
482 got_object_tree_close(tree1);
483 if (tree2)
484 got_object_tree_close(tree2);
485 if (treeobj1)
486 got_object_close(treeobj1);
487 if (treeobj2)
488 got_object_close(treeobj2);
489 return err;
492 static const struct got_error *
493 diff_deleted_tree(struct got_object_id *id, const char *label,
494 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
495 int diff_content)
497 const struct got_error *err;
498 struct got_object *treeobj = NULL;
499 struct got_tree_object *tree = NULL;
501 err = got_object_open(&treeobj, repo, id);
502 if (err)
503 goto done;
505 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
506 err = got_error(GOT_ERR_OBJ_TYPE);
507 goto done;
510 err = got_object_tree_open(&tree, repo, treeobj);
511 if (err)
512 goto done;
514 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
515 diff_content);
516 done:
517 if (tree)
518 got_object_tree_close(tree);
519 if (treeobj)
520 got_object_close(treeobj);
521 return err;
524 static const struct got_error *
525 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
526 const char *label1, const char *label2, struct got_repository *repo,
527 got_diff_blob_cb cb, void *cb_arg)
529 /* XXX TODO */
530 return NULL;
533 static const struct got_error *
534 diff_entry_old_new(struct got_tree_entry *te1,
535 struct got_tree_entry *te2, const char *label1, const char *label2,
536 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
537 int diff_content)
539 const struct got_error *err = NULL;
540 int id_match;
542 if (got_object_tree_entry_is_submodule(te1))
543 return NULL;
545 if (te2 == NULL) {
546 if (S_ISDIR(te1->mode))
547 err = diff_deleted_tree(&te1->id, label1, repo,
548 cb, cb_arg, diff_content);
549 else {
550 if (diff_content)
551 err = diff_deleted_blob(&te1->id, label1,
552 te1->mode, repo, cb, cb_arg);
553 else
554 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
555 label1, NULL, te1->mode, 0, repo);
557 return err;
558 } else if (got_object_tree_entry_is_submodule(te2))
559 return NULL;
561 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
562 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
563 if (!id_match)
564 return diff_modified_tree(&te1->id, &te2->id,
565 label1, label2, repo, cb, cb_arg, diff_content);
566 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
567 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
568 if (!id_match ||
569 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
570 (te2->mode & (S_IFLNK | S_IXUSR))) {
571 if (diff_content)
572 return diff_modified_blob(&te1->id, &te2->id,
573 label1, label2, te1->mode, te2->mode,
574 repo, cb, cb_arg);
575 else
576 return cb(cb_arg, NULL, NULL, &te1->id,
577 &te2->id, label1, label2, te1->mode,
578 te2->mode, repo);
582 if (id_match)
583 return NULL;
585 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
586 cb, cb_arg);
589 static const struct got_error *
590 diff_entry_new_old(struct got_tree_entry *te2,
591 struct got_tree_entry *te1, const char *label2,
592 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
593 int diff_content)
595 if (te1 != NULL) /* handled by diff_entry_old_new() */
596 return NULL;
598 if (got_object_tree_entry_is_submodule(te2))
599 return NULL;
601 if (S_ISDIR(te2->mode))
602 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
603 diff_content);
605 if (diff_content)
606 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
607 cb_arg);
609 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
610 te2->mode, repo);
613 const struct got_error *
614 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
615 struct got_blob_object *blob2, struct got_object_id *id1,
616 struct got_object_id *id2, const char *label1, const char *label2,
617 mode_t mode1, mode_t mode2, struct got_repository *repo)
619 const struct got_error *err = NULL;
620 struct got_pathlist_head *paths = arg;
621 struct got_diff_changed_path *change = NULL;
622 char *path = NULL;
624 path = strdup(label2 ? label2 : label1);
625 if (path == NULL)
626 return got_error_from_errno("malloc");
628 change = malloc(sizeof(*change));
629 if (change == NULL) {
630 err = got_error_from_errno("malloc");
631 goto done;
634 change->status = GOT_STATUS_NO_CHANGE;
635 if (id1 == NULL)
636 change->status = GOT_STATUS_ADD;
637 else if (id2 == NULL)
638 change->status = GOT_STATUS_DELETE;
639 else {
640 if (got_object_id_cmp(id1, id2) != 0)
641 change->status = GOT_STATUS_MODIFY;
642 else if (mode1 != mode2)
643 change->status = GOT_STATUS_MODE_CHANGE;
646 err = got_pathlist_insert(NULL, paths, path, change);
647 done:
648 if (err) {
649 free(path);
650 free(change);
652 return err;
655 const struct got_error *
656 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
657 const char *label1, const char *label2, struct got_repository *repo,
658 got_diff_blob_cb cb, void *cb_arg, int diff_content)
660 const struct got_error *err = NULL;
661 struct got_tree_entry *te1 = NULL;
662 struct got_tree_entry *te2 = NULL;
663 char *l1 = NULL, *l2 = NULL;
664 int tidx1 = 0, tidx2 = 0;
666 if (tree1) {
667 te1 = got_object_tree_get_entry(tree1, 0);
668 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
669 te1->name) == -1)
670 return got_error_from_errno("asprintf");
672 if (tree2) {
673 te2 = got_object_tree_get_entry(tree2, 0);
674 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
675 te2->name) == -1)
676 return got_error_from_errno("asprintf");
679 do {
680 if (te1) {
681 struct got_tree_entry *te = NULL;
682 if (tree2)
683 te = got_object_tree_find_entry(tree2,
684 te1->name);
685 if (te) {
686 free(l2);
687 l2 = NULL;
688 if (te && asprintf(&l2, "%s%s%s", label2,
689 label2[0] ? "/" : "", te->name) == -1)
690 return
691 got_error_from_errno("asprintf");
693 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
694 cb_arg, diff_content);
695 if (err)
696 break;
699 if (te2) {
700 struct got_tree_entry *te = NULL;
701 if (tree1)
702 te = got_object_tree_find_entry(tree1,
703 te2->name);
704 free(l2);
705 if (te) {
706 if (asprintf(&l2, "%s%s%s", label2,
707 label2[0] ? "/" : "", te->name) == -1)
708 return
709 got_error_from_errno("asprintf");
710 } else {
711 if (asprintf(&l2, "%s%s%s", label2,
712 label2[0] ? "/" : "", te2->name) == -1)
713 return
714 got_error_from_errno("asprintf");
716 err = diff_entry_new_old(te2, te, l2, repo,
717 cb, cb_arg, diff_content);
718 if (err)
719 break;
722 free(l1);
723 l1 = NULL;
724 if (te1) {
725 tidx1++;
726 te1 = got_object_tree_get_entry(tree1, tidx1);
727 if (te1 &&
728 asprintf(&l1, "%s%s%s", label1,
729 label1[0] ? "/" : "", te1->name) == -1)
730 return got_error_from_errno("asprintf");
732 free(l2);
733 l2 = NULL;
734 if (te2) {
735 tidx2++;
736 te2 = got_object_tree_get_entry(tree2, tidx2);
737 if (te2 &&
738 asprintf(&l2, "%s%s%s", label2,
739 label2[0] ? "/" : "", te2->name) == -1)
740 return got_error_from_errno("asprintf");
742 } while (te1 || te2);
744 return err;
747 const struct got_error *
748 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
749 struct got_object_id *id1, struct got_object_id *id2,
750 const char *label1, const char *label2, int diff_context,
751 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
753 const struct got_error *err;
754 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
756 if (id1 == NULL && id2 == NULL)
757 return got_error(GOT_ERR_NO_OBJ);
759 if (id1) {
760 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
761 if (err)
762 goto done;
764 if (id2) {
765 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
766 if (err)
767 goto done;
769 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
770 label1, label2, diff_context, ignore_whitespace, outfile);
771 done:
772 if (blob1)
773 got_object_blob_close(blob1);
774 if (blob2)
775 got_object_blob_close(blob2);
776 return err;
779 const struct got_error *
780 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
781 struct got_object_id *id1, struct got_object_id *id2,
782 char *label1, char *label2, int diff_context, int ignore_whitespace,
783 struct got_repository *repo, FILE *outfile)
785 const struct got_error *err;
786 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
787 struct got_diff_blob_output_unidiff_arg arg;
788 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
790 if (id1 == NULL && id2 == NULL)
791 return got_error(GOT_ERR_NO_OBJ);
793 if (id1) {
794 err = got_object_open_as_tree(&tree1, repo, id1);
795 if (err)
796 goto done;
798 if (id2) {
799 err = got_object_open_as_tree(&tree2, repo, id2);
800 if (err)
801 goto done;
803 arg.diff_context = diff_context;
804 arg.ignore_whitespace = ignore_whitespace;
805 arg.outfile = outfile;
806 if (want_lineoffsets) {
807 arg.line_offsets = *line_offsets;
808 arg.nlines = *nlines;
809 } else {
810 arg.line_offsets = NULL;
811 arg.nlines = 0;
813 err = got_diff_tree(tree1, tree2, label1, label2, repo,
814 got_diff_blob_output_unidiff, &arg, 1);
816 if (want_lineoffsets) {
817 *line_offsets = arg.line_offsets; /* was likely re-allocated */
818 *nlines = arg.nlines;
820 done:
821 if (tree1)
822 got_object_tree_close(tree1);
823 if (tree2)
824 got_object_tree_close(tree2);
825 return err;
828 const struct got_error *
829 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
830 struct got_object_id *id1, struct got_object_id *id2,
831 int diff_context, int ignore_whitespace,
832 struct got_repository *repo, FILE *outfile)
834 const struct got_error *err;
835 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
837 if (id2 == NULL)
838 return got_error(GOT_ERR_NO_OBJ);
840 if (id1) {
841 err = got_object_open_as_commit(&commit1, repo, id1);
842 if (err)
843 goto done;
846 err = got_object_open_as_commit(&commit2, repo, id2);
847 if (err)
848 goto done;
850 err = got_diff_objects_as_trees(line_offsets, nlines,
851 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
852 got_object_commit_get_tree_id(commit2), "", "", diff_context,
853 ignore_whitespace, repo, outfile);
854 done:
855 if (commit1)
856 got_object_commit_close(commit1);
857 if (commit2)
858 got_object_commit_close(commit2);
859 return err;
862 const struct got_error *
863 got_diff_files(struct got_diffreg_result **resultp,
864 FILE *f1, const char *label1, FILE *f2, const char *label2,
865 int diff_context, int ignore_whitespace, FILE *outfile)
867 const struct got_error *err = NULL;
868 struct got_diffreg_result *diffreg_result = NULL;
870 if (resultp)
871 *resultp = NULL;
873 if (outfile) {
874 fprintf(outfile, "file - %s\n",
875 f1 == NULL ? "/dev/null" : label1);
876 fprintf(outfile, "file + %s\n",
877 f2 == NULL ? "/dev/null" : label2);
880 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
881 ignore_whitespace);
882 if (err)
883 goto done;
885 if (outfile) {
886 err = got_diffreg_output(NULL, NULL, diffreg_result,
887 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
888 diff_context, outfile);
889 if (err)
890 goto done;
893 done:
894 if (resultp && err == NULL)
895 *resultp = diffreg_result;
896 else if (diffreg_result) {
897 const struct got_error *free_err;
898 free_err = got_diffreg_result_free(diffreg_result);
899 if (free_err && err == NULL)
900 err = free_err;
903 return err;