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 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];
76 if (resultp)
77 *resultp = NULL;
79 if (blob1) {
80 f1 = got_opentemp();
81 if (f1 == NULL)
82 return got_error_from_errno("got_opentemp");
83 }
85 if (blob2) {
86 f2 = got_opentemp();
87 if (f2 == NULL) {
88 err = got_error_from_errno("got_opentemp");
89 fclose(f1);
90 return err;
91 }
92 }
94 size1 = 0;
95 if (blob1) {
96 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
98 blob1);
99 if (err)
100 goto done;
101 } else
102 idstr1 = "/dev/null";
104 size2 = 0;
105 if (blob2) {
106 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
108 blob2);
109 if (err)
110 goto done;
111 } else
112 idstr2 = "/dev/null";
114 if (outfile) {
115 char *modestr1 = NULL, *modestr2 = NULL;
116 int modebits;
117 if (mode1 && mode1 != mode2) {
118 if (S_ISLNK(mode1))
119 modebits = S_IFLNK;
120 else
121 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122 if (asprintf(&modestr1, " (mode %o)",
123 mode1 & modebits) == -1) {
124 err = got_error_from_errno("asprintf");
125 goto done;
128 if (mode2 && mode1 != mode2) {
129 if (S_ISLNK(mode2))
130 modebits = S_IFLNK;
131 else
132 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133 if (asprintf(&modestr2, " (mode %o)",
134 mode2 & modebits) == -1) {
135 err = got_error_from_errno("asprintf");
136 goto done;
139 n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 modestr1 ? modestr1 : "");
141 if (n < 0)
142 goto done;
143 outoff += n;
144 if (line_offsets) {
145 err = add_line_offset(line_offsets, nlines, outoff);
146 if (err)
147 goto done;
150 n = fprintf(outfile, "blob + %s%s\n", idstr2,
151 modestr2 ? modestr2 : "");
152 if (n < 0)
153 goto done;
154 outoff += n;
155 if (line_offsets) {
156 err = add_line_offset(line_offsets, nlines, outoff);
157 if (err)
158 goto done;
161 free(modestr1);
162 free(modestr2);
164 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
165 ignore_whitespace);
166 if (err)
167 goto done;
169 if (outfile) {
170 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
171 label1 ? label1 : idstr1,
172 label2 ? label2 : idstr2,
173 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
174 if (err)
175 goto done;
178 if (resultp && err == NULL)
179 *resultp = result;
180 else {
181 free_err = got_diffreg_result_free(result);
182 if (free_err && err == NULL)
183 err = free_err;
185 done:
186 if (f1 && fclose(f1) != 0 && err == NULL)
187 err = got_error_from_errno("fclose");
188 if (f2 && fclose(f2) != 0 && err == NULL)
189 err = got_error_from_errno("fclose");
190 return err;
193 const struct got_error *
194 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
195 struct got_blob_object *blob2, struct got_object_id *id1,
196 struct got_object_id *id2, const char *label1, const char *label2,
197 mode_t mode1, mode_t mode2, struct got_repository *repo)
199 struct got_diff_blob_output_unidiff_arg *a = arg;
201 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
202 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
203 a->ignore_whitespace, a->outfile);
206 const struct got_error *
207 got_diff_blob(off_t **line_offsets, size_t *nlines,
208 struct got_blob_object *blob1, struct got_blob_object *blob2,
209 const char *label1, const char *label2, int diff_context,
210 int ignore_whitespace, FILE *outfile)
212 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
213 label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
216 static const struct got_error *
217 diff_blob_file(struct got_diffreg_result **resultp,
218 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
219 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
221 const struct got_error *err = NULL, *free_err;
222 FILE *f1 = NULL;
223 char hex1[SHA1_DIGEST_STRING_LENGTH];
224 char *idstr1 = NULL;
225 off_t size1;
226 struct got_diffreg_result *result = NULL;
228 if (resultp)
229 *resultp = NULL;
231 size1 = 0;
232 if (blob1) {
233 f1 = got_opentemp();
234 if (f1 == NULL)
235 return got_error_from_errno("got_opentemp");
236 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
237 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
238 blob1);
239 if (err)
240 goto done;
241 } else {
242 idstr1 = "/dev/null";
245 if (outfile) {
246 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
247 fprintf(outfile, "file + %s\n",
248 f2 == NULL ? "/dev/null" : label2);
251 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
252 ignore_whitespace);
253 if (err)
254 goto done;
256 if (outfile) {
257 err = got_diffreg_output(NULL, NULL, result, f1, f2,
258 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
259 outfile);
260 if (err)
261 goto done;
264 if (resultp && err == NULL)
265 *resultp = result;
266 else if (result) {
267 free_err = got_diffreg_result_free(result);
268 if (free_err && err == NULL)
269 err = free_err;
271 done:
272 if (f1 && fclose(f1) != 0 && err == NULL)
273 err = got_error_from_errno("fclose");
274 return err;
277 const struct got_error *
278 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
279 FILE *f2, size_t size2, const char *label2, int diff_context,
280 int ignore_whitespace, FILE *outfile)
282 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
283 diff_context, ignore_whitespace, outfile);
286 static const struct got_error *
287 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
288 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
290 const struct got_error *err;
291 struct got_blob_object *blob = NULL;
292 struct got_object *obj = NULL;
294 err = got_object_open(&obj, repo, id);
295 if (err)
296 return err;
298 err = got_object_blob_open(&blob, repo, obj, 8192);
299 if (err)
300 goto done;
301 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
302 done:
303 got_object_close(obj);
304 if (blob)
305 got_object_blob_close(blob);
306 return err;
309 static const struct got_error *
310 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
311 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
312 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
314 const struct got_error *err;
315 struct got_object *obj1 = NULL;
316 struct got_object *obj2 = NULL;
317 struct got_blob_object *blob1 = NULL;
318 struct got_blob_object *blob2 = NULL;
320 err = got_object_open(&obj1, repo, id1);
321 if (err)
322 return err;
323 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
324 err = got_error(GOT_ERR_OBJ_TYPE);
325 goto done;
328 err = got_object_open(&obj2, repo, id2);
329 if (err)
330 goto done;
331 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
332 err = got_error(GOT_ERR_BAD_OBJ_DATA);
333 goto done;
336 err = got_object_blob_open(&blob1, repo, obj1, 8192);
337 if (err)
338 goto done;
340 err = got_object_blob_open(&blob2, repo, obj2, 8192);
341 if (err)
342 goto done;
344 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
345 repo);
346 done:
347 if (obj1)
348 got_object_close(obj1);
349 if (obj2)
350 got_object_close(obj2);
351 if (blob1)
352 got_object_blob_close(blob1);
353 if (blob2)
354 got_object_blob_close(blob2);
355 return err;
358 static const struct got_error *
359 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
360 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
362 const struct got_error *err;
363 struct got_blob_object *blob = NULL;
364 struct got_object *obj = NULL;
366 err = got_object_open(&obj, repo, id);
367 if (err)
368 return err;
370 err = got_object_blob_open(&blob, repo, obj, 8192);
371 if (err)
372 goto done;
373 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
374 done:
375 got_object_close(obj);
376 if (blob)
377 got_object_blob_close(blob);
378 return err;
381 static const struct got_error *
382 diff_added_tree(struct got_object_id *id, const char *label,
383 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
384 int diff_content)
386 const struct got_error *err = NULL;
387 struct got_object *treeobj = NULL;
388 struct got_tree_object *tree = NULL;
390 err = got_object_open(&treeobj, repo, id);
391 if (err)
392 goto done;
394 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
395 err = got_error(GOT_ERR_OBJ_TYPE);
396 goto done;
399 err = got_object_tree_open(&tree, repo, treeobj);
400 if (err)
401 goto done;
403 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
404 diff_content);
405 done:
406 if (tree)
407 got_object_tree_close(tree);
408 if (treeobj)
409 got_object_close(treeobj);
410 return err;
413 static const struct got_error *
414 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
415 const char *label1, const char *label2, struct got_repository *repo,
416 got_diff_blob_cb cb, void *cb_arg, int diff_content)
418 const struct got_error *err;
419 struct got_object *treeobj1 = NULL;
420 struct got_object *treeobj2 = NULL;
421 struct got_tree_object *tree1 = NULL;
422 struct got_tree_object *tree2 = NULL;
424 err = got_object_open(&treeobj1, repo, id1);
425 if (err)
426 goto done;
428 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
429 err = got_error(GOT_ERR_OBJ_TYPE);
430 goto done;
433 err = got_object_open(&treeobj2, repo, id2);
434 if (err)
435 goto done;
437 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
438 err = got_error(GOT_ERR_OBJ_TYPE);
439 goto done;
442 err = got_object_tree_open(&tree1, repo, treeobj1);
443 if (err)
444 goto done;
446 err = got_object_tree_open(&tree2, repo, treeobj2);
447 if (err)
448 goto done;
450 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
451 diff_content);
453 done:
454 if (tree1)
455 got_object_tree_close(tree1);
456 if (tree2)
457 got_object_tree_close(tree2);
458 if (treeobj1)
459 got_object_close(treeobj1);
460 if (treeobj2)
461 got_object_close(treeobj2);
462 return err;
465 static const struct got_error *
466 diff_deleted_tree(struct got_object_id *id, const char *label,
467 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
468 int diff_content)
470 const struct got_error *err;
471 struct got_object *treeobj = NULL;
472 struct got_tree_object *tree = NULL;
474 err = got_object_open(&treeobj, repo, id);
475 if (err)
476 goto done;
478 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
479 err = got_error(GOT_ERR_OBJ_TYPE);
480 goto done;
483 err = got_object_tree_open(&tree, repo, treeobj);
484 if (err)
485 goto done;
487 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
488 diff_content);
489 done:
490 if (tree)
491 got_object_tree_close(tree);
492 if (treeobj)
493 got_object_close(treeobj);
494 return err;
497 static const struct got_error *
498 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
499 const char *label1, const char *label2, struct got_repository *repo,
500 got_diff_blob_cb cb, void *cb_arg)
502 /* XXX TODO */
503 return NULL;
506 static const struct got_error *
507 diff_entry_old_new(struct got_tree_entry *te1,
508 struct got_tree_entry *te2, const char *label1, const char *label2,
509 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
510 int diff_content)
512 const struct got_error *err = NULL;
513 int id_match;
515 if (got_object_tree_entry_is_submodule(te1))
516 return NULL;
518 if (te2 == NULL) {
519 if (S_ISDIR(te1->mode))
520 err = diff_deleted_tree(&te1->id, label1, repo,
521 cb, cb_arg, diff_content);
522 else {
523 if (diff_content)
524 err = diff_deleted_blob(&te1->id, label1,
525 te1->mode, repo, cb, cb_arg);
526 else
527 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
528 label1, NULL, te1->mode, 0, repo);
530 return err;
531 } else if (got_object_tree_entry_is_submodule(te2))
532 return NULL;
534 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
535 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
536 if (!id_match)
537 return diff_modified_tree(&te1->id, &te2->id,
538 label1, label2, repo, cb, cb_arg, diff_content);
539 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
540 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
541 if (!id_match ||
542 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
543 (te2->mode & (S_IFLNK | S_IXUSR))) {
544 if (diff_content)
545 return diff_modified_blob(&te1->id, &te2->id,
546 label1, label2, te1->mode, te2->mode,
547 repo, cb, cb_arg);
548 else
549 return cb(cb_arg, NULL, NULL, &te1->id,
550 &te2->id, label1, label2, te1->mode,
551 te2->mode, repo);
555 if (id_match)
556 return NULL;
558 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
559 cb, cb_arg);
562 static const struct got_error *
563 diff_entry_new_old(struct got_tree_entry *te2,
564 struct got_tree_entry *te1, const char *label2,
565 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
566 int diff_content)
568 if (te1 != NULL) /* handled by diff_entry_old_new() */
569 return NULL;
571 if (got_object_tree_entry_is_submodule(te2))
572 return NULL;
574 if (S_ISDIR(te2->mode))
575 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
576 diff_content);
578 if (diff_content)
579 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
580 cb_arg);
582 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
583 te2->mode, repo);
586 const struct got_error *
587 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
588 struct got_blob_object *blob2, struct got_object_id *id1,
589 struct got_object_id *id2, const char *label1, const char *label2,
590 mode_t mode1, mode_t mode2, struct got_repository *repo)
592 const struct got_error *err = NULL;
593 struct got_pathlist_head *paths = arg;
594 struct got_diff_changed_path *change = NULL;
595 char *path = NULL;
597 path = strdup(label2 ? label2 : label1);
598 if (path == NULL)
599 return got_error_from_errno("malloc");
601 change = malloc(sizeof(*change));
602 if (change == NULL) {
603 err = got_error_from_errno("malloc");
604 goto done;
607 change->status = GOT_STATUS_NO_CHANGE;
608 if (id1 == NULL)
609 change->status = GOT_STATUS_ADD;
610 else if (id2 == NULL)
611 change->status = GOT_STATUS_DELETE;
612 else {
613 if (got_object_id_cmp(id1, id2) != 0)
614 change->status = GOT_STATUS_MODIFY;
615 else if (mode1 != mode2)
616 change->status = GOT_STATUS_MODE_CHANGE;
619 err = got_pathlist_insert(NULL, paths, path, change);
620 done:
621 if (err) {
622 free(path);
623 free(change);
625 return err;
628 const struct got_error *
629 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
630 const char *label1, const char *label2, struct got_repository *repo,
631 got_diff_blob_cb cb, void *cb_arg, int diff_content)
633 const struct got_error *err = NULL;
634 struct got_tree_entry *te1 = NULL;
635 struct got_tree_entry *te2 = NULL;
636 char *l1 = NULL, *l2 = NULL;
637 int tidx1 = 0, tidx2 = 0;
639 if (tree1) {
640 te1 = got_object_tree_get_entry(tree1, 0);
641 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
642 te1->name) == -1)
643 return got_error_from_errno("asprintf");
645 if (tree2) {
646 te2 = got_object_tree_get_entry(tree2, 0);
647 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
648 te2->name) == -1)
649 return got_error_from_errno("asprintf");
652 do {
653 if (te1) {
654 struct got_tree_entry *te = NULL;
655 if (tree2)
656 te = got_object_tree_find_entry(tree2,
657 te1->name);
658 if (te) {
659 free(l2);
660 l2 = NULL;
661 if (te && asprintf(&l2, "%s%s%s", label2,
662 label2[0] ? "/" : "", te->name) == -1)
663 return
664 got_error_from_errno("asprintf");
666 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
667 cb_arg, diff_content);
668 if (err)
669 break;
672 if (te2) {
673 struct got_tree_entry *te = NULL;
674 if (tree1)
675 te = got_object_tree_find_entry(tree1,
676 te2->name);
677 free(l2);
678 if (te) {
679 if (asprintf(&l2, "%s%s%s", label2,
680 label2[0] ? "/" : "", te->name) == -1)
681 return
682 got_error_from_errno("asprintf");
683 } else {
684 if (asprintf(&l2, "%s%s%s", label2,
685 label2[0] ? "/" : "", te2->name) == -1)
686 return
687 got_error_from_errno("asprintf");
689 err = diff_entry_new_old(te2, te, l2, repo,
690 cb, cb_arg, diff_content);
691 if (err)
692 break;
695 free(l1);
696 l1 = NULL;
697 if (te1) {
698 tidx1++;
699 te1 = got_object_tree_get_entry(tree1, tidx1);
700 if (te1 &&
701 asprintf(&l1, "%s%s%s", label1,
702 label1[0] ? "/" : "", te1->name) == -1)
703 return got_error_from_errno("asprintf");
705 free(l2);
706 l2 = NULL;
707 if (te2) {
708 tidx2++;
709 te2 = got_object_tree_get_entry(tree2, tidx2);
710 if (te2 &&
711 asprintf(&l2, "%s%s%s", label2,
712 label2[0] ? "/" : "", te2->name) == -1)
713 return got_error_from_errno("asprintf");
715 } while (te1 || te2);
717 return err;
720 const struct got_error *
721 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
722 struct got_object_id *id1, struct got_object_id *id2,
723 const char *label1, const char *label2, int diff_context,
724 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
726 const struct got_error *err;
727 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
729 if (id1 == NULL && id2 == NULL)
730 return got_error(GOT_ERR_NO_OBJ);
732 if (id1) {
733 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
734 if (err)
735 goto done;
737 if (id2) {
738 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
739 if (err)
740 goto done;
742 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
743 label1, label2, diff_context, ignore_whitespace, outfile);
744 done:
745 if (blob1)
746 got_object_blob_close(blob1);
747 if (blob2)
748 got_object_blob_close(blob2);
749 return err;
752 const struct got_error *
753 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
754 struct got_object_id *id1, struct got_object_id *id2,
755 char *label1, char *label2, int diff_context, int ignore_whitespace,
756 struct got_repository *repo, FILE *outfile)
758 const struct got_error *err;
759 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
760 struct got_diff_blob_output_unidiff_arg arg;
761 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
763 if (id1 == NULL && id2 == NULL)
764 return got_error(GOT_ERR_NO_OBJ);
766 if (id1) {
767 err = got_object_open_as_tree(&tree1, repo, id1);
768 if (err)
769 goto done;
771 if (id2) {
772 err = got_object_open_as_tree(&tree2, repo, id2);
773 if (err)
774 goto done;
776 arg.diff_context = diff_context;
777 arg.ignore_whitespace = ignore_whitespace;
778 arg.outfile = outfile;
779 if (want_lineoffsets) {
780 arg.line_offsets = *line_offsets;
781 arg.nlines = *nlines;
782 } else {
783 arg.line_offsets = NULL;
784 arg.nlines = 0;
786 err = got_diff_tree(tree1, tree2, label1, label2, repo,
787 got_diff_blob_output_unidiff, &arg, 1);
789 if (want_lineoffsets) {
790 *line_offsets = arg.line_offsets; /* was likely re-allocated */
791 *nlines = arg.nlines;
793 done:
794 if (tree1)
795 got_object_tree_close(tree1);
796 if (tree2)
797 got_object_tree_close(tree2);
798 return err;
801 const struct got_error *
802 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
803 struct got_object_id *id1, struct got_object_id *id2,
804 int diff_context, int ignore_whitespace,
805 struct got_repository *repo, FILE *outfile)
807 const struct got_error *err;
808 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
810 if (id2 == NULL)
811 return got_error(GOT_ERR_NO_OBJ);
813 if (id1) {
814 err = got_object_open_as_commit(&commit1, repo, id1);
815 if (err)
816 goto done;
819 err = got_object_open_as_commit(&commit2, repo, id2);
820 if (err)
821 goto done;
823 err = got_diff_objects_as_trees(line_offsets, nlines,
824 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
825 got_object_commit_get_tree_id(commit2), "", "", diff_context,
826 ignore_whitespace, repo, outfile);
827 done:
828 if (commit1)
829 got_object_commit_close(commit1);
830 if (commit2)
831 got_object_commit_close(commit2);
832 return err;
835 const struct got_error *
836 got_diff_files(struct got_diffreg_result **resultp,
837 FILE *f1, const char *label1, FILE *f2, const char *label2,
838 int diff_context, int ignore_whitespace, FILE *outfile)
840 const struct got_error *err = NULL;
841 struct got_diffreg_result *diffreg_result = NULL;
843 if (resultp)
844 *resultp = NULL;
846 if (outfile) {
847 fprintf(outfile, "file - %s\n",
848 f1 == NULL ? "/dev/null" : label1);
849 fprintf(outfile, "file + %s\n",
850 f2 == NULL ? "/dev/null" : label2);
853 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
854 ignore_whitespace);
855 if (err)
856 goto done;
858 if (outfile) {
859 err = got_diffreg_output(NULL, NULL, diffreg_result,
860 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
861 diff_context, outfile);
862 if (err)
863 goto done;
866 done:
867 if (resultp && err == NULL)
868 *resultp = diffreg_result;
869 else if (diffreg_result) {
870 const struct got_error *free_err;
871 free_err = got_diffreg_result_free(diffreg_result);
872 if (free_err && err == NULL)
873 err = free_err;
876 return err;