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_path.h"
32 #include "got_cancel.h"
33 #include "got_worktree.h"
34 #include "got_opentemp.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, FILE *f1, FILE *f2,
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 char hex1[SHA1_DIGEST_STRING_LENGTH];
64 char hex2[SHA1_DIGEST_STRING_LENGTH];
65 const char *idstr1 = NULL, *idstr2 = NULL;
66 off_t size1, size2;
67 struct got_diffreg_result *result;
68 off_t outoff = 0;
69 int n;
71 if (line_offsets && *line_offsets && *nlines > 0)
72 outoff = (*line_offsets)[*nlines - 1];
73 else if (line_offsets) {
74 err = add_line_offset(line_offsets, nlines, 0);
75 if (err)
76 goto done;
77 }
79 if (resultp)
80 *resultp = NULL;
82 if (f1) {
83 err = got_opentemp_truncate(f1);
84 if (err)
85 goto done;
86 }
87 if (f2) {
88 err = got_opentemp_truncate(f2);
89 if (err)
90 goto done;
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,
170 blob1 != NULL, blob2 != NULL,
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 return err;
189 const struct got_error *
190 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
191 struct got_blob_object *blob2, FILE *f1, FILE *f2,
192 struct got_object_id *id1, struct got_object_id *id2,
193 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
194 struct got_repository *repo)
196 struct got_diff_blob_output_unidiff_arg *a = arg;
198 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
199 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
200 a->ignore_whitespace, a->force_text_diff, a->outfile);
203 const struct got_error *
204 got_diff_blob(off_t **line_offsets, size_t *nlines,
205 struct got_blob_object *blob1, struct got_blob_object *blob2,
206 FILE *f1, FILE *f2, const char *label1, const char *label2,
207 int diff_context, int ignore_whitespace, int force_text_diff,
208 FILE *outfile)
210 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
211 label1, label2, 0, 0, diff_context, ignore_whitespace,
212 force_text_diff, outfile);
215 static const struct got_error *
216 diff_blob_file(struct got_diffreg_result **resultp,
217 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
218 FILE *f2, size_t size2, const char *label2, int diff_context,
219 int ignore_whitespace, int force_text_diff, FILE *outfile)
221 const struct got_error *err = NULL, *free_err;
222 char hex1[SHA1_DIGEST_STRING_LENGTH];
223 const char *idstr1 = NULL;
224 struct got_diffreg_result *result = NULL;
226 if (resultp)
227 *resultp = NULL;
229 if (blob1)
230 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
231 else
232 idstr1 = "/dev/null";
234 if (outfile) {
235 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 fprintf(outfile, "file + %s\n",
237 f2 == NULL ? "/dev/null" : label2);
240 err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
241 ignore_whitespace, force_text_diff);
242 if (err)
243 goto done;
245 if (outfile) {
246 err = got_diffreg_output(NULL, NULL, result,
247 f1 != NULL, f2 != NULL,
248 label2, /* show local file's path, not a blob ID */
249 label2, GOT_DIFF_OUTPUT_UNIDIFF,
250 diff_context, outfile);
251 if (err)
252 goto done;
255 if (resultp && err == NULL)
256 *resultp = result;
257 else if (result) {
258 free_err = got_diffreg_result_free(result);
259 if (free_err && err == NULL)
260 err = free_err;
262 done:
263 return err;
266 const struct got_error *
267 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
268 const char *label1, FILE *f2, size_t size2, const char *label2,
269 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
271 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
272 diff_context, ignore_whitespace, force_text_diff, outfile);
275 static const struct got_error *
276 diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
277 mode_t mode, struct got_repository *repo,
278 got_diff_blob_cb cb, void *cb_arg)
280 const struct got_error *err;
281 struct got_blob_object *blob = NULL;
282 struct got_object *obj = NULL;
283 int fd = -1;
285 err = got_object_open(&obj, repo, id);
286 if (err)
287 return err;
289 fd = got_opentempfd();
290 if (fd == -1) {
291 err = got_error_from_errno("got_opentempfd");
292 goto done;
295 err = got_object_blob_open(&blob, repo, obj, 8192, fd);
296 if (err)
297 goto done;
298 err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
299 NULL, label, 0, mode, repo);
300 done:
301 got_object_close(obj);
302 if (fd != -1 && close(fd) == -1 && err == NULL)
303 err = got_error_from_errno("close");
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 FILE *f1, FILE *f2, const char *label1, const char *label2,
312 mode_t mode1, mode_t mode2, struct got_repository *repo,
313 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;
320 int fd1 = -1, fd2 = -1;
322 err = got_object_open(&obj1, repo, id1);
323 if (err)
324 return err;
326 fd1 = got_opentempfd();
327 if (fd1 == -1) {
328 err = got_error_from_errno("got_opentempfd");
329 goto done;
331 fd2 = got_opentempfd();
332 if (fd2 == -1) {
333 err = got_error_from_errno("got_opentempfd");
334 goto done;
337 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
339 goto done;
342 err = got_object_open(&obj2, repo, id2);
343 if (err)
344 goto done;
345 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 goto done;
350 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
351 if (err)
352 goto done;
354 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
355 if (err)
356 goto done;
358 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
359 mode1, mode2, repo);
360 done:
361 if (obj1)
362 got_object_close(obj1);
363 if (obj2)
364 got_object_close(obj2);
365 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
366 err = got_error_from_errno("close");
367 if (blob1)
368 got_object_blob_close(blob1);
369 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
370 err = got_error_from_errno("close");
371 if (blob2)
372 got_object_blob_close(blob2);
373 return err;
376 static const struct got_error *
377 diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
378 mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
380 const struct got_error *err;
381 struct got_blob_object *blob = NULL;
382 struct got_object *obj = NULL;
383 int fd = -1;
385 fd = got_opentempfd();
386 if (fd == -1)
387 return got_error_from_errno("got_opentempfd");
389 err = got_object_open(&obj, repo, id);
390 if (err)
391 return err;
393 err = got_object_blob_open(&blob, repo, obj, 8192, fd);
394 if (err)
395 goto done;
396 err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
397 mode, 0, repo);
398 done:
399 got_object_close(obj);
400 if (fd != -1 && close(fd) == -1 && err == NULL)
401 err = got_error_from_errno("close");
402 if (blob)
403 got_object_blob_close(blob);
404 return err;
407 static const struct got_error *
408 diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
409 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
410 int diff_content)
412 const struct got_error *err = NULL;
413 struct got_object *treeobj = NULL;
414 struct got_tree_object *tree = NULL;
416 err = got_object_open(&treeobj, repo, id);
417 if (err)
418 goto done;
420 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
421 err = got_error(GOT_ERR_OBJ_TYPE);
422 goto done;
425 err = got_object_tree_open(&tree, repo, treeobj);
426 if (err)
427 goto done;
429 err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
430 repo, cb, cb_arg, diff_content);
431 done:
432 if (tree)
433 got_object_tree_close(tree);
434 if (treeobj)
435 got_object_close(treeobj);
436 return err;
439 static const struct got_error *
440 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
441 FILE *f1, FILE *f2, const char *label1, const char *label2,
442 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
443 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, f1, f2, label1, label2,
478 repo, cb, cb_arg, 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, FILE *f, 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, f, NULL, label, NULL,
515 repo, cb, cb_arg, 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, struct got_tree_entry *te2,
535 FILE *f1, FILE *f2, 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, f1, label1, repo,
548 cb, cb_arg, diff_content);
549 else {
550 if (diff_content)
551 err = diff_deleted_blob(&te1->id, f1, label1,
552 te1->mode, repo, cb, cb_arg);
553 else
554 err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 &te1->id, NULL, label1, NULL,
556 te1->mode, 0, repo);
558 return err;
559 } else if (got_object_tree_entry_is_submodule(te2))
560 return NULL;
562 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
563 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
564 if (!id_match)
565 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
566 label1, label2, repo, cb, cb_arg, diff_content);
567 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
568 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
569 if (!id_match ||
570 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
571 (te2->mode & (S_IFLNK | S_IXUSR))) {
572 if (diff_content)
573 return diff_modified_blob(&te1->id, &te2->id,
574 f1, f2, label1, label2, te1->mode,
575 te2->mode, repo, cb, cb_arg);
576 else
577 return cb(cb_arg, NULL, NULL, NULL, NULL,
578 &te1->id, &te2->id, label1, label2,
579 te1->mode, te2->mode, repo);
583 if (id_match)
584 return NULL;
586 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
587 cb, cb_arg);
590 static const struct got_error *
591 diff_entry_new_old(struct got_tree_entry *te2,
592 struct got_tree_entry *te1, FILE *f2, const char *label2,
593 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
594 int diff_content)
596 if (te1 != NULL) /* handled by diff_entry_old_new() */
597 return NULL;
599 if (got_object_tree_entry_is_submodule(te2))
600 return NULL;
602 if (S_ISDIR(te2->mode))
603 return diff_added_tree(&te2->id, f2, label2,
604 repo, cb, cb_arg, diff_content);
606 if (diff_content)
607 return diff_added_blob(&te2->id, f2, label2, te2->mode,
608 repo, cb, cb_arg);
610 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
611 NULL, label2, 0, te2->mode, repo);
614 const struct got_error *
615 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
616 struct got_blob_object *blob2, FILE *f1, FILE *f2,
617 struct got_object_id *id1, struct got_object_id *id2,
618 const char *label1, const char *label2,
619 mode_t mode1, mode_t mode2, struct got_repository *repo)
621 const struct got_error *err = NULL;
622 struct got_pathlist_head *paths = arg;
623 struct got_diff_changed_path *change = NULL;
624 char *path = NULL;
626 path = strdup(label2 ? label2 : label1);
627 if (path == NULL)
628 return got_error_from_errno("malloc");
630 change = malloc(sizeof(*change));
631 if (change == NULL) {
632 err = got_error_from_errno("malloc");
633 goto done;
636 change->status = GOT_STATUS_NO_CHANGE;
637 if (id1 == NULL)
638 change->status = GOT_STATUS_ADD;
639 else if (id2 == NULL)
640 change->status = GOT_STATUS_DELETE;
641 else {
642 if (got_object_id_cmp(id1, id2) != 0)
643 change->status = GOT_STATUS_MODIFY;
644 else if (mode1 != mode2)
645 change->status = GOT_STATUS_MODE_CHANGE;
648 err = got_pathlist_append(paths, path, change);
649 done:
650 if (err) {
651 free(path);
652 free(change);
654 return err;
657 const struct got_error *
658 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
659 FILE *f1, FILE *f2, const char *label1, const char *label2,
660 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
661 int diff_content)
663 const struct got_error *err = NULL;
664 struct got_tree_entry *te1 = NULL;
665 struct got_tree_entry *te2 = NULL;
666 char *l1 = NULL, *l2 = NULL;
667 int tidx1 = 0, tidx2 = 0;
669 if (tree1) {
670 te1 = got_object_tree_get_entry(tree1, 0);
671 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
672 te1->name) == -1)
673 return got_error_from_errno("asprintf");
675 if (tree2) {
676 te2 = got_object_tree_get_entry(tree2, 0);
677 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
678 te2->name) == -1)
679 return got_error_from_errno("asprintf");
682 do {
683 if (te1) {
684 struct got_tree_entry *te = NULL;
685 if (tree2)
686 te = got_object_tree_find_entry(tree2,
687 te1->name);
688 if (te) {
689 free(l2);
690 l2 = NULL;
691 if (te && asprintf(&l2, "%s%s%s", label2,
692 label2[0] ? "/" : "", te->name) == -1)
693 return
694 got_error_from_errno("asprintf");
696 err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
697 repo, cb, cb_arg, diff_content);
698 if (err)
699 break;
702 if (te2) {
703 struct got_tree_entry *te = NULL;
704 if (tree1)
705 te = got_object_tree_find_entry(tree1,
706 te2->name);
707 free(l2);
708 if (te) {
709 if (asprintf(&l2, "%s%s%s", label2,
710 label2[0] ? "/" : "", te->name) == -1)
711 return
712 got_error_from_errno("asprintf");
713 } else {
714 if (asprintf(&l2, "%s%s%s", label2,
715 label2[0] ? "/" : "", te2->name) == -1)
716 return
717 got_error_from_errno("asprintf");
719 err = diff_entry_new_old(te2, te, f2, l2, repo,
720 cb, cb_arg, diff_content);
721 if (err)
722 break;
725 free(l1);
726 l1 = NULL;
727 if (te1) {
728 tidx1++;
729 te1 = got_object_tree_get_entry(tree1, tidx1);
730 if (te1 &&
731 asprintf(&l1, "%s%s%s", label1,
732 label1[0] ? "/" : "", te1->name) == -1)
733 return got_error_from_errno("asprintf");
735 free(l2);
736 l2 = NULL;
737 if (te2) {
738 tidx2++;
739 te2 = got_object_tree_get_entry(tree2, tidx2);
740 if (te2 &&
741 asprintf(&l2, "%s%s%s", label2,
742 label2[0] ? "/" : "", te2->name) == -1)
743 return got_error_from_errno("asprintf");
745 } while (te1 || te2);
747 return err;
750 const struct got_error *
751 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
752 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
753 const char *label1, const char *label2, int diff_context,
754 int ignore_whitespace, int force_text_diff,
755 struct got_repository *repo, FILE *outfile)
757 const struct got_error *err;
758 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
759 int fd1 = -1, fd2 = -1;
761 if (id1 == NULL && id2 == NULL)
762 return got_error(GOT_ERR_NO_OBJ);
764 fd1 = got_opentempfd();
765 if (fd1 == -1)
766 return got_error_from_errno("got_opentempfd");
767 fd2 = got_opentempfd();
768 if (fd2 == -1) {
769 err = got_error_from_errno("got_opentempfd");
770 goto done;
773 if (id1) {
774 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
775 if (err)
776 goto done;
778 if (id2) {
779 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
780 if (err)
781 goto done;
783 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
784 label1, label2, diff_context, ignore_whitespace, force_text_diff,
785 outfile);
786 done:
787 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
788 err = got_error_from_errno("close");
789 if (blob1)
790 got_object_blob_close(blob1);
791 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
792 err = got_error_from_errno("close");
793 if (blob2)
794 got_object_blob_close(blob2);
795 return err;
798 static const struct got_error *
799 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
800 FILE *f1, FILE *f2, struct got_pathlist_head *paths,
801 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
803 const struct got_error *err = NULL;
804 struct got_pathlist_entry *pe;
805 struct got_object_id *id1 = NULL, *id2 = NULL;
806 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
807 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
808 int fd1 = -1, fd2 = -1;
810 fd1 = got_opentempfd();
811 if (fd1 == -1)
812 return got_error_from_errno("got_opentempfd");
813 fd2 = got_opentempfd();
814 if (fd2 == -1) {
815 err = got_error_from_errno("got_opentempfd");
816 goto done;
818 TAILQ_FOREACH(pe, paths, entry) {
819 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
820 mode_t mode1 = 0, mode2 = 0;
822 free(id1);
823 id1 = NULL;
824 free(id2);
825 id2 = NULL;
826 if (subtree1) {
827 got_object_tree_close(subtree1);
828 subtree1 = NULL;
830 if (subtree2) {
831 got_object_tree_close(subtree2);
832 subtree2 = NULL;
834 if (blob1) {
835 got_object_blob_close(blob1);
836 blob1 = NULL;
838 if (blob2) {
839 got_object_blob_close(blob2);
840 blob2 = NULL;
843 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
844 pe->path);
845 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
846 goto done;
847 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
848 pe->path);
849 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
850 goto done;
851 if (id1 == NULL && id2 == NULL) {
852 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
853 goto done;
855 if (id1) {
856 err = got_object_get_type(&type1, repo, id1);
857 if (err)
858 goto done;
860 if (id2) {
861 err = got_object_get_type(&type2, repo, id2);
862 if (err)
863 goto done;
865 if (type1 == GOT_OBJ_TYPE_ANY &&
866 type2 == GOT_OBJ_TYPE_ANY) {
867 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
868 goto done;
869 } else if (type1 != GOT_OBJ_TYPE_ANY &&
870 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
871 err = got_error(GOT_ERR_OBJ_TYPE);
872 goto done;
875 if (type1 == GOT_OBJ_TYPE_BLOB ||
876 type2 == GOT_OBJ_TYPE_BLOB) {
877 if (id1) {
878 err = got_object_open_as_blob(&blob1, repo,
879 id1, 8192, fd1);
880 if (err)
881 goto done;
883 if (id2) {
884 err = got_object_open_as_blob(&blob2, repo,
885 id2, 8192, fd2);
886 if (err)
887 goto done;
889 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
890 id1 ? pe->path : "/dev/null",
891 id2 ? pe->path : "/dev/null",
892 mode1, mode2, repo);
893 if (err)
894 goto done;
895 } else if (type1 == GOT_OBJ_TYPE_TREE ||
896 type2 == GOT_OBJ_TYPE_TREE) {
897 if (id1) {
898 err = got_object_open_as_tree(&subtree1, repo,
899 id1);
900 if (err)
901 goto done;
903 if (id2) {
904 err = got_object_open_as_tree(&subtree2, repo,
905 id2);
906 if (err)
907 goto done;
909 err = got_diff_tree(subtree1, subtree2, f1, f2,
910 id1 ? pe->path : "/dev/null",
911 id2 ? pe->path : "/dev/null",
912 repo, cb, cb_arg, 1);
913 if (err)
914 goto done;
915 } else {
916 err = got_error(GOT_ERR_OBJ_TYPE);
917 goto done;
919 if (ftruncate(fd1, 0L) == -1)
920 return got_error_from_errno("ftruncate");
921 if (ftruncate(fd2, 0L) == -1)
922 return got_error_from_errno("ftruncate");
924 done:
925 free(id1);
926 free(id2);
927 if (subtree1)
928 got_object_tree_close(subtree1);
929 if (subtree2)
930 got_object_tree_close(subtree2);
931 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
932 err = got_error_from_errno("close");
933 if (blob1)
934 got_object_blob_close(blob1);
935 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
936 err = got_error_from_errno("close");
937 if (blob2)
938 got_object_blob_close(blob2);
939 return err;
942 static const struct got_error *
943 show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
944 int ch, const char *id_str, FILE *outfile)
946 const struct got_error *err;
947 int n;
948 off_t outoff = 0;
950 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
951 if (line_offsets != NULL && *line_offsets != NULL) {
952 if (*nlines == 0) {
953 err = add_line_offset(line_offsets, nlines, 0);
954 if (err)
955 return err;
956 } else
957 outoff = (*line_offsets)[*nlines - 1];
959 outoff += n;
960 err = add_line_offset(line_offsets, nlines, outoff);
961 if (err)
962 return err;
965 return NULL;
968 static const struct got_error *
969 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
970 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
971 struct got_pathlist_head *paths, const char *label1, const char *label2,
972 int diff_context, int ignore_whitespace, int force_text_diff,
973 struct got_repository *repo, FILE *outfile)
975 const struct got_error *err;
976 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
977 struct got_diff_blob_output_unidiff_arg arg;
978 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
980 if (id1 == NULL && id2 == NULL)
981 return got_error(GOT_ERR_NO_OBJ);
983 if (id1) {
984 err = got_object_open_as_tree(&tree1, repo, id1);
985 if (err)
986 goto done;
988 if (id2) {
989 err = got_object_open_as_tree(&tree2, repo, id2);
990 if (err)
991 goto done;
994 arg.diff_context = diff_context;
995 arg.ignore_whitespace = ignore_whitespace;
996 arg.force_text_diff = force_text_diff;
997 arg.outfile = outfile;
998 if (want_lineoffsets) {
999 arg.line_offsets = *line_offsets;
1000 arg.nlines = *nlines;
1001 } else {
1002 arg.line_offsets = NULL;
1003 arg.nlines = 0;
1005 if (paths == NULL || TAILQ_EMPTY(paths)) {
1006 err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
1007 repo, got_diff_blob_output_unidiff, &arg, 1);
1008 } else {
1009 err = diff_paths(tree1, tree2, f1, f2, paths, repo,
1010 got_diff_blob_output_unidiff, &arg);
1012 if (want_lineoffsets) {
1013 *line_offsets = arg.line_offsets; /* was likely re-allocated */
1014 *nlines = arg.nlines;
1016 done:
1017 if (tree1)
1018 got_object_tree_close(tree1);
1019 if (tree2)
1020 got_object_tree_close(tree2);
1021 return err;
1024 const struct got_error *
1025 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
1026 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1027 struct got_pathlist_head *paths, const char *label1, const char *label2,
1028 int diff_context, int ignore_whitespace, int force_text_diff,
1029 struct got_repository *repo, FILE *outfile)
1031 const struct got_error *err;
1032 char *idstr = NULL;
1034 if (id1 == NULL && id2 == NULL)
1035 return got_error(GOT_ERR_NO_OBJ);
1037 if (id1) {
1038 err = got_object_id_str(&idstr, id1);
1039 if (err)
1040 goto done;
1041 err = show_object_id(line_offsets, nlines, "tree", '-',
1042 idstr, outfile);
1043 if (err)
1044 goto done;
1045 free(idstr);
1046 idstr = NULL;
1047 } else {
1048 err = show_object_id(line_offsets, nlines, "tree", '-',
1049 "/dev/null", outfile);
1050 if (err)
1051 goto done;
1054 if (id2) {
1055 err = got_object_id_str(&idstr, id2);
1056 if (err)
1057 goto done;
1058 err = show_object_id(line_offsets, nlines, "tree", '+',
1059 idstr, outfile);
1060 if (err)
1061 goto done;
1062 free(idstr);
1063 idstr = NULL;
1064 } else {
1065 err = show_object_id(line_offsets, nlines, "tree", '+',
1066 "/dev/null", outfile);
1067 if (err)
1068 goto done;
1071 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
1072 paths, label1, label2, diff_context, ignore_whitespace,
1073 force_text_diff, repo, outfile);
1074 done:
1075 free(idstr);
1076 return err;
1079 const struct got_error *
1080 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1081 FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1082 struct got_pathlist_head *paths,
1083 int diff_context, int ignore_whitespace, int force_text_diff,
1084 struct got_repository *repo, FILE *outfile)
1086 const struct got_error *err;
1087 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1088 char *idstr = NULL;
1090 if (id2 == NULL)
1091 return got_error(GOT_ERR_NO_OBJ);
1093 if (id1) {
1094 err = got_object_open_as_commit(&commit1, repo, id1);
1095 if (err)
1096 goto done;
1097 err = got_object_id_str(&idstr, id1);
1098 if (err)
1099 goto done;
1100 err = show_object_id(line_offsets, nlines, "commit", '-',
1101 idstr, outfile);
1102 if (err)
1103 goto done;
1104 free(idstr);
1105 idstr = NULL;
1106 } else {
1107 err = show_object_id(line_offsets, nlines, "commit", '-',
1108 "/dev/null", outfile);
1109 if (err)
1110 goto done;
1113 err = got_object_open_as_commit(&commit2, repo, id2);
1114 if (err)
1115 goto done;
1117 err = got_object_id_str(&idstr, id2);
1118 if (err)
1119 goto done;
1120 err = show_object_id(line_offsets, nlines, "commit", '+',
1121 idstr, outfile);
1122 if (err)
1123 goto done;
1125 err = diff_objects_as_trees(line_offsets, nlines, f1, f2,
1126 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1127 got_object_commit_get_tree_id(commit2), paths, "", "",
1128 diff_context, ignore_whitespace, force_text_diff, repo, outfile);
1129 done:
1130 if (commit1)
1131 got_object_commit_close(commit1);
1132 if (commit2)
1133 got_object_commit_close(commit2);
1134 free(idstr);
1135 return err;
1138 const struct got_error *
1139 got_diff_files(struct got_diffreg_result **resultp,
1140 FILE *f1, const char *label1, FILE *f2, const char *label2,
1141 int diff_context, int ignore_whitespace, int force_text_diff,
1142 FILE *outfile)
1144 const struct got_error *err = NULL;
1145 struct got_diffreg_result *diffreg_result = NULL;
1147 if (resultp)
1148 *resultp = NULL;
1150 if (outfile) {
1151 fprintf(outfile, "file - %s\n",
1152 f1 == NULL ? "/dev/null" : label1);
1153 fprintf(outfile, "file + %s\n",
1154 f2 == NULL ? "/dev/null" : label2);
1157 err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1158 ignore_whitespace, force_text_diff);
1159 if (err)
1160 goto done;
1162 if (outfile) {
1163 err = got_diffreg_output(NULL, NULL, diffreg_result,
1164 f1 != NULL, f2 != NULL, label1, label2,
1165 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1166 if (err)
1167 goto done;
1170 done:
1171 if (resultp && err == NULL)
1172 *resultp = diffreg_result;
1173 else if (diffreg_result) {
1174 const struct got_error *free_err;
1175 free_err = got_diffreg_result_free(diffreg_result);
1176 if (free_err && err == NULL)
1177 err = free_err;
1180 return err;