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, struct stat *sb2, 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 char *mode = NULL;
245 /* display file mode for new added files only */
246 if (f2_exists && blob1 == NULL) {
247 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
249 if (S_ISLNK(sb2->st_mode))
250 mmask = S_IFLNK;
251 if (asprintf(&mode, " (mode %o)",
252 sb2->st_mode & mmask) == -1)
253 return got_error_from_errno("asprintf");
255 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
256 fprintf(outfile, "file + %s%s\n",
257 f2_exists ? label2 : "/dev/null", mode ? mode : "");
258 free(mode);
261 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
262 force_text_diff);
263 if (err)
264 goto done;
266 if (outfile) {
267 err = got_diffreg_output(NULL, NULL, result,
268 blob1 != NULL, f2_exists,
269 label2, /* show local file's path, not a blob ID */
270 label2, GOT_DIFF_OUTPUT_UNIDIFF,
271 diff_context, outfile);
272 if (err)
273 goto done;
276 if (resultp && err == NULL)
277 *resultp = result;
278 else if (result) {
279 free_err = got_diffreg_result_free(result);
280 if (free_err && err == NULL)
281 err = free_err;
283 done:
284 return err;
287 const struct got_error *
288 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
289 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
290 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
291 int ignore_whitespace, int force_text_diff, FILE *outfile)
293 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
294 sb2, label2, diff_algo, diff_context, ignore_whitespace,
295 force_text_diff, outfile);
298 static const struct got_error *
299 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
300 const char *label, mode_t mode, struct got_repository *repo,
301 got_diff_blob_cb cb, void *cb_arg)
303 const struct got_error *err;
304 struct got_blob_object *blob = NULL;
305 struct got_object *obj = NULL;
307 err = got_object_open(&obj, repo, id);
308 if (err)
309 return err;
311 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
312 if (err)
313 goto done;
314 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
315 NULL, label, 0, mode, repo);
316 done:
317 got_object_close(obj);
318 if (blob)
319 got_object_blob_close(blob);
320 return err;
323 static const struct got_error *
324 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
325 FILE *f1, FILE *f2, int fd1, int fd2,
326 const char *label1, const char *label2,
327 mode_t mode1, mode_t mode2, struct got_repository *repo,
328 got_diff_blob_cb cb, void *cb_arg)
330 const struct got_error *err;
331 struct got_object *obj1 = NULL;
332 struct got_object *obj2 = NULL;
333 struct got_blob_object *blob1 = NULL;
334 struct got_blob_object *blob2 = NULL;
336 err = got_object_open(&obj1, repo, id1);
337 if (err)
338 return err;
340 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 err = got_object_open(&obj2, repo, id2);
346 if (err)
347 goto done;
348 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
349 err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 goto done;
353 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
354 if (err)
355 goto done;
357 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
358 if (err)
359 goto done;
361 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
362 mode1, mode2, repo);
363 done:
364 if (obj1)
365 got_object_close(obj1);
366 if (obj2)
367 got_object_close(obj2);
368 if (blob1)
369 got_object_blob_close(blob1);
370 if (blob2)
371 got_object_blob_close(blob2);
372 return err;
375 static const struct got_error *
376 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
377 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
378 got_diff_blob_cb cb, void *cb_arg)
380 const struct got_error *err;
381 struct got_blob_object *blob = NULL;
382 struct got_object *obj = NULL;
384 err = got_object_open(&obj, repo, id);
385 if (err)
386 return err;
388 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
389 if (err)
390 goto done;
391 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
392 mode, 0, repo);
393 done:
394 got_object_close(obj);
395 if (blob)
396 got_object_blob_close(blob);
397 return err;
400 static const struct got_error *
401 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
402 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
403 void *cb_arg, int diff_content)
405 const struct got_error *err = NULL;
406 struct got_object *treeobj = NULL;
407 struct got_tree_object *tree = NULL;
409 err = got_object_open(&treeobj, repo, id);
410 if (err)
411 goto done;
413 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
414 err = got_error(GOT_ERR_OBJ_TYPE);
415 goto done;
418 err = got_object_tree_open(&tree, repo, treeobj);
419 if (err)
420 goto done;
422 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
423 repo, cb, cb_arg, diff_content);
424 done:
425 if (tree)
426 got_object_tree_close(tree);
427 if (treeobj)
428 got_object_close(treeobj);
429 return err;
432 static const struct got_error *
433 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
434 FILE *f1, FILE *f2, int fd1, int fd2,
435 const char *label1, const char *label2,
436 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
437 int diff_content)
439 const struct got_error *err;
440 struct got_object *treeobj1 = NULL;
441 struct got_object *treeobj2 = NULL;
442 struct got_tree_object *tree1 = NULL;
443 struct got_tree_object *tree2 = NULL;
445 err = got_object_open(&treeobj1, repo, id1);
446 if (err)
447 goto done;
449 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
450 err = got_error(GOT_ERR_OBJ_TYPE);
451 goto done;
454 err = got_object_open(&treeobj2, repo, id2);
455 if (err)
456 goto done;
458 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
459 err = got_error(GOT_ERR_OBJ_TYPE);
460 goto done;
463 err = got_object_tree_open(&tree1, repo, treeobj1);
464 if (err)
465 goto done;
467 err = got_object_tree_open(&tree2, repo, treeobj2);
468 if (err)
469 goto done;
471 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
472 label1, label2, repo, cb, cb_arg, diff_content);
474 done:
475 if (tree1)
476 got_object_tree_close(tree1);
477 if (tree2)
478 got_object_tree_close(tree2);
479 if (treeobj1)
480 got_object_close(treeobj1);
481 if (treeobj2)
482 got_object_close(treeobj2);
483 return err;
486 static const struct got_error *
487 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
488 FILE *f2, const char *label, struct got_repository *repo,
489 got_diff_blob_cb cb, void *cb_arg, int diff_content)
491 const struct got_error *err;
492 struct got_object *treeobj = NULL;
493 struct got_tree_object *tree = NULL;
495 err = got_object_open(&treeobj, repo, id);
496 if (err)
497 goto done;
499 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
500 err = got_error(GOT_ERR_OBJ_TYPE);
501 goto done;
504 err = got_object_tree_open(&tree, repo, treeobj);
505 if (err)
506 goto done;
508 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
509 repo, cb, cb_arg, diff_content);
510 done:
511 if (tree)
512 got_object_tree_close(tree);
513 if (treeobj)
514 got_object_close(treeobj);
515 return err;
518 static const struct got_error *
519 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
520 const char *label1, const char *label2, struct got_repository *repo,
521 got_diff_blob_cb cb, void *cb_arg)
523 /* XXX TODO */
524 return NULL;
527 static const struct got_error *
528 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
529 FILE *f1, FILE *f2, int fd1, int fd2,
530 const char *label1, const char *label2,
531 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
532 int diff_content)
534 const struct got_error *err = NULL;
535 int id_match;
537 if (got_object_tree_entry_is_submodule(te1))
538 return NULL;
540 if (te2 == NULL) {
541 if (S_ISDIR(te1->mode))
542 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
543 label1, repo, cb, cb_arg, diff_content);
544 else {
545 if (diff_content)
546 err = diff_deleted_blob(&te1->id, f1, fd1,
547 f2, label1, te1->mode, repo, cb, cb_arg);
548 else
549 err = cb(cb_arg, NULL, NULL, NULL, NULL,
550 &te1->id, NULL, label1, NULL,
551 te1->mode, 0, repo);
553 return err;
554 } else if (got_object_tree_entry_is_submodule(te2))
555 return NULL;
557 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
558 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
559 if (!id_match)
560 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
561 fd1, fd2, label1, label2, repo, cb, cb_arg,
562 diff_content);
563 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
564 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
565 if (!id_match ||
566 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
567 (te2->mode & (S_IFLNK | S_IXUSR))) {
568 if (diff_content)
569 return diff_modified_blob(&te1->id, &te2->id,
570 f1, f2, fd1, fd2, label1, label2,
571 te1->mode, te2->mode, repo, cb, cb_arg);
572 else
573 return cb(cb_arg, NULL, NULL, NULL, NULL,
574 &te1->id, &te2->id, label1, label2,
575 te1->mode, te2->mode, repo);
579 if (id_match)
580 return NULL;
582 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
583 cb, cb_arg);
586 static const struct got_error *
587 diff_entry_new_old(struct got_tree_entry *te2,
588 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
589 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
590 int diff_content)
592 if (te1 != NULL) /* handled by diff_entry_old_new() */
593 return NULL;
595 if (got_object_tree_entry_is_submodule(te2))
596 return NULL;
598 if (S_ISDIR(te2->mode))
599 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
600 repo, cb, cb_arg, diff_content);
602 if (diff_content)
603 return diff_added_blob(&te2->id, f1, f2, fd2,
604 label2, te2->mode, repo, cb, cb_arg);
606 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
607 NULL, label2, 0, te2->mode, repo);
610 const struct got_error *
611 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
612 struct got_blob_object *blob2, FILE *f1, FILE *f2,
613 struct got_object_id *id1, struct got_object_id *id2,
614 const char *label1, const char *label2,
615 mode_t mode1, mode_t mode2, struct got_repository *repo)
617 const struct got_error *err = NULL;
618 struct got_pathlist_head *paths = arg;
619 struct got_diff_changed_path *change = NULL;
620 char *path = NULL;
622 path = strdup(label2 ? label2 : label1);
623 if (path == NULL)
624 return got_error_from_errno("malloc");
626 change = malloc(sizeof(*change));
627 if (change == NULL) {
628 err = got_error_from_errno("malloc");
629 goto done;
632 change->status = GOT_STATUS_NO_CHANGE;
633 if (id1 == NULL)
634 change->status = GOT_STATUS_ADD;
635 else if (id2 == NULL)
636 change->status = GOT_STATUS_DELETE;
637 else {
638 if (got_object_id_cmp(id1, id2) != 0)
639 change->status = GOT_STATUS_MODIFY;
640 else if (mode1 != mode2)
641 change->status = GOT_STATUS_MODE_CHANGE;
644 err = got_pathlist_append(paths, path, change);
645 done:
646 if (err) {
647 free(path);
648 free(change);
650 return err;
653 const struct got_error *
654 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
655 FILE *f1, FILE *f2, int fd1, int fd2,
656 const char *label1, const char *label2,
657 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
658 int diff_content)
660 const struct got_error *err = NULL;
661 struct got_tree_entry *te1 = NULL;
662 struct got_tree_entry *te2 = NULL;
663 char *l1 = NULL, *l2 = NULL;
664 int tidx1 = 0, tidx2 = 0;
666 if (tree1) {
667 te1 = got_object_tree_get_entry(tree1, 0);
668 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
669 te1->name) == -1)
670 return got_error_from_errno("asprintf");
672 if (tree2) {
673 te2 = got_object_tree_get_entry(tree2, 0);
674 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
675 te2->name) == -1)
676 return got_error_from_errno("asprintf");
679 do {
680 if (te1) {
681 struct got_tree_entry *te = NULL;
682 if (tree2)
683 te = got_object_tree_find_entry(tree2,
684 te1->name);
685 if (te) {
686 free(l2);
687 l2 = NULL;
688 if (te && asprintf(&l2, "%s%s%s", label2,
689 label2[0] ? "/" : "", te->name) == -1)
690 return
691 got_error_from_errno("asprintf");
693 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
694 l1, l2, repo, cb, cb_arg, diff_content);
695 if (err)
696 break;
699 if (te2) {
700 struct got_tree_entry *te = NULL;
701 if (tree1)
702 te = got_object_tree_find_entry(tree1,
703 te2->name);
704 free(l2);
705 if (te) {
706 if (asprintf(&l2, "%s%s%s", label2,
707 label2[0] ? "/" : "", te->name) == -1)
708 return
709 got_error_from_errno("asprintf");
710 } else {
711 if (asprintf(&l2, "%s%s%s", label2,
712 label2[0] ? "/" : "", te2->name) == -1)
713 return
714 got_error_from_errno("asprintf");
716 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
717 repo, cb, cb_arg, diff_content);
718 if (err)
719 break;
722 free(l1);
723 l1 = NULL;
724 if (te1) {
725 tidx1++;
726 te1 = got_object_tree_get_entry(tree1, tidx1);
727 if (te1 &&
728 asprintf(&l1, "%s%s%s", label1,
729 label1[0] ? "/" : "", te1->name) == -1)
730 return got_error_from_errno("asprintf");
732 free(l2);
733 l2 = NULL;
734 if (te2) {
735 tidx2++;
736 te2 = got_object_tree_get_entry(tree2, tidx2);
737 if (te2 &&
738 asprintf(&l2, "%s%s%s", label2,
739 label2[0] ? "/" : "", te2->name) == -1)
740 return got_error_from_errno("asprintf");
742 } while (te1 || te2);
744 return err;
747 const struct got_error *
748 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
749 FILE *f1, FILE *f2, int fd1, int fd2,
750 struct got_object_id *id1, struct got_object_id *id2,
751 const char *label1, const char *label2,
752 enum got_diff_algorithm diff_algo, int diff_context,
753 int ignore_whitespace, int force_text_diff,
754 struct got_repository *repo, FILE *outfile)
756 const struct got_error *err;
757 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
759 if (id1 == NULL && id2 == NULL)
760 return got_error(GOT_ERR_NO_OBJ);
762 if (id1) {
763 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
764 if (err)
765 goto done;
767 if (id2) {
768 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
769 if (err)
770 goto done;
772 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
773 diff_algo, diff_context, ignore_whitespace, force_text_diff,
774 outfile);
775 done:
776 if (blob1)
777 got_object_blob_close(blob1);
778 if (blob2)
779 got_object_blob_close(blob2);
780 return err;
783 static const struct got_error *
784 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
785 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
786 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
788 const struct got_error *err = NULL;
789 struct got_pathlist_entry *pe;
790 struct got_object_id *id1 = NULL, *id2 = NULL;
791 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
792 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
794 TAILQ_FOREACH(pe, paths, entry) {
795 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
796 mode_t mode1 = 0, mode2 = 0;
798 free(id1);
799 id1 = NULL;
800 free(id2);
801 id2 = NULL;
802 if (subtree1) {
803 got_object_tree_close(subtree1);
804 subtree1 = NULL;
806 if (subtree2) {
807 got_object_tree_close(subtree2);
808 subtree2 = NULL;
810 if (blob1) {
811 got_object_blob_close(blob1);
812 blob1 = NULL;
814 if (blob2) {
815 got_object_blob_close(blob2);
816 blob2 = NULL;
819 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
820 pe->path);
821 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
822 goto done;
823 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
824 pe->path);
825 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
826 goto done;
827 if (id1 == NULL && id2 == NULL) {
828 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
829 goto done;
831 if (id1) {
832 err = got_object_get_type(&type1, repo, id1);
833 if (err)
834 goto done;
836 if (id2) {
837 err = got_object_get_type(&type2, repo, id2);
838 if (err)
839 goto done;
841 if (type1 == GOT_OBJ_TYPE_ANY &&
842 type2 == GOT_OBJ_TYPE_ANY) {
843 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
844 goto done;
845 } else if (type1 != GOT_OBJ_TYPE_ANY &&
846 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
847 err = got_error(GOT_ERR_OBJ_TYPE);
848 goto done;
851 if (type1 == GOT_OBJ_TYPE_BLOB ||
852 type2 == GOT_OBJ_TYPE_BLOB) {
853 if (id1) {
854 err = got_object_open_as_blob(&blob1, repo,
855 id1, 8192, fd1);
856 if (err)
857 goto done;
859 if (id2) {
860 err = got_object_open_as_blob(&blob2, repo,
861 id2, 8192, fd2);
862 if (err)
863 goto done;
865 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
866 id1 ? pe->path : "/dev/null",
867 id2 ? pe->path : "/dev/null",
868 mode1, mode2, repo);
869 if (err)
870 goto done;
871 } else if (type1 == GOT_OBJ_TYPE_TREE ||
872 type2 == GOT_OBJ_TYPE_TREE) {
873 if (id1) {
874 err = got_object_open_as_tree(&subtree1, repo,
875 id1);
876 if (err)
877 goto done;
879 if (id2) {
880 err = got_object_open_as_tree(&subtree2, repo,
881 id2);
882 if (err)
883 goto done;
885 err = got_diff_tree(subtree1, subtree2, f1, f2,
886 fd1, fd2,
887 id1 ? pe->path : "/dev/null",
888 id2 ? pe->path : "/dev/null",
889 repo, cb, cb_arg, 1);
890 if (err)
891 goto done;
892 } else {
893 err = got_error(GOT_ERR_OBJ_TYPE);
894 goto done;
897 done:
898 free(id1);
899 free(id2);
900 if (subtree1)
901 got_object_tree_close(subtree1);
902 if (subtree2)
903 got_object_tree_close(subtree2);
904 if (blob1)
905 got_object_blob_close(blob1);
906 if (blob2)
907 got_object_blob_close(blob2);
908 return err;
911 static const struct got_error *
912 show_object_id(struct got_diff_line **lines, size_t *nlines,
913 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
915 const struct got_error *err;
916 int n;
917 off_t outoff = 0;
919 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
920 if (n < 0)
921 return got_error_from_errno("fprintf");
923 if (lines != NULL && *lines != NULL) {
924 if (*nlines == 0) {
925 err = add_line_metadata(lines, nlines, 0,
926 GOT_DIFF_LINE_META);
927 if (err)
928 return err;
929 } else
930 outoff = (*lines)[*nlines - 1].offset;
932 outoff += n;
933 err = add_line_metadata(lines, nlines, outoff,
934 GOT_DIFF_LINE_META);
935 if (err)
936 return err;
939 return NULL;
942 static const struct got_error *
943 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
944 FILE *f1, FILE *f2, int fd1, int fd2,
945 struct got_object_id *id1, struct got_object_id *id2,
946 struct got_pathlist_head *paths, const char *label1, const char *label2,
947 int diff_context, int ignore_whitespace, int force_text_diff,
948 struct got_repository *repo, FILE *outfile,
949 enum got_diff_algorithm diff_algo)
951 const struct got_error *err;
952 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
953 struct got_diff_blob_output_unidiff_arg arg;
954 int want_linemeta = (lines != NULL && *lines != NULL);
956 if (id1 == NULL && id2 == NULL)
957 return got_error(GOT_ERR_NO_OBJ);
959 if (id1) {
960 err = got_object_open_as_tree(&tree1, repo, id1);
961 if (err)
962 goto done;
964 if (id2) {
965 err = got_object_open_as_tree(&tree2, repo, id2);
966 if (err)
967 goto done;
970 arg.diff_algo = diff_algo;
971 arg.diff_context = diff_context;
972 arg.ignore_whitespace = ignore_whitespace;
973 arg.force_text_diff = force_text_diff;
974 arg.outfile = outfile;
975 if (want_linemeta) {
976 arg.lines = *lines;
977 arg.nlines = *nlines;
978 } else {
979 arg.lines = NULL;
980 arg.nlines = 0;
982 if (paths == NULL || TAILQ_EMPTY(paths)) {
983 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
984 label1, label2, repo,
985 got_diff_blob_output_unidiff, &arg, 1);
986 } else {
987 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
988 got_diff_blob_output_unidiff, &arg);
990 if (want_linemeta) {
991 *lines = arg.lines; /* was likely re-allocated */
992 *nlines = arg.nlines;
994 done:
995 if (tree1)
996 got_object_tree_close(tree1);
997 if (tree2)
998 got_object_tree_close(tree2);
999 return err;
1002 const struct got_error *
1003 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1004 FILE *f1, FILE *f2, int fd1, int fd2,
1005 struct got_object_id *id1, struct got_object_id *id2,
1006 struct got_pathlist_head *paths, const char *label1, const char *label2,
1007 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1008 int force_text_diff, struct got_repository *repo, FILE *outfile)
1010 const struct got_error *err;
1011 char *idstr = NULL;
1013 if (id1 == NULL && id2 == NULL)
1014 return got_error(GOT_ERR_NO_OBJ);
1016 if (id1) {
1017 err = got_object_id_str(&idstr, id1);
1018 if (err)
1019 goto done;
1020 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1021 if (err)
1022 goto done;
1023 free(idstr);
1024 idstr = NULL;
1025 } else {
1026 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1027 outfile);
1028 if (err)
1029 goto done;
1032 if (id2) {
1033 err = got_object_id_str(&idstr, id2);
1034 if (err)
1035 goto done;
1036 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1037 if (err)
1038 goto done;
1039 free(idstr);
1040 idstr = NULL;
1041 } else {
1042 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1043 outfile);
1044 if (err)
1045 goto done;
1048 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1049 paths, label1, label2, diff_context, ignore_whitespace,
1050 force_text_diff, repo, outfile, diff_algo);
1051 done:
1052 free(idstr);
1053 return err;
1056 const struct got_error *
1057 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1058 FILE *f1, FILE *f2, int fd1, int fd2,
1059 struct got_object_id *id1, struct got_object_id *id2,
1060 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1061 int diff_context, int ignore_whitespace, int force_text_diff,
1062 struct got_repository *repo, FILE *outfile)
1064 const struct got_error *err;
1065 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1066 char *idstr = NULL;
1068 if (id2 == NULL)
1069 return got_error(GOT_ERR_NO_OBJ);
1071 if (id1) {
1072 err = got_object_open_as_commit(&commit1, repo, id1);
1073 if (err)
1074 goto done;
1075 err = got_object_id_str(&idstr, id1);
1076 if (err)
1077 goto done;
1078 err = show_object_id(lines, nlines, "commit", '-', idstr,
1079 outfile);
1080 if (err)
1081 goto done;
1082 free(idstr);
1083 idstr = NULL;
1084 } else {
1085 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1086 outfile);
1087 if (err)
1088 goto done;
1091 err = got_object_open_as_commit(&commit2, repo, id2);
1092 if (err)
1093 goto done;
1095 err = got_object_id_str(&idstr, id2);
1096 if (err)
1097 goto done;
1098 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1099 if (err)
1100 goto done;
1102 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1103 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1104 got_object_commit_get_tree_id(commit2), paths, "", "",
1105 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1106 diff_algo);
1107 done:
1108 if (commit1)
1109 got_object_commit_close(commit1);
1110 if (commit2)
1111 got_object_commit_close(commit2);
1112 free(idstr);
1113 return err;
1116 const struct got_error *
1117 got_diff_files(struct got_diffreg_result **resultp,
1118 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1119 const char *label2, int diff_context, int ignore_whitespace,
1120 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1122 const struct got_error *err = NULL;
1123 struct got_diffreg_result *diffreg_result = NULL;
1125 if (resultp)
1126 *resultp = NULL;
1128 if (outfile) {
1129 fprintf(outfile, "file - %s\n",
1130 f1_exists ? label1 : "/dev/null");
1131 fprintf(outfile, "file + %s\n",
1132 f2_exists ? label2 : "/dev/null");
1135 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1136 ignore_whitespace, force_text_diff);
1137 if (err)
1138 goto done;
1140 if (outfile) {
1141 err = got_diffreg_output(NULL, NULL, diffreg_result,
1142 f1_exists, f2_exists, label1, label2,
1143 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1144 if (err)
1145 goto done;
1148 done:
1149 if (resultp && err == NULL)
1150 *resultp = diffreg_result;
1151 else if (diffreg_result) {
1152 const struct got_error *free_err;
1153 free_err = got_diffreg_result_free(diffreg_result);
1154 if (free_err && err == NULL)
1155 err = free_err;
1158 return err;