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 #ifndef MAX
43 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
44 #endif
46 static const struct got_error *
47 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 off_t off, uint8_t type)
49 {
50 struct got_diff_line *p;
52 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 if (p == NULL)
54 return got_error_from_errno("reallocarray");
55 *lines = p;
56 (*lines)[*nlines].offset = off;
57 (*lines)[*nlines].type = type;
58 (*nlines)++;
60 return NULL;
61 }
63 static void
64 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
65 uint32_t add, uint32_t rm)
66 {
67 int d1 = 1, d2 = 1;
69 if (maxlen)
70 *maxlen = MAX(*maxlen, len);
72 while (add /= 10)
73 ++d1;
74 *add_cols = MAX(*add_cols, d1);
76 while (rm /= 10)
77 ++d2;
78 *rm_cols = MAX(*rm_cols, d2);
79 }
81 static const struct got_error *
82 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
83 struct diff_result *r, int force_text, int status)
84 {
85 const struct got_error *err;
86 struct got_pathlist_entry *pe;
87 struct got_diff_changed_path *change = NULL;
88 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
89 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
90 int i;
92 change = calloc(1, sizeof(*change));
93 if (change == NULL)
94 return got_error_from_errno("calloc");
96 if (!isbin || force_text) {
97 for (i = 0; i < r->chunks.len; ++i) {
98 struct diff_chunk *c;
99 int clc, crc;
101 c = diff_chunk_get(r, i);
102 clc = diff_chunk_get_left_count(c);
103 crc = diff_chunk_get_right_count(c);
105 if (crc && !clc)
106 change->add += crc;
107 if (clc && !crc)
108 change->rm += clc;
112 change->status = status;
113 ds->ins += change->add;
114 ds->del += change->rm;
115 ++ds->nfiles;
117 err = got_pathlist_append(ds->paths, path, change);
118 if (err) {
119 free(change);
120 return err;
123 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
124 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
125 pe->path_len, change->add, change->rm);
127 return NULL;
130 static const struct got_error *
131 diff_blobs(struct got_diff_line **lines, size_t *nlines,
132 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
133 struct got_blob_object *blob2, FILE *f1, FILE *f2,
134 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
135 int diff_context, int ignore_whitespace, int force_text_diff,
136 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
137 enum got_diff_algorithm diff_algo)
139 const struct got_error *err = NULL, *free_err;
140 char hex1[SHA1_DIGEST_STRING_LENGTH];
141 char hex2[SHA1_DIGEST_STRING_LENGTH];
142 const char *idstr1 = NULL, *idstr2 = NULL;
143 char *modestr1 = NULL, *modestr2 = NULL;
144 off_t size1, size2;
145 struct got_diffreg_result *result = NULL;
146 off_t outoff = 0;
147 int n;
149 if (lines && *lines && *nlines > 0)
150 outoff = (*lines)[*nlines - 1].offset;
151 else if (lines) {
152 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
153 if (err)
154 goto done;
157 if (resultp)
158 *resultp = NULL;
160 if (f1) {
161 err = got_opentemp_truncate(f1);
162 if (err)
163 goto done;
165 if (f2) {
166 err = got_opentemp_truncate(f2);
167 if (err)
168 goto done;
171 size1 = 0;
172 if (blob1) {
173 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
174 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
175 blob1);
176 if (err)
177 goto done;
178 } else
179 idstr1 = "/dev/null";
181 size2 = 0;
182 if (blob2) {
183 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
184 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
185 blob2);
186 if (err)
187 goto done;
188 } else
189 idstr2 = "/dev/null";
191 if (outfile) {
192 int modebits;
194 if (mode1 && mode1 != mode2) {
195 if (S_ISLNK(mode1))
196 modebits = S_IFLNK;
197 else
198 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
199 if (asprintf(&modestr1, " (mode %o)",
200 mode1 & modebits) == -1) {
201 err = got_error_from_errno("asprintf");
202 goto done;
205 if (mode2 && mode1 != mode2) {
206 if (S_ISLNK(mode2))
207 modebits = S_IFLNK;
208 else
209 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
210 if (asprintf(&modestr2, " (mode %o)",
211 mode2 & modebits) == -1) {
212 err = got_error_from_errno("asprintf");
213 goto done;
216 n = fprintf(outfile, "blob - %s%s\n", idstr1,
217 modestr1 ? modestr1 : "");
218 if (n < 0)
219 goto done;
220 outoff += n;
221 if (lines) {
222 err = add_line_metadata(lines, nlines, outoff,
223 GOT_DIFF_LINE_BLOB_MIN);
224 if (err)
225 goto done;
228 n = fprintf(outfile, "blob + %s%s\n", idstr2,
229 modestr2 ? modestr2 : "");
230 if (n < 0)
231 goto done;
232 outoff += n;
233 if (lines) {
234 err = add_line_metadata(lines, nlines, outoff,
235 GOT_DIFF_LINE_BLOB_PLUS);
236 if (err)
237 goto done;
241 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
242 force_text_diff);
243 if (err)
244 goto done;
246 if (show_diffstat) {
247 char *path = NULL;
248 int status = GOT_STATUS_NO_CHANGE;
250 /*
251 * Ignore 'm'ode status change: if there's no accompanying
252 * content change, there'll be no diffstat, and if there
253 * are actual changes, 'M'odified takes precedence.
254 */
255 if (blob1 == NULL)
256 status = GOT_STATUS_ADD;
257 else if (blob2 == NULL)
258 status = GOT_STATUS_DELETE;
259 else
260 status = GOT_STATUS_MODIFY;
262 if (label1 == NULL && label2 == NULL) {
263 /* diffstat of blobs, show hash instead of path */
264 if (asprintf(&path, "%.10s -> %.10s",
265 idstr1, idstr2) == -1) {
266 err = got_error_from_errno("asprintf");
267 goto done;
269 } else {
270 if (label2 != NULL &&
271 (status != GOT_STATUS_DELETE || label1 == NULL))
272 path = strdup(label2);
273 else
274 path = strdup(label1);
275 if (path == NULL) {
276 err = got_error_from_errno("strdup");
277 goto done;
281 err = get_diffstat(ds, path, result->result, force_text_diff,
282 status);
283 if (err) {
284 free(path);
285 goto done;
289 if (outfile) {
290 err = got_diffreg_output(lines, nlines, result,
291 blob1 != NULL, blob2 != NULL,
292 label1 ? label1 : idstr1,
293 label2 ? label2 : idstr2,
294 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
295 if (err)
296 goto done;
299 done:
300 free(modestr1);
301 free(modestr2);
302 if (resultp && err == NULL)
303 *resultp = result;
304 else if (result) {
305 free_err = got_diffreg_result_free(result);
306 if (free_err && err == NULL)
307 err = free_err;
310 return err;
313 const struct got_error *
314 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
315 struct got_blob_object *blob2, FILE *f1, FILE *f2,
316 struct got_object_id *id1, struct got_object_id *id2,
317 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
318 struct got_repository *repo)
320 struct got_diff_blob_output_unidiff_arg *a = arg;
322 return diff_blobs(&a->lines, &a->nlines, NULL,
323 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
324 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
325 a->diffstat, a->outfile, a->diff_algo);
328 const struct got_error *
329 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
330 struct got_blob_object *blob1, struct got_blob_object *blob2,
331 FILE *f1, FILE *f2, const char *label1, const char *label2,
332 enum got_diff_algorithm diff_algo, int diff_context,
333 int ignore_whitespace, int force_text_diff, int show_diffstat,
334 struct got_diffstat_cb_arg *ds, FILE *outfile)
336 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
337 label1, label2, 0, 0, diff_context, ignore_whitespace,
338 force_text_diff, show_diffstat, ds, outfile, diff_algo);
341 static const struct got_error *
342 diff_blob_file(struct got_diffreg_result **resultp,
343 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
344 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
345 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
346 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
347 FILE *outfile)
349 const struct got_error *err = NULL, *free_err;
350 char hex1[SHA1_DIGEST_STRING_LENGTH];
351 const char *idstr1 = NULL;
352 struct got_diffreg_result *result = NULL;
354 if (resultp)
355 *resultp = NULL;
357 if (blob1)
358 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
359 else
360 idstr1 = "/dev/null";
362 if (outfile) {
363 char *mode = NULL;
365 /* display file mode for new added files only */
366 if (f2_exists && blob1 == NULL) {
367 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
369 if (S_ISLNK(sb2->st_mode))
370 mmask = S_IFLNK;
371 if (asprintf(&mode, " (mode %o)",
372 sb2->st_mode & mmask) == -1)
373 return got_error_from_errno("asprintf");
375 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
376 fprintf(outfile, "file + %s%s\n",
377 f2_exists ? label2 : "/dev/null", mode ? mode : "");
378 free(mode);
381 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
382 force_text_diff);
383 if (err)
384 goto done;
386 if (outfile) {
387 err = got_diffreg_output(NULL, NULL, result,
388 blob1 != NULL, f2_exists,
389 label2, /* show local file's path, not a blob ID */
390 label2, GOT_DIFF_OUTPUT_UNIDIFF,
391 diff_context, outfile);
392 if (err)
393 goto done;
396 if (show_diffstat) {
397 char *path = NULL;
398 int status = GOT_STATUS_NO_CHANGE;
400 /*
401 * Ignore 'm'ode status change: if there's no accompanying
402 * content change, there'll be no diffstat, and if there
403 * are actual changes, 'M'odified takes precedence.
404 */
405 if (blob1 == NULL)
406 status = GOT_STATUS_ADD;
407 else if (!f2_exists)
408 status = GOT_STATUS_DELETE;
409 else
410 status = GOT_STATUS_MODIFY;
412 if (label2 != NULL &&
413 (status != GOT_STATUS_DELETE || label1 == NULL))
414 path = strdup(label2);
415 else
416 path = strdup(label1);
417 if (path == NULL) {
418 err = got_error_from_errno("strdup");
419 goto done;
422 err = get_diffstat(ds, path, result->result, force_text_diff,
423 status);
424 if (err) {
425 free(path);
426 goto done;
430 done:
431 if (resultp && err == NULL)
432 *resultp = result;
433 else if (result) {
434 free_err = got_diffreg_result_free(result);
435 if (free_err && err == NULL)
436 err = free_err;
438 return err;
441 const struct got_error *
442 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
443 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
444 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
445 int ignore_whitespace, int force_text_diff, int show_diffstat,
446 struct got_diffstat_cb_arg *ds, FILE *outfile)
448 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
449 sb2, label2, diff_algo, diff_context, ignore_whitespace,
450 force_text_diff, show_diffstat, ds, outfile);
453 static const struct got_error *
454 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
455 const char *label, mode_t mode, struct got_repository *repo,
456 got_diff_blob_cb cb, void *cb_arg)
458 const struct got_error *err;
459 struct got_blob_object *blob = NULL;
460 struct got_object *obj = NULL;
462 err = got_object_open(&obj, repo, id);
463 if (err)
464 return err;
466 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
467 if (err)
468 goto done;
469 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
470 NULL, label, 0, mode, repo);
471 done:
472 got_object_close(obj);
473 if (blob)
474 got_object_blob_close(blob);
475 return err;
478 static const struct got_error *
479 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
480 FILE *f1, FILE *f2, int fd1, int fd2,
481 const char *label1, const char *label2,
482 mode_t mode1, mode_t mode2, struct got_repository *repo,
483 got_diff_blob_cb cb, void *cb_arg)
485 const struct got_error *err;
486 struct got_object *obj1 = NULL;
487 struct got_object *obj2 = NULL;
488 struct got_blob_object *blob1 = NULL;
489 struct got_blob_object *blob2 = NULL;
491 err = got_object_open(&obj1, repo, id1);
492 if (err)
493 return err;
495 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
496 err = got_error(GOT_ERR_OBJ_TYPE);
497 goto done;
500 err = got_object_open(&obj2, repo, id2);
501 if (err)
502 goto done;
503 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 goto done;
508 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
509 if (err)
510 goto done;
512 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
513 if (err)
514 goto done;
516 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
517 mode1, mode2, repo);
518 done:
519 if (obj1)
520 got_object_close(obj1);
521 if (obj2)
522 got_object_close(obj2);
523 if (blob1)
524 got_object_blob_close(blob1);
525 if (blob2)
526 got_object_blob_close(blob2);
527 return err;
530 static const struct got_error *
531 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
532 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
533 got_diff_blob_cb cb, void *cb_arg)
535 const struct got_error *err;
536 struct got_blob_object *blob = NULL;
537 struct got_object *obj = NULL;
539 err = got_object_open(&obj, repo, id);
540 if (err)
541 return err;
543 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
544 if (err)
545 goto done;
546 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
547 mode, 0, repo);
548 done:
549 got_object_close(obj);
550 if (blob)
551 got_object_blob_close(blob);
552 return err;
555 static const struct got_error *
556 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
557 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
558 void *cb_arg, int diff_content)
560 const struct got_error *err = NULL;
561 struct got_object *treeobj = NULL;
562 struct got_tree_object *tree = NULL;
564 err = got_object_open(&treeobj, repo, id);
565 if (err)
566 goto done;
568 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
570 goto done;
573 err = got_object_tree_open(&tree, repo, treeobj);
574 if (err)
575 goto done;
577 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
578 repo, cb, cb_arg, diff_content);
579 done:
580 if (tree)
581 got_object_tree_close(tree);
582 if (treeobj)
583 got_object_close(treeobj);
584 return err;
587 static const struct got_error *
588 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
589 FILE *f1, FILE *f2, int fd1, int fd2,
590 const char *label1, const char *label2,
591 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
592 int diff_content)
594 const struct got_error *err;
595 struct got_object *treeobj1 = NULL;
596 struct got_object *treeobj2 = NULL;
597 struct got_tree_object *tree1 = NULL;
598 struct got_tree_object *tree2 = NULL;
600 err = got_object_open(&treeobj1, repo, id1);
601 if (err)
602 goto done;
604 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
605 err = got_error(GOT_ERR_OBJ_TYPE);
606 goto done;
609 err = got_object_open(&treeobj2, repo, id2);
610 if (err)
611 goto done;
613 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
614 err = got_error(GOT_ERR_OBJ_TYPE);
615 goto done;
618 err = got_object_tree_open(&tree1, repo, treeobj1);
619 if (err)
620 goto done;
622 err = got_object_tree_open(&tree2, repo, treeobj2);
623 if (err)
624 goto done;
626 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
627 label1, label2, repo, cb, cb_arg, diff_content);
629 done:
630 if (tree1)
631 got_object_tree_close(tree1);
632 if (tree2)
633 got_object_tree_close(tree2);
634 if (treeobj1)
635 got_object_close(treeobj1);
636 if (treeobj2)
637 got_object_close(treeobj2);
638 return err;
641 static const struct got_error *
642 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
643 FILE *f2, const char *label, struct got_repository *repo,
644 got_diff_blob_cb cb, void *cb_arg, int diff_content)
646 const struct got_error *err;
647 struct got_object *treeobj = NULL;
648 struct got_tree_object *tree = NULL;
650 err = got_object_open(&treeobj, repo, id);
651 if (err)
652 goto done;
654 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
655 err = got_error(GOT_ERR_OBJ_TYPE);
656 goto done;
659 err = got_object_tree_open(&tree, repo, treeobj);
660 if (err)
661 goto done;
663 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
664 repo, cb, cb_arg, diff_content);
665 done:
666 if (tree)
667 got_object_tree_close(tree);
668 if (treeobj)
669 got_object_close(treeobj);
670 return err;
673 static const struct got_error *
674 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
675 const char *label1, const char *label2, struct got_repository *repo,
676 got_diff_blob_cb cb, void *cb_arg)
678 /* XXX TODO */
679 return NULL;
682 static const struct got_error *
683 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
684 FILE *f1, FILE *f2, int fd1, int fd2,
685 const char *label1, const char *label2,
686 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
687 int diff_content)
689 const struct got_error *err = NULL;
690 int id_match;
692 if (got_object_tree_entry_is_submodule(te1))
693 return NULL;
695 if (te2 == NULL) {
696 if (S_ISDIR(te1->mode))
697 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
698 label1, repo, cb, cb_arg, diff_content);
699 else {
700 if (diff_content)
701 err = diff_deleted_blob(&te1->id, f1, fd1,
702 f2, label1, te1->mode, repo, cb, cb_arg);
703 else
704 err = cb(cb_arg, NULL, NULL, NULL, NULL,
705 &te1->id, NULL, label1, NULL,
706 te1->mode, 0, repo);
708 return err;
709 } else if (got_object_tree_entry_is_submodule(te2))
710 return NULL;
712 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
713 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
714 if (!id_match)
715 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
716 fd1, fd2, label1, label2, repo, cb, cb_arg,
717 diff_content);
718 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
719 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
720 if (!id_match ||
721 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
722 (te2->mode & (S_IFLNK | S_IXUSR))) {
723 if (diff_content)
724 return diff_modified_blob(&te1->id, &te2->id,
725 f1, f2, fd1, fd2, label1, label2,
726 te1->mode, te2->mode, repo, cb, cb_arg);
727 else
728 return cb(cb_arg, NULL, NULL, NULL, NULL,
729 &te1->id, &te2->id, label1, label2,
730 te1->mode, te2->mode, repo);
734 if (id_match)
735 return NULL;
737 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
738 cb, cb_arg);
741 static const struct got_error *
742 diff_entry_new_old(struct got_tree_entry *te2,
743 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
744 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
745 int diff_content)
747 if (te1 != NULL) /* handled by diff_entry_old_new() */
748 return NULL;
750 if (got_object_tree_entry_is_submodule(te2))
751 return NULL;
753 if (S_ISDIR(te2->mode))
754 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
755 repo, cb, cb_arg, diff_content);
757 if (diff_content)
758 return diff_added_blob(&te2->id, f1, f2, fd2,
759 label2, te2->mode, repo, cb, cb_arg);
761 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
762 NULL, label2, 0, te2->mode, repo);
765 const struct got_error *
766 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
767 struct got_blob_object *blob2, FILE *f1, FILE *f2,
768 struct got_object_id *id1, struct got_object_id *id2,
769 const char *label1, const char *label2,
770 mode_t mode1, mode_t mode2, struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_diffreg_result *result = NULL;
774 struct got_diffstat_cb_arg *a = arg;
775 char *path = NULL;
776 int status = GOT_STATUS_NO_CHANGE;
778 path = strdup(label2 ? label2 : label1);
779 if (path == NULL)
780 return got_error_from_errno("strdup");
782 if (id1 == NULL)
783 status = GOT_STATUS_ADD;
784 else if (id2 == NULL)
785 status = GOT_STATUS_DELETE;
786 else {
787 if (got_object_id_cmp(id1, id2) != 0)
788 status = GOT_STATUS_MODIFY;
789 else if (mode1 != mode2)
790 status = GOT_STATUS_MODE_CHANGE;
793 if (f1) {
794 err = got_opentemp_truncate(f1);
795 if (err)
796 goto done;
798 if (f2) {
799 err = got_opentemp_truncate(f2);
800 if (err)
801 goto done;
804 if (blob1) {
805 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
806 blob1);
807 if (err)
808 goto done;
810 if (blob2) {
811 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
812 blob2);
813 if (err)
814 goto done;
817 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
818 a->force_text);
819 if (err)
820 goto done;
822 err = get_diffstat(a, path, result->result, a->force_text, status);
824 done:
825 if (result) {
826 const struct got_error *free_err;
828 free_err = got_diffreg_result_free(result);
829 if (free_err && err == NULL)
830 err = free_err;
832 if (err)
833 free(path);
834 return err;
837 const struct got_error *
838 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
839 struct got_blob_object *blob2, FILE *f1, FILE *f2,
840 struct got_object_id *id1, struct got_object_id *id2,
841 const char *label1, const char *label2,
842 mode_t mode1, mode_t mode2, struct got_repository *repo)
844 const struct got_error *err = NULL;
845 struct got_pathlist_head *paths = arg;
846 struct got_diff_changed_path *change = NULL;
847 char *path = NULL;
849 path = strdup(label2 ? label2 : label1);
850 if (path == NULL)
851 return got_error_from_errno("strdup");
853 change = malloc(sizeof(*change));
854 if (change == NULL) {
855 err = got_error_from_errno("malloc");
856 goto done;
859 change->status = GOT_STATUS_NO_CHANGE;
860 if (id1 == NULL)
861 change->status = GOT_STATUS_ADD;
862 else if (id2 == NULL)
863 change->status = GOT_STATUS_DELETE;
864 else {
865 if (got_object_id_cmp(id1, id2) != 0)
866 change->status = GOT_STATUS_MODIFY;
867 else if (mode1 != mode2)
868 change->status = GOT_STATUS_MODE_CHANGE;
871 err = got_pathlist_append(paths, path, change);
872 done:
873 if (err) {
874 free(path);
875 free(change);
877 return err;
880 const struct got_error *
881 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
882 FILE *f1, FILE *f2, int fd1, int fd2,
883 const char *label1, const char *label2,
884 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
885 int diff_content)
887 const struct got_error *err = NULL;
888 struct got_tree_entry *te1 = NULL;
889 struct got_tree_entry *te2 = NULL;
890 char *l1 = NULL, *l2 = NULL;
891 int tidx1 = 0, tidx2 = 0;
893 if (tree1) {
894 te1 = got_object_tree_get_entry(tree1, 0);
895 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
896 te1->name) == -1)
897 return got_error_from_errno("asprintf");
899 if (tree2) {
900 te2 = got_object_tree_get_entry(tree2, 0);
901 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
902 te2->name) == -1) {
903 err = got_error_from_errno("asprintf");
904 goto done;
908 do {
909 if (te1) {
910 struct got_tree_entry *te = NULL;
912 if (tree2)
913 te = got_object_tree_find_entry(tree2,
914 te1->name);
915 if (te) {
916 free(l2);
917 l2 = NULL;
918 if (te && asprintf(&l2, "%s%s%s", label2,
919 label2[0] ? "/" : "", te->name) == -1) {
920 err = got_error_from_errno("asprintf");
921 goto done;
925 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
926 l1, l2, repo, cb, cb_arg, diff_content);
927 if (err)
928 break;
931 if (te2) {
932 struct got_tree_entry *te = NULL;
934 if (tree1)
935 te = got_object_tree_find_entry(tree1,
936 te2->name);
938 free(l2);
939 l2 = NULL;
940 if (te) {
941 if (asprintf(&l2, "%s%s%s", label2,
942 label2[0] ? "/" : "", te->name) == -1) {
943 err = got_error_from_errno("asprintf");
944 goto done;
946 } else {
947 if (asprintf(&l2, "%s%s%s", label2,
948 label2[0] ? "/" : "", te2->name) == -1) {
949 err = got_error_from_errno("asprintf");
950 goto done;
954 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
955 repo, cb, cb_arg, diff_content);
956 if (err)
957 break;
960 free(l1);
961 l1 = NULL;
962 if (te1) {
963 tidx1++;
964 te1 = got_object_tree_get_entry(tree1, tidx1);
965 if (te1 &&
966 asprintf(&l1, "%s%s%s", label1,
967 label1[0] ? "/" : "", te1->name) == -1) {
968 err = got_error_from_errno("asprintf");
969 goto done;
973 free(l2);
974 l2 = NULL;
975 if (te2) {
976 tidx2++;
977 te2 = got_object_tree_get_entry(tree2, tidx2);
978 if (te2 &&
979 asprintf(&l2, "%s%s%s", label2,
980 label2[0] ? "/" : "", te2->name) == -1) {
981 err = got_error_from_errno("asprintf");
982 goto done;
985 } while (te1 || te2);
987 done:
988 free(l1);
989 free(l2);
990 return err;
993 const struct got_error *
994 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
995 FILE *f1, FILE *f2, int fd1, int fd2,
996 struct got_object_id *id1, struct got_object_id *id2,
997 const char *label1, const char *label2,
998 enum got_diff_algorithm diff_algo, int diff_context,
999 int ignore_whitespace, int force_text_diff, int show_diffstat,
1000 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
1002 const struct got_error *err;
1003 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1005 if (id1 == NULL && id2 == NULL)
1006 return got_error(GOT_ERR_NO_OBJ);
1008 if (id1) {
1009 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1010 if (err)
1011 goto done;
1013 if (id2) {
1014 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1015 if (err)
1016 goto done;
1018 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1019 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1020 show_diffstat, ds, outfile);
1021 done:
1022 if (blob1)
1023 got_object_blob_close(blob1);
1024 if (blob2)
1025 got_object_blob_close(blob2);
1026 return err;
1029 static const struct got_error *
1030 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1031 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1032 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1034 const struct got_error *err = NULL;
1035 struct got_pathlist_entry *pe;
1036 struct got_object_id *id1 = NULL, *id2 = NULL;
1037 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1038 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1040 TAILQ_FOREACH(pe, paths, entry) {
1041 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1042 mode_t mode1 = 0, mode2 = 0;
1044 free(id1);
1045 id1 = NULL;
1046 free(id2);
1047 id2 = NULL;
1048 if (subtree1) {
1049 got_object_tree_close(subtree1);
1050 subtree1 = NULL;
1052 if (subtree2) {
1053 got_object_tree_close(subtree2);
1054 subtree2 = NULL;
1056 if (blob1) {
1057 got_object_blob_close(blob1);
1058 blob1 = NULL;
1060 if (blob2) {
1061 got_object_blob_close(blob2);
1062 blob2 = NULL;
1065 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1066 pe->path);
1067 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1068 goto done;
1069 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1070 pe->path);
1071 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1072 goto done;
1073 if (id1 == NULL && id2 == NULL) {
1074 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1075 goto done;
1077 if (id1) {
1078 err = got_object_get_type(&type1, repo, id1);
1079 if (err)
1080 goto done;
1082 if (id2) {
1083 err = got_object_get_type(&type2, repo, id2);
1084 if (err)
1085 goto done;
1087 if (type1 == GOT_OBJ_TYPE_ANY &&
1088 type2 == GOT_OBJ_TYPE_ANY) {
1089 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1090 goto done;
1091 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1092 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1093 err = got_error(GOT_ERR_OBJ_TYPE);
1094 goto done;
1097 if (type1 == GOT_OBJ_TYPE_BLOB ||
1098 type2 == GOT_OBJ_TYPE_BLOB) {
1099 if (id1) {
1100 err = got_object_open_as_blob(&blob1, repo,
1101 id1, 8192, fd1);
1102 if (err)
1103 goto done;
1105 if (id2) {
1106 err = got_object_open_as_blob(&blob2, repo,
1107 id2, 8192, fd2);
1108 if (err)
1109 goto done;
1111 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1112 id1 ? pe->path : "/dev/null",
1113 id2 ? pe->path : "/dev/null",
1114 mode1, mode2, repo);
1115 if (err)
1116 goto done;
1117 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1118 type2 == GOT_OBJ_TYPE_TREE) {
1119 if (id1) {
1120 err = got_object_open_as_tree(&subtree1, repo,
1121 id1);
1122 if (err)
1123 goto done;
1125 if (id2) {
1126 err = got_object_open_as_tree(&subtree2, repo,
1127 id2);
1128 if (err)
1129 goto done;
1131 err = got_diff_tree(subtree1, subtree2, f1, f2,
1132 fd1, fd2,
1133 id1 ? pe->path : "/dev/null",
1134 id2 ? pe->path : "/dev/null",
1135 repo, cb, cb_arg, 1);
1136 if (err)
1137 goto done;
1138 } else {
1139 err = got_error(GOT_ERR_OBJ_TYPE);
1140 goto done;
1143 done:
1144 free(id1);
1145 free(id2);
1146 if (subtree1)
1147 got_object_tree_close(subtree1);
1148 if (subtree2)
1149 got_object_tree_close(subtree2);
1150 if (blob1)
1151 got_object_blob_close(blob1);
1152 if (blob2)
1153 got_object_blob_close(blob2);
1154 return err;
1157 static const struct got_error *
1158 show_object_id(struct got_diff_line **lines, size_t *nlines,
1159 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1161 const struct got_error *err;
1162 int n;
1163 off_t outoff = 0;
1165 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1166 if (n < 0)
1167 return got_error_from_errno("fprintf");
1169 if (lines != NULL && *lines != NULL) {
1170 if (*nlines == 0) {
1171 err = add_line_metadata(lines, nlines, 0,
1172 GOT_DIFF_LINE_META);
1173 if (err)
1174 return err;
1175 } else
1176 outoff = (*lines)[*nlines - 1].offset;
1178 outoff += n;
1179 err = add_line_metadata(lines, nlines, outoff,
1180 GOT_DIFF_LINE_META);
1181 if (err)
1182 return err;
1185 return NULL;
1188 static const struct got_error *
1189 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1190 FILE *f1, FILE *f2, int fd1, int fd2,
1191 struct got_object_id *id1, struct got_object_id *id2,
1192 struct got_pathlist_head *paths, const char *label1, const char *label2,
1193 int diff_context, int ignore_whitespace, int force_text_diff,
1194 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1195 struct got_repository *repo, FILE *outfile,
1196 enum got_diff_algorithm diff_algo)
1198 const struct got_error *err;
1199 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1200 struct got_diff_blob_output_unidiff_arg arg;
1201 int want_linemeta = (lines != NULL && *lines != NULL);
1203 if (id1 == NULL && id2 == NULL)
1204 return got_error(GOT_ERR_NO_OBJ);
1206 if (id1) {
1207 err = got_object_open_as_tree(&tree1, repo, id1);
1208 if (err)
1209 goto done;
1211 if (id2) {
1212 err = got_object_open_as_tree(&tree2, repo, id2);
1213 if (err)
1214 goto done;
1217 arg.diff_algo = diff_algo;
1218 arg.diff_context = diff_context;
1219 arg.ignore_whitespace = ignore_whitespace;
1220 arg.force_text_diff = force_text_diff;
1221 arg.show_diffstat = show_diffstat;
1222 arg.diffstat = dsa;
1223 arg.outfile = outfile;
1224 if (want_linemeta) {
1225 arg.lines = *lines;
1226 arg.nlines = *nlines;
1227 } else {
1228 arg.lines = NULL;
1229 arg.nlines = 0;
1231 if (paths == NULL || TAILQ_EMPTY(paths))
1232 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1233 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1234 else
1235 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1236 got_diff_blob_output_unidiff, &arg);
1237 if (want_linemeta) {
1238 *lines = arg.lines; /* was likely re-allocated */
1239 *nlines = arg.nlines;
1241 done:
1242 if (tree1)
1243 got_object_tree_close(tree1);
1244 if (tree2)
1245 got_object_tree_close(tree2);
1246 return err;
1249 const struct got_error *
1250 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1251 FILE *f1, FILE *f2, int fd1, int fd2,
1252 struct got_object_id *id1, struct got_object_id *id2,
1253 struct got_pathlist_head *paths, const char *label1, const char *label2,
1254 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1255 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1256 struct got_repository *repo, FILE *outfile)
1258 const struct got_error *err;
1259 char *idstr = NULL;
1261 if (id1 == NULL && id2 == NULL)
1262 return got_error(GOT_ERR_NO_OBJ);
1264 if (id1) {
1265 err = got_object_id_str(&idstr, id1);
1266 if (err)
1267 goto done;
1268 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1269 if (err)
1270 goto done;
1271 free(idstr);
1272 idstr = NULL;
1273 } else {
1274 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1275 outfile);
1276 if (err)
1277 goto done;
1280 if (id2) {
1281 err = got_object_id_str(&idstr, id2);
1282 if (err)
1283 goto done;
1284 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1285 if (err)
1286 goto done;
1287 free(idstr);
1288 idstr = NULL;
1289 } else {
1290 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1291 outfile);
1292 if (err)
1293 goto done;
1296 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1297 paths, label1, label2, diff_context, ignore_whitespace,
1298 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1299 done:
1300 free(idstr);
1301 return err;
1304 const struct got_error *
1305 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1306 FILE *f1, FILE *f2, int fd1, int fd2,
1307 struct got_object_id *id1, struct got_object_id *id2,
1308 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1309 int diff_context, int ignore_whitespace, int force_text_diff,
1310 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1311 struct got_repository *repo, FILE *outfile)
1313 const struct got_error *err;
1314 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1315 char *idstr = NULL;
1317 if (id2 == NULL)
1318 return got_error(GOT_ERR_NO_OBJ);
1320 if (id1) {
1321 err = got_object_open_as_commit(&commit1, repo, id1);
1322 if (err)
1323 goto done;
1324 err = got_object_id_str(&idstr, id1);
1325 if (err)
1326 goto done;
1327 err = show_object_id(lines, nlines, "commit", '-', idstr,
1328 outfile);
1329 if (err)
1330 goto done;
1331 free(idstr);
1332 idstr = NULL;
1333 } else {
1334 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1335 outfile);
1336 if (err)
1337 goto done;
1340 err = got_object_open_as_commit(&commit2, repo, id2);
1341 if (err)
1342 goto done;
1344 err = got_object_id_str(&idstr, id2);
1345 if (err)
1346 goto done;
1347 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1348 if (err)
1349 goto done;
1351 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1352 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1353 got_object_commit_get_tree_id(commit2), paths, "", "",
1354 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1355 dsa, repo, outfile, diff_algo);
1356 done:
1357 if (commit1)
1358 got_object_commit_close(commit1);
1359 if (commit2)
1360 got_object_commit_close(commit2);
1361 free(idstr);
1362 return err;
1365 const struct got_error *
1366 got_diff_files(struct got_diffreg_result **resultp,
1367 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1368 const char *label2, int diff_context, int ignore_whitespace,
1369 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1371 const struct got_error *err = NULL;
1372 struct got_diffreg_result *diffreg_result = NULL;
1374 if (resultp)
1375 *resultp = NULL;
1377 if (outfile) {
1378 fprintf(outfile, "file - %s\n",
1379 f1_exists ? label1 : "/dev/null");
1380 fprintf(outfile, "file + %s\n",
1381 f2_exists ? label2 : "/dev/null");
1384 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1385 ignore_whitespace, force_text_diff);
1386 if (err)
1387 goto done;
1389 if (outfile) {
1390 err = got_diffreg_output(NULL, NULL, diffreg_result,
1391 f1_exists, f2_exists, label1, label2,
1392 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1393 if (err)
1394 goto done;
1397 done:
1398 if (resultp && err == NULL)
1399 *resultp = diffreg_result;
1400 else if (diffreg_result) {
1401 const struct got_error *free_err;
1403 free_err = got_diffreg_result_free(diffreg_result);
1404 if (free_err && err == NULL)
1405 err = free_err;
1408 return err;