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 return err;
121 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
122 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
123 pe->path_len, change->add, change->rm);
125 return NULL;
128 static const struct got_error *
129 diff_blobs(struct got_diff_line **lines, size_t *nlines,
130 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
131 struct got_blob_object *blob2, FILE *f1, FILE *f2,
132 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
133 int diff_context, int ignore_whitespace, int force_text_diff,
134 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
135 enum got_diff_algorithm diff_algo)
137 const struct got_error *err = NULL, *free_err;
138 char hex1[SHA1_DIGEST_STRING_LENGTH];
139 char hex2[SHA1_DIGEST_STRING_LENGTH];
140 const char *idstr1 = NULL, *idstr2 = NULL;
141 off_t size1, size2;
142 struct got_diffreg_result *result = NULL;
143 off_t outoff = 0;
144 int n;
146 if (lines && *lines && *nlines > 0)
147 outoff = (*lines)[*nlines - 1].offset;
148 else if (lines) {
149 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
150 if (err)
151 goto done;
154 if (resultp)
155 *resultp = NULL;
157 if (f1) {
158 err = got_opentemp_truncate(f1);
159 if (err)
160 goto done;
162 if (f2) {
163 err = got_opentemp_truncate(f2);
164 if (err)
165 goto done;
168 size1 = 0;
169 if (blob1) {
170 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
171 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
172 blob1);
173 if (err)
174 goto done;
175 } else
176 idstr1 = "/dev/null";
178 size2 = 0;
179 if (blob2) {
180 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
181 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
182 blob2);
183 if (err)
184 goto done;
185 } else
186 idstr2 = "/dev/null";
188 if (outfile) {
189 char *modestr1 = NULL, *modestr2 = NULL;
190 int modebits;
191 if (mode1 && mode1 != mode2) {
192 if (S_ISLNK(mode1))
193 modebits = S_IFLNK;
194 else
195 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
196 if (asprintf(&modestr1, " (mode %o)",
197 mode1 & modebits) == -1) {
198 err = got_error_from_errno("asprintf");
199 goto done;
202 if (mode2 && mode1 != mode2) {
203 if (S_ISLNK(mode2))
204 modebits = S_IFLNK;
205 else
206 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
207 if (asprintf(&modestr2, " (mode %o)",
208 mode2 & modebits) == -1) {
209 err = got_error_from_errno("asprintf");
210 goto done;
213 n = fprintf(outfile, "blob - %s%s\n", idstr1,
214 modestr1 ? modestr1 : "");
215 if (n < 0)
216 goto done;
217 outoff += n;
218 if (lines) {
219 err = add_line_metadata(lines, nlines, outoff,
220 GOT_DIFF_LINE_BLOB_MIN);
221 if (err)
222 goto done;
225 n = fprintf(outfile, "blob + %s%s\n", idstr2,
226 modestr2 ? modestr2 : "");
227 if (n < 0)
228 goto done;
229 outoff += n;
230 if (lines) {
231 err = add_line_metadata(lines, nlines, outoff,
232 GOT_DIFF_LINE_BLOB_PLUS);
233 if (err)
234 goto done;
237 free(modestr1);
238 free(modestr2);
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 if (resultp && err == NULL)
301 *resultp = result;
302 else if (result) {
303 free_err = got_diffreg_result_free(result);
304 if (free_err && err == NULL)
305 err = free_err;
308 return err;
311 const struct got_error *
312 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
313 struct got_blob_object *blob2, FILE *f1, FILE *f2,
314 struct got_object_id *id1, struct got_object_id *id2,
315 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
316 struct got_repository *repo)
318 struct got_diff_blob_output_unidiff_arg *a = arg;
320 return diff_blobs(&a->lines, &a->nlines, NULL,
321 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
322 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
323 a->diffstat, a->outfile, a->diff_algo);
326 const struct got_error *
327 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
328 struct got_blob_object *blob1, struct got_blob_object *blob2,
329 FILE *f1, FILE *f2, const char *label1, const char *label2,
330 enum got_diff_algorithm diff_algo, int diff_context,
331 int ignore_whitespace, int force_text_diff, int show_diffstat,
332 struct got_diffstat_cb_arg *ds, FILE *outfile)
334 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
335 label1, label2, 0, 0, diff_context, ignore_whitespace,
336 force_text_diff, show_diffstat, ds, outfile, diff_algo);
339 static const struct got_error *
340 diff_blob_file(struct got_diffreg_result **resultp,
341 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
342 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
343 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
344 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
345 FILE *outfile)
347 const struct got_error *err = NULL, *free_err;
348 char hex1[SHA1_DIGEST_STRING_LENGTH];
349 const char *idstr1 = NULL;
350 struct got_diffreg_result *result = NULL;
352 if (resultp)
353 *resultp = NULL;
355 if (blob1)
356 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
357 else
358 idstr1 = "/dev/null";
360 if (outfile) {
361 char *mode = NULL;
363 /* display file mode for new added files only */
364 if (f2_exists && blob1 == NULL) {
365 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
367 if (S_ISLNK(sb2->st_mode))
368 mmask = S_IFLNK;
369 if (asprintf(&mode, " (mode %o)",
370 sb2->st_mode & mmask) == -1)
371 return got_error_from_errno("asprintf");
373 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
374 fprintf(outfile, "file + %s%s\n",
375 f2_exists ? label2 : "/dev/null", mode ? mode : "");
376 free(mode);
379 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
380 force_text_diff);
381 if (err)
382 goto done;
384 if (outfile) {
385 err = got_diffreg_output(NULL, NULL, result,
386 blob1 != NULL, f2_exists,
387 label2, /* show local file's path, not a blob ID */
388 label2, GOT_DIFF_OUTPUT_UNIDIFF,
389 diff_context, outfile);
390 if (err)
391 goto done;
394 if (show_diffstat) {
395 char *path = NULL;
396 int status = GOT_STATUS_NO_CHANGE;
398 /*
399 * Ignore 'm'ode status change: if there's no accompanying
400 * content change, there'll be no diffstat, and if there
401 * are actual changes, 'M'odified takes precedence.
402 */
403 if (blob1 == NULL)
404 status = GOT_STATUS_ADD;
405 else if (!f2_exists)
406 status = GOT_STATUS_DELETE;
407 else
408 status = GOT_STATUS_MODIFY;
410 if (label2 != NULL &&
411 (status != GOT_STATUS_DELETE || label1 == NULL))
412 path = strdup(label2);
413 else
414 path = strdup(label1);
415 if (path == NULL) {
416 err = got_error_from_errno("strdup");
417 goto done;
420 err = get_diffstat(ds, path, result->result, force_text_diff,
421 status);
422 if (err) {
423 free(path);
424 goto done;
428 done:
429 if (resultp && err == NULL)
430 *resultp = result;
431 else if (result) {
432 free_err = got_diffreg_result_free(result);
433 if (free_err && err == NULL)
434 err = free_err;
436 return err;
439 const struct got_error *
440 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
441 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
442 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
443 int ignore_whitespace, int force_text_diff, int show_diffstat,
444 struct got_diffstat_cb_arg *ds, FILE *outfile)
446 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
447 sb2, label2, diff_algo, diff_context, ignore_whitespace,
448 force_text_diff, show_diffstat, ds, outfile);
451 static const struct got_error *
452 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
453 const char *label, mode_t mode, struct got_repository *repo,
454 got_diff_blob_cb cb, void *cb_arg)
456 const struct got_error *err;
457 struct got_blob_object *blob = NULL;
458 struct got_object *obj = NULL;
460 err = got_object_open(&obj, repo, id);
461 if (err)
462 return err;
464 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
465 if (err)
466 goto done;
467 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
468 NULL, label, 0, mode, repo);
469 done:
470 got_object_close(obj);
471 if (blob)
472 got_object_blob_close(blob);
473 return err;
476 static const struct got_error *
477 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
478 FILE *f1, FILE *f2, int fd1, int fd2,
479 const char *label1, const char *label2,
480 mode_t mode1, mode_t mode2, struct got_repository *repo,
481 got_diff_blob_cb cb, void *cb_arg)
483 const struct got_error *err;
484 struct got_object *obj1 = NULL;
485 struct got_object *obj2 = NULL;
486 struct got_blob_object *blob1 = NULL;
487 struct got_blob_object *blob2 = NULL;
489 err = got_object_open(&obj1, repo, id1);
490 if (err)
491 return err;
493 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
494 err = got_error(GOT_ERR_OBJ_TYPE);
495 goto done;
498 err = got_object_open(&obj2, repo, id2);
499 if (err)
500 goto done;
501 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 goto done;
506 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
507 if (err)
508 goto done;
510 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
511 if (err)
512 goto done;
514 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
515 mode1, mode2, repo);
516 done:
517 if (obj1)
518 got_object_close(obj1);
519 if (obj2)
520 got_object_close(obj2);
521 if (blob1)
522 got_object_blob_close(blob1);
523 if (blob2)
524 got_object_blob_close(blob2);
525 return err;
528 static const struct got_error *
529 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
530 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
531 got_diff_blob_cb cb, void *cb_arg)
533 const struct got_error *err;
534 struct got_blob_object *blob = NULL;
535 struct got_object *obj = NULL;
537 err = got_object_open(&obj, repo, id);
538 if (err)
539 return err;
541 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
542 if (err)
543 goto done;
544 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
545 mode, 0, repo);
546 done:
547 got_object_close(obj);
548 if (blob)
549 got_object_blob_close(blob);
550 return err;
553 static const struct got_error *
554 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
555 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
556 void *cb_arg, int diff_content)
558 const struct got_error *err = NULL;
559 struct got_object *treeobj = NULL;
560 struct got_tree_object *tree = NULL;
562 err = got_object_open(&treeobj, repo, id);
563 if (err)
564 goto done;
566 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
567 err = got_error(GOT_ERR_OBJ_TYPE);
568 goto done;
571 err = got_object_tree_open(&tree, repo, treeobj);
572 if (err)
573 goto done;
575 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
576 repo, cb, cb_arg, diff_content);
577 done:
578 if (tree)
579 got_object_tree_close(tree);
580 if (treeobj)
581 got_object_close(treeobj);
582 return err;
585 static const struct got_error *
586 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
587 FILE *f1, FILE *f2, int fd1, int fd2,
588 const char *label1, const char *label2,
589 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
590 int diff_content)
592 const struct got_error *err;
593 struct got_object *treeobj1 = NULL;
594 struct got_object *treeobj2 = NULL;
595 struct got_tree_object *tree1 = NULL;
596 struct got_tree_object *tree2 = NULL;
598 err = got_object_open(&treeobj1, repo, id1);
599 if (err)
600 goto done;
602 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
603 err = got_error(GOT_ERR_OBJ_TYPE);
604 goto done;
607 err = got_object_open(&treeobj2, repo, id2);
608 if (err)
609 goto done;
611 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
612 err = got_error(GOT_ERR_OBJ_TYPE);
613 goto done;
616 err = got_object_tree_open(&tree1, repo, treeobj1);
617 if (err)
618 goto done;
620 err = got_object_tree_open(&tree2, repo, treeobj2);
621 if (err)
622 goto done;
624 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
625 label1, label2, repo, cb, cb_arg, diff_content);
627 done:
628 if (tree1)
629 got_object_tree_close(tree1);
630 if (tree2)
631 got_object_tree_close(tree2);
632 if (treeobj1)
633 got_object_close(treeobj1);
634 if (treeobj2)
635 got_object_close(treeobj2);
636 return err;
639 static const struct got_error *
640 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
641 FILE *f2, const char *label, struct got_repository *repo,
642 got_diff_blob_cb cb, void *cb_arg, int diff_content)
644 const struct got_error *err;
645 struct got_object *treeobj = NULL;
646 struct got_tree_object *tree = NULL;
648 err = got_object_open(&treeobj, repo, id);
649 if (err)
650 goto done;
652 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
653 err = got_error(GOT_ERR_OBJ_TYPE);
654 goto done;
657 err = got_object_tree_open(&tree, repo, treeobj);
658 if (err)
659 goto done;
661 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
662 repo, cb, cb_arg, diff_content);
663 done:
664 if (tree)
665 got_object_tree_close(tree);
666 if (treeobj)
667 got_object_close(treeobj);
668 return err;
671 static const struct got_error *
672 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
673 const char *label1, const char *label2, struct got_repository *repo,
674 got_diff_blob_cb cb, void *cb_arg)
676 /* XXX TODO */
677 return NULL;
680 static const struct got_error *
681 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
682 FILE *f1, FILE *f2, int fd1, int fd2,
683 const char *label1, const char *label2,
684 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
685 int diff_content)
687 const struct got_error *err = NULL;
688 int id_match;
690 if (got_object_tree_entry_is_submodule(te1))
691 return NULL;
693 if (te2 == NULL) {
694 if (S_ISDIR(te1->mode))
695 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
696 label1, repo, cb, cb_arg, diff_content);
697 else {
698 if (diff_content)
699 err = diff_deleted_blob(&te1->id, f1, fd1,
700 f2, label1, te1->mode, repo, cb, cb_arg);
701 else
702 err = cb(cb_arg, NULL, NULL, NULL, NULL,
703 &te1->id, NULL, label1, NULL,
704 te1->mode, 0, repo);
706 return err;
707 } else if (got_object_tree_entry_is_submodule(te2))
708 return NULL;
710 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
711 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
712 if (!id_match)
713 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
714 fd1, fd2, label1, label2, repo, cb, cb_arg,
715 diff_content);
716 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
717 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
718 if (!id_match ||
719 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
720 (te2->mode & (S_IFLNK | S_IXUSR))) {
721 if (diff_content)
722 return diff_modified_blob(&te1->id, &te2->id,
723 f1, f2, fd1, fd2, label1, label2,
724 te1->mode, te2->mode, repo, cb, cb_arg);
725 else
726 return cb(cb_arg, NULL, NULL, NULL, NULL,
727 &te1->id, &te2->id, label1, label2,
728 te1->mode, te2->mode, repo);
732 if (id_match)
733 return NULL;
735 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
736 cb, cb_arg);
739 static const struct got_error *
740 diff_entry_new_old(struct got_tree_entry *te2,
741 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
742 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
743 int diff_content)
745 if (te1 != NULL) /* handled by diff_entry_old_new() */
746 return NULL;
748 if (got_object_tree_entry_is_submodule(te2))
749 return NULL;
751 if (S_ISDIR(te2->mode))
752 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
753 repo, cb, cb_arg, diff_content);
755 if (diff_content)
756 return diff_added_blob(&te2->id, f1, f2, fd2,
757 label2, te2->mode, repo, cb, cb_arg);
759 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
760 NULL, label2, 0, te2->mode, repo);
763 const struct got_error *
764 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
765 struct got_blob_object *blob2, FILE *f1, FILE *f2,
766 struct got_object_id *id1, struct got_object_id *id2,
767 const char *label1, const char *label2,
768 mode_t mode1, mode_t mode2, struct got_repository *repo)
770 const struct got_error *err = NULL;
771 struct got_diffreg_result *result = NULL;
772 struct got_diffstat_cb_arg *a = arg;
773 char *path = NULL;
774 int status = GOT_STATUS_NO_CHANGE;
776 path = strdup(label2 ? label2 : label1);
777 if (path == NULL)
778 return got_error_from_errno("strdup");
780 if (id1 == NULL)
781 status = GOT_STATUS_ADD;
782 else if (id2 == NULL)
783 status = GOT_STATUS_DELETE;
784 else {
785 if (got_object_id_cmp(id1, id2) != 0)
786 status = GOT_STATUS_MODIFY;
787 else if (mode1 != mode2)
788 status = GOT_STATUS_MODE_CHANGE;
791 if (f1) {
792 err = got_opentemp_truncate(f1);
793 if (err)
794 goto done;
796 if (f2) {
797 err = got_opentemp_truncate(f2);
798 if (err)
799 goto done;
802 if (blob1) {
803 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
804 blob1);
805 if (err)
806 goto done;
808 if (blob2) {
809 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
810 blob2);
811 if (err)
812 goto done;
815 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
816 a->force_text);
817 if (err)
818 goto done;
820 err = get_diffstat(a, path, result->result, a->force_text, status);
822 done:
823 if (result) {
824 const struct got_error *free_err;
826 free_err = got_diffreg_result_free(result);
827 if (free_err && err == NULL)
828 err = free_err;
830 if (err)
831 free(path);
832 return err;
835 const struct got_error *
836 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
837 struct got_blob_object *blob2, FILE *f1, FILE *f2,
838 struct got_object_id *id1, struct got_object_id *id2,
839 const char *label1, const char *label2,
840 mode_t mode1, mode_t mode2, struct got_repository *repo)
842 const struct got_error *err = NULL;
843 struct got_pathlist_head *paths = arg;
844 struct got_diff_changed_path *change = NULL;
845 char *path = NULL;
847 path = strdup(label2 ? label2 : label1);
848 if (path == NULL)
849 return got_error_from_errno("strdup");
851 change = malloc(sizeof(*change));
852 if (change == NULL) {
853 err = got_error_from_errno("malloc");
854 goto done;
857 change->status = GOT_STATUS_NO_CHANGE;
858 if (id1 == NULL)
859 change->status = GOT_STATUS_ADD;
860 else if (id2 == NULL)
861 change->status = GOT_STATUS_DELETE;
862 else {
863 if (got_object_id_cmp(id1, id2) != 0)
864 change->status = GOT_STATUS_MODIFY;
865 else if (mode1 != mode2)
866 change->status = GOT_STATUS_MODE_CHANGE;
869 err = got_pathlist_append(paths, path, change);
870 done:
871 if (err) {
872 free(path);
873 free(change);
875 return err;
878 const struct got_error *
879 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
880 FILE *f1, FILE *f2, int fd1, int fd2,
881 const char *label1, const char *label2,
882 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
883 int diff_content)
885 const struct got_error *err = NULL;
886 struct got_tree_entry *te1 = NULL;
887 struct got_tree_entry *te2 = NULL;
888 char *l1 = NULL, *l2 = NULL;
889 int tidx1 = 0, tidx2 = 0;
891 if (tree1) {
892 te1 = got_object_tree_get_entry(tree1, 0);
893 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
894 te1->name) == -1)
895 return got_error_from_errno("asprintf");
897 if (tree2) {
898 te2 = got_object_tree_get_entry(tree2, 0);
899 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
900 te2->name) == -1)
901 return got_error_from_errno("asprintf");
904 do {
905 if (te1) {
906 struct got_tree_entry *te = NULL;
907 if (tree2)
908 te = got_object_tree_find_entry(tree2,
909 te1->name);
910 if (te) {
911 free(l2);
912 l2 = NULL;
913 if (te && asprintf(&l2, "%s%s%s", label2,
914 label2[0] ? "/" : "", te->name) == -1)
915 return
916 got_error_from_errno("asprintf");
918 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
919 l1, l2, repo, cb, cb_arg, diff_content);
920 if (err)
921 break;
924 if (te2) {
925 struct got_tree_entry *te = NULL;
926 if (tree1)
927 te = got_object_tree_find_entry(tree1,
928 te2->name);
929 free(l2);
930 if (te) {
931 if (asprintf(&l2, "%s%s%s", label2,
932 label2[0] ? "/" : "", te->name) == -1)
933 return
934 got_error_from_errno("asprintf");
935 } else {
936 if (asprintf(&l2, "%s%s%s", label2,
937 label2[0] ? "/" : "", te2->name) == -1)
938 return
939 got_error_from_errno("asprintf");
941 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
942 repo, cb, cb_arg, diff_content);
943 if (err)
944 break;
947 free(l1);
948 l1 = NULL;
949 if (te1) {
950 tidx1++;
951 te1 = got_object_tree_get_entry(tree1, tidx1);
952 if (te1 &&
953 asprintf(&l1, "%s%s%s", label1,
954 label1[0] ? "/" : "", te1->name) == -1)
955 return got_error_from_errno("asprintf");
957 free(l2);
958 l2 = NULL;
959 if (te2) {
960 tidx2++;
961 te2 = got_object_tree_get_entry(tree2, tidx2);
962 if (te2 &&
963 asprintf(&l2, "%s%s%s", label2,
964 label2[0] ? "/" : "", te2->name) == -1)
965 return got_error_from_errno("asprintf");
967 } while (te1 || te2);
969 return err;
972 const struct got_error *
973 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
974 FILE *f1, FILE *f2, int fd1, int fd2,
975 struct got_object_id *id1, struct got_object_id *id2,
976 const char *label1, const char *label2,
977 enum got_diff_algorithm diff_algo, int diff_context,
978 int ignore_whitespace, int force_text_diff, int show_diffstat,
979 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
981 const struct got_error *err;
982 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
984 if (id1 == NULL && id2 == NULL)
985 return got_error(GOT_ERR_NO_OBJ);
987 if (id1) {
988 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
989 if (err)
990 goto done;
992 if (id2) {
993 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
994 if (err)
995 goto done;
997 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
998 diff_algo, diff_context, ignore_whitespace, force_text_diff,
999 show_diffstat, ds, outfile);
1000 done:
1001 if (blob1)
1002 got_object_blob_close(blob1);
1003 if (blob2)
1004 got_object_blob_close(blob2);
1005 return err;
1008 static const struct got_error *
1009 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1010 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1011 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1013 const struct got_error *err = NULL;
1014 struct got_pathlist_entry *pe;
1015 struct got_object_id *id1 = NULL, *id2 = NULL;
1016 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1017 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1019 TAILQ_FOREACH(pe, paths, entry) {
1020 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1021 mode_t mode1 = 0, mode2 = 0;
1023 free(id1);
1024 id1 = NULL;
1025 free(id2);
1026 id2 = NULL;
1027 if (subtree1) {
1028 got_object_tree_close(subtree1);
1029 subtree1 = NULL;
1031 if (subtree2) {
1032 got_object_tree_close(subtree2);
1033 subtree2 = NULL;
1035 if (blob1) {
1036 got_object_blob_close(blob1);
1037 blob1 = NULL;
1039 if (blob2) {
1040 got_object_blob_close(blob2);
1041 blob2 = NULL;
1044 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1045 pe->path);
1046 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1047 goto done;
1048 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1049 pe->path);
1050 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1051 goto done;
1052 if (id1 == NULL && id2 == NULL) {
1053 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1054 goto done;
1056 if (id1) {
1057 err = got_object_get_type(&type1, repo, id1);
1058 if (err)
1059 goto done;
1061 if (id2) {
1062 err = got_object_get_type(&type2, repo, id2);
1063 if (err)
1064 goto done;
1066 if (type1 == GOT_OBJ_TYPE_ANY &&
1067 type2 == GOT_OBJ_TYPE_ANY) {
1068 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1069 goto done;
1070 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1071 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1072 err = got_error(GOT_ERR_OBJ_TYPE);
1073 goto done;
1076 if (type1 == GOT_OBJ_TYPE_BLOB ||
1077 type2 == GOT_OBJ_TYPE_BLOB) {
1078 if (id1) {
1079 err = got_object_open_as_blob(&blob1, repo,
1080 id1, 8192, fd1);
1081 if (err)
1082 goto done;
1084 if (id2) {
1085 err = got_object_open_as_blob(&blob2, repo,
1086 id2, 8192, fd2);
1087 if (err)
1088 goto done;
1090 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1091 id1 ? pe->path : "/dev/null",
1092 id2 ? pe->path : "/dev/null",
1093 mode1, mode2, repo);
1094 if (err)
1095 goto done;
1096 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1097 type2 == GOT_OBJ_TYPE_TREE) {
1098 if (id1) {
1099 err = got_object_open_as_tree(&subtree1, repo,
1100 id1);
1101 if (err)
1102 goto done;
1104 if (id2) {
1105 err = got_object_open_as_tree(&subtree2, repo,
1106 id2);
1107 if (err)
1108 goto done;
1110 err = got_diff_tree(subtree1, subtree2, f1, f2,
1111 fd1, fd2,
1112 id1 ? pe->path : "/dev/null",
1113 id2 ? pe->path : "/dev/null",
1114 repo, cb, cb_arg, 1);
1115 if (err)
1116 goto done;
1117 } else {
1118 err = got_error(GOT_ERR_OBJ_TYPE);
1119 goto done;
1122 done:
1123 free(id1);
1124 free(id2);
1125 if (subtree1)
1126 got_object_tree_close(subtree1);
1127 if (subtree2)
1128 got_object_tree_close(subtree2);
1129 if (blob1)
1130 got_object_blob_close(blob1);
1131 if (blob2)
1132 got_object_blob_close(blob2);
1133 return err;
1136 static const struct got_error *
1137 show_object_id(struct got_diff_line **lines, size_t *nlines,
1138 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1140 const struct got_error *err;
1141 int n;
1142 off_t outoff = 0;
1144 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1145 if (n < 0)
1146 return got_error_from_errno("fprintf");
1148 if (lines != NULL && *lines != NULL) {
1149 if (*nlines == 0) {
1150 err = add_line_metadata(lines, nlines, 0,
1151 GOT_DIFF_LINE_META);
1152 if (err)
1153 return err;
1154 } else
1155 outoff = (*lines)[*nlines - 1].offset;
1157 outoff += n;
1158 err = add_line_metadata(lines, nlines, outoff,
1159 GOT_DIFF_LINE_META);
1160 if (err)
1161 return err;
1164 return NULL;
1167 static const struct got_error *
1168 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1169 FILE *f1, FILE *f2, int fd1, int fd2,
1170 struct got_object_id *id1, struct got_object_id *id2,
1171 struct got_pathlist_head *paths, const char *label1, const char *label2,
1172 int diff_context, int ignore_whitespace, int force_text_diff,
1173 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1174 struct got_repository *repo, FILE *outfile,
1175 enum got_diff_algorithm diff_algo)
1177 const struct got_error *err;
1178 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1179 struct got_diff_blob_output_unidiff_arg arg;
1180 int want_linemeta = (lines != NULL && *lines != NULL);
1182 if (id1 == NULL && id2 == NULL)
1183 return got_error(GOT_ERR_NO_OBJ);
1185 if (id1) {
1186 err = got_object_open_as_tree(&tree1, repo, id1);
1187 if (err)
1188 goto done;
1190 if (id2) {
1191 err = got_object_open_as_tree(&tree2, repo, id2);
1192 if (err)
1193 goto done;
1196 arg.diff_algo = diff_algo;
1197 arg.diff_context = diff_context;
1198 arg.ignore_whitespace = ignore_whitespace;
1199 arg.force_text_diff = force_text_diff;
1200 arg.show_diffstat = show_diffstat;
1201 arg.diffstat = dsa;
1202 arg.outfile = outfile;
1203 if (want_linemeta) {
1204 arg.lines = *lines;
1205 arg.nlines = *nlines;
1206 } else {
1207 arg.lines = NULL;
1208 arg.nlines = 0;
1210 if (paths == NULL || TAILQ_EMPTY(paths))
1211 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1212 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1213 else
1214 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1215 got_diff_blob_output_unidiff, &arg);
1216 if (want_linemeta) {
1217 *lines = arg.lines; /* was likely re-allocated */
1218 *nlines = arg.nlines;
1220 done:
1221 if (tree1)
1222 got_object_tree_close(tree1);
1223 if (tree2)
1224 got_object_tree_close(tree2);
1225 return err;
1228 const struct got_error *
1229 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1230 FILE *f1, FILE *f2, int fd1, int fd2,
1231 struct got_object_id *id1, struct got_object_id *id2,
1232 struct got_pathlist_head *paths, const char *label1, const char *label2,
1233 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1234 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1235 struct got_repository *repo, FILE *outfile)
1237 const struct got_error *err;
1238 char *idstr = NULL;
1240 if (id1 == NULL && id2 == NULL)
1241 return got_error(GOT_ERR_NO_OBJ);
1243 if (id1) {
1244 err = got_object_id_str(&idstr, id1);
1245 if (err)
1246 goto done;
1247 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1248 if (err)
1249 goto done;
1250 free(idstr);
1251 idstr = NULL;
1252 } else {
1253 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1254 outfile);
1255 if (err)
1256 goto done;
1259 if (id2) {
1260 err = got_object_id_str(&idstr, id2);
1261 if (err)
1262 goto done;
1263 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1264 if (err)
1265 goto done;
1266 free(idstr);
1267 idstr = NULL;
1268 } else {
1269 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1270 outfile);
1271 if (err)
1272 goto done;
1275 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1276 paths, label1, label2, diff_context, ignore_whitespace,
1277 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1278 done:
1279 free(idstr);
1280 return err;
1283 const struct got_error *
1284 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1285 FILE *f1, FILE *f2, int fd1, int fd2,
1286 struct got_object_id *id1, struct got_object_id *id2,
1287 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1288 int diff_context, int ignore_whitespace, int force_text_diff,
1289 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1290 struct got_repository *repo, FILE *outfile)
1292 const struct got_error *err;
1293 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1294 char *idstr = NULL;
1296 if (id2 == NULL)
1297 return got_error(GOT_ERR_NO_OBJ);
1299 if (id1) {
1300 err = got_object_open_as_commit(&commit1, repo, id1);
1301 if (err)
1302 goto done;
1303 err = got_object_id_str(&idstr, id1);
1304 if (err)
1305 goto done;
1306 err = show_object_id(lines, nlines, "commit", '-', idstr,
1307 outfile);
1308 if (err)
1309 goto done;
1310 free(idstr);
1311 idstr = NULL;
1312 } else {
1313 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1314 outfile);
1315 if (err)
1316 goto done;
1319 err = got_object_open_as_commit(&commit2, repo, id2);
1320 if (err)
1321 goto done;
1323 err = got_object_id_str(&idstr, id2);
1324 if (err)
1325 goto done;
1326 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1327 if (err)
1328 goto done;
1330 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1331 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1332 got_object_commit_get_tree_id(commit2), paths, "", "",
1333 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1334 dsa, repo, outfile, diff_algo);
1335 done:
1336 if (commit1)
1337 got_object_commit_close(commit1);
1338 if (commit2)
1339 got_object_commit_close(commit2);
1340 free(idstr);
1341 return err;
1344 const struct got_error *
1345 got_diff_files(struct got_diffreg_result **resultp,
1346 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1347 const char *label2, int diff_context, int ignore_whitespace,
1348 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1350 const struct got_error *err = NULL;
1351 struct got_diffreg_result *diffreg_result = NULL;
1353 if (resultp)
1354 *resultp = NULL;
1356 if (outfile) {
1357 fprintf(outfile, "file - %s\n",
1358 f1_exists ? label1 : "/dev/null");
1359 fprintf(outfile, "file + %s\n",
1360 f2_exists ? label2 : "/dev/null");
1363 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1364 ignore_whitespace, force_text_diff);
1365 if (err)
1366 goto done;
1368 if (outfile) {
1369 err = got_diffreg_output(NULL, NULL, diffreg_result,
1370 f1_exists, f2_exists, label1, label2,
1371 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1372 if (err)
1373 goto done;
1376 done:
1377 if (resultp && err == NULL)
1378 *resultp = diffreg_result;
1379 else if (diffreg_result) {
1380 const struct got_error *free_err;
1381 free_err = got_diffreg_result_free(diffreg_result);
1382 if (free_err && err == NULL)
1383 err = free_err;
1386 return err;