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, struct stat *sb2, 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 char *mode = NULL;
244 /* display file mode for new added files only */
245 if (f2_exists && blob1 == NULL) {
246 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
248 if (S_ISLNK(sb2->st_mode))
249 mmask = S_IFLNK;
250 if (asprintf(&mode, " (mode %o)",
251 sb2->st_mode & mmask) == -1)
252 return got_error_from_errno("asprintf");
254 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
255 fprintf(outfile, "file + %s%s\n",
256 f2_exists ? label2 : "/dev/null", mode ? mode : "");
257 free(mode);
260 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
261 force_text_diff);
262 if (err)
263 goto done;
265 if (outfile) {
266 err = got_diffreg_output(NULL, NULL, result,
267 blob1 != NULL, f2_exists,
268 label2, /* show local file's path, not a blob ID */
269 label2, GOT_DIFF_OUTPUT_UNIDIFF,
270 diff_context, outfile);
271 if (err)
272 goto done;
275 if (resultp && err == NULL)
276 *resultp = result;
277 else if (result) {
278 free_err = got_diffreg_result_free(result);
279 if (free_err && err == NULL)
280 err = free_err;
282 done:
283 return err;
286 const struct got_error *
287 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
288 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
289 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
290 int ignore_whitespace, int force_text_diff, FILE *outfile)
292 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
293 sb2, label2, diff_algo, diff_context, ignore_whitespace,
294 force_text_diff, outfile);
297 static const struct got_error *
298 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
299 const char *label, mode_t mode, struct got_repository *repo,
300 got_diff_blob_cb cb, void *cb_arg)
302 const struct got_error *err;
303 struct got_blob_object *blob = NULL;
304 struct got_object *obj = NULL;
306 err = got_object_open(&obj, repo, id);
307 if (err)
308 return err;
310 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
311 if (err)
312 goto done;
313 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
314 NULL, label, 0, mode, repo);
315 done:
316 got_object_close(obj);
317 if (blob)
318 got_object_blob_close(blob);
319 return err;
322 static const struct got_error *
323 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
324 FILE *f1, FILE *f2, int fd1, int fd2,
325 const char *label1, const char *label2,
326 mode_t mode1, mode_t mode2, struct got_repository *repo,
327 got_diff_blob_cb cb, void *cb_arg)
329 const struct got_error *err;
330 struct got_object *obj1 = NULL;
331 struct got_object *obj2 = NULL;
332 struct got_blob_object *blob1 = NULL;
333 struct got_blob_object *blob2 = NULL;
335 err = got_object_open(&obj1, repo, id1);
336 if (err)
337 return err;
339 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
340 err = got_error(GOT_ERR_OBJ_TYPE);
341 goto done;
344 err = got_object_open(&obj2, repo, id2);
345 if (err)
346 goto done;
347 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
348 err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 goto done;
352 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
353 if (err)
354 goto done;
356 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
357 if (err)
358 goto done;
360 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
361 mode1, mode2, repo);
362 done:
363 if (obj1)
364 got_object_close(obj1);
365 if (obj2)
366 got_object_close(obj2);
367 if (blob1)
368 got_object_blob_close(blob1);
369 if (blob2)
370 got_object_blob_close(blob2);
371 return err;
374 static const struct got_error *
375 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
376 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
377 got_diff_blob_cb cb, void *cb_arg)
379 const struct got_error *err;
380 struct got_blob_object *blob = NULL;
381 struct got_object *obj = NULL;
383 err = got_object_open(&obj, repo, id);
384 if (err)
385 return err;
387 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
388 if (err)
389 goto done;
390 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
391 mode, 0, repo);
392 done:
393 got_object_close(obj);
394 if (blob)
395 got_object_blob_close(blob);
396 return err;
399 static const struct got_error *
400 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
401 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
402 void *cb_arg, int diff_content)
404 const struct got_error *err = NULL;
405 struct got_object *treeobj = NULL;
406 struct got_tree_object *tree = NULL;
408 err = got_object_open(&treeobj, repo, id);
409 if (err)
410 goto done;
412 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
413 err = got_error(GOT_ERR_OBJ_TYPE);
414 goto done;
417 err = got_object_tree_open(&tree, repo, treeobj);
418 if (err)
419 goto done;
421 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
422 repo, cb, cb_arg, diff_content);
423 done:
424 if (tree)
425 got_object_tree_close(tree);
426 if (treeobj)
427 got_object_close(treeobj);
428 return err;
431 static const struct got_error *
432 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
433 FILE *f1, FILE *f2, int fd1, int fd2,
434 const char *label1, const char *label2,
435 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
436 int diff_content)
438 const struct got_error *err;
439 struct got_object *treeobj1 = NULL;
440 struct got_object *treeobj2 = NULL;
441 struct got_tree_object *tree1 = NULL;
442 struct got_tree_object *tree2 = NULL;
444 err = got_object_open(&treeobj1, repo, id1);
445 if (err)
446 goto done;
448 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
449 err = got_error(GOT_ERR_OBJ_TYPE);
450 goto done;
453 err = got_object_open(&treeobj2, repo, id2);
454 if (err)
455 goto done;
457 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
458 err = got_error(GOT_ERR_OBJ_TYPE);
459 goto done;
462 err = got_object_tree_open(&tree1, repo, treeobj1);
463 if (err)
464 goto done;
466 err = got_object_tree_open(&tree2, repo, treeobj2);
467 if (err)
468 goto done;
470 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
471 label1, label2, repo, cb, cb_arg, diff_content);
473 done:
474 if (tree1)
475 got_object_tree_close(tree1);
476 if (tree2)
477 got_object_tree_close(tree2);
478 if (treeobj1)
479 got_object_close(treeobj1);
480 if (treeobj2)
481 got_object_close(treeobj2);
482 return err;
485 static const struct got_error *
486 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
487 FILE *f2, const char *label, struct got_repository *repo,
488 got_diff_blob_cb cb, void *cb_arg, int diff_content)
490 const struct got_error *err;
491 struct got_object *treeobj = NULL;
492 struct got_tree_object *tree = NULL;
494 err = got_object_open(&treeobj, repo, id);
495 if (err)
496 goto done;
498 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
499 err = got_error(GOT_ERR_OBJ_TYPE);
500 goto done;
503 err = got_object_tree_open(&tree, repo, treeobj);
504 if (err)
505 goto done;
507 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
508 repo, cb, cb_arg, diff_content);
509 done:
510 if (tree)
511 got_object_tree_close(tree);
512 if (treeobj)
513 got_object_close(treeobj);
514 return err;
517 static const struct got_error *
518 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
519 const char *label1, const char *label2, struct got_repository *repo,
520 got_diff_blob_cb cb, void *cb_arg)
522 /* XXX TODO */
523 return NULL;
526 static const struct got_error *
527 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
528 FILE *f1, FILE *f2, int fd1, int fd2,
529 const char *label1, const char *label2,
530 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
531 int diff_content)
533 const struct got_error *err = NULL;
534 int id_match;
536 if (got_object_tree_entry_is_submodule(te1))
537 return NULL;
539 if (te2 == NULL) {
540 if (S_ISDIR(te1->mode))
541 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
542 label1, repo, cb, cb_arg, diff_content);
543 else {
544 if (diff_content)
545 err = diff_deleted_blob(&te1->id, f1, fd1,
546 f2, label1, te1->mode, repo, cb, cb_arg);
547 else
548 err = cb(cb_arg, NULL, NULL, NULL, NULL,
549 &te1->id, NULL, label1, NULL,
550 te1->mode, 0, repo);
552 return err;
553 } else if (got_object_tree_entry_is_submodule(te2))
554 return NULL;
556 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
557 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
558 if (!id_match)
559 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
560 fd1, fd2, label1, label2, repo, cb, cb_arg,
561 diff_content);
562 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
563 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
564 if (!id_match ||
565 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
566 (te2->mode & (S_IFLNK | S_IXUSR))) {
567 if (diff_content)
568 return diff_modified_blob(&te1->id, &te2->id,
569 f1, f2, fd1, fd2, label1, label2,
570 te1->mode, te2->mode, repo, cb, cb_arg);
571 else
572 return cb(cb_arg, NULL, NULL, NULL, NULL,
573 &te1->id, &te2->id, label1, label2,
574 te1->mode, te2->mode, repo);
578 if (id_match)
579 return NULL;
581 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
582 cb, cb_arg);
585 static const struct got_error *
586 diff_entry_new_old(struct got_tree_entry *te2,
587 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
588 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
589 int diff_content)
591 if (te1 != NULL) /* handled by diff_entry_old_new() */
592 return NULL;
594 if (got_object_tree_entry_is_submodule(te2))
595 return NULL;
597 if (S_ISDIR(te2->mode))
598 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
599 repo, cb, cb_arg, diff_content);
601 if (diff_content)
602 return diff_added_blob(&te2->id, f1, f2, fd2,
603 label2, te2->mode, repo, cb, cb_arg);
605 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
606 NULL, label2, 0, te2->mode, repo);
609 const struct got_error *
610 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
611 struct got_blob_object *blob2, FILE *f1, FILE *f2,
612 struct got_object_id *id1, struct got_object_id *id2,
613 const char *label1, const char *label2,
614 mode_t mode1, mode_t mode2, struct got_repository *repo)
616 const struct got_error *err = NULL;
617 struct got_pathlist_head *paths = arg;
618 struct got_diff_changed_path *change = NULL;
619 char *path = NULL;
621 path = strdup(label2 ? label2 : label1);
622 if (path == NULL)
623 return got_error_from_errno("malloc");
625 change = malloc(sizeof(*change));
626 if (change == NULL) {
627 err = got_error_from_errno("malloc");
628 goto done;
631 change->status = GOT_STATUS_NO_CHANGE;
632 if (id1 == NULL)
633 change->status = GOT_STATUS_ADD;
634 else if (id2 == NULL)
635 change->status = GOT_STATUS_DELETE;
636 else {
637 if (got_object_id_cmp(id1, id2) != 0)
638 change->status = GOT_STATUS_MODIFY;
639 else if (mode1 != mode2)
640 change->status = GOT_STATUS_MODE_CHANGE;
643 err = got_pathlist_append(paths, path, change);
644 done:
645 if (err) {
646 free(path);
647 free(change);
649 return err;
652 const struct got_error *
653 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
654 FILE *f1, FILE *f2, int fd1, int fd2,
655 const char *label1, const char *label2,
656 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
657 int diff_content)
659 const struct got_error *err = NULL;
660 struct got_tree_entry *te1 = NULL;
661 struct got_tree_entry *te2 = NULL;
662 char *l1 = NULL, *l2 = NULL;
663 int tidx1 = 0, tidx2 = 0;
665 if (tree1) {
666 te1 = got_object_tree_get_entry(tree1, 0);
667 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
668 te1->name) == -1)
669 return got_error_from_errno("asprintf");
671 if (tree2) {
672 te2 = got_object_tree_get_entry(tree2, 0);
673 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
674 te2->name) == -1)
675 return got_error_from_errno("asprintf");
678 do {
679 if (te1) {
680 struct got_tree_entry *te = NULL;
681 if (tree2)
682 te = got_object_tree_find_entry(tree2,
683 te1->name);
684 if (te) {
685 free(l2);
686 l2 = NULL;
687 if (te && asprintf(&l2, "%s%s%s", label2,
688 label2[0] ? "/" : "", te->name) == -1)
689 return
690 got_error_from_errno("asprintf");
692 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
693 l1, l2, repo, cb, cb_arg, diff_content);
694 if (err)
695 break;
698 if (te2) {
699 struct got_tree_entry *te = NULL;
700 if (tree1)
701 te = got_object_tree_find_entry(tree1,
702 te2->name);
703 free(l2);
704 if (te) {
705 if (asprintf(&l2, "%s%s%s", label2,
706 label2[0] ? "/" : "", te->name) == -1)
707 return
708 got_error_from_errno("asprintf");
709 } else {
710 if (asprintf(&l2, "%s%s%s", label2,
711 label2[0] ? "/" : "", te2->name) == -1)
712 return
713 got_error_from_errno("asprintf");
715 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
716 repo, cb, cb_arg, diff_content);
717 if (err)
718 break;
721 free(l1);
722 l1 = NULL;
723 if (te1) {
724 tidx1++;
725 te1 = got_object_tree_get_entry(tree1, tidx1);
726 if (te1 &&
727 asprintf(&l1, "%s%s%s", label1,
728 label1[0] ? "/" : "", te1->name) == -1)
729 return got_error_from_errno("asprintf");
731 free(l2);
732 l2 = NULL;
733 if (te2) {
734 tidx2++;
735 te2 = got_object_tree_get_entry(tree2, tidx2);
736 if (te2 &&
737 asprintf(&l2, "%s%s%s", label2,
738 label2[0] ? "/" : "", te2->name) == -1)
739 return got_error_from_errno("asprintf");
741 } while (te1 || te2);
743 return err;
746 const struct got_error *
747 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
748 FILE *f1, FILE *f2, int fd1, int fd2,
749 struct got_object_id *id1, struct got_object_id *id2,
750 const char *label1, const char *label2,
751 enum got_diff_algorithm diff_algo, int diff_context,
752 int ignore_whitespace, int force_text_diff,
753 struct got_repository *repo, FILE *outfile)
755 const struct got_error *err;
756 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
758 if (id1 == NULL && id2 == NULL)
759 return got_error(GOT_ERR_NO_OBJ);
761 if (id1) {
762 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
763 if (err)
764 goto done;
766 if (id2) {
767 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
768 if (err)
769 goto done;
771 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
772 diff_algo, diff_context, ignore_whitespace, force_text_diff,
773 outfile);
774 done:
775 if (blob1)
776 got_object_blob_close(blob1);
777 if (blob2)
778 got_object_blob_close(blob2);
779 return err;
782 static const struct got_error *
783 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
784 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
785 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
787 const struct got_error *err = NULL;
788 struct got_pathlist_entry *pe;
789 struct got_object_id *id1 = NULL, *id2 = NULL;
790 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
791 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
793 TAILQ_FOREACH(pe, paths, entry) {
794 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
795 mode_t mode1 = 0, mode2 = 0;
797 free(id1);
798 id1 = NULL;
799 free(id2);
800 id2 = NULL;
801 if (subtree1) {
802 got_object_tree_close(subtree1);
803 subtree1 = NULL;
805 if (subtree2) {
806 got_object_tree_close(subtree2);
807 subtree2 = NULL;
809 if (blob1) {
810 got_object_blob_close(blob1);
811 blob1 = NULL;
813 if (blob2) {
814 got_object_blob_close(blob2);
815 blob2 = NULL;
818 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
819 pe->path);
820 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
821 goto done;
822 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
823 pe->path);
824 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
825 goto done;
826 if (id1 == NULL && id2 == NULL) {
827 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
828 goto done;
830 if (id1) {
831 err = got_object_get_type(&type1, repo, id1);
832 if (err)
833 goto done;
835 if (id2) {
836 err = got_object_get_type(&type2, repo, id2);
837 if (err)
838 goto done;
840 if (type1 == GOT_OBJ_TYPE_ANY &&
841 type2 == GOT_OBJ_TYPE_ANY) {
842 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
843 goto done;
844 } else if (type1 != GOT_OBJ_TYPE_ANY &&
845 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
846 err = got_error(GOT_ERR_OBJ_TYPE);
847 goto done;
850 if (type1 == GOT_OBJ_TYPE_BLOB ||
851 type2 == GOT_OBJ_TYPE_BLOB) {
852 if (id1) {
853 err = got_object_open_as_blob(&blob1, repo,
854 id1, 8192, fd1);
855 if (err)
856 goto done;
858 if (id2) {
859 err = got_object_open_as_blob(&blob2, repo,
860 id2, 8192, fd2);
861 if (err)
862 goto done;
864 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
865 id1 ? pe->path : "/dev/null",
866 id2 ? pe->path : "/dev/null",
867 mode1, mode2, repo);
868 if (err)
869 goto done;
870 } else if (type1 == GOT_OBJ_TYPE_TREE ||
871 type2 == GOT_OBJ_TYPE_TREE) {
872 if (id1) {
873 err = got_object_open_as_tree(&subtree1, repo,
874 id1);
875 if (err)
876 goto done;
878 if (id2) {
879 err = got_object_open_as_tree(&subtree2, repo,
880 id2);
881 if (err)
882 goto done;
884 err = got_diff_tree(subtree1, subtree2, f1, f2,
885 fd1, fd2,
886 id1 ? pe->path : "/dev/null",
887 id2 ? pe->path : "/dev/null",
888 repo, cb, cb_arg, 1);
889 if (err)
890 goto done;
891 } else {
892 err = got_error(GOT_ERR_OBJ_TYPE);
893 goto done;
896 done:
897 free(id1);
898 free(id2);
899 if (subtree1)
900 got_object_tree_close(subtree1);
901 if (subtree2)
902 got_object_tree_close(subtree2);
903 if (blob1)
904 got_object_blob_close(blob1);
905 if (blob2)
906 got_object_blob_close(blob2);
907 return err;
910 static const struct got_error *
911 show_object_id(struct got_diff_line **lines, size_t *nlines,
912 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
914 const struct got_error *err;
915 int n;
916 off_t outoff = 0;
918 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
919 if (n < 0)
920 return got_error_from_errno("fprintf");
922 if (lines != NULL && *lines != NULL) {
923 if (*nlines == 0) {
924 err = add_line_metadata(lines, nlines, 0,
925 GOT_DIFF_LINE_META);
926 if (err)
927 return err;
928 } else
929 outoff = (*lines)[*nlines - 1].offset;
931 outoff += n;
932 err = add_line_metadata(lines, nlines, outoff,
933 GOT_DIFF_LINE_META);
934 if (err)
935 return err;
938 return NULL;
941 static const struct got_error *
942 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
943 FILE *f1, FILE *f2, int fd1, int fd2,
944 struct got_object_id *id1, struct got_object_id *id2,
945 struct got_pathlist_head *paths, const char *label1, const char *label2,
946 int diff_context, int ignore_whitespace, int force_text_diff,
947 struct got_repository *repo, FILE *outfile,
948 enum got_diff_algorithm diff_algo)
950 const struct got_error *err;
951 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
952 struct got_diff_blob_output_unidiff_arg arg;
953 int want_linemeta = (lines != NULL && *lines != NULL);
955 if (id1 == NULL && id2 == NULL)
956 return got_error(GOT_ERR_NO_OBJ);
958 if (id1) {
959 err = got_object_open_as_tree(&tree1, repo, id1);
960 if (err)
961 goto done;
963 if (id2) {
964 err = got_object_open_as_tree(&tree2, repo, id2);
965 if (err)
966 goto done;
969 arg.diff_algo = diff_algo;
970 arg.diff_context = diff_context;
971 arg.ignore_whitespace = ignore_whitespace;
972 arg.force_text_diff = force_text_diff;
973 arg.outfile = outfile;
974 if (want_linemeta) {
975 arg.lines = *lines;
976 arg.nlines = *nlines;
977 } else {
978 arg.lines = NULL;
979 arg.nlines = 0;
981 if (paths == NULL || TAILQ_EMPTY(paths)) {
982 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
983 label1, label2, repo,
984 got_diff_blob_output_unidiff, &arg, 1);
985 } else {
986 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
987 got_diff_blob_output_unidiff, &arg);
989 if (want_linemeta) {
990 *lines = arg.lines; /* was likely re-allocated */
991 *nlines = arg.nlines;
993 done:
994 if (tree1)
995 got_object_tree_close(tree1);
996 if (tree2)
997 got_object_tree_close(tree2);
998 return err;
1001 const struct got_error *
1002 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1003 FILE *f1, FILE *f2, int fd1, int fd2,
1004 struct got_object_id *id1, struct got_object_id *id2,
1005 struct got_pathlist_head *paths, const char *label1, const char *label2,
1006 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1007 int force_text_diff, struct got_repository *repo, FILE *outfile)
1009 const struct got_error *err;
1010 char *idstr = NULL;
1012 if (id1 == NULL && id2 == NULL)
1013 return got_error(GOT_ERR_NO_OBJ);
1015 if (id1) {
1016 err = got_object_id_str(&idstr, id1);
1017 if (err)
1018 goto done;
1019 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1020 if (err)
1021 goto done;
1022 free(idstr);
1023 idstr = NULL;
1024 } else {
1025 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1026 outfile);
1027 if (err)
1028 goto done;
1031 if (id2) {
1032 err = got_object_id_str(&idstr, id2);
1033 if (err)
1034 goto done;
1035 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1036 if (err)
1037 goto done;
1038 free(idstr);
1039 idstr = NULL;
1040 } else {
1041 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1042 outfile);
1043 if (err)
1044 goto done;
1047 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1048 paths, label1, label2, diff_context, ignore_whitespace,
1049 force_text_diff, repo, outfile, diff_algo);
1050 done:
1051 free(idstr);
1052 return err;
1055 const struct got_error *
1056 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1057 FILE *f1, FILE *f2, int fd1, int fd2,
1058 struct got_object_id *id1, struct got_object_id *id2,
1059 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1060 int diff_context, int ignore_whitespace, int force_text_diff,
1061 struct got_repository *repo, FILE *outfile)
1063 const struct got_error *err;
1064 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1065 char *idstr = NULL;
1067 if (id2 == NULL)
1068 return got_error(GOT_ERR_NO_OBJ);
1070 if (id1) {
1071 err = got_object_open_as_commit(&commit1, repo, id1);
1072 if (err)
1073 goto done;
1074 err = got_object_id_str(&idstr, id1);
1075 if (err)
1076 goto done;
1077 err = show_object_id(lines, nlines, "commit", '-', idstr,
1078 outfile);
1079 if (err)
1080 goto done;
1081 free(idstr);
1082 idstr = NULL;
1083 } else {
1084 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1085 outfile);
1086 if (err)
1087 goto done;
1090 err = got_object_open_as_commit(&commit2, repo, id2);
1091 if (err)
1092 goto done;
1094 err = got_object_id_str(&idstr, id2);
1095 if (err)
1096 goto done;
1097 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1098 if (err)
1099 goto done;
1101 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1102 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1103 got_object_commit_get_tree_id(commit2), paths, "", "",
1104 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1105 diff_algo);
1106 done:
1107 if (commit1)
1108 got_object_commit_close(commit1);
1109 if (commit2)
1110 got_object_commit_close(commit2);
1111 free(idstr);
1112 return err;
1115 const struct got_error *
1116 got_diff_files(struct got_diffreg_result **resultp,
1117 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1118 const char *label2, int diff_context, int ignore_whitespace,
1119 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1121 const struct got_error *err = NULL;
1122 struct got_diffreg_result *diffreg_result = NULL;
1124 if (resultp)
1125 *resultp = NULL;
1127 if (outfile) {
1128 fprintf(outfile, "file - %s\n",
1129 f1_exists ? label1 : "/dev/null");
1130 fprintf(outfile, "file + %s\n",
1131 f2_exists ? label2 : "/dev/null");
1134 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1135 ignore_whitespace, force_text_diff);
1136 if (err)
1137 goto done;
1139 if (outfile) {
1140 err = got_diffreg_output(NULL, NULL, diffreg_result,
1141 f1_exists, f2_exists, label1, label2,
1142 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1143 if (err)
1144 goto done;
1147 done:
1148 if (resultp && err == NULL)
1149 *resultp = diffreg_result;
1150 else if (diffreg_result) {
1151 const struct got_error *free_err;
1152 free_err = got_diffreg_result_free(diffreg_result);
1153 if (free_err && err == NULL)
1154 err = free_err;
1157 return err;