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 <zlib.h>
26 #include "got_compat.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
31 #include "got_diff.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_worktree.h"
35 #include "got_opentemp.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_metadata(struct got_diff_line **lines, size_t *nlines,
44 off_t off, uint8_t type)
45 {
46 struct got_diff_line *p;
48 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
49 if (p == NULL)
50 return got_error_from_errno("reallocarray");
51 *lines = p;
52 (*lines)[*nlines].offset = off;
53 (*lines)[*nlines].type = type;
54 (*nlines)++;
56 return NULL;
57 }
59 static const struct got_error *
60 diff_blobs(struct got_diff_line **lines, size_t *nlines,
61 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
62 struct got_blob_object *blob2, FILE *f1, FILE *f2,
63 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
64 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
65 enum got_diff_algorithm diff_algo)
66 {
67 const struct got_error *err = NULL, *free_err;
68 char hex1[SHA1_DIGEST_STRING_LENGTH];
69 char hex2[SHA1_DIGEST_STRING_LENGTH];
70 const char *idstr1 = NULL, *idstr2 = NULL;
71 off_t size1, size2;
72 struct got_diffreg_result *result;
73 off_t outoff = 0;
74 int n;
76 if (lines && *lines && *nlines > 0)
77 outoff = (*lines)[*nlines - 1].offset;
78 else if (lines) {
79 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
80 if (err)
81 goto done;
82 }
84 if (resultp)
85 *resultp = NULL;
87 if (f1) {
88 err = got_opentemp_truncate(f1);
89 if (err)
90 goto done;
91 }
92 if (f2) {
93 err = got_opentemp_truncate(f2);
94 if (err)
95 goto done;
96 }
98 size1 = 0;
99 if (blob1) {
100 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
101 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
102 blob1);
103 if (err)
104 goto done;
105 } else
106 idstr1 = "/dev/null";
108 size2 = 0;
109 if (blob2) {
110 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
111 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
112 blob2);
113 if (err)
114 goto done;
115 } else
116 idstr2 = "/dev/null";
118 if (outfile) {
119 char *modestr1 = NULL, *modestr2 = NULL;
120 int modebits;
121 if (mode1 && mode1 != mode2) {
122 if (S_ISLNK(mode1))
123 modebits = S_IFLNK;
124 else
125 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
126 if (asprintf(&modestr1, " (mode %o)",
127 mode1 & modebits) == -1) {
128 err = got_error_from_errno("asprintf");
129 goto done;
132 if (mode2 && mode1 != mode2) {
133 if (S_ISLNK(mode2))
134 modebits = S_IFLNK;
135 else
136 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
137 if (asprintf(&modestr2, " (mode %o)",
138 mode2 & modebits) == -1) {
139 err = got_error_from_errno("asprintf");
140 goto done;
143 n = fprintf(outfile, "blob - %s%s\n", idstr1,
144 modestr1 ? modestr1 : "");
145 if (n < 0)
146 goto done;
147 outoff += n;
148 if (lines) {
149 err = add_line_metadata(lines, nlines, outoff,
150 GOT_DIFF_LINE_BLOB_MIN);
151 if (err)
152 goto done;
155 n = fprintf(outfile, "blob + %s%s\n", idstr2,
156 modestr2 ? modestr2 : "");
157 if (n < 0)
158 goto done;
159 outoff += n;
160 if (lines) {
161 err = add_line_metadata(lines, nlines, outoff,
162 GOT_DIFF_LINE_BLOB_PLUS);
163 if (err)
164 goto done;
167 free(modestr1);
168 free(modestr2);
170 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
171 force_text_diff);
172 if (err)
173 goto done;
175 if (outfile) {
176 err = got_diffreg_output(lines, nlines, result,
177 blob1 != NULL, blob2 != NULL,
178 label1 ? label1 : idstr1,
179 label2 ? label2 : idstr2,
180 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
181 if (err)
182 goto done;
185 if (resultp && err == NULL)
186 *resultp = result;
187 else {
188 free_err = got_diffreg_result_free(result);
189 if (free_err && err == NULL)
190 err = free_err;
192 done:
193 return err;
196 const struct got_error *
197 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
198 struct got_blob_object *blob2, FILE *f1, FILE *f2,
199 struct got_object_id *id1, struct got_object_id *id2,
200 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
201 struct got_repository *repo)
203 struct got_diff_blob_output_unidiff_arg *a = arg;
205 return diff_blobs(&a->lines, &a->nlines, NULL,
206 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
207 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
210 const struct got_error *
211 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
212 struct got_blob_object *blob1, struct got_blob_object *blob2,
213 FILE *f1, FILE *f2, const char *label1, const char *label2,
214 enum got_diff_algorithm diff_algo, int diff_context,
215 int ignore_whitespace, int force_text_diff, FILE *outfile)
217 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
218 label1, label2, 0, 0, diff_context, ignore_whitespace,
219 force_text_diff, outfile, diff_algo);
222 static const struct got_error *
223 diff_blob_file(struct got_diffreg_result **resultp,
224 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
225 FILE *f2, int f2_exists, size_t size2, const char *label2,
226 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
227 int force_text_diff, FILE *outfile)
229 const struct got_error *err = NULL, *free_err;
230 char hex1[SHA1_DIGEST_STRING_LENGTH];
231 const char *idstr1 = NULL;
232 struct got_diffreg_result *result = NULL;
234 if (resultp)
235 *resultp = NULL;
237 if (blob1)
238 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
239 else
240 idstr1 = "/dev/null";
242 if (outfile) {
243 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
244 fprintf(outfile, "file + %s\n",
245 f2_exists ? label2 : "/dev/null");
248 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
249 force_text_diff);
250 if (err)
251 goto done;
253 if (outfile) {
254 err = got_diffreg_output(NULL, NULL, result,
255 blob1 != NULL, f2_exists,
256 label2, /* show local file's path, not a blob ID */
257 label2, GOT_DIFF_OUTPUT_UNIDIFF,
258 diff_context, outfile);
259 if (err)
260 goto done;
263 if (resultp && err == NULL)
264 *resultp = result;
265 else if (result) {
266 free_err = got_diffreg_result_free(result);
267 if (free_err && err == NULL)
268 err = free_err;
270 done:
271 return err;
274 const struct got_error *
275 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
276 const char *label1, FILE *f2, int f2_exists, size_t size2,
277 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
278 int ignore_whitespace, int force_text_diff, FILE *outfile)
280 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
281 size2, label2, diff_algo, diff_context, ignore_whitespace,
282 force_text_diff, outfile );
285 static const struct got_error *
286 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
287 const char *label, mode_t mode, struct got_repository *repo,
288 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, fd2);
299 if (err)
300 goto done;
301 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
302 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 FILE *f1, FILE *f2, int fd1, int fd2,
313 const char *label1, const char *label2,
314 mode_t mode1, mode_t mode2, struct got_repository *repo,
315 got_diff_blob_cb cb, void *cb_arg)
317 const struct got_error *err;
318 struct got_object *obj1 = NULL;
319 struct got_object *obj2 = NULL;
320 struct got_blob_object *blob1 = NULL;
321 struct got_blob_object *blob2 = NULL;
323 err = got_object_open(&obj1, repo, id1);
324 if (err)
325 return err;
327 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
328 err = got_error(GOT_ERR_OBJ_TYPE);
329 goto done;
332 err = got_object_open(&obj2, repo, id2);
333 if (err)
334 goto done;
335 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
336 err = got_error(GOT_ERR_BAD_OBJ_DATA);
337 goto done;
340 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
341 if (err)
342 goto done;
344 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
345 if (err)
346 goto done;
348 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
349 mode1, mode2, repo);
350 done:
351 if (obj1)
352 got_object_close(obj1);
353 if (obj2)
354 got_object_close(obj2);
355 if (blob1)
356 got_object_blob_close(blob1);
357 if (blob2)
358 got_object_blob_close(blob2);
359 return err;
362 static const struct got_error *
363 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
364 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
365 got_diff_blob_cb cb, void *cb_arg)
367 const struct got_error *err;
368 struct got_blob_object *blob = NULL;
369 struct got_object *obj = NULL;
371 err = got_object_open(&obj, repo, id);
372 if (err)
373 return err;
375 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
376 if (err)
377 goto done;
378 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
379 mode, 0, repo);
380 done:
381 got_object_close(obj);
382 if (blob)
383 got_object_blob_close(blob);
384 return err;
387 static const struct got_error *
388 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
389 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
390 void *cb_arg, int diff_content)
392 const struct got_error *err = NULL;
393 struct got_object *treeobj = NULL;
394 struct got_tree_object *tree = NULL;
396 err = got_object_open(&treeobj, repo, id);
397 if (err)
398 goto done;
400 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
401 err = got_error(GOT_ERR_OBJ_TYPE);
402 goto done;
405 err = got_object_tree_open(&tree, repo, treeobj);
406 if (err)
407 goto done;
409 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
410 repo, cb, cb_arg, diff_content);
411 done:
412 if (tree)
413 got_object_tree_close(tree);
414 if (treeobj)
415 got_object_close(treeobj);
416 return err;
419 static const struct got_error *
420 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
421 FILE *f1, FILE *f2, int fd1, int fd2,
422 const char *label1, const char *label2,
423 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
424 int diff_content)
426 const struct got_error *err;
427 struct got_object *treeobj1 = NULL;
428 struct got_object *treeobj2 = NULL;
429 struct got_tree_object *tree1 = NULL;
430 struct got_tree_object *tree2 = NULL;
432 err = got_object_open(&treeobj1, repo, id1);
433 if (err)
434 goto done;
436 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
437 err = got_error(GOT_ERR_OBJ_TYPE);
438 goto done;
441 err = got_object_open(&treeobj2, repo, id2);
442 if (err)
443 goto done;
445 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
446 err = got_error(GOT_ERR_OBJ_TYPE);
447 goto done;
450 err = got_object_tree_open(&tree1, repo, treeobj1);
451 if (err)
452 goto done;
454 err = got_object_tree_open(&tree2, repo, treeobj2);
455 if (err)
456 goto done;
458 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
459 label1, label2, repo, cb, cb_arg, diff_content);
461 done:
462 if (tree1)
463 got_object_tree_close(tree1);
464 if (tree2)
465 got_object_tree_close(tree2);
466 if (treeobj1)
467 got_object_close(treeobj1);
468 if (treeobj2)
469 got_object_close(treeobj2);
470 return err;
473 static const struct got_error *
474 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
475 FILE *f2, const char *label, struct got_repository *repo,
476 got_diff_blob_cb cb, void *cb_arg, int diff_content)
478 const struct got_error *err;
479 struct got_object *treeobj = NULL;
480 struct got_tree_object *tree = NULL;
482 err = got_object_open(&treeobj, repo, id);
483 if (err)
484 goto done;
486 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
487 err = got_error(GOT_ERR_OBJ_TYPE);
488 goto done;
491 err = got_object_tree_open(&tree, repo, treeobj);
492 if (err)
493 goto done;
495 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
496 repo, cb, cb_arg, diff_content);
497 done:
498 if (tree)
499 got_object_tree_close(tree);
500 if (treeobj)
501 got_object_close(treeobj);
502 return err;
505 static const struct got_error *
506 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
507 const char *label1, const char *label2, struct got_repository *repo,
508 got_diff_blob_cb cb, void *cb_arg)
510 /* XXX TODO */
511 return NULL;
514 static const struct got_error *
515 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
516 FILE *f1, FILE *f2, int fd1, int fd2,
517 const char *label1, const char *label2,
518 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
519 int diff_content)
521 const struct got_error *err = NULL;
522 int id_match;
524 if (got_object_tree_entry_is_submodule(te1))
525 return NULL;
527 if (te2 == NULL) {
528 if (S_ISDIR(te1->mode))
529 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
530 label1, repo, cb, cb_arg, diff_content);
531 else {
532 if (diff_content)
533 err = diff_deleted_blob(&te1->id, f1, fd1,
534 f2, label1, te1->mode, repo, cb, cb_arg);
535 else
536 err = cb(cb_arg, NULL, NULL, NULL, NULL,
537 &te1->id, NULL, label1, NULL,
538 te1->mode, 0, repo);
540 return err;
541 } else if (got_object_tree_entry_is_submodule(te2))
542 return NULL;
544 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
545 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
546 if (!id_match)
547 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
548 fd1, fd2, label1, label2, repo, cb, cb_arg,
549 diff_content);
550 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
551 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
552 if (!id_match ||
553 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
554 (te2->mode & (S_IFLNK | S_IXUSR))) {
555 if (diff_content)
556 return diff_modified_blob(&te1->id, &te2->id,
557 f1, f2, fd1, fd2, label1, label2,
558 te1->mode, te2->mode, repo, cb, cb_arg);
559 else
560 return cb(cb_arg, NULL, NULL, NULL, NULL,
561 &te1->id, &te2->id, label1, label2,
562 te1->mode, te2->mode, repo);
566 if (id_match)
567 return NULL;
569 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
570 cb, cb_arg);
573 static const struct got_error *
574 diff_entry_new_old(struct got_tree_entry *te2,
575 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
576 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
577 int diff_content)
579 if (te1 != NULL) /* handled by diff_entry_old_new() */
580 return NULL;
582 if (got_object_tree_entry_is_submodule(te2))
583 return NULL;
585 if (S_ISDIR(te2->mode))
586 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
587 repo, cb, cb_arg, diff_content);
589 if (diff_content)
590 return diff_added_blob(&te2->id, f1, f2, fd2,
591 label2, te2->mode, repo, cb, cb_arg);
593 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
594 NULL, label2, 0, te2->mode, repo);
597 const struct got_error *
598 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
599 struct got_blob_object *blob2, FILE *f1, FILE *f2,
600 struct got_object_id *id1, struct got_object_id *id2,
601 const char *label1, const char *label2,
602 mode_t mode1, mode_t mode2, struct got_repository *repo)
604 const struct got_error *err = NULL;
605 struct got_pathlist_head *paths = arg;
606 struct got_diff_changed_path *change = NULL;
607 char *path = NULL;
609 path = strdup(label2 ? label2 : label1);
610 if (path == NULL)
611 return got_error_from_errno("malloc");
613 change = malloc(sizeof(*change));
614 if (change == NULL) {
615 err = got_error_from_errno("malloc");
616 goto done;
619 change->status = GOT_STATUS_NO_CHANGE;
620 if (id1 == NULL)
621 change->status = GOT_STATUS_ADD;
622 else if (id2 == NULL)
623 change->status = GOT_STATUS_DELETE;
624 else {
625 if (got_object_id_cmp(id1, id2) != 0)
626 change->status = GOT_STATUS_MODIFY;
627 else if (mode1 != mode2)
628 change->status = GOT_STATUS_MODE_CHANGE;
631 err = got_pathlist_append(paths, path, change);
632 done:
633 if (err) {
634 free(path);
635 free(change);
637 return err;
640 const struct got_error *
641 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
642 FILE *f1, FILE *f2, int fd1, int fd2,
643 const char *label1, const char *label2,
644 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
645 int diff_content)
647 const struct got_error *err = NULL;
648 struct got_tree_entry *te1 = NULL;
649 struct got_tree_entry *te2 = NULL;
650 char *l1 = NULL, *l2 = NULL;
651 int tidx1 = 0, tidx2 = 0;
653 if (tree1) {
654 te1 = got_object_tree_get_entry(tree1, 0);
655 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
656 te1->name) == -1)
657 return got_error_from_errno("asprintf");
659 if (tree2) {
660 te2 = got_object_tree_get_entry(tree2, 0);
661 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
662 te2->name) == -1)
663 return got_error_from_errno("asprintf");
666 do {
667 if (te1) {
668 struct got_tree_entry *te = NULL;
669 if (tree2)
670 te = got_object_tree_find_entry(tree2,
671 te1->name);
672 if (te) {
673 free(l2);
674 l2 = NULL;
675 if (te && asprintf(&l2, "%s%s%s", label2,
676 label2[0] ? "/" : "", te->name) == -1)
677 return
678 got_error_from_errno("asprintf");
680 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
681 l1, l2, repo, cb, cb_arg, diff_content);
682 if (err)
683 break;
686 if (te2) {
687 struct got_tree_entry *te = NULL;
688 if (tree1)
689 te = got_object_tree_find_entry(tree1,
690 te2->name);
691 free(l2);
692 if (te) {
693 if (asprintf(&l2, "%s%s%s", label2,
694 label2[0] ? "/" : "", te->name) == -1)
695 return
696 got_error_from_errno("asprintf");
697 } else {
698 if (asprintf(&l2, "%s%s%s", label2,
699 label2[0] ? "/" : "", te2->name) == -1)
700 return
701 got_error_from_errno("asprintf");
703 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
704 repo, cb, cb_arg, diff_content);
705 if (err)
706 break;
709 free(l1);
710 l1 = NULL;
711 if (te1) {
712 tidx1++;
713 te1 = got_object_tree_get_entry(tree1, tidx1);
714 if (te1 &&
715 asprintf(&l1, "%s%s%s", label1,
716 label1[0] ? "/" : "", te1->name) == -1)
717 return got_error_from_errno("asprintf");
719 free(l2);
720 l2 = NULL;
721 if (te2) {
722 tidx2++;
723 te2 = got_object_tree_get_entry(tree2, tidx2);
724 if (te2 &&
725 asprintf(&l2, "%s%s%s", label2,
726 label2[0] ? "/" : "", te2->name) == -1)
727 return got_error_from_errno("asprintf");
729 } while (te1 || te2);
731 return err;
734 const struct got_error *
735 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
736 FILE *f1, FILE *f2, int fd1, int fd2,
737 struct got_object_id *id1, struct got_object_id *id2,
738 const char *label1, const char *label2,
739 enum got_diff_algorithm diff_algo, int diff_context,
740 int ignore_whitespace, int force_text_diff,
741 struct got_repository *repo, FILE *outfile)
743 const struct got_error *err;
744 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
746 if (id1 == NULL && id2 == NULL)
747 return got_error(GOT_ERR_NO_OBJ);
749 if (id1) {
750 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
751 if (err)
752 goto done;
754 if (id2) {
755 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
756 if (err)
757 goto done;
759 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
760 diff_algo, diff_context, ignore_whitespace, force_text_diff,
761 outfile);
762 done:
763 if (blob1)
764 got_object_blob_close(blob1);
765 if (blob2)
766 got_object_blob_close(blob2);
767 return err;
770 static const struct got_error *
771 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
772 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
773 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
775 const struct got_error *err = NULL;
776 struct got_pathlist_entry *pe;
777 struct got_object_id *id1 = NULL, *id2 = NULL;
778 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
779 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
781 TAILQ_FOREACH(pe, paths, entry) {
782 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
783 mode_t mode1 = 0, mode2 = 0;
785 free(id1);
786 id1 = NULL;
787 free(id2);
788 id2 = NULL;
789 if (subtree1) {
790 got_object_tree_close(subtree1);
791 subtree1 = NULL;
793 if (subtree2) {
794 got_object_tree_close(subtree2);
795 subtree2 = NULL;
797 if (blob1) {
798 got_object_blob_close(blob1);
799 blob1 = NULL;
801 if (blob2) {
802 got_object_blob_close(blob2);
803 blob2 = NULL;
806 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
807 pe->path);
808 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
809 goto done;
810 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
811 pe->path);
812 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
813 goto done;
814 if (id1 == NULL && id2 == NULL) {
815 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
816 goto done;
818 if (id1) {
819 err = got_object_get_type(&type1, repo, id1);
820 if (err)
821 goto done;
823 if (id2) {
824 err = got_object_get_type(&type2, repo, id2);
825 if (err)
826 goto done;
828 if (type1 == GOT_OBJ_TYPE_ANY &&
829 type2 == GOT_OBJ_TYPE_ANY) {
830 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
831 goto done;
832 } else if (type1 != GOT_OBJ_TYPE_ANY &&
833 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
834 err = got_error(GOT_ERR_OBJ_TYPE);
835 goto done;
838 if (type1 == GOT_OBJ_TYPE_BLOB ||
839 type2 == GOT_OBJ_TYPE_BLOB) {
840 if (id1) {
841 err = got_object_open_as_blob(&blob1, repo,
842 id1, 8192, fd1);
843 if (err)
844 goto done;
846 if (id2) {
847 err = got_object_open_as_blob(&blob2, repo,
848 id2, 8192, fd2);
849 if (err)
850 goto done;
852 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
853 id1 ? pe->path : "/dev/null",
854 id2 ? pe->path : "/dev/null",
855 mode1, mode2, repo);
856 if (err)
857 goto done;
858 } else if (type1 == GOT_OBJ_TYPE_TREE ||
859 type2 == GOT_OBJ_TYPE_TREE) {
860 if (id1) {
861 err = got_object_open_as_tree(&subtree1, repo,
862 id1);
863 if (err)
864 goto done;
866 if (id2) {
867 err = got_object_open_as_tree(&subtree2, repo,
868 id2);
869 if (err)
870 goto done;
872 err = got_diff_tree(subtree1, subtree2, f1, f2,
873 fd1, fd2,
874 id1 ? pe->path : "/dev/null",
875 id2 ? pe->path : "/dev/null",
876 repo, cb, cb_arg, 1);
877 if (err)
878 goto done;
879 } else {
880 err = got_error(GOT_ERR_OBJ_TYPE);
881 goto done;
884 done:
885 free(id1);
886 free(id2);
887 if (subtree1)
888 got_object_tree_close(subtree1);
889 if (subtree2)
890 got_object_tree_close(subtree2);
891 if (blob1)
892 got_object_blob_close(blob1);
893 if (blob2)
894 got_object_blob_close(blob2);
895 return err;
898 static const struct got_error *
899 show_object_id(struct got_diff_line **lines, size_t *nlines,
900 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
902 const struct got_error *err;
903 int n;
904 off_t outoff = 0;
906 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
907 if (n < 0)
908 return got_error_from_errno("fprintf");
910 if (lines != NULL && *lines != NULL) {
911 if (*nlines == 0) {
912 err = add_line_metadata(lines, nlines, 0,
913 GOT_DIFF_LINE_META);
914 if (err)
915 return err;
916 } else
917 outoff = (*lines)[*nlines - 1].offset;
919 outoff += n;
920 err = add_line_metadata(lines, nlines, outoff,
921 GOT_DIFF_LINE_META);
922 if (err)
923 return err;
926 return NULL;
929 static const struct got_error *
930 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
931 FILE *f1, FILE *f2, int fd1, int fd2,
932 struct got_object_id *id1, struct got_object_id *id2,
933 struct got_pathlist_head *paths, const char *label1, const char *label2,
934 int diff_context, int ignore_whitespace, int force_text_diff,
935 struct got_repository *repo, FILE *outfile,
936 enum got_diff_algorithm diff_algo)
938 const struct got_error *err;
939 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
940 struct got_diff_blob_output_unidiff_arg arg;
941 int want_linemeta = (lines != NULL && *lines != NULL);
943 if (id1 == NULL && id2 == NULL)
944 return got_error(GOT_ERR_NO_OBJ);
946 if (id1) {
947 err = got_object_open_as_tree(&tree1, repo, id1);
948 if (err)
949 goto done;
951 if (id2) {
952 err = got_object_open_as_tree(&tree2, repo, id2);
953 if (err)
954 goto done;
957 arg.diff_algo = diff_algo;
958 arg.diff_context = diff_context;
959 arg.ignore_whitespace = ignore_whitespace;
960 arg.force_text_diff = force_text_diff;
961 arg.outfile = outfile;
962 if (want_linemeta) {
963 arg.lines = *lines;
964 arg.nlines = *nlines;
965 } else {
966 arg.lines = NULL;
967 arg.nlines = 0;
969 if (paths == NULL || TAILQ_EMPTY(paths)) {
970 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
971 label1, label2, repo,
972 got_diff_blob_output_unidiff, &arg, 1);
973 } else {
974 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
975 got_diff_blob_output_unidiff, &arg);
977 if (want_linemeta) {
978 *lines = arg.lines; /* was likely re-allocated */
979 *nlines = arg.nlines;
981 done:
982 if (tree1)
983 got_object_tree_close(tree1);
984 if (tree2)
985 got_object_tree_close(tree2);
986 return err;
989 const struct got_error *
990 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
991 FILE *f1, FILE *f2, int fd1, int fd2,
992 struct got_object_id *id1, struct got_object_id *id2,
993 struct got_pathlist_head *paths, const char *label1, const char *label2,
994 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
995 int force_text_diff, struct got_repository *repo, FILE *outfile)
997 const struct got_error *err;
998 char *idstr = NULL;
1000 if (id1 == NULL && id2 == NULL)
1001 return got_error(GOT_ERR_NO_OBJ);
1003 if (id1) {
1004 err = got_object_id_str(&idstr, id1);
1005 if (err)
1006 goto done;
1007 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1008 if (err)
1009 goto done;
1010 free(idstr);
1011 idstr = NULL;
1012 } else {
1013 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1014 outfile);
1015 if (err)
1016 goto done;
1019 if (id2) {
1020 err = got_object_id_str(&idstr, id2);
1021 if (err)
1022 goto done;
1023 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1024 if (err)
1025 goto done;
1026 free(idstr);
1027 idstr = NULL;
1028 } else {
1029 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1030 outfile);
1031 if (err)
1032 goto done;
1035 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1036 paths, label1, label2, diff_context, ignore_whitespace,
1037 force_text_diff, repo, outfile, diff_algo);
1038 done:
1039 free(idstr);
1040 return err;
1043 const struct got_error *
1044 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1045 FILE *f1, FILE *f2, int fd1, int fd2,
1046 struct got_object_id *id1, struct got_object_id *id2,
1047 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1048 int diff_context, int ignore_whitespace, int force_text_diff,
1049 struct got_repository *repo, FILE *outfile)
1051 const struct got_error *err;
1052 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1053 char *idstr = NULL;
1055 if (id2 == NULL)
1056 return got_error(GOT_ERR_NO_OBJ);
1058 if (id1) {
1059 err = got_object_open_as_commit(&commit1, repo, id1);
1060 if (err)
1061 goto done;
1062 err = got_object_id_str(&idstr, id1);
1063 if (err)
1064 goto done;
1065 err = show_object_id(lines, nlines, "commit", '-', idstr,
1066 outfile);
1067 if (err)
1068 goto done;
1069 free(idstr);
1070 idstr = NULL;
1071 } else {
1072 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1073 outfile);
1074 if (err)
1075 goto done;
1078 err = got_object_open_as_commit(&commit2, repo, id2);
1079 if (err)
1080 goto done;
1082 err = got_object_id_str(&idstr, id2);
1083 if (err)
1084 goto done;
1085 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1086 if (err)
1087 goto done;
1089 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1090 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1091 got_object_commit_get_tree_id(commit2), paths, "", "",
1092 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1093 diff_algo);
1094 done:
1095 if (commit1)
1096 got_object_commit_close(commit1);
1097 if (commit2)
1098 got_object_commit_close(commit2);
1099 free(idstr);
1100 return err;
1103 const struct got_error *
1104 got_diff_files(struct got_diffreg_result **resultp,
1105 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1106 const char *label2, int diff_context, int ignore_whitespace,
1107 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1109 const struct got_error *err = NULL;
1110 struct got_diffreg_result *diffreg_result = NULL;
1112 if (resultp)
1113 *resultp = NULL;
1115 if (outfile) {
1116 fprintf(outfile, "file - %s\n",
1117 f1_exists ? label1 : "/dev/null");
1118 fprintf(outfile, "file + %s\n",
1119 f2_exists ? label2 : "/dev/null");
1122 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1123 ignore_whitespace, force_text_diff);
1124 if (err)
1125 goto done;
1127 if (outfile) {
1128 err = got_diffreg_output(NULL, NULL, diffreg_result,
1129 f1_exists, f2_exists, label1, label2,
1130 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1131 if (err)
1132 goto done;
1135 done:
1136 if (resultp && err == NULL)
1137 *resultp = diffreg_result;
1138 else if (diffreg_result) {
1139 const struct got_error *free_err;
1140 free_err = got_diffreg_result_free(diffreg_result);
1141 if (free_err && err == NULL)
1142 err = free_err;
1145 return err;