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 <sha2.h>
26 #include <zlib.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 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
137 enum got_diff_algorithm diff_algo)
139 const struct got_error *err = NULL, *free_err;
140 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
141 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
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 (diffstat) {
247 char *path = NULL;
248 int status = GOT_STATUS_NO_CHANGE;
250 if (blob1 == NULL)
251 status = GOT_STATUS_ADD;
252 else if (blob2 == NULL)
253 status = GOT_STATUS_DELETE;
254 else {
255 if (strcmp(idstr1, idstr2) != 0)
256 status = GOT_STATUS_MODIFY;
257 else if (mode1 != mode2)
258 status = GOT_STATUS_MODE_CHANGE;
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(diffstat, path, result->result,
281 force_text_diff, 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->diffstat, a->outfile,
324 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,
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, 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, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
347 const struct got_error *err = NULL, *free_err;
348 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
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 (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(diffstat, path, result->result,
421 force_text_diff, 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,
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, 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_tree_entry *te1, struct got_tree_entry *te2,
673 FILE *f1, FILE *f2, int fd1, int fd2,
674 const char *label1, const char *label2, struct got_repository *repo,
675 got_diff_blob_cb cb, void *cb_arg, int diff_content)
677 const struct got_error *err = NULL;
679 /*
680 * Handle files changing into directories and vice-versa.
681 * Disregard edge cases with FIFOs, device nodes, etc for now.
682 */
683 if (!S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
684 if (S_ISREG(te1->mode)) {
685 if (diff_content) {
686 err = diff_deleted_blob(&te1->id, f1, fd1,
687 f2, label1, te1->mode, repo, cb, cb_arg);
688 } else {
689 err = cb(cb_arg, NULL, NULL, NULL, NULL,
690 &te1->id, NULL, label1, NULL,
691 te1->mode, 0, repo);
693 if (err)
694 return err;
696 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
697 repo, cb, cb_arg, diff_content);
698 } else if (S_ISDIR(te1->mode) && !S_ISDIR(te2->mode)) {
699 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
700 label1, repo, cb, cb_arg, diff_content);
701 if (err)
702 return err;
703 if (S_ISREG(te2->mode)) {
704 if (diff_content) {
705 err = diff_added_blob(&te2->id, f1, f2, fd2,
706 label2, te2->mode, repo, cb, cb_arg);
707 } else {
708 err = cb(cb_arg, NULL, NULL, NULL, NULL, NULL,
709 &te2->id, NULL, label2, 0, te2->mode, repo);
711 if (err)
712 return err;
716 return NULL;
719 static const struct got_error *
720 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
721 FILE *f1, FILE *f2, int fd1, int fd2,
722 const char *label1, const char *label2,
723 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
724 int diff_content)
726 const struct got_error *err = NULL;
727 int id_match;
729 if (got_object_tree_entry_is_submodule(te1))
730 return NULL;
732 if (te2 == NULL) {
733 if (S_ISDIR(te1->mode))
734 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
735 label1, repo, cb, cb_arg, diff_content);
736 else {
737 if (diff_content)
738 err = diff_deleted_blob(&te1->id, f1, fd1,
739 f2, label1, te1->mode, repo, cb, cb_arg);
740 else
741 err = cb(cb_arg, NULL, NULL, NULL, NULL,
742 &te1->id, NULL, label1, NULL,
743 te1->mode, 0, repo);
745 return err;
746 } else if (got_object_tree_entry_is_submodule(te2))
747 return NULL;
749 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
750 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
751 if (!id_match)
752 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
753 fd1, fd2, label1, label2, repo, cb, cb_arg,
754 diff_content);
755 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
756 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
757 if (!id_match ||
758 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
759 (te2->mode & (S_IFLNK | S_IXUSR))) {
760 if (diff_content)
761 return diff_modified_blob(&te1->id, &te2->id,
762 f1, f2, fd1, fd2, label1, label2,
763 te1->mode, te2->mode, repo, cb, cb_arg);
764 else
765 return cb(cb_arg, NULL, NULL, NULL, NULL,
766 &te1->id, &te2->id, label1, label2,
767 te1->mode, te2->mode, repo);
771 if (id_match)
772 return NULL;
774 return diff_kind_mismatch(te1, te2, f1, f2, fd1, fd2,
775 label1, label2, repo, cb, cb_arg, diff_content);
778 static const struct got_error *
779 diff_entry_new_old(struct got_tree_entry *te2,
780 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
781 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
782 int diff_content)
784 if (te1 != NULL) /* handled by diff_entry_old_new() */
785 return NULL;
787 if (got_object_tree_entry_is_submodule(te2))
788 return NULL;
790 if (S_ISDIR(te2->mode))
791 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
792 repo, cb, cb_arg, diff_content);
794 if (diff_content)
795 return diff_added_blob(&te2->id, f1, f2, fd2,
796 label2, te2->mode, repo, cb, cb_arg);
798 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
799 NULL, label2, 0, te2->mode, repo);
802 const struct got_error *
803 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
804 struct got_blob_object *blob2, FILE *f1, FILE *f2,
805 struct got_object_id *id1, struct got_object_id *id2,
806 const char *label1, const char *label2,
807 mode_t mode1, mode_t mode2, struct got_repository *repo)
809 const struct got_error *err = NULL;
810 struct got_diffreg_result *result = NULL;
811 struct got_diffstat_cb_arg *a = arg;
812 char *path = NULL;
813 int status = GOT_STATUS_NO_CHANGE;
815 path = strdup(label2 ? label2 : label1);
816 if (path == NULL)
817 return got_error_from_errno("strdup");
819 if (id1 == NULL)
820 status = GOT_STATUS_ADD;
821 else if (id2 == NULL)
822 status = GOT_STATUS_DELETE;
823 else {
824 if (got_object_id_cmp(id1, id2) != 0)
825 status = GOT_STATUS_MODIFY;
826 else if (mode1 != mode2)
827 status = GOT_STATUS_MODE_CHANGE;
830 if (f1) {
831 err = got_opentemp_truncate(f1);
832 if (err)
833 goto done;
835 if (f2) {
836 err = got_opentemp_truncate(f2);
837 if (err)
838 goto done;
841 if (blob1) {
842 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
843 blob1);
844 if (err)
845 goto done;
847 if (blob2) {
848 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
849 blob2);
850 if (err)
851 goto done;
854 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
855 a->force_text);
856 if (err)
857 goto done;
859 err = get_diffstat(a, path, result->result, a->force_text, status);
861 done:
862 if (result) {
863 const struct got_error *free_err;
865 free_err = got_diffreg_result_free(result);
866 if (free_err && err == NULL)
867 err = free_err;
869 if (err)
870 free(path);
871 return err;
874 const struct got_error *
875 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
876 struct got_blob_object *blob2, FILE *f1, FILE *f2,
877 struct got_object_id *id1, struct got_object_id *id2,
878 const char *label1, const char *label2,
879 mode_t mode1, mode_t mode2, struct got_repository *repo)
881 const struct got_error *err = NULL;
882 struct got_pathlist_head *paths = arg;
883 struct got_diff_changed_path *change = NULL;
884 char *path = NULL;
886 path = strdup(label2 ? label2 : label1);
887 if (path == NULL)
888 return got_error_from_errno("strdup");
890 change = malloc(sizeof(*change));
891 if (change == NULL) {
892 err = got_error_from_errno("malloc");
893 goto done;
896 change->status = GOT_STATUS_NO_CHANGE;
897 if (id1 == NULL)
898 change->status = GOT_STATUS_ADD;
899 else if (id2 == NULL)
900 change->status = GOT_STATUS_DELETE;
901 else {
902 if (got_object_id_cmp(id1, id2) != 0)
903 change->status = GOT_STATUS_MODIFY;
904 else if (mode1 != mode2)
905 change->status = GOT_STATUS_MODE_CHANGE;
908 err = got_pathlist_append(paths, path, change);
909 done:
910 if (err) {
911 free(path);
912 free(change);
914 return err;
917 const struct got_error *
918 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
919 FILE *f1, FILE *f2, int fd1, int fd2,
920 const char *label1, const char *label2,
921 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
922 int diff_content)
924 const struct got_error *err = NULL;
925 struct got_tree_entry *te1 = NULL;
926 struct got_tree_entry *te2 = NULL;
927 char *l1 = NULL, *l2 = NULL;
928 int tidx1 = 0, tidx2 = 0;
930 if (tree1) {
931 te1 = got_object_tree_get_entry(tree1, 0);
932 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
933 te1->name) == -1)
934 return got_error_from_errno("asprintf");
936 if (tree2) {
937 te2 = got_object_tree_get_entry(tree2, 0);
938 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
939 te2->name) == -1) {
940 err = got_error_from_errno("asprintf");
941 goto done;
945 do {
946 if (te1) {
947 struct got_tree_entry *te = NULL;
949 if (tree2)
950 te = got_object_tree_find_entry(tree2,
951 te1->name);
952 if (te) {
953 free(l2);
954 l2 = NULL;
955 if (te && asprintf(&l2, "%s%s%s", label2,
956 label2[0] ? "/" : "", te->name) == -1) {
957 err = got_error_from_errno("asprintf");
958 goto done;
962 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
963 l1, l2, repo, cb, cb_arg, diff_content);
964 if (err)
965 break;
968 if (te2) {
969 struct got_tree_entry *te = NULL;
971 if (tree1)
972 te = got_object_tree_find_entry(tree1,
973 te2->name);
975 free(l2);
976 l2 = NULL;
977 if (te) {
978 if (asprintf(&l2, "%s%s%s", label2,
979 label2[0] ? "/" : "", te->name) == -1) {
980 err = got_error_from_errno("asprintf");
981 goto done;
983 } else {
984 if (asprintf(&l2, "%s%s%s", label2,
985 label2[0] ? "/" : "", te2->name) == -1) {
986 err = got_error_from_errno("asprintf");
987 goto done;
991 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
992 repo, cb, cb_arg, diff_content);
993 if (err)
994 break;
997 free(l1);
998 l1 = NULL;
999 if (te1) {
1000 tidx1++;
1001 te1 = got_object_tree_get_entry(tree1, tidx1);
1002 if (te1 &&
1003 asprintf(&l1, "%s%s%s", label1,
1004 label1[0] ? "/" : "", te1->name) == -1) {
1005 err = got_error_from_errno("asprintf");
1006 goto done;
1010 free(l2);
1011 l2 = NULL;
1012 if (te2) {
1013 tidx2++;
1014 te2 = got_object_tree_get_entry(tree2, tidx2);
1015 if (te2 &&
1016 asprintf(&l2, "%s%s%s", label2,
1017 label2[0] ? "/" : "", te2->name) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 goto done;
1022 } while (te1 || te2);
1024 done:
1025 free(l1);
1026 free(l2);
1027 return err;
1030 const struct got_error *
1031 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
1032 FILE *f1, FILE *f2, int fd1, int fd2,
1033 struct got_object_id *id1, struct got_object_id *id2,
1034 const char *label1, const char *label2,
1035 enum got_diff_algorithm diff_algo, int diff_context,
1036 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1037 struct got_repository *repo, FILE *outfile)
1039 const struct got_error *err;
1040 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1042 if (id1 == NULL && id2 == NULL)
1043 return got_error(GOT_ERR_NO_OBJ);
1045 if (id1) {
1046 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1047 if (err)
1048 goto done;
1050 if (id2) {
1051 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1052 if (err)
1053 goto done;
1055 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1056 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1057 ds, outfile);
1058 done:
1059 if (blob1)
1060 got_object_blob_close(blob1);
1061 if (blob2)
1062 got_object_blob_close(blob2);
1063 return err;
1066 static const struct got_error *
1067 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1068 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1069 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1071 const struct got_error *err = NULL;
1072 struct got_pathlist_entry *pe;
1073 struct got_object_id *id1 = NULL, *id2 = NULL;
1074 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1075 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1077 TAILQ_FOREACH(pe, paths, entry) {
1078 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1079 mode_t mode1 = 0, mode2 = 0;
1081 free(id1);
1082 id1 = NULL;
1083 free(id2);
1084 id2 = NULL;
1085 if (subtree1) {
1086 got_object_tree_close(subtree1);
1087 subtree1 = NULL;
1089 if (subtree2) {
1090 got_object_tree_close(subtree2);
1091 subtree2 = NULL;
1093 if (blob1) {
1094 got_object_blob_close(blob1);
1095 blob1 = NULL;
1097 if (blob2) {
1098 got_object_blob_close(blob2);
1099 blob2 = NULL;
1102 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1103 pe->path);
1104 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1105 goto done;
1106 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1107 pe->path);
1108 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1109 goto done;
1110 if (id1 == NULL && id2 == NULL) {
1111 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1112 goto done;
1114 if (id1) {
1115 err = got_object_get_type(&type1, repo, id1);
1116 if (err)
1117 goto done;
1119 if (id2) {
1120 err = got_object_get_type(&type2, repo, id2);
1121 if (err)
1122 goto done;
1124 if (type1 == GOT_OBJ_TYPE_ANY &&
1125 type2 == GOT_OBJ_TYPE_ANY) {
1126 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1127 goto done;
1128 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1129 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1130 err = got_error(GOT_ERR_OBJ_TYPE);
1131 goto done;
1134 if (type1 == GOT_OBJ_TYPE_BLOB ||
1135 type2 == GOT_OBJ_TYPE_BLOB) {
1136 if (id1) {
1137 err = got_object_open_as_blob(&blob1, repo,
1138 id1, 8192, fd1);
1139 if (err)
1140 goto done;
1142 if (id2) {
1143 err = got_object_open_as_blob(&blob2, repo,
1144 id2, 8192, fd2);
1145 if (err)
1146 goto done;
1148 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1149 id1 ? pe->path : "/dev/null",
1150 id2 ? pe->path : "/dev/null",
1151 mode1, mode2, repo);
1152 if (err)
1153 goto done;
1154 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1155 type2 == GOT_OBJ_TYPE_TREE) {
1156 if (id1) {
1157 err = got_object_open_as_tree(&subtree1, repo,
1158 id1);
1159 if (err)
1160 goto done;
1162 if (id2) {
1163 err = got_object_open_as_tree(&subtree2, repo,
1164 id2);
1165 if (err)
1166 goto done;
1168 err = got_diff_tree(subtree1, subtree2, f1, f2,
1169 fd1, fd2,
1170 id1 ? pe->path : "/dev/null",
1171 id2 ? pe->path : "/dev/null",
1172 repo, cb, cb_arg, 1);
1173 if (err)
1174 goto done;
1175 } else {
1176 err = got_error(GOT_ERR_OBJ_TYPE);
1177 goto done;
1180 done:
1181 free(id1);
1182 free(id2);
1183 if (subtree1)
1184 got_object_tree_close(subtree1);
1185 if (subtree2)
1186 got_object_tree_close(subtree2);
1187 if (blob1)
1188 got_object_blob_close(blob1);
1189 if (blob2)
1190 got_object_blob_close(blob2);
1191 return err;
1194 static const struct got_error *
1195 show_object_id(struct got_diff_line **lines, size_t *nlines,
1196 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1198 const struct got_error *err;
1199 int n;
1200 off_t outoff = 0;
1202 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1203 if (n < 0)
1204 return got_error_from_errno("fprintf");
1206 if (lines != NULL && *lines != NULL) {
1207 if (*nlines == 0) {
1208 err = add_line_metadata(lines, nlines, 0,
1209 GOT_DIFF_LINE_META);
1210 if (err)
1211 return err;
1212 } else
1213 outoff = (*lines)[*nlines - 1].offset;
1215 outoff += n;
1216 err = add_line_metadata(lines, nlines, outoff,
1217 GOT_DIFF_LINE_META);
1218 if (err)
1219 return err;
1222 return NULL;
1225 static const struct got_error *
1226 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1227 FILE *f1, FILE *f2, int fd1, int fd2,
1228 struct got_object_id *id1, struct got_object_id *id2,
1229 struct got_pathlist_head *paths, const char *label1, const char *label2,
1230 int diff_context, int ignore_whitespace, int force_text_diff,
1231 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1232 FILE *outfile, enum got_diff_algorithm diff_algo)
1234 const struct got_error *err;
1235 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1236 struct got_diff_blob_output_unidiff_arg arg;
1237 int want_linemeta = (lines != NULL && *lines != NULL);
1239 if (id1 == NULL && id2 == NULL)
1240 return got_error(GOT_ERR_NO_OBJ);
1242 if (id1) {
1243 err = got_object_open_as_tree(&tree1, repo, id1);
1244 if (err)
1245 goto done;
1247 if (id2) {
1248 err = got_object_open_as_tree(&tree2, repo, id2);
1249 if (err)
1250 goto done;
1253 arg.diff_algo = diff_algo;
1254 arg.diff_context = diff_context;
1255 arg.ignore_whitespace = ignore_whitespace;
1256 arg.force_text_diff = force_text_diff;
1257 arg.diffstat = dsa;
1258 arg.outfile = outfile;
1259 if (want_linemeta) {
1260 arg.lines = *lines;
1261 arg.nlines = *nlines;
1262 } else {
1263 arg.lines = NULL;
1264 arg.nlines = 0;
1266 if (paths == NULL || TAILQ_EMPTY(paths))
1267 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1268 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1269 else
1270 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1271 got_diff_blob_output_unidiff, &arg);
1272 if (want_linemeta) {
1273 *lines = arg.lines; /* was likely re-allocated */
1274 *nlines = arg.nlines;
1276 done:
1277 if (tree1)
1278 got_object_tree_close(tree1);
1279 if (tree2)
1280 got_object_tree_close(tree2);
1281 return err;
1284 const struct got_error *
1285 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1286 FILE *f1, FILE *f2, int fd1, int fd2,
1287 struct got_object_id *id1, struct got_object_id *id2,
1288 struct got_pathlist_head *paths, const char *label1, const char *label2,
1289 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1290 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1291 struct got_repository *repo, FILE *outfile)
1293 const struct got_error *err;
1294 char *idstr = NULL;
1296 if (id1 == NULL && id2 == NULL)
1297 return got_error(GOT_ERR_NO_OBJ);
1299 if (id1) {
1300 err = got_object_id_str(&idstr, id1);
1301 if (err)
1302 goto done;
1303 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1304 if (err)
1305 goto done;
1306 free(idstr);
1307 idstr = NULL;
1308 } else {
1309 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1310 outfile);
1311 if (err)
1312 goto done;
1315 if (id2) {
1316 err = got_object_id_str(&idstr, id2);
1317 if (err)
1318 goto done;
1319 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1320 if (err)
1321 goto done;
1322 free(idstr);
1323 idstr = NULL;
1324 } else {
1325 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1326 outfile);
1327 if (err)
1328 goto done;
1331 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1332 paths, label1, label2, diff_context, ignore_whitespace,
1333 force_text_diff, dsa, repo, outfile, diff_algo);
1334 done:
1335 free(idstr);
1336 return err;
1339 const struct got_error *
1340 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1341 FILE *f1, FILE *f2, int fd1, int fd2,
1342 struct got_object_id *id1, struct got_object_id *id2,
1343 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1344 int diff_context, int ignore_whitespace, int force_text_diff,
1345 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1347 const struct got_error *err;
1348 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1349 char *idstr = NULL;
1351 if (id2 == NULL)
1352 return got_error(GOT_ERR_NO_OBJ);
1354 if (id1) {
1355 err = got_object_open_as_commit(&commit1, repo, id1);
1356 if (err)
1357 goto done;
1358 err = got_object_id_str(&idstr, id1);
1359 if (err)
1360 goto done;
1361 err = show_object_id(lines, nlines, "commit", '-', idstr,
1362 outfile);
1363 if (err)
1364 goto done;
1365 free(idstr);
1366 idstr = NULL;
1367 } else {
1368 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1369 outfile);
1370 if (err)
1371 goto done;
1374 err = got_object_open_as_commit(&commit2, repo, id2);
1375 if (err)
1376 goto done;
1378 err = got_object_id_str(&idstr, id2);
1379 if (err)
1380 goto done;
1381 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1382 if (err)
1383 goto done;
1385 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1386 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1387 got_object_commit_get_tree_id(commit2), paths, "", "",
1388 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1389 outfile, diff_algo);
1390 done:
1391 if (commit1)
1392 got_object_commit_close(commit1);
1393 if (commit2)
1394 got_object_commit_close(commit2);
1395 free(idstr);
1396 return err;
1399 const struct got_error *
1400 got_diff_files(struct got_diffreg_result **resultp,
1401 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1402 const char *label2, int diff_context, int ignore_whitespace,
1403 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1405 const struct got_error *err = NULL;
1406 struct got_diffreg_result *diffreg_result = NULL;
1408 if (resultp)
1409 *resultp = NULL;
1411 if (outfile) {
1412 fprintf(outfile, "file - %s\n",
1413 f1_exists ? label1 : "/dev/null");
1414 fprintf(outfile, "file + %s\n",
1415 f2_exists ? label2 : "/dev/null");
1418 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1419 ignore_whitespace, force_text_diff);
1420 if (err)
1421 goto done;
1423 if (outfile) {
1424 err = got_diffreg_output(NULL, NULL, diffreg_result,
1425 f1_exists, f2_exists, label1, label2,
1426 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1427 if (err)
1428 goto done;
1431 done:
1432 if (resultp && err == NULL)
1433 *resultp = diffreg_result;
1434 else if (diffreg_result) {
1435 const struct got_error *free_err;
1437 free_err = got_diffreg_result_free(diffreg_result);
1438 if (free_err && err == NULL)
1439 err = free_err;
1442 return err;