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 fclose(f1);
89 return err;
90 }
91 }
93 size1 = 0;
94 if (blob1) {
95 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
96 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
97 blob1);
98 if (err)
99 goto done;
100 } else
101 idstr1 = "/dev/null";
103 size2 = 0;
104 if (blob2) {
105 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
106 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
107 blob2);
108 if (err)
109 goto done;
110 } else
111 idstr2 = "/dev/null";
113 if (outfile) {
114 char *modestr1 = NULL, *modestr2 = NULL;
115 int modebits;
116 if (mode1 && mode1 != mode2) {
117 if (S_ISLNK(mode1))
118 modebits = S_IFLNK;
119 else
120 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
121 if (asprintf(&modestr1, " (mode %o)",
122 mode1 & modebits) == -1) {
123 err = got_error_from_errno("asprintf");
124 goto done;
127 if (mode2 && mode1 != mode2) {
128 if (S_ISLNK(mode2))
129 modebits = S_IFLNK;
130 else
131 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
132 if (asprintf(&modestr2, " (mode %o)",
133 mode2 & modebits) == -1) {
134 err = got_error_from_errno("asprintf");
135 goto done;
138 n = fprintf(outfile, "blob - %s%s\n", idstr1,
139 modestr1 ? modestr1 : "");
140 if (n < 0)
141 goto done;
142 outoff += n;
143 if (line_offsets) {
144 err = add_line_offset(line_offsets, nlines, outoff);
145 if (err)
146 goto done;
149 n = fprintf(outfile, "blob + %s%s\n", idstr2,
150 modestr2 ? modestr2 : "");
151 if (n < 0)
152 goto done;
153 outoff += n;
154 if (line_offsets) {
155 err = add_line_offset(line_offsets, nlines, outoff);
156 if (err)
157 goto done;
160 free(modestr1);
161 free(modestr2);
163 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
164 ignore_whitespace, force_text_diff);
165 if (err)
166 goto done;
168 if (outfile) {
169 err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
170 label1 ? label1 : idstr1,
171 label2 ? label2 : idstr2,
172 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
173 if (err)
174 goto done;
177 if (resultp && err == NULL)
178 *resultp = result;
179 else {
180 free_err = got_diffreg_result_free(result);
181 if (free_err && err == NULL)
182 err = free_err;
184 done:
185 if (f1 && fclose(f1) != 0 && err == NULL)
186 err = got_error_from_errno("fclose");
187 if (f2 && fclose(f2) != 0 && err == NULL)
188 err = got_error_from_errno("fclose");
189 return err;
192 const struct got_error *
193 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
194 struct got_blob_object *blob2, struct got_object_id *id1,
195 struct got_object_id *id2, const char *label1, const char *label2,
196 mode_t mode1, mode_t mode2, struct got_repository *repo)
198 struct got_diff_blob_output_unidiff_arg *a = arg;
200 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
201 blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
202 a->ignore_whitespace, a->force_text_diff, a->outfile);
205 const struct got_error *
206 got_diff_blob(off_t **line_offsets, size_t *nlines,
207 struct got_blob_object *blob1, struct got_blob_object *blob2,
208 const char *label1, const char *label2, int diff_context,
209 int ignore_whitespace, int force_text_diff, FILE *outfile)
211 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
212 label1, label2, 0, 0, diff_context, ignore_whitespace,
213 force_text_diff, 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,
220 int force_text_diff, FILE *outfile)
222 const struct got_error *err = NULL, *free_err;
223 FILE *f1 = NULL;
224 char hex1[SHA1_DIGEST_STRING_LENGTH];
225 char *idstr1 = NULL;
226 off_t size1;
227 struct got_diffreg_result *result = NULL;
229 if (resultp)
230 *resultp = NULL;
232 size1 = 0;
233 if (blob1) {
234 f1 = got_opentemp();
235 if (f1 == NULL)
236 return got_error_from_errno("got_opentemp");
237 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
238 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
239 blob1);
240 if (err)
241 goto done;
242 } else {
243 idstr1 = "/dev/null";
246 if (outfile) {
247 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
248 fprintf(outfile, "file + %s\n",
249 f2 == NULL ? "/dev/null" : label2);
252 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
253 ignore_whitespace, force_text_diff);
254 if (err)
255 goto done;
257 if (outfile) {
258 err = got_diffreg_output(NULL, NULL, result, f1, f2,
259 label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
260 outfile);
261 if (err)
262 goto done;
265 if (resultp && err == NULL)
266 *resultp = result;
267 else if (result) {
268 free_err = got_diffreg_result_free(result);
269 if (free_err && err == NULL)
270 err = free_err;
272 done:
273 if (f1 && fclose(f1) != 0 && err == NULL)
274 err = got_error_from_errno("fclose");
275 return err;
278 const struct got_error *
279 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
280 FILE *f2, size_t size2, const char *label2, int diff_context,
281 int ignore_whitespace, int force_text_diff, FILE *outfile)
283 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
284 diff_context, ignore_whitespace, force_text_diff, outfile);
287 static const struct got_error *
288 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
289 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
291 const struct got_error *err;
292 struct got_blob_object *blob = NULL;
293 struct got_object *obj = NULL;
295 err = got_object_open(&obj, repo, id);
296 if (err)
297 return err;
299 err = got_object_blob_open(&blob, repo, obj, 8192);
300 if (err)
301 goto done;
302 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
303 done:
304 got_object_close(obj);
305 if (blob)
306 got_object_blob_close(blob);
307 return err;
310 static const struct got_error *
311 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
312 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
313 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
315 const struct got_error *err;
316 struct got_object *obj1 = NULL;
317 struct got_object *obj2 = NULL;
318 struct got_blob_object *blob1 = NULL;
319 struct got_blob_object *blob2 = NULL;
321 err = got_object_open(&obj1, repo, id1);
322 if (err)
323 return err;
324 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
325 err = got_error(GOT_ERR_OBJ_TYPE);
326 goto done;
329 err = got_object_open(&obj2, repo, id2);
330 if (err)
331 goto done;
332 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
333 err = got_error(GOT_ERR_BAD_OBJ_DATA);
334 goto done;
337 err = got_object_blob_open(&blob1, repo, obj1, 8192);
338 if (err)
339 goto done;
341 err = got_object_blob_open(&blob2, repo, obj2, 8192);
342 if (err)
343 goto done;
345 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
346 repo);
347 done:
348 if (obj1)
349 got_object_close(obj1);
350 if (obj2)
351 got_object_close(obj2);
352 if (blob1)
353 got_object_blob_close(blob1);
354 if (blob2)
355 got_object_blob_close(blob2);
356 return err;
359 static const struct got_error *
360 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
361 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
363 const struct got_error *err;
364 struct got_blob_object *blob = NULL;
365 struct got_object *obj = NULL;
367 err = got_object_open(&obj, repo, id);
368 if (err)
369 return err;
371 err = got_object_blob_open(&blob, repo, obj, 8192);
372 if (err)
373 goto done;
374 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
375 done:
376 got_object_close(obj);
377 if (blob)
378 got_object_blob_close(blob);
379 return err;
382 static const struct got_error *
383 diff_added_tree(struct got_object_id *id, const char *label,
384 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
385 int diff_content)
387 const struct got_error *err = NULL;
388 struct got_object *treeobj = NULL;
389 struct got_tree_object *tree = NULL;
391 err = got_object_open(&treeobj, repo, id);
392 if (err)
393 goto done;
395 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
396 err = got_error(GOT_ERR_OBJ_TYPE);
397 goto done;
400 err = got_object_tree_open(&tree, repo, treeobj);
401 if (err)
402 goto done;
404 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
405 diff_content);
406 done:
407 if (tree)
408 got_object_tree_close(tree);
409 if (treeobj)
410 got_object_close(treeobj);
411 return err;
414 static const struct got_error *
415 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
416 const char *label1, const char *label2, struct got_repository *repo,
417 got_diff_blob_cb cb, void *cb_arg, int diff_content)
419 const struct got_error *err;
420 struct got_object *treeobj1 = NULL;
421 struct got_object *treeobj2 = NULL;
422 struct got_tree_object *tree1 = NULL;
423 struct got_tree_object *tree2 = NULL;
425 err = got_object_open(&treeobj1, repo, id1);
426 if (err)
427 goto done;
429 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
430 err = got_error(GOT_ERR_OBJ_TYPE);
431 goto done;
434 err = got_object_open(&treeobj2, repo, id2);
435 if (err)
436 goto done;
438 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
439 err = got_error(GOT_ERR_OBJ_TYPE);
440 goto done;
443 err = got_object_tree_open(&tree1, repo, treeobj1);
444 if (err)
445 goto done;
447 err = got_object_tree_open(&tree2, repo, treeobj2);
448 if (err)
449 goto done;
451 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
452 diff_content);
454 done:
455 if (tree1)
456 got_object_tree_close(tree1);
457 if (tree2)
458 got_object_tree_close(tree2);
459 if (treeobj1)
460 got_object_close(treeobj1);
461 if (treeobj2)
462 got_object_close(treeobj2);
463 return err;
466 static const struct got_error *
467 diff_deleted_tree(struct got_object_id *id, const char *label,
468 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
469 int diff_content)
471 const struct got_error *err;
472 struct got_object *treeobj = NULL;
473 struct got_tree_object *tree = NULL;
475 err = got_object_open(&treeobj, repo, id);
476 if (err)
477 goto done;
479 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
480 err = got_error(GOT_ERR_OBJ_TYPE);
481 goto done;
484 err = got_object_tree_open(&tree, repo, treeobj);
485 if (err)
486 goto done;
488 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
489 diff_content);
490 done:
491 if (tree)
492 got_object_tree_close(tree);
493 if (treeobj)
494 got_object_close(treeobj);
495 return err;
498 static const struct got_error *
499 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
500 const char *label1, const char *label2, struct got_repository *repo,
501 got_diff_blob_cb cb, void *cb_arg)
503 /* XXX TODO */
504 return NULL;
507 static const struct got_error *
508 diff_entry_old_new(struct got_tree_entry *te1,
509 struct got_tree_entry *te2, const char *label1, const char *label2,
510 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
511 int diff_content)
513 const struct got_error *err = NULL;
514 int id_match;
516 if (got_object_tree_entry_is_submodule(te1))
517 return NULL;
519 if (te2 == NULL) {
520 if (S_ISDIR(te1->mode))
521 err = diff_deleted_tree(&te1->id, label1, repo,
522 cb, cb_arg, diff_content);
523 else {
524 if (diff_content)
525 err = diff_deleted_blob(&te1->id, label1,
526 te1->mode, repo, cb, cb_arg);
527 else
528 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
529 label1, NULL, te1->mode, 0, repo);
531 return err;
532 } else if (got_object_tree_entry_is_submodule(te2))
533 return NULL;
535 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
536 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
537 if (!id_match)
538 return diff_modified_tree(&te1->id, &te2->id,
539 label1, label2, repo, cb, cb_arg, diff_content);
540 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
541 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
542 if (!id_match ||
543 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
544 (te2->mode & (S_IFLNK | S_IXUSR))) {
545 if (diff_content)
546 return diff_modified_blob(&te1->id, &te2->id,
547 label1, label2, te1->mode, te2->mode,
548 repo, cb, cb_arg);
549 else
550 return cb(cb_arg, NULL, NULL, &te1->id,
551 &te2->id, label1, label2, te1->mode,
552 te2->mode, repo);
556 if (id_match)
557 return NULL;
559 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
560 cb, cb_arg);
563 static const struct got_error *
564 diff_entry_new_old(struct got_tree_entry *te2,
565 struct got_tree_entry *te1, const char *label2,
566 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
567 int diff_content)
569 if (te1 != NULL) /* handled by diff_entry_old_new() */
570 return NULL;
572 if (got_object_tree_entry_is_submodule(te2))
573 return NULL;
575 if (S_ISDIR(te2->mode))
576 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
577 diff_content);
579 if (diff_content)
580 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
581 cb_arg);
583 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
584 te2->mode, repo);
587 const struct got_error *
588 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
589 struct got_blob_object *blob2, struct got_object_id *id1,
590 struct got_object_id *id2, const char *label1, const char *label2,
591 mode_t mode1, mode_t mode2, struct got_repository *repo)
593 const struct got_error *err = NULL;
594 struct got_pathlist_head *paths = arg;
595 struct got_diff_changed_path *change = NULL;
596 char *path = NULL;
598 path = strdup(label2 ? label2 : label1);
599 if (path == NULL)
600 return got_error_from_errno("malloc");
602 change = malloc(sizeof(*change));
603 if (change == NULL) {
604 err = got_error_from_errno("malloc");
605 goto done;
608 change->status = GOT_STATUS_NO_CHANGE;
609 if (id1 == NULL)
610 change->status = GOT_STATUS_ADD;
611 else if (id2 == NULL)
612 change->status = GOT_STATUS_DELETE;
613 else {
614 if (got_object_id_cmp(id1, id2) != 0)
615 change->status = GOT_STATUS_MODIFY;
616 else if (mode1 != mode2)
617 change->status = GOT_STATUS_MODE_CHANGE;
620 err = got_pathlist_insert(NULL, paths, path, change);
621 done:
622 if (err) {
623 free(path);
624 free(change);
626 return err;
629 const struct got_error *
630 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
631 const char *label1, const char *label2, struct got_repository *repo,
632 got_diff_blob_cb cb, void *cb_arg, int diff_content)
634 const struct got_error *err = NULL;
635 struct got_tree_entry *te1 = NULL;
636 struct got_tree_entry *te2 = NULL;
637 char *l1 = NULL, *l2 = NULL;
638 int tidx1 = 0, tidx2 = 0;
640 if (tree1) {
641 te1 = got_object_tree_get_entry(tree1, 0);
642 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
643 te1->name) == -1)
644 return got_error_from_errno("asprintf");
646 if (tree2) {
647 te2 = got_object_tree_get_entry(tree2, 0);
648 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
649 te2->name) == -1)
650 return got_error_from_errno("asprintf");
653 do {
654 if (te1) {
655 struct got_tree_entry *te = NULL;
656 if (tree2)
657 te = got_object_tree_find_entry(tree2,
658 te1->name);
659 if (te) {
660 free(l2);
661 l2 = NULL;
662 if (te && asprintf(&l2, "%s%s%s", label2,
663 label2[0] ? "/" : "", te->name) == -1)
664 return
665 got_error_from_errno("asprintf");
667 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
668 cb_arg, diff_content);
669 if (err)
670 break;
673 if (te2) {
674 struct got_tree_entry *te = NULL;
675 if (tree1)
676 te = got_object_tree_find_entry(tree1,
677 te2->name);
678 free(l2);
679 if (te) {
680 if (asprintf(&l2, "%s%s%s", label2,
681 label2[0] ? "/" : "", te->name) == -1)
682 return
683 got_error_from_errno("asprintf");
684 } else {
685 if (asprintf(&l2, "%s%s%s", label2,
686 label2[0] ? "/" : "", te2->name) == -1)
687 return
688 got_error_from_errno("asprintf");
690 err = diff_entry_new_old(te2, te, l2, repo,
691 cb, cb_arg, diff_content);
692 if (err)
693 break;
696 free(l1);
697 l1 = NULL;
698 if (te1) {
699 tidx1++;
700 te1 = got_object_tree_get_entry(tree1, tidx1);
701 if (te1 &&
702 asprintf(&l1, "%s%s%s", label1,
703 label1[0] ? "/" : "", te1->name) == -1)
704 return got_error_from_errno("asprintf");
706 free(l2);
707 l2 = NULL;
708 if (te2) {
709 tidx2++;
710 te2 = got_object_tree_get_entry(tree2, tidx2);
711 if (te2 &&
712 asprintf(&l2, "%s%s%s", label2,
713 label2[0] ? "/" : "", te2->name) == -1)
714 return got_error_from_errno("asprintf");
716 } while (te1 || te2);
718 return err;
721 const struct got_error *
722 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
723 struct got_object_id *id1, struct got_object_id *id2,
724 const char *label1, const char *label2, int diff_context,
725 int ignore_whitespace, int force_text_diff,
726 struct got_repository *repo, FILE *outfile)
728 const struct got_error *err;
729 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
731 if (id1 == NULL && id2 == NULL)
732 return got_error(GOT_ERR_NO_OBJ);
734 if (id1) {
735 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
736 if (err)
737 goto done;
739 if (id2) {
740 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
741 if (err)
742 goto done;
744 err = got_diff_blob(line_offsets, nlines, blob1, blob2,
745 label1, label2, diff_context, ignore_whitespace, force_text_diff,
746 outfile);
747 done:
748 if (blob1)
749 got_object_blob_close(blob1);
750 if (blob2)
751 got_object_blob_close(blob2);
752 return err;
755 const struct got_error *
756 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
757 struct got_object_id *id1, struct got_object_id *id2,
758 char *label1, char *label2, int diff_context, int ignore_whitespace,
759 int force_text_diff, struct got_repository *repo, FILE *outfile)
761 const struct got_error *err;
762 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
763 struct got_diff_blob_output_unidiff_arg arg;
764 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
766 if (id1 == NULL && id2 == NULL)
767 return got_error(GOT_ERR_NO_OBJ);
769 if (id1) {
770 err = got_object_open_as_tree(&tree1, repo, id1);
771 if (err)
772 goto done;
774 if (id2) {
775 err = got_object_open_as_tree(&tree2, repo, id2);
776 if (err)
777 goto done;
779 arg.diff_context = diff_context;
780 arg.ignore_whitespace = ignore_whitespace;
781 arg.force_text_diff = force_text_diff;
782 arg.outfile = outfile;
783 if (want_lineoffsets) {
784 arg.line_offsets = *line_offsets;
785 arg.nlines = *nlines;
786 } else {
787 arg.line_offsets = NULL;
788 arg.nlines = 0;
790 err = got_diff_tree(tree1, tree2, label1, label2, repo,
791 got_diff_blob_output_unidiff, &arg, 1);
793 if (want_lineoffsets) {
794 *line_offsets = arg.line_offsets; /* was likely re-allocated */
795 *nlines = arg.nlines;
797 done:
798 if (tree1)
799 got_object_tree_close(tree1);
800 if (tree2)
801 got_object_tree_close(tree2);
802 return err;
805 const struct got_error *
806 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
807 struct got_object_id *id1, struct got_object_id *id2,
808 int diff_context, int ignore_whitespace, int force_text_diff,
809 struct got_repository *repo, FILE *outfile)
811 const struct got_error *err;
812 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
814 if (id2 == NULL)
815 return got_error(GOT_ERR_NO_OBJ);
817 if (id1) {
818 err = got_object_open_as_commit(&commit1, repo, id1);
819 if (err)
820 goto done;
823 err = got_object_open_as_commit(&commit2, repo, id2);
824 if (err)
825 goto done;
827 err = got_diff_objects_as_trees(line_offsets, nlines,
828 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
829 got_object_commit_get_tree_id(commit2), "", "", diff_context,
830 ignore_whitespace, force_text_diff, repo, outfile);
831 done:
832 if (commit1)
833 got_object_commit_close(commit1);
834 if (commit2)
835 got_object_commit_close(commit2);
836 return err;
839 const struct got_error *
840 got_diff_files(struct got_diffreg_result **resultp,
841 FILE *f1, const char *label1, FILE *f2, const char *label2,
842 int diff_context, int ignore_whitespace, int force_text_diff,
843 FILE *outfile)
845 const struct got_error *err = NULL;
846 struct got_diffreg_result *diffreg_result = NULL;
848 if (resultp)
849 *resultp = NULL;
851 if (outfile) {
852 fprintf(outfile, "file - %s\n",
853 f1 == NULL ? "/dev/null" : label1);
854 fprintf(outfile, "file + %s\n",
855 f2 == NULL ? "/dev/null" : label2);
858 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
859 ignore_whitespace, force_text_diff);
860 if (err)
861 goto done;
863 if (outfile) {
864 err = got_diffreg_output(NULL, NULL, diffreg_result,
865 f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
866 diff_context, outfile);
867 if (err)
868 goto done;
871 done:
872 if (resultp && err == NULL)
873 *resultp = diffreg_result;
874 else if (diffreg_result) {
875 const struct got_error *free_err;
876 free_err = got_diffreg_result_free(diffreg_result);
877 if (free_err && err == NULL)
878 err = free_err;
881 return err;