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_metadata(struct got_diff_line **lines, size_t *nlines,
43 off_t off, uint8_t type)
44 {
45 struct got_diff_line *p;
47 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
48 if (p == NULL)
49 return got_error_from_errno("reallocarray");
50 *lines = p;
51 (*lines)[*nlines].offset = off;
52 (*lines)[*nlines].type = type;
53 (*nlines)++;
55 return NULL;
56 }
58 static const struct got_error *
59 diff_blobs(struct got_diff_line **lines, size_t *nlines,
60 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
61 struct got_blob_object *blob2, FILE *f1, FILE *f2,
62 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
63 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
64 enum got_diff_algorithm diff_algo)
65 {
66 const struct got_error *err = NULL, *free_err;
67 char hex1[SHA1_DIGEST_STRING_LENGTH];
68 char hex2[SHA1_DIGEST_STRING_LENGTH];
69 const char *idstr1 = NULL, *idstr2 = NULL;
70 off_t size1, size2;
71 struct got_diffreg_result *result;
72 off_t outoff = 0;
73 int n;
75 if (lines && *lines && *nlines > 0)
76 outoff = (*lines)[*nlines - 1].offset;
77 else if (lines) {
78 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
79 if (err)
80 goto done;
81 }
83 if (resultp)
84 *resultp = NULL;
86 if (f1) {
87 err = got_opentemp_truncate(f1);
88 if (err)
89 goto done;
90 }
91 if (f2) {
92 err = got_opentemp_truncate(f2);
93 if (err)
94 goto done;
95 }
97 size1 = 0;
98 if (blob1) {
99 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
100 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
101 blob1);
102 if (err)
103 goto done;
104 } else
105 idstr1 = "/dev/null";
107 size2 = 0;
108 if (blob2) {
109 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
110 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
111 blob2);
112 if (err)
113 goto done;
114 } else
115 idstr2 = "/dev/null";
117 if (outfile) {
118 char *modestr1 = NULL, *modestr2 = NULL;
119 int modebits;
120 if (mode1 && mode1 != mode2) {
121 if (S_ISLNK(mode1))
122 modebits = S_IFLNK;
123 else
124 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
125 if (asprintf(&modestr1, " (mode %o)",
126 mode1 & modebits) == -1) {
127 err = got_error_from_errno("asprintf");
128 goto done;
131 if (mode2 && mode1 != mode2) {
132 if (S_ISLNK(mode2))
133 modebits = S_IFLNK;
134 else
135 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
136 if (asprintf(&modestr2, " (mode %o)",
137 mode2 & modebits) == -1) {
138 err = got_error_from_errno("asprintf");
139 goto done;
142 n = fprintf(outfile, "blob - %s%s\n", idstr1,
143 modestr1 ? modestr1 : "");
144 if (n < 0)
145 goto done;
146 outoff += n;
147 if (lines) {
148 err = add_line_metadata(lines, nlines, outoff,
149 GOT_DIFF_LINE_BLOB_MIN);
150 if (err)
151 goto done;
154 n = fprintf(outfile, "blob + %s%s\n", idstr2,
155 modestr2 ? modestr2 : "");
156 if (n < 0)
157 goto done;
158 outoff += n;
159 if (lines) {
160 err = add_line_metadata(lines, nlines, outoff,
161 GOT_DIFF_LINE_BLOB_PLUS);
162 if (err)
163 goto done;
166 free(modestr1);
167 free(modestr2);
169 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
170 force_text_diff);
171 if (err)
172 goto done;
174 if (outfile) {
175 err = got_diffreg_output(lines, nlines, result,
176 blob1 != NULL, blob2 != NULL,
177 label1 ? label1 : idstr1,
178 label2 ? label2 : idstr2,
179 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
180 if (err)
181 goto done;
184 if (resultp && err == NULL)
185 *resultp = result;
186 else {
187 free_err = got_diffreg_result_free(result);
188 if (free_err && err == NULL)
189 err = free_err;
191 done:
192 return err;
195 const struct got_error *
196 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
197 struct got_blob_object *blob2, FILE *f1, FILE *f2,
198 struct got_object_id *id1, struct got_object_id *id2,
199 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
200 struct got_repository *repo)
202 struct got_diff_blob_output_unidiff_arg *a = arg;
204 return diff_blobs(&a->lines, &a->nlines, NULL,
205 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
206 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
209 const struct got_error *
210 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
211 struct got_blob_object *blob1, struct got_blob_object *blob2,
212 FILE *f1, FILE *f2, const char *label1, const char *label2,
213 enum got_diff_algorithm diff_algo, int diff_context,
214 int ignore_whitespace, int force_text_diff, FILE *outfile)
216 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
217 label1, label2, 0, 0, diff_context, ignore_whitespace,
218 force_text_diff, outfile, diff_algo);
221 static const struct got_error *
222 diff_blob_file(struct got_diffreg_result **resultp,
223 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
224 FILE *f2, int f2_exists, size_t size2, const char *label2,
225 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
226 int force_text_diff, FILE *outfile)
228 const struct got_error *err = NULL, *free_err;
229 char hex1[SHA1_DIGEST_STRING_LENGTH];
230 const char *idstr1 = NULL;
231 struct got_diffreg_result *result = NULL;
233 if (resultp)
234 *resultp = NULL;
236 if (blob1)
237 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
238 else
239 idstr1 = "/dev/null";
241 if (outfile) {
242 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
243 fprintf(outfile, "file + %s\n",
244 f2_exists ? label2 : "/dev/null");
247 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
248 force_text_diff);
249 if (err)
250 goto done;
252 if (outfile) {
253 err = got_diffreg_output(NULL, NULL, result,
254 blob1 != NULL, f2_exists,
255 label2, /* show local file's path, not a blob ID */
256 label2, GOT_DIFF_OUTPUT_UNIDIFF,
257 diff_context, outfile);
258 if (err)
259 goto done;
262 if (resultp && err == NULL)
263 *resultp = result;
264 else if (result) {
265 free_err = got_diffreg_result_free(result);
266 if (free_err && err == NULL)
267 err = free_err;
269 done:
270 return err;
273 const struct got_error *
274 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
275 const char *label1, FILE *f2, int f2_exists, size_t size2,
276 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
277 int ignore_whitespace, int force_text_diff, FILE *outfile)
279 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
280 size2, label2, diff_algo, diff_context, ignore_whitespace,
281 force_text_diff, outfile );
284 static const struct got_error *
285 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
286 const char *label, mode_t mode, struct got_repository *repo,
287 got_diff_blob_cb cb, void *cb_arg)
289 const struct got_error *err;
290 struct got_blob_object *blob = NULL;
291 struct got_object *obj = NULL;
293 err = got_object_open(&obj, repo, id);
294 if (err)
295 return err;
297 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
298 if (err)
299 goto done;
300 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
301 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 FILE *f1, FILE *f2, int fd1, int fd2,
312 const char *label1, const char *label2,
313 mode_t mode1, mode_t mode2, struct got_repository *repo,
314 got_diff_blob_cb cb, void *cb_arg)
316 const struct got_error *err;
317 struct got_object *obj1 = NULL;
318 struct got_object *obj2 = NULL;
319 struct got_blob_object *blob1 = NULL;
320 struct got_blob_object *blob2 = NULL;
322 err = got_object_open(&obj1, repo, id1);
323 if (err)
324 return err;
326 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
328 goto done;
331 err = got_object_open(&obj2, repo, id2);
332 if (err)
333 goto done;
334 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 goto done;
339 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
340 if (err)
341 goto done;
343 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
344 if (err)
345 goto done;
347 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
348 mode1, mode2, repo);
349 done:
350 if (obj1)
351 got_object_close(obj1);
352 if (obj2)
353 got_object_close(obj2);
354 if (blob1)
355 got_object_blob_close(blob1);
356 if (blob2)
357 got_object_blob_close(blob2);
358 return err;
361 static const struct got_error *
362 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
363 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
364 got_diff_blob_cb cb, void *cb_arg)
366 const struct got_error *err;
367 struct got_blob_object *blob = NULL;
368 struct got_object *obj = NULL;
370 err = got_object_open(&obj, repo, id);
371 if (err)
372 return err;
374 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
375 if (err)
376 goto done;
377 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
378 mode, 0, repo);
379 done:
380 got_object_close(obj);
381 if (blob)
382 got_object_blob_close(blob);
383 return err;
386 static const struct got_error *
387 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
388 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
389 void *cb_arg, int diff_content)
391 const struct got_error *err = NULL;
392 struct got_object *treeobj = NULL;
393 struct got_tree_object *tree = NULL;
395 err = got_object_open(&treeobj, repo, id);
396 if (err)
397 goto done;
399 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
400 err = got_error(GOT_ERR_OBJ_TYPE);
401 goto done;
404 err = got_object_tree_open(&tree, repo, treeobj);
405 if (err)
406 goto done;
408 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
409 repo, cb, cb_arg, diff_content);
410 done:
411 if (tree)
412 got_object_tree_close(tree);
413 if (treeobj)
414 got_object_close(treeobj);
415 return err;
418 static const struct got_error *
419 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
420 FILE *f1, FILE *f2, int fd1, int fd2,
421 const char *label1, const char *label2,
422 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
423 int diff_content)
425 const struct got_error *err;
426 struct got_object *treeobj1 = NULL;
427 struct got_object *treeobj2 = NULL;
428 struct got_tree_object *tree1 = NULL;
429 struct got_tree_object *tree2 = NULL;
431 err = got_object_open(&treeobj1, repo, id1);
432 if (err)
433 goto done;
435 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
436 err = got_error(GOT_ERR_OBJ_TYPE);
437 goto done;
440 err = got_object_open(&treeobj2, repo, id2);
441 if (err)
442 goto done;
444 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
445 err = got_error(GOT_ERR_OBJ_TYPE);
446 goto done;
449 err = got_object_tree_open(&tree1, repo, treeobj1);
450 if (err)
451 goto done;
453 err = got_object_tree_open(&tree2, repo, treeobj2);
454 if (err)
455 goto done;
457 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
458 label1, label2, repo, cb, cb_arg, diff_content);
460 done:
461 if (tree1)
462 got_object_tree_close(tree1);
463 if (tree2)
464 got_object_tree_close(tree2);
465 if (treeobj1)
466 got_object_close(treeobj1);
467 if (treeobj2)
468 got_object_close(treeobj2);
469 return err;
472 static const struct got_error *
473 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
474 FILE *f2, const char *label, struct got_repository *repo,
475 got_diff_blob_cb cb, void *cb_arg, int diff_content)
477 const struct got_error *err;
478 struct got_object *treeobj = NULL;
479 struct got_tree_object *tree = NULL;
481 err = got_object_open(&treeobj, repo, id);
482 if (err)
483 goto done;
485 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
486 err = got_error(GOT_ERR_OBJ_TYPE);
487 goto done;
490 err = got_object_tree_open(&tree, repo, treeobj);
491 if (err)
492 goto done;
494 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
495 repo, cb, cb_arg, diff_content);
496 done:
497 if (tree)
498 got_object_tree_close(tree);
499 if (treeobj)
500 got_object_close(treeobj);
501 return err;
504 static const struct got_error *
505 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
506 const char *label1, const char *label2, struct got_repository *repo,
507 got_diff_blob_cb cb, void *cb_arg)
509 /* XXX TODO */
510 return NULL;
513 static const struct got_error *
514 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
515 FILE *f1, FILE *f2, int fd1, int fd2,
516 const char *label1, const char *label2,
517 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
518 int diff_content)
520 const struct got_error *err = NULL;
521 int id_match;
523 if (got_object_tree_entry_is_submodule(te1))
524 return NULL;
526 if (te2 == NULL) {
527 if (S_ISDIR(te1->mode))
528 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
529 label1, repo, cb, cb_arg, diff_content);
530 else {
531 if (diff_content)
532 err = diff_deleted_blob(&te1->id, f1, fd1,
533 f2, label1, te1->mode, repo, cb, cb_arg);
534 else
535 err = cb(cb_arg, NULL, NULL, NULL, NULL,
536 &te1->id, NULL, label1, NULL,
537 te1->mode, 0, repo);
539 return err;
540 } else if (got_object_tree_entry_is_submodule(te2))
541 return NULL;
543 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
544 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
545 if (!id_match)
546 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
547 fd1, fd2, label1, label2, repo, cb, cb_arg,
548 diff_content);
549 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
550 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
551 if (!id_match ||
552 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
553 (te2->mode & (S_IFLNK | S_IXUSR))) {
554 if (diff_content)
555 return diff_modified_blob(&te1->id, &te2->id,
556 f1, f2, fd1, fd2, label1, label2,
557 te1->mode, te2->mode, repo, cb, cb_arg);
558 else
559 return cb(cb_arg, NULL, NULL, NULL, NULL,
560 &te1->id, &te2->id, label1, label2,
561 te1->mode, te2->mode, repo);
565 if (id_match)
566 return NULL;
568 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
569 cb, cb_arg);
572 static const struct got_error *
573 diff_entry_new_old(struct got_tree_entry *te2,
574 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
575 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
576 int diff_content)
578 if (te1 != NULL) /* handled by diff_entry_old_new() */
579 return NULL;
581 if (got_object_tree_entry_is_submodule(te2))
582 return NULL;
584 if (S_ISDIR(te2->mode))
585 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
586 repo, cb, cb_arg, diff_content);
588 if (diff_content)
589 return diff_added_blob(&te2->id, f1, f2, fd2,
590 label2, te2->mode, repo, cb, cb_arg);
592 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
593 NULL, label2, 0, te2->mode, repo);
596 const struct got_error *
597 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
598 struct got_blob_object *blob2, FILE *f1, FILE *f2,
599 struct got_object_id *id1, struct got_object_id *id2,
600 const char *label1, const char *label2,
601 mode_t mode1, mode_t mode2, struct got_repository *repo)
603 const struct got_error *err = NULL;
604 struct got_pathlist_head *paths = arg;
605 struct got_diff_changed_path *change = NULL;
606 char *path = NULL;
608 path = strdup(label2 ? label2 : label1);
609 if (path == NULL)
610 return got_error_from_errno("malloc");
612 change = malloc(sizeof(*change));
613 if (change == NULL) {
614 err = got_error_from_errno("malloc");
615 goto done;
618 change->status = GOT_STATUS_NO_CHANGE;
619 if (id1 == NULL)
620 change->status = GOT_STATUS_ADD;
621 else if (id2 == NULL)
622 change->status = GOT_STATUS_DELETE;
623 else {
624 if (got_object_id_cmp(id1, id2) != 0)
625 change->status = GOT_STATUS_MODIFY;
626 else if (mode1 != mode2)
627 change->status = GOT_STATUS_MODE_CHANGE;
630 err = got_pathlist_append(paths, path, change);
631 done:
632 if (err) {
633 free(path);
634 free(change);
636 return err;
639 const struct got_error *
640 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
641 FILE *f1, FILE *f2, int fd1, int fd2,
642 const char *label1, const char *label2,
643 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
644 int diff_content)
646 const struct got_error *err = NULL;
647 struct got_tree_entry *te1 = NULL;
648 struct got_tree_entry *te2 = NULL;
649 char *l1 = NULL, *l2 = NULL;
650 int tidx1 = 0, tidx2 = 0;
652 if (tree1) {
653 te1 = got_object_tree_get_entry(tree1, 0);
654 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
655 te1->name) == -1)
656 return got_error_from_errno("asprintf");
658 if (tree2) {
659 te2 = got_object_tree_get_entry(tree2, 0);
660 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
661 te2->name) == -1)
662 return got_error_from_errno("asprintf");
665 do {
666 if (te1) {
667 struct got_tree_entry *te = NULL;
668 if (tree2)
669 te = got_object_tree_find_entry(tree2,
670 te1->name);
671 if (te) {
672 free(l2);
673 l2 = NULL;
674 if (te && asprintf(&l2, "%s%s%s", label2,
675 label2[0] ? "/" : "", te->name) == -1)
676 return
677 got_error_from_errno("asprintf");
679 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
680 l1, l2, repo, cb, cb_arg, diff_content);
681 if (err)
682 break;
685 if (te2) {
686 struct got_tree_entry *te = NULL;
687 if (tree1)
688 te = got_object_tree_find_entry(tree1,
689 te2->name);
690 free(l2);
691 if (te) {
692 if (asprintf(&l2, "%s%s%s", label2,
693 label2[0] ? "/" : "", te->name) == -1)
694 return
695 got_error_from_errno("asprintf");
696 } else {
697 if (asprintf(&l2, "%s%s%s", label2,
698 label2[0] ? "/" : "", te2->name) == -1)
699 return
700 got_error_from_errno("asprintf");
702 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
703 repo, cb, cb_arg, diff_content);
704 if (err)
705 break;
708 free(l1);
709 l1 = NULL;
710 if (te1) {
711 tidx1++;
712 te1 = got_object_tree_get_entry(tree1, tidx1);
713 if (te1 &&
714 asprintf(&l1, "%s%s%s", label1,
715 label1[0] ? "/" : "", te1->name) == -1)
716 return got_error_from_errno("asprintf");
718 free(l2);
719 l2 = NULL;
720 if (te2) {
721 tidx2++;
722 te2 = got_object_tree_get_entry(tree2, tidx2);
723 if (te2 &&
724 asprintf(&l2, "%s%s%s", label2,
725 label2[0] ? "/" : "", te2->name) == -1)
726 return got_error_from_errno("asprintf");
728 } while (te1 || te2);
730 return err;
733 const struct got_error *
734 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
735 FILE *f1, FILE *f2, int fd1, int fd2,
736 struct got_object_id *id1, struct got_object_id *id2,
737 const char *label1, const char *label2,
738 enum got_diff_algorithm diff_algo, int diff_context,
739 int ignore_whitespace, int force_text_diff,
740 struct got_repository *repo, FILE *outfile)
742 const struct got_error *err;
743 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
745 if (id1 == NULL && id2 == NULL)
746 return got_error(GOT_ERR_NO_OBJ);
748 if (id1) {
749 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
750 if (err)
751 goto done;
753 if (id2) {
754 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
755 if (err)
756 goto done;
758 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
759 diff_algo, diff_context, ignore_whitespace, force_text_diff,
760 outfile);
761 done:
762 if (blob1)
763 got_object_blob_close(blob1);
764 if (blob2)
765 got_object_blob_close(blob2);
766 return err;
769 static const struct got_error *
770 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
771 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
772 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
774 const struct got_error *err = NULL;
775 struct got_pathlist_entry *pe;
776 struct got_object_id *id1 = NULL, *id2 = NULL;
777 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
778 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
780 TAILQ_FOREACH(pe, paths, entry) {
781 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
782 mode_t mode1 = 0, mode2 = 0;
784 free(id1);
785 id1 = NULL;
786 free(id2);
787 id2 = NULL;
788 if (subtree1) {
789 got_object_tree_close(subtree1);
790 subtree1 = NULL;
792 if (subtree2) {
793 got_object_tree_close(subtree2);
794 subtree2 = NULL;
796 if (blob1) {
797 got_object_blob_close(blob1);
798 blob1 = NULL;
800 if (blob2) {
801 got_object_blob_close(blob2);
802 blob2 = NULL;
805 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
806 pe->path);
807 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
808 goto done;
809 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
810 pe->path);
811 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
812 goto done;
813 if (id1 == NULL && id2 == NULL) {
814 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
815 goto done;
817 if (id1) {
818 err = got_object_get_type(&type1, repo, id1);
819 if (err)
820 goto done;
822 if (id2) {
823 err = got_object_get_type(&type2, repo, id2);
824 if (err)
825 goto done;
827 if (type1 == GOT_OBJ_TYPE_ANY &&
828 type2 == GOT_OBJ_TYPE_ANY) {
829 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
830 goto done;
831 } else if (type1 != GOT_OBJ_TYPE_ANY &&
832 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
833 err = got_error(GOT_ERR_OBJ_TYPE);
834 goto done;
837 if (type1 == GOT_OBJ_TYPE_BLOB ||
838 type2 == GOT_OBJ_TYPE_BLOB) {
839 if (id1) {
840 err = got_object_open_as_blob(&blob1, repo,
841 id1, 8192, fd1);
842 if (err)
843 goto done;
845 if (id2) {
846 err = got_object_open_as_blob(&blob2, repo,
847 id2, 8192, fd2);
848 if (err)
849 goto done;
851 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
852 id1 ? pe->path : "/dev/null",
853 id2 ? pe->path : "/dev/null",
854 mode1, mode2, repo);
855 if (err)
856 goto done;
857 } else if (type1 == GOT_OBJ_TYPE_TREE ||
858 type2 == GOT_OBJ_TYPE_TREE) {
859 if (id1) {
860 err = got_object_open_as_tree(&subtree1, repo,
861 id1);
862 if (err)
863 goto done;
865 if (id2) {
866 err = got_object_open_as_tree(&subtree2, repo,
867 id2);
868 if (err)
869 goto done;
871 err = got_diff_tree(subtree1, subtree2, f1, f2,
872 fd1, fd2,
873 id1 ? pe->path : "/dev/null",
874 id2 ? pe->path : "/dev/null",
875 repo, cb, cb_arg, 1);
876 if (err)
877 goto done;
878 } else {
879 err = got_error(GOT_ERR_OBJ_TYPE);
880 goto done;
883 done:
884 free(id1);
885 free(id2);
886 if (subtree1)
887 got_object_tree_close(subtree1);
888 if (subtree2)
889 got_object_tree_close(subtree2);
890 if (blob1)
891 got_object_blob_close(blob1);
892 if (blob2)
893 got_object_blob_close(blob2);
894 return err;
897 static const struct got_error *
898 show_object_id(struct got_diff_line **lines, size_t *nlines,
899 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
901 const struct got_error *err;
902 int n;
903 off_t outoff = 0;
905 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
906 if (n < 0)
907 return got_error_from_errno("fprintf");
909 if (lines != NULL && *lines != NULL) {
910 if (*nlines == 0) {
911 err = add_line_metadata(lines, nlines, 0,
912 GOT_DIFF_LINE_META);
913 if (err)
914 return err;
915 } else
916 outoff = (*lines)[*nlines - 1].offset;
918 outoff += n;
919 err = add_line_metadata(lines, nlines, outoff,
920 GOT_DIFF_LINE_META);
921 if (err)
922 return err;
925 return NULL;
928 static const struct got_error *
929 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
930 FILE *f1, FILE *f2, int fd1, int fd2,
931 struct got_object_id *id1, struct got_object_id *id2,
932 struct got_pathlist_head *paths, const char *label1, const char *label2,
933 int diff_context, int ignore_whitespace, int force_text_diff,
934 struct got_repository *repo, FILE *outfile,
935 enum got_diff_algorithm diff_algo)
937 const struct got_error *err;
938 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
939 struct got_diff_blob_output_unidiff_arg arg;
940 int want_linemeta = (lines != NULL && *lines != NULL);
942 if (id1 == NULL && id2 == NULL)
943 return got_error(GOT_ERR_NO_OBJ);
945 if (id1) {
946 err = got_object_open_as_tree(&tree1, repo, id1);
947 if (err)
948 goto done;
950 if (id2) {
951 err = got_object_open_as_tree(&tree2, repo, id2);
952 if (err)
953 goto done;
956 arg.diff_algo = diff_algo;
957 arg.diff_context = diff_context;
958 arg.ignore_whitespace = ignore_whitespace;
959 arg.force_text_diff = force_text_diff;
960 arg.outfile = outfile;
961 if (want_linemeta) {
962 arg.lines = *lines;
963 arg.nlines = *nlines;
964 } else {
965 arg.lines = NULL;
966 arg.nlines = 0;
968 if (paths == NULL || TAILQ_EMPTY(paths)) {
969 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
970 label1, label2, repo,
971 got_diff_blob_output_unidiff, &arg, 1);
972 } else {
973 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
974 got_diff_blob_output_unidiff, &arg);
976 if (want_linemeta) {
977 *lines = arg.lines; /* was likely re-allocated */
978 *nlines = arg.nlines;
980 done:
981 if (tree1)
982 got_object_tree_close(tree1);
983 if (tree2)
984 got_object_tree_close(tree2);
985 return err;
988 const struct got_error *
989 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
990 FILE *f1, FILE *f2, int fd1, int fd2,
991 struct got_object_id *id1, struct got_object_id *id2,
992 struct got_pathlist_head *paths, const char *label1, const char *label2,
993 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
994 int force_text_diff, struct got_repository *repo, FILE *outfile)
996 const struct got_error *err;
997 char *idstr = NULL;
999 if (id1 == NULL && id2 == NULL)
1000 return got_error(GOT_ERR_NO_OBJ);
1002 if (id1) {
1003 err = got_object_id_str(&idstr, id1);
1004 if (err)
1005 goto done;
1006 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1007 if (err)
1008 goto done;
1009 free(idstr);
1010 idstr = NULL;
1011 } else {
1012 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1013 outfile);
1014 if (err)
1015 goto done;
1018 if (id2) {
1019 err = got_object_id_str(&idstr, id2);
1020 if (err)
1021 goto done;
1022 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1023 if (err)
1024 goto done;
1025 free(idstr);
1026 idstr = NULL;
1027 } else {
1028 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1029 outfile);
1030 if (err)
1031 goto done;
1034 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1035 paths, label1, label2, diff_context, ignore_whitespace,
1036 force_text_diff, repo, outfile, diff_algo);
1037 done:
1038 free(idstr);
1039 return err;
1042 const struct got_error *
1043 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1044 FILE *f1, FILE *f2, int fd1, int fd2,
1045 struct got_object_id *id1, struct got_object_id *id2,
1046 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1047 int diff_context, int ignore_whitespace, int force_text_diff,
1048 struct got_repository *repo, FILE *outfile)
1050 const struct got_error *err;
1051 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1052 char *idstr = NULL;
1054 if (id2 == NULL)
1055 return got_error(GOT_ERR_NO_OBJ);
1057 if (id1) {
1058 err = got_object_open_as_commit(&commit1, repo, id1);
1059 if (err)
1060 goto done;
1061 err = got_object_id_str(&idstr, id1);
1062 if (err)
1063 goto done;
1064 err = show_object_id(lines, nlines, "commit", '-', idstr,
1065 outfile);
1066 if (err)
1067 goto done;
1068 free(idstr);
1069 idstr = NULL;
1070 } else {
1071 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1072 outfile);
1073 if (err)
1074 goto done;
1077 err = got_object_open_as_commit(&commit2, repo, id2);
1078 if (err)
1079 goto done;
1081 err = got_object_id_str(&idstr, id2);
1082 if (err)
1083 goto done;
1084 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1085 if (err)
1086 goto done;
1088 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1089 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1090 got_object_commit_get_tree_id(commit2), paths, "", "",
1091 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1092 diff_algo);
1093 done:
1094 if (commit1)
1095 got_object_commit_close(commit1);
1096 if (commit2)
1097 got_object_commit_close(commit2);
1098 free(idstr);
1099 return err;
1102 const struct got_error *
1103 got_diff_files(struct got_diffreg_result **resultp,
1104 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1105 const char *label2, int diff_context, int ignore_whitespace,
1106 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1108 const struct got_error *err = NULL;
1109 struct got_diffreg_result *diffreg_result = NULL;
1111 if (resultp)
1112 *resultp = NULL;
1114 if (outfile) {
1115 fprintf(outfile, "file - %s\n",
1116 f1_exists ? label1 : "/dev/null");
1117 fprintf(outfile, "file + %s\n",
1118 f2_exists ? label2 : "/dev/null");
1121 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1122 ignore_whitespace, force_text_diff);
1123 if (err)
1124 goto done;
1126 if (outfile) {
1127 err = got_diffreg_output(NULL, NULL, diffreg_result,
1128 f1_exists, f2_exists, label1, label2,
1129 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1130 if (err)
1131 goto done;
1134 done:
1135 if (resultp && err == NULL)
1136 *resultp = diffreg_result;
1137 else if (diffreg_result) {
1138 const struct got_error *free_err;
1139 free_err = got_diffreg_result_free(diffreg_result);
1140 if (free_err && err == NULL)
1141 err = free_err;
1144 return err;