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 #ifndef MAX
42 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
43 #endif
45 static const struct got_error *
46 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
47 off_t off, uint8_t type)
48 {
49 struct got_diff_line *p;
51 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
52 if (p == NULL)
53 return got_error_from_errno("reallocarray");
54 *lines = p;
55 (*lines)[*nlines].offset = off;
56 (*lines)[*nlines].type = type;
57 (*nlines)++;
59 return NULL;
60 }
62 static void
63 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
64 uint32_t add, uint32_t rm)
65 {
66 int d1 = 1, d2 = 1;
68 if (maxlen)
69 *maxlen = MAX(*maxlen, len);
71 while (add /= 10)
72 ++d1;
73 *add_cols = MAX(*add_cols, d1);
75 while (rm /= 10)
76 ++d2;
77 *rm_cols = MAX(*rm_cols, d2);
78 }
80 static const struct got_error *
81 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
82 struct diff_result *r, int force_text, int status)
83 {
84 const struct got_error *err;
85 struct got_pathlist_entry *pe;
86 struct got_diff_changed_path *change = NULL;
87 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
88 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
89 int i;
91 change = calloc(1, sizeof(*change));
92 if (change == NULL)
93 return got_error_from_errno("calloc");
95 if (!isbin || force_text) {
96 for (i = 0; i < r->chunks.len; ++i) {
97 struct diff_chunk *c;
98 int clc, crc;
100 c = diff_chunk_get(r, i);
101 clc = diff_chunk_get_left_count(c);
102 crc = diff_chunk_get_right_count(c);
104 if (crc && !clc)
105 change->add += crc;
106 if (clc && !crc)
107 change->rm += clc;
111 change->status = status;
112 ds->ins += change->add;
113 ds->del += change->rm;
114 ++ds->nfiles;
116 err = got_pathlist_append(ds->paths, path, change);
117 if (err) {
118 free(change);
119 return err;
122 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
123 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
124 pe->path_len, change->add, change->rm);
126 return NULL;
129 static const struct got_error *
130 diff_blobs(struct got_diff_line **lines, size_t *nlines,
131 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
132 struct got_blob_object *blob2, FILE *f1, FILE *f2,
133 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
134 int diff_context, int ignore_whitespace, int force_text_diff,
135 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
136 enum got_diff_algorithm diff_algo)
138 const struct got_error *err = NULL, *free_err;
139 char hex1[SHA1_DIGEST_STRING_LENGTH];
140 char hex2[SHA1_DIGEST_STRING_LENGTH];
141 const char *idstr1 = NULL, *idstr2 = NULL;
142 char *modestr1 = NULL, *modestr2 = NULL;
143 off_t size1, size2;
144 struct got_diffreg_result *result = NULL;
145 off_t outoff = 0;
146 int n;
148 if (lines && *lines && *nlines > 0)
149 outoff = (*lines)[*nlines - 1].offset;
150 else if (lines) {
151 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
152 if (err)
153 goto done;
156 if (resultp)
157 *resultp = NULL;
159 if (f1) {
160 err = got_opentemp_truncate(f1);
161 if (err)
162 goto done;
164 if (f2) {
165 err = got_opentemp_truncate(f2);
166 if (err)
167 goto done;
170 size1 = 0;
171 if (blob1) {
172 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
173 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
174 blob1);
175 if (err)
176 goto done;
177 } else
178 idstr1 = "/dev/null";
180 size2 = 0;
181 if (blob2) {
182 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
183 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
184 blob2);
185 if (err)
186 goto done;
187 } else
188 idstr2 = "/dev/null";
190 if (outfile) {
191 int modebits;
193 if (mode1 && mode1 != mode2) {
194 if (S_ISLNK(mode1))
195 modebits = S_IFLNK;
196 else
197 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
198 if (asprintf(&modestr1, " (mode %o)",
199 mode1 & modebits) == -1) {
200 err = got_error_from_errno("asprintf");
201 goto done;
204 if (mode2 && mode1 != mode2) {
205 if (S_ISLNK(mode2))
206 modebits = S_IFLNK;
207 else
208 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
209 if (asprintf(&modestr2, " (mode %o)",
210 mode2 & modebits) == -1) {
211 err = got_error_from_errno("asprintf");
212 goto done;
215 n = fprintf(outfile, "blob - %s%s\n", idstr1,
216 modestr1 ? modestr1 : "");
217 if (n < 0)
218 goto done;
219 outoff += n;
220 if (lines) {
221 err = add_line_metadata(lines, nlines, outoff,
222 GOT_DIFF_LINE_BLOB_MIN);
223 if (err)
224 goto done;
227 n = fprintf(outfile, "blob + %s%s\n", idstr2,
228 modestr2 ? modestr2 : "");
229 if (n < 0)
230 goto done;
231 outoff += n;
232 if (lines) {
233 err = add_line_metadata(lines, nlines, outoff,
234 GOT_DIFF_LINE_BLOB_PLUS);
235 if (err)
236 goto done;
240 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
241 force_text_diff);
242 if (err)
243 goto done;
245 if (show_diffstat) {
246 char *path = NULL;
247 int status = GOT_STATUS_NO_CHANGE;
249 /*
250 * Ignore 'm'ode status change: if there's no accompanying
251 * content change, there'll be no diffstat, and if there
252 * are actual changes, 'M'odified takes precedence.
253 */
254 if (blob1 == NULL)
255 status = GOT_STATUS_ADD;
256 else if (blob2 == NULL)
257 status = GOT_STATUS_DELETE;
258 else
259 status = GOT_STATUS_MODIFY;
261 if (label1 == NULL && label2 == NULL) {
262 /* diffstat of blobs, show hash instead of path */
263 if (asprintf(&path, "%.10s -> %.10s",
264 idstr1, idstr2) == -1) {
265 err = got_error_from_errno("asprintf");
266 goto done;
268 } else {
269 if (label2 != NULL &&
270 (status != GOT_STATUS_DELETE || label1 == NULL))
271 path = strdup(label2);
272 else
273 path = strdup(label1);
274 if (path == NULL) {
275 err = got_error_from_errno("strdup");
276 goto done;
280 err = get_diffstat(ds, path, result->result, force_text_diff,
281 status);
282 if (err) {
283 free(path);
284 goto done;
288 if (outfile) {
289 err = got_diffreg_output(lines, nlines, result,
290 blob1 != NULL, blob2 != NULL,
291 label1 ? label1 : idstr1,
292 label2 ? label2 : idstr2,
293 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
294 if (err)
295 goto done;
298 done:
299 free(modestr1);
300 free(modestr2);
301 if (resultp && err == NULL)
302 *resultp = result;
303 else if (result) {
304 free_err = got_diffreg_result_free(result);
305 if (free_err && err == NULL)
306 err = free_err;
309 return err;
312 const struct got_error *
313 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
314 struct got_blob_object *blob2, FILE *f1, FILE *f2,
315 struct got_object_id *id1, struct got_object_id *id2,
316 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 struct got_repository *repo)
319 struct got_diff_blob_output_unidiff_arg *a = arg;
321 return diff_blobs(&a->lines, &a->nlines, NULL,
322 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
323 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
324 a->diffstat, a->outfile, a->diff_algo);
327 const struct got_error *
328 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
329 struct got_blob_object *blob1, struct got_blob_object *blob2,
330 FILE *f1, FILE *f2, const char *label1, const char *label2,
331 enum got_diff_algorithm diff_algo, int diff_context,
332 int ignore_whitespace, int force_text_diff, int show_diffstat,
333 struct got_diffstat_cb_arg *ds, FILE *outfile)
335 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
336 label1, label2, 0, 0, diff_context, ignore_whitespace,
337 force_text_diff, show_diffstat, ds, outfile, diff_algo);
340 static const struct got_error *
341 diff_blob_file(struct got_diffreg_result **resultp,
342 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
343 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
344 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
345 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
346 FILE *outfile)
348 const struct got_error *err = NULL, *free_err;
349 char hex1[SHA1_DIGEST_STRING_LENGTH];
350 const char *idstr1 = NULL;
351 struct got_diffreg_result *result = NULL;
353 if (resultp)
354 *resultp = NULL;
356 if (blob1)
357 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
358 else
359 idstr1 = "/dev/null";
361 if (outfile) {
362 char *mode = NULL;
364 /* display file mode for new added files only */
365 if (f2_exists && blob1 == NULL) {
366 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
368 if (S_ISLNK(sb2->st_mode))
369 mmask = S_IFLNK;
370 if (asprintf(&mode, " (mode %o)",
371 sb2->st_mode & mmask) == -1)
372 return got_error_from_errno("asprintf");
374 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
375 fprintf(outfile, "file + %s%s\n",
376 f2_exists ? label2 : "/dev/null", mode ? mode : "");
377 free(mode);
380 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
381 force_text_diff);
382 if (err)
383 goto done;
385 if (outfile) {
386 err = got_diffreg_output(NULL, NULL, result,
387 blob1 != NULL, f2_exists,
388 label2, /* show local file's path, not a blob ID */
389 label2, GOT_DIFF_OUTPUT_UNIDIFF,
390 diff_context, outfile);
391 if (err)
392 goto done;
395 if (show_diffstat) {
396 char *path = NULL;
397 int status = GOT_STATUS_NO_CHANGE;
399 /*
400 * Ignore 'm'ode status change: if there's no accompanying
401 * content change, there'll be no diffstat, and if there
402 * are actual changes, 'M'odified takes precedence.
403 */
404 if (blob1 == NULL)
405 status = GOT_STATUS_ADD;
406 else if (!f2_exists)
407 status = GOT_STATUS_DELETE;
408 else
409 status = GOT_STATUS_MODIFY;
411 if (label2 != NULL &&
412 (status != GOT_STATUS_DELETE || label1 == NULL))
413 path = strdup(label2);
414 else
415 path = strdup(label1);
416 if (path == NULL) {
417 err = got_error_from_errno("strdup");
418 goto done;
421 err = get_diffstat(ds, path, result->result, force_text_diff,
422 status);
423 if (err) {
424 free(path);
425 goto done;
429 done:
430 if (resultp && err == NULL)
431 *resultp = result;
432 else if (result) {
433 free_err = got_diffreg_result_free(result);
434 if (free_err && err == NULL)
435 err = free_err;
437 return err;
440 const struct got_error *
441 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
442 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
443 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
444 int ignore_whitespace, int force_text_diff, int show_diffstat,
445 struct got_diffstat_cb_arg *ds, FILE *outfile)
447 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
448 sb2, label2, diff_algo, diff_context, ignore_whitespace,
449 force_text_diff, show_diffstat, ds, outfile);
452 static const struct got_error *
453 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
454 const char *label, mode_t mode, struct got_repository *repo,
455 got_diff_blob_cb cb, void *cb_arg)
457 const struct got_error *err;
458 struct got_blob_object *blob = NULL;
459 struct got_object *obj = NULL;
461 err = got_object_open(&obj, repo, id);
462 if (err)
463 return err;
465 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
466 if (err)
467 goto done;
468 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
469 NULL, label, 0, mode, repo);
470 done:
471 got_object_close(obj);
472 if (blob)
473 got_object_blob_close(blob);
474 return err;
477 static const struct got_error *
478 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
479 FILE *f1, FILE *f2, int fd1, int fd2,
480 const char *label1, const char *label2,
481 mode_t mode1, mode_t mode2, struct got_repository *repo,
482 got_diff_blob_cb cb, void *cb_arg)
484 const struct got_error *err;
485 struct got_object *obj1 = NULL;
486 struct got_object *obj2 = NULL;
487 struct got_blob_object *blob1 = NULL;
488 struct got_blob_object *blob2 = NULL;
490 err = got_object_open(&obj1, repo, id1);
491 if (err)
492 return err;
494 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
495 err = got_error(GOT_ERR_OBJ_TYPE);
496 goto done;
499 err = got_object_open(&obj2, repo, id2);
500 if (err)
501 goto done;
502 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 goto done;
507 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
508 if (err)
509 goto done;
511 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
512 if (err)
513 goto done;
515 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
516 mode1, mode2, repo);
517 done:
518 if (obj1)
519 got_object_close(obj1);
520 if (obj2)
521 got_object_close(obj2);
522 if (blob1)
523 got_object_blob_close(blob1);
524 if (blob2)
525 got_object_blob_close(blob2);
526 return err;
529 static const struct got_error *
530 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
531 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
532 got_diff_blob_cb cb, void *cb_arg)
534 const struct got_error *err;
535 struct got_blob_object *blob = NULL;
536 struct got_object *obj = NULL;
538 err = got_object_open(&obj, repo, id);
539 if (err)
540 return err;
542 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
543 if (err)
544 goto done;
545 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
546 mode, 0, repo);
547 done:
548 got_object_close(obj);
549 if (blob)
550 got_object_blob_close(blob);
551 return err;
554 static const struct got_error *
555 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
556 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
557 void *cb_arg, int diff_content)
559 const struct got_error *err = NULL;
560 struct got_object *treeobj = NULL;
561 struct got_tree_object *tree = NULL;
563 err = got_object_open(&treeobj, repo, id);
564 if (err)
565 goto done;
567 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
568 err = got_error(GOT_ERR_OBJ_TYPE);
569 goto done;
572 err = got_object_tree_open(&tree, repo, treeobj);
573 if (err)
574 goto done;
576 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
577 repo, cb, cb_arg, diff_content);
578 done:
579 if (tree)
580 got_object_tree_close(tree);
581 if (treeobj)
582 got_object_close(treeobj);
583 return err;
586 static const struct got_error *
587 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
588 FILE *f1, FILE *f2, int fd1, int fd2,
589 const char *label1, const char *label2,
590 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
591 int diff_content)
593 const struct got_error *err;
594 struct got_object *treeobj1 = NULL;
595 struct got_object *treeobj2 = NULL;
596 struct got_tree_object *tree1 = NULL;
597 struct got_tree_object *tree2 = NULL;
599 err = got_object_open(&treeobj1, repo, id1);
600 if (err)
601 goto done;
603 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
604 err = got_error(GOT_ERR_OBJ_TYPE);
605 goto done;
608 err = got_object_open(&treeobj2, repo, id2);
609 if (err)
610 goto done;
612 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
613 err = got_error(GOT_ERR_OBJ_TYPE);
614 goto done;
617 err = got_object_tree_open(&tree1, repo, treeobj1);
618 if (err)
619 goto done;
621 err = got_object_tree_open(&tree2, repo, treeobj2);
622 if (err)
623 goto done;
625 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
626 label1, label2, repo, cb, cb_arg, diff_content);
628 done:
629 if (tree1)
630 got_object_tree_close(tree1);
631 if (tree2)
632 got_object_tree_close(tree2);
633 if (treeobj1)
634 got_object_close(treeobj1);
635 if (treeobj2)
636 got_object_close(treeobj2);
637 return err;
640 static const struct got_error *
641 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
642 FILE *f2, const char *label, struct got_repository *repo,
643 got_diff_blob_cb cb, void *cb_arg, int diff_content)
645 const struct got_error *err;
646 struct got_object *treeobj = NULL;
647 struct got_tree_object *tree = NULL;
649 err = got_object_open(&treeobj, repo, id);
650 if (err)
651 goto done;
653 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
654 err = got_error(GOT_ERR_OBJ_TYPE);
655 goto done;
658 err = got_object_tree_open(&tree, repo, treeobj);
659 if (err)
660 goto done;
662 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
663 repo, cb, cb_arg, diff_content);
664 done:
665 if (tree)
666 got_object_tree_close(tree);
667 if (treeobj)
668 got_object_close(treeobj);
669 return err;
672 static const struct got_error *
673 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
674 const char *label1, const char *label2, struct got_repository *repo,
675 got_diff_blob_cb cb, void *cb_arg)
677 /* XXX TODO */
678 return NULL;
681 static const struct got_error *
682 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
683 FILE *f1, FILE *f2, int fd1, int fd2,
684 const char *label1, const char *label2,
685 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
686 int diff_content)
688 const struct got_error *err = NULL;
689 int id_match;
691 if (got_object_tree_entry_is_submodule(te1))
692 return NULL;
694 if (te2 == NULL) {
695 if (S_ISDIR(te1->mode))
696 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
697 label1, repo, cb, cb_arg, diff_content);
698 else {
699 if (diff_content)
700 err = diff_deleted_blob(&te1->id, f1, fd1,
701 f2, label1, te1->mode, repo, cb, cb_arg);
702 else
703 err = cb(cb_arg, NULL, NULL, NULL, NULL,
704 &te1->id, NULL, label1, NULL,
705 te1->mode, 0, repo);
707 return err;
708 } else if (got_object_tree_entry_is_submodule(te2))
709 return NULL;
711 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
712 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
713 if (!id_match)
714 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
715 fd1, fd2, label1, label2, repo, cb, cb_arg,
716 diff_content);
717 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
718 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
719 if (!id_match ||
720 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
721 (te2->mode & (S_IFLNK | S_IXUSR))) {
722 if (diff_content)
723 return diff_modified_blob(&te1->id, &te2->id,
724 f1, f2, fd1, fd2, label1, label2,
725 te1->mode, te2->mode, repo, cb, cb_arg);
726 else
727 return cb(cb_arg, NULL, NULL, NULL, NULL,
728 &te1->id, &te2->id, label1, label2,
729 te1->mode, te2->mode, repo);
733 if (id_match)
734 return NULL;
736 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
737 cb, cb_arg);
740 static const struct got_error *
741 diff_entry_new_old(struct got_tree_entry *te2,
742 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
743 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
744 int diff_content)
746 if (te1 != NULL) /* handled by diff_entry_old_new() */
747 return NULL;
749 if (got_object_tree_entry_is_submodule(te2))
750 return NULL;
752 if (S_ISDIR(te2->mode))
753 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
754 repo, cb, cb_arg, diff_content);
756 if (diff_content)
757 return diff_added_blob(&te2->id, f1, f2, fd2,
758 label2, te2->mode, repo, cb, cb_arg);
760 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
761 NULL, label2, 0, te2->mode, repo);
764 const struct got_error *
765 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
766 struct got_blob_object *blob2, FILE *f1, FILE *f2,
767 struct got_object_id *id1, struct got_object_id *id2,
768 const char *label1, const char *label2,
769 mode_t mode1, mode_t mode2, struct got_repository *repo)
771 const struct got_error *err = NULL;
772 struct got_diffreg_result *result = NULL;
773 struct got_diffstat_cb_arg *a = arg;
774 char *path = NULL;
775 int status = GOT_STATUS_NO_CHANGE;
777 path = strdup(label2 ? label2 : label1);
778 if (path == NULL)
779 return got_error_from_errno("strdup");
781 if (id1 == NULL)
782 status = GOT_STATUS_ADD;
783 else if (id2 == NULL)
784 status = GOT_STATUS_DELETE;
785 else {
786 if (got_object_id_cmp(id1, id2) != 0)
787 status = GOT_STATUS_MODIFY;
788 else if (mode1 != mode2)
789 status = GOT_STATUS_MODE_CHANGE;
792 if (f1) {
793 err = got_opentemp_truncate(f1);
794 if (err)
795 goto done;
797 if (f2) {
798 err = got_opentemp_truncate(f2);
799 if (err)
800 goto done;
803 if (blob1) {
804 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
805 blob1);
806 if (err)
807 goto done;
809 if (blob2) {
810 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
811 blob2);
812 if (err)
813 goto done;
816 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
817 a->force_text);
818 if (err)
819 goto done;
821 err = get_diffstat(a, path, result->result, a->force_text, status);
823 done:
824 if (result) {
825 const struct got_error *free_err;
827 free_err = got_diffreg_result_free(result);
828 if (free_err && err == NULL)
829 err = free_err;
831 if (err)
832 free(path);
833 return err;
836 const struct got_error *
837 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
838 struct got_blob_object *blob2, FILE *f1, FILE *f2,
839 struct got_object_id *id1, struct got_object_id *id2,
840 const char *label1, const char *label2,
841 mode_t mode1, mode_t mode2, struct got_repository *repo)
843 const struct got_error *err = NULL;
844 struct got_pathlist_head *paths = arg;
845 struct got_diff_changed_path *change = NULL;
846 char *path = NULL;
848 path = strdup(label2 ? label2 : label1);
849 if (path == NULL)
850 return got_error_from_errno("strdup");
852 change = malloc(sizeof(*change));
853 if (change == NULL) {
854 err = got_error_from_errno("malloc");
855 goto done;
858 change->status = GOT_STATUS_NO_CHANGE;
859 if (id1 == NULL)
860 change->status = GOT_STATUS_ADD;
861 else if (id2 == NULL)
862 change->status = GOT_STATUS_DELETE;
863 else {
864 if (got_object_id_cmp(id1, id2) != 0)
865 change->status = GOT_STATUS_MODIFY;
866 else if (mode1 != mode2)
867 change->status = GOT_STATUS_MODE_CHANGE;
870 err = got_pathlist_append(paths, path, change);
871 done:
872 if (err) {
873 free(path);
874 free(change);
876 return err;
879 const struct got_error *
880 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
881 FILE *f1, FILE *f2, int fd1, int fd2,
882 const char *label1, const char *label2,
883 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
884 int diff_content)
886 const struct got_error *err = NULL;
887 struct got_tree_entry *te1 = NULL;
888 struct got_tree_entry *te2 = NULL;
889 char *l1 = NULL, *l2 = NULL;
890 int tidx1 = 0, tidx2 = 0;
892 if (tree1) {
893 te1 = got_object_tree_get_entry(tree1, 0);
894 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
895 te1->name) == -1)
896 return got_error_from_errno("asprintf");
898 if (tree2) {
899 te2 = got_object_tree_get_entry(tree2, 0);
900 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
901 te2->name) == -1) {
902 err = got_error_from_errno("asprintf");
903 goto done;
907 do {
908 if (te1) {
909 struct got_tree_entry *te = NULL;
911 if (tree2)
912 te = got_object_tree_find_entry(tree2,
913 te1->name);
914 if (te) {
915 free(l2);
916 l2 = NULL;
917 if (te && asprintf(&l2, "%s%s%s", label2,
918 label2[0] ? "/" : "", te->name) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
924 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
925 l1, l2, repo, cb, cb_arg, diff_content);
926 if (err)
927 break;
930 if (te2) {
931 struct got_tree_entry *te = NULL;
933 if (tree1)
934 te = got_object_tree_find_entry(tree1,
935 te2->name);
937 free(l2);
938 l2 = NULL;
939 if (te) {
940 if (asprintf(&l2, "%s%s%s", label2,
941 label2[0] ? "/" : "", te->name) == -1) {
942 err = got_error_from_errno("asprintf");
943 goto done;
945 } else {
946 if (asprintf(&l2, "%s%s%s", label2,
947 label2[0] ? "/" : "", te2->name) == -1) {
948 err = got_error_from_errno("asprintf");
949 goto done;
953 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
954 repo, cb, cb_arg, diff_content);
955 if (err)
956 break;
959 free(l1);
960 l1 = NULL;
961 if (te1) {
962 tidx1++;
963 te1 = got_object_tree_get_entry(tree1, tidx1);
964 if (te1 &&
965 asprintf(&l1, "%s%s%s", label1,
966 label1[0] ? "/" : "", te1->name) == -1) {
967 err = got_error_from_errno("asprintf");
968 goto done;
972 free(l2);
973 l2 = NULL;
974 if (te2) {
975 tidx2++;
976 te2 = got_object_tree_get_entry(tree2, tidx2);
977 if (te2 &&
978 asprintf(&l2, "%s%s%s", label2,
979 label2[0] ? "/" : "", te2->name) == -1) {
980 err = got_error_from_errno("asprintf");
981 goto done;
984 } while (te1 || te2);
986 done:
987 free(l1);
988 free(l2);
989 return err;
992 const struct got_error *
993 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
994 FILE *f1, FILE *f2, int fd1, int fd2,
995 struct got_object_id *id1, struct got_object_id *id2,
996 const char *label1, const char *label2,
997 enum got_diff_algorithm diff_algo, int diff_context,
998 int ignore_whitespace, int force_text_diff, int show_diffstat,
999 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
1001 const struct got_error *err;
1002 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1004 if (id1 == NULL && id2 == NULL)
1005 return got_error(GOT_ERR_NO_OBJ);
1007 if (id1) {
1008 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1009 if (err)
1010 goto done;
1012 if (id2) {
1013 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1014 if (err)
1015 goto done;
1017 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1018 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1019 show_diffstat, ds, outfile);
1020 done:
1021 if (blob1)
1022 got_object_blob_close(blob1);
1023 if (blob2)
1024 got_object_blob_close(blob2);
1025 return err;
1028 static const struct got_error *
1029 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1030 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1031 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1033 const struct got_error *err = NULL;
1034 struct got_pathlist_entry *pe;
1035 struct got_object_id *id1 = NULL, *id2 = NULL;
1036 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1037 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1039 TAILQ_FOREACH(pe, paths, entry) {
1040 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1041 mode_t mode1 = 0, mode2 = 0;
1043 free(id1);
1044 id1 = NULL;
1045 free(id2);
1046 id2 = NULL;
1047 if (subtree1) {
1048 got_object_tree_close(subtree1);
1049 subtree1 = NULL;
1051 if (subtree2) {
1052 got_object_tree_close(subtree2);
1053 subtree2 = NULL;
1055 if (blob1) {
1056 got_object_blob_close(blob1);
1057 blob1 = NULL;
1059 if (blob2) {
1060 got_object_blob_close(blob2);
1061 blob2 = NULL;
1064 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1065 pe->path);
1066 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1067 goto done;
1068 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1069 pe->path);
1070 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1071 goto done;
1072 if (id1 == NULL && id2 == NULL) {
1073 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1074 goto done;
1076 if (id1) {
1077 err = got_object_get_type(&type1, repo, id1);
1078 if (err)
1079 goto done;
1081 if (id2) {
1082 err = got_object_get_type(&type2, repo, id2);
1083 if (err)
1084 goto done;
1086 if (type1 == GOT_OBJ_TYPE_ANY &&
1087 type2 == GOT_OBJ_TYPE_ANY) {
1088 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1089 goto done;
1090 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1091 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1092 err = got_error(GOT_ERR_OBJ_TYPE);
1093 goto done;
1096 if (type1 == GOT_OBJ_TYPE_BLOB ||
1097 type2 == GOT_OBJ_TYPE_BLOB) {
1098 if (id1) {
1099 err = got_object_open_as_blob(&blob1, repo,
1100 id1, 8192, fd1);
1101 if (err)
1102 goto done;
1104 if (id2) {
1105 err = got_object_open_as_blob(&blob2, repo,
1106 id2, 8192, fd2);
1107 if (err)
1108 goto done;
1110 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1111 id1 ? pe->path : "/dev/null",
1112 id2 ? pe->path : "/dev/null",
1113 mode1, mode2, repo);
1114 if (err)
1115 goto done;
1116 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1117 type2 == GOT_OBJ_TYPE_TREE) {
1118 if (id1) {
1119 err = got_object_open_as_tree(&subtree1, repo,
1120 id1);
1121 if (err)
1122 goto done;
1124 if (id2) {
1125 err = got_object_open_as_tree(&subtree2, repo,
1126 id2);
1127 if (err)
1128 goto done;
1130 err = got_diff_tree(subtree1, subtree2, f1, f2,
1131 fd1, fd2,
1132 id1 ? pe->path : "/dev/null",
1133 id2 ? pe->path : "/dev/null",
1134 repo, cb, cb_arg, 1);
1135 if (err)
1136 goto done;
1137 } else {
1138 err = got_error(GOT_ERR_OBJ_TYPE);
1139 goto done;
1142 done:
1143 free(id1);
1144 free(id2);
1145 if (subtree1)
1146 got_object_tree_close(subtree1);
1147 if (subtree2)
1148 got_object_tree_close(subtree2);
1149 if (blob1)
1150 got_object_blob_close(blob1);
1151 if (blob2)
1152 got_object_blob_close(blob2);
1153 return err;
1156 static const struct got_error *
1157 show_object_id(struct got_diff_line **lines, size_t *nlines,
1158 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1160 const struct got_error *err;
1161 int n;
1162 off_t outoff = 0;
1164 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1165 if (n < 0)
1166 return got_error_from_errno("fprintf");
1168 if (lines != NULL && *lines != NULL) {
1169 if (*nlines == 0) {
1170 err = add_line_metadata(lines, nlines, 0,
1171 GOT_DIFF_LINE_META);
1172 if (err)
1173 return err;
1174 } else
1175 outoff = (*lines)[*nlines - 1].offset;
1177 outoff += n;
1178 err = add_line_metadata(lines, nlines, outoff,
1179 GOT_DIFF_LINE_META);
1180 if (err)
1181 return err;
1184 return NULL;
1187 static const struct got_error *
1188 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1189 FILE *f1, FILE *f2, int fd1, int fd2,
1190 struct got_object_id *id1, struct got_object_id *id2,
1191 struct got_pathlist_head *paths, const char *label1, const char *label2,
1192 int diff_context, int ignore_whitespace, int force_text_diff,
1193 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1194 struct got_repository *repo, FILE *outfile,
1195 enum got_diff_algorithm diff_algo)
1197 const struct got_error *err;
1198 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1199 struct got_diff_blob_output_unidiff_arg arg;
1200 int want_linemeta = (lines != NULL && *lines != NULL);
1202 if (id1 == NULL && id2 == NULL)
1203 return got_error(GOT_ERR_NO_OBJ);
1205 if (id1) {
1206 err = got_object_open_as_tree(&tree1, repo, id1);
1207 if (err)
1208 goto done;
1210 if (id2) {
1211 err = got_object_open_as_tree(&tree2, repo, id2);
1212 if (err)
1213 goto done;
1216 arg.diff_algo = diff_algo;
1217 arg.diff_context = diff_context;
1218 arg.ignore_whitespace = ignore_whitespace;
1219 arg.force_text_diff = force_text_diff;
1220 arg.show_diffstat = show_diffstat;
1221 arg.diffstat = dsa;
1222 arg.outfile = outfile;
1223 if (want_linemeta) {
1224 arg.lines = *lines;
1225 arg.nlines = *nlines;
1226 } else {
1227 arg.lines = NULL;
1228 arg.nlines = 0;
1230 if (paths == NULL || TAILQ_EMPTY(paths))
1231 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1232 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1233 else
1234 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1235 got_diff_blob_output_unidiff, &arg);
1236 if (want_linemeta) {
1237 *lines = arg.lines; /* was likely re-allocated */
1238 *nlines = arg.nlines;
1240 done:
1241 if (tree1)
1242 got_object_tree_close(tree1);
1243 if (tree2)
1244 got_object_tree_close(tree2);
1245 return err;
1248 const struct got_error *
1249 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1250 FILE *f1, FILE *f2, int fd1, int fd2,
1251 struct got_object_id *id1, struct got_object_id *id2,
1252 struct got_pathlist_head *paths, const char *label1, const char *label2,
1253 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1254 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1255 struct got_repository *repo, FILE *outfile)
1257 const struct got_error *err;
1258 char *idstr = NULL;
1260 if (id1 == NULL && id2 == NULL)
1261 return got_error(GOT_ERR_NO_OBJ);
1263 if (id1) {
1264 err = got_object_id_str(&idstr, id1);
1265 if (err)
1266 goto done;
1267 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1268 if (err)
1269 goto done;
1270 free(idstr);
1271 idstr = NULL;
1272 } else {
1273 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1274 outfile);
1275 if (err)
1276 goto done;
1279 if (id2) {
1280 err = got_object_id_str(&idstr, id2);
1281 if (err)
1282 goto done;
1283 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1284 if (err)
1285 goto done;
1286 free(idstr);
1287 idstr = NULL;
1288 } else {
1289 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1290 outfile);
1291 if (err)
1292 goto done;
1295 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1296 paths, label1, label2, diff_context, ignore_whitespace,
1297 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1298 done:
1299 free(idstr);
1300 return err;
1303 const struct got_error *
1304 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1305 FILE *f1, FILE *f2, int fd1, int fd2,
1306 struct got_object_id *id1, struct got_object_id *id2,
1307 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1308 int diff_context, int ignore_whitespace, int force_text_diff,
1309 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1310 struct got_repository *repo, FILE *outfile)
1312 const struct got_error *err;
1313 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1314 char *idstr = NULL;
1316 if (id2 == NULL)
1317 return got_error(GOT_ERR_NO_OBJ);
1319 if (id1) {
1320 err = got_object_open_as_commit(&commit1, repo, id1);
1321 if (err)
1322 goto done;
1323 err = got_object_id_str(&idstr, id1);
1324 if (err)
1325 goto done;
1326 err = show_object_id(lines, nlines, "commit", '-', idstr,
1327 outfile);
1328 if (err)
1329 goto done;
1330 free(idstr);
1331 idstr = NULL;
1332 } else {
1333 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1334 outfile);
1335 if (err)
1336 goto done;
1339 err = got_object_open_as_commit(&commit2, repo, id2);
1340 if (err)
1341 goto done;
1343 err = got_object_id_str(&idstr, id2);
1344 if (err)
1345 goto done;
1346 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1347 if (err)
1348 goto done;
1350 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1351 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1352 got_object_commit_get_tree_id(commit2), paths, "", "",
1353 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1354 dsa, repo, outfile, diff_algo);
1355 done:
1356 if (commit1)
1357 got_object_commit_close(commit1);
1358 if (commit2)
1359 got_object_commit_close(commit2);
1360 free(idstr);
1361 return err;
1364 const struct got_error *
1365 got_diff_files(struct got_diffreg_result **resultp,
1366 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1367 const char *label2, int diff_context, int ignore_whitespace,
1368 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1370 const struct got_error *err = NULL;
1371 struct got_diffreg_result *diffreg_result = NULL;
1373 if (resultp)
1374 *resultp = NULL;
1376 if (outfile) {
1377 fprintf(outfile, "file - %s\n",
1378 f1_exists ? label1 : "/dev/null");
1379 fprintf(outfile, "file + %s\n",
1380 f2_exists ? label2 : "/dev/null");
1383 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1384 ignore_whitespace, force_text_diff);
1385 if (err)
1386 goto done;
1388 if (outfile) {
1389 err = got_diffreg_output(NULL, NULL, diffreg_result,
1390 f1_exists, f2_exists, label1, label2,
1391 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1392 if (err)
1393 goto done;
1396 done:
1397 if (resultp && err == NULL)
1398 *resultp = diffreg_result;
1399 else if (diffreg_result) {
1400 const struct got_error *free_err;
1402 free_err = got_diffreg_result_free(diffreg_result);
1403 if (free_err && err == NULL)
1404 err = free_err;
1407 return err;