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("malloc");
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 return err;
120 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
121 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
122 pe->path_len, change->add, change->rm);
124 return NULL;
127 static const struct got_error *
128 diff_blobs(struct got_diff_line **lines, size_t *nlines,
129 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
130 struct got_blob_object *blob2, FILE *f1, FILE *f2,
131 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
132 int diff_context, int ignore_whitespace, int force_text_diff,
133 int show_diffstat, struct got_diffstat_cb_arg *ds, FILE *outfile,
134 enum got_diff_algorithm diff_algo)
136 const struct got_error *err = NULL, *free_err;
137 char hex1[SHA1_DIGEST_STRING_LENGTH];
138 char hex2[SHA1_DIGEST_STRING_LENGTH];
139 const char *idstr1 = NULL, *idstr2 = NULL;
140 off_t size1, size2;
141 struct got_diffreg_result *result = NULL;
142 off_t outoff = 0;
143 int n;
145 if (lines && *lines && *nlines > 0)
146 outoff = (*lines)[*nlines - 1].offset;
147 else if (lines) {
148 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
149 if (err)
150 goto done;
153 if (resultp)
154 *resultp = NULL;
156 if (f1) {
157 err = got_opentemp_truncate(f1);
158 if (err)
159 goto done;
161 if (f2) {
162 err = got_opentemp_truncate(f2);
163 if (err)
164 goto done;
167 size1 = 0;
168 if (blob1) {
169 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
170 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
171 blob1);
172 if (err)
173 goto done;
174 } else
175 idstr1 = "/dev/null";
177 size2 = 0;
178 if (blob2) {
179 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
180 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
181 blob2);
182 if (err)
183 goto done;
184 } else
185 idstr2 = "/dev/null";
187 if (outfile) {
188 char *modestr1 = NULL, *modestr2 = NULL;
189 int modebits;
190 if (mode1 && mode1 != mode2) {
191 if (S_ISLNK(mode1))
192 modebits = S_IFLNK;
193 else
194 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
195 if (asprintf(&modestr1, " (mode %o)",
196 mode1 & modebits) == -1) {
197 err = got_error_from_errno("asprintf");
198 goto done;
201 if (mode2 && mode1 != mode2) {
202 if (S_ISLNK(mode2))
203 modebits = S_IFLNK;
204 else
205 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
206 if (asprintf(&modestr2, " (mode %o)",
207 mode2 & modebits) == -1) {
208 err = got_error_from_errno("asprintf");
209 goto done;
212 n = fprintf(outfile, "blob - %s%s\n", idstr1,
213 modestr1 ? modestr1 : "");
214 if (n < 0)
215 goto done;
216 outoff += n;
217 if (lines) {
218 err = add_line_metadata(lines, nlines, outoff,
219 GOT_DIFF_LINE_BLOB_MIN);
220 if (err)
221 goto done;
224 n = fprintf(outfile, "blob + %s%s\n", idstr2,
225 modestr2 ? modestr2 : "");
226 if (n < 0)
227 goto done;
228 outoff += n;
229 if (lines) {
230 err = add_line_metadata(lines, nlines, outoff,
231 GOT_DIFF_LINE_BLOB_PLUS);
232 if (err)
233 goto done;
236 free(modestr1);
237 free(modestr2);
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 if (label1 == NULL && label2 == NULL) {
250 /* diffstat of blobs, show hash instead of path */
251 if (asprintf(&path, "%.10s -> %.10s",
252 idstr1, idstr2) == -1) {
253 err = got_error_from_errno("asprintf");
254 goto done;
256 } else {
257 path = strdup(label2 ? label2 : label1);
258 if (path == NULL) {
259 err = got_error_from_errno("malloc");
260 goto done;
264 /*
265 * Ignore 'm'ode status change: if there's no accompanying
266 * content change, there'll be no diffstat, and if there
267 * are actual changes, 'M'odified takes precedence.
268 */
269 if (blob1 == NULL)
270 status = GOT_STATUS_ADD;
271 else if (blob2 == NULL)
272 status = GOT_STATUS_DELETE;
273 else
274 status = GOT_STATUS_MODIFY;
276 err = get_diffstat(ds, path, result->result, force_text_diff,
277 status);
278 if (err) {
279 free(path);
280 goto done;
284 if (outfile) {
285 err = got_diffreg_output(lines, nlines, result,
286 blob1 != NULL, blob2 != NULL,
287 label1 ? label1 : idstr1,
288 label2 ? label2 : idstr2,
289 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
290 if (err)
291 goto done;
294 done:
295 if (resultp && err == NULL)
296 *resultp = result;
297 else if (result) {
298 free_err = got_diffreg_result_free(result);
299 if (free_err && err == NULL)
300 err = free_err;
303 return err;
306 const struct got_error *
307 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
308 struct got_blob_object *blob2, FILE *f1, FILE *f2,
309 struct got_object_id *id1, struct got_object_id *id2,
310 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
311 struct got_repository *repo)
313 struct got_diff_blob_output_unidiff_arg *a = arg;
315 return diff_blobs(&a->lines, &a->nlines, NULL,
316 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
317 a->ignore_whitespace, a->force_text_diff, a->show_diffstat,
318 a->diffstat, a->outfile, a->diff_algo);
321 const struct got_error *
322 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
323 struct got_blob_object *blob1, struct got_blob_object *blob2,
324 FILE *f1, FILE *f2, const char *label1, const char *label2,
325 enum got_diff_algorithm diff_algo, int diff_context,
326 int ignore_whitespace, int force_text_diff, int show_diffstat,
327 struct got_diffstat_cb_arg *ds, FILE *outfile)
329 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
330 label1, label2, 0, 0, diff_context, ignore_whitespace,
331 force_text_diff, show_diffstat, ds, outfile, diff_algo);
334 static const struct got_error *
335 diff_blob_file(struct got_diffreg_result **resultp,
336 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
337 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
338 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
339 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *ds,
340 FILE *outfile)
342 const struct got_error *err = NULL, *free_err;
343 char hex1[SHA1_DIGEST_STRING_LENGTH];
344 const char *idstr1 = NULL;
345 struct got_diffreg_result *result = NULL;
347 if (resultp)
348 *resultp = NULL;
350 if (blob1)
351 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
352 else
353 idstr1 = "/dev/null";
355 if (outfile) {
356 char *mode = NULL;
358 /* display file mode for new added files only */
359 if (f2_exists && blob1 == NULL) {
360 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
362 if (S_ISLNK(sb2->st_mode))
363 mmask = S_IFLNK;
364 if (asprintf(&mode, " (mode %o)",
365 sb2->st_mode & mmask) == -1)
366 return got_error_from_errno("asprintf");
368 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
369 fprintf(outfile, "file + %s%s\n",
370 f2_exists ? label2 : "/dev/null", mode ? mode : "");
371 free(mode);
374 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
375 force_text_diff);
376 if (err)
377 goto done;
379 if (outfile) {
380 err = got_diffreg_output(NULL, NULL, result,
381 blob1 != NULL, f2_exists,
382 label2, /* show local file's path, not a blob ID */
383 label2, GOT_DIFF_OUTPUT_UNIDIFF,
384 diff_context, outfile);
385 if (err)
386 goto done;
389 if (show_diffstat) {
390 char *path = NULL;
391 int status = GOT_STATUS_NO_CHANGE;
393 path = strdup(label2 ? label2 : label1);
394 if (path == NULL) {
395 err = got_error_from_errno("malloc");
396 goto done;
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 err = get_diffstat(ds, path, result->result, force_text_diff,
412 status);
413 if (err) {
414 free(path);
415 goto done;
419 done:
420 if (resultp && err == NULL)
421 *resultp = result;
422 else if (result) {
423 free_err = got_diffreg_result_free(result);
424 if (free_err && err == NULL)
425 err = free_err;
427 return err;
430 const struct got_error *
431 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
432 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
433 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
434 int ignore_whitespace, int force_text_diff, int show_diffstat,
435 struct got_diffstat_cb_arg *ds, FILE *outfile)
437 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
438 sb2, label2, diff_algo, diff_context, ignore_whitespace,
439 force_text_diff, show_diffstat, ds, outfile);
442 static const struct got_error *
443 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
444 const char *label, mode_t mode, struct got_repository *repo,
445 got_diff_blob_cb cb, void *cb_arg)
447 const struct got_error *err;
448 struct got_blob_object *blob = NULL;
449 struct got_object *obj = NULL;
451 err = got_object_open(&obj, repo, id);
452 if (err)
453 return err;
455 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
456 if (err)
457 goto done;
458 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
459 NULL, label, 0, mode, repo);
460 done:
461 got_object_close(obj);
462 if (blob)
463 got_object_blob_close(blob);
464 return err;
467 static const struct got_error *
468 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
469 FILE *f1, FILE *f2, int fd1, int fd2,
470 const char *label1, const char *label2,
471 mode_t mode1, mode_t mode2, struct got_repository *repo,
472 got_diff_blob_cb cb, void *cb_arg)
474 const struct got_error *err;
475 struct got_object *obj1 = NULL;
476 struct got_object *obj2 = NULL;
477 struct got_blob_object *blob1 = NULL;
478 struct got_blob_object *blob2 = NULL;
480 err = got_object_open(&obj1, repo, id1);
481 if (err)
482 return err;
484 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
485 err = got_error(GOT_ERR_OBJ_TYPE);
486 goto done;
489 err = got_object_open(&obj2, repo, id2);
490 if (err)
491 goto done;
492 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
493 err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 goto done;
497 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
498 if (err)
499 goto done;
501 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
502 if (err)
503 goto done;
505 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
506 mode1, mode2, repo);
507 done:
508 if (obj1)
509 got_object_close(obj1);
510 if (obj2)
511 got_object_close(obj2);
512 if (blob1)
513 got_object_blob_close(blob1);
514 if (blob2)
515 got_object_blob_close(blob2);
516 return err;
519 static const struct got_error *
520 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
521 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
522 got_diff_blob_cb cb, void *cb_arg)
524 const struct got_error *err;
525 struct got_blob_object *blob = NULL;
526 struct got_object *obj = NULL;
528 err = got_object_open(&obj, repo, id);
529 if (err)
530 return err;
532 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
533 if (err)
534 goto done;
535 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
536 mode, 0, repo);
537 done:
538 got_object_close(obj);
539 if (blob)
540 got_object_blob_close(blob);
541 return err;
544 static const struct got_error *
545 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
546 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
547 void *cb_arg, int diff_content)
549 const struct got_error *err = NULL;
550 struct got_object *treeobj = NULL;
551 struct got_tree_object *tree = NULL;
553 err = got_object_open(&treeobj, repo, id);
554 if (err)
555 goto done;
557 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
558 err = got_error(GOT_ERR_OBJ_TYPE);
559 goto done;
562 err = got_object_tree_open(&tree, repo, treeobj);
563 if (err)
564 goto done;
566 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
567 repo, cb, cb_arg, diff_content);
568 done:
569 if (tree)
570 got_object_tree_close(tree);
571 if (treeobj)
572 got_object_close(treeobj);
573 return err;
576 static const struct got_error *
577 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
578 FILE *f1, FILE *f2, int fd1, int fd2,
579 const char *label1, const char *label2,
580 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
581 int diff_content)
583 const struct got_error *err;
584 struct got_object *treeobj1 = NULL;
585 struct got_object *treeobj2 = NULL;
586 struct got_tree_object *tree1 = NULL;
587 struct got_tree_object *tree2 = NULL;
589 err = got_object_open(&treeobj1, repo, id1);
590 if (err)
591 goto done;
593 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
594 err = got_error(GOT_ERR_OBJ_TYPE);
595 goto done;
598 err = got_object_open(&treeobj2, repo, id2);
599 if (err)
600 goto done;
602 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
603 err = got_error(GOT_ERR_OBJ_TYPE);
604 goto done;
607 err = got_object_tree_open(&tree1, repo, treeobj1);
608 if (err)
609 goto done;
611 err = got_object_tree_open(&tree2, repo, treeobj2);
612 if (err)
613 goto done;
615 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
616 label1, label2, repo, cb, cb_arg, diff_content);
618 done:
619 if (tree1)
620 got_object_tree_close(tree1);
621 if (tree2)
622 got_object_tree_close(tree2);
623 if (treeobj1)
624 got_object_close(treeobj1);
625 if (treeobj2)
626 got_object_close(treeobj2);
627 return err;
630 static const struct got_error *
631 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
632 FILE *f2, const char *label, struct got_repository *repo,
633 got_diff_blob_cb cb, void *cb_arg, int diff_content)
635 const struct got_error *err;
636 struct got_object *treeobj = NULL;
637 struct got_tree_object *tree = NULL;
639 err = got_object_open(&treeobj, repo, id);
640 if (err)
641 goto done;
643 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
644 err = got_error(GOT_ERR_OBJ_TYPE);
645 goto done;
648 err = got_object_tree_open(&tree, repo, treeobj);
649 if (err)
650 goto done;
652 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
653 repo, cb, cb_arg, diff_content);
654 done:
655 if (tree)
656 got_object_tree_close(tree);
657 if (treeobj)
658 got_object_close(treeobj);
659 return err;
662 static const struct got_error *
663 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
664 const char *label1, const char *label2, struct got_repository *repo,
665 got_diff_blob_cb cb, void *cb_arg)
667 /* XXX TODO */
668 return NULL;
671 static const struct got_error *
672 diff_entry_old_new(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,
675 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
676 int diff_content)
678 const struct got_error *err = NULL;
679 int id_match;
681 if (got_object_tree_entry_is_submodule(te1))
682 return NULL;
684 if (te2 == NULL) {
685 if (S_ISDIR(te1->mode))
686 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
687 label1, repo, cb, cb_arg, diff_content);
688 else {
689 if (diff_content)
690 err = diff_deleted_blob(&te1->id, f1, fd1,
691 f2, label1, te1->mode, repo, cb, cb_arg);
692 else
693 err = cb(cb_arg, NULL, NULL, NULL, NULL,
694 &te1->id, NULL, label1, NULL,
695 te1->mode, 0, repo);
697 return err;
698 } else if (got_object_tree_entry_is_submodule(te2))
699 return NULL;
701 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
702 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
703 if (!id_match)
704 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
705 fd1, fd2, label1, label2, repo, cb, cb_arg,
706 diff_content);
707 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
708 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
709 if (!id_match ||
710 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
711 (te2->mode & (S_IFLNK | S_IXUSR))) {
712 if (diff_content)
713 return diff_modified_blob(&te1->id, &te2->id,
714 f1, f2, fd1, fd2, label1, label2,
715 te1->mode, te2->mode, repo, cb, cb_arg);
716 else
717 return cb(cb_arg, NULL, NULL, NULL, NULL,
718 &te1->id, &te2->id, label1, label2,
719 te1->mode, te2->mode, repo);
723 if (id_match)
724 return NULL;
726 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
727 cb, cb_arg);
730 static const struct got_error *
731 diff_entry_new_old(struct got_tree_entry *te2,
732 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
733 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
734 int diff_content)
736 if (te1 != NULL) /* handled by diff_entry_old_new() */
737 return NULL;
739 if (got_object_tree_entry_is_submodule(te2))
740 return NULL;
742 if (S_ISDIR(te2->mode))
743 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
744 repo, cb, cb_arg, diff_content);
746 if (diff_content)
747 return diff_added_blob(&te2->id, f1, f2, fd2,
748 label2, te2->mode, repo, cb, cb_arg);
750 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
751 NULL, label2, 0, te2->mode, repo);
754 const struct got_error *
755 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
756 struct got_blob_object *blob2, FILE *f1, FILE *f2,
757 struct got_object_id *id1, struct got_object_id *id2,
758 const char *label1, const char *label2,
759 mode_t mode1, mode_t mode2, struct got_repository *repo)
761 const struct got_error *err = NULL;
762 struct got_diffreg_result *result = NULL;
763 struct got_diffstat_cb_arg *a = arg;
764 char *path = NULL;
765 int status = GOT_STATUS_NO_CHANGE;
767 path = strdup(label2 ? label2 : label1);
768 if (path == NULL)
769 return got_error_from_errno("malloc");
771 if (id1 == NULL)
772 status = GOT_STATUS_ADD;
773 else if (id2 == NULL)
774 status = GOT_STATUS_DELETE;
775 else {
776 if (got_object_id_cmp(id1, id2) != 0)
777 status = GOT_STATUS_MODIFY;
778 else if (mode1 != mode2)
779 status = GOT_STATUS_MODE_CHANGE;
782 if (f1) {
783 err = got_opentemp_truncate(f1);
784 if (err)
785 goto done;
787 if (f2) {
788 err = got_opentemp_truncate(f2);
789 if (err)
790 goto done;
793 if (blob1) {
794 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
795 blob1);
796 if (err)
797 goto done;
799 if (blob2) {
800 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
801 blob2);
802 if (err)
803 goto done;
806 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
807 a->force_text);
808 if (err)
809 goto done;
811 err = get_diffstat(a, path, result->result, a->force_text, status);
813 done:
814 if (result) {
815 const struct got_error *free_err;
817 free_err = got_diffreg_result_free(result);
818 if (free_err && err == NULL)
819 err = free_err;
821 if (err)
822 free(path);
823 return err;
826 const struct got_error *
827 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
828 struct got_blob_object *blob2, FILE *f1, FILE *f2,
829 struct got_object_id *id1, struct got_object_id *id2,
830 const char *label1, const char *label2,
831 mode_t mode1, mode_t mode2, struct got_repository *repo)
833 const struct got_error *err = NULL;
834 struct got_pathlist_head *paths = arg;
835 struct got_diff_changed_path *change = NULL;
836 char *path = NULL;
838 path = strdup(label2 ? label2 : label1);
839 if (path == NULL)
840 return got_error_from_errno("malloc");
842 change = malloc(sizeof(*change));
843 if (change == NULL) {
844 err = got_error_from_errno("malloc");
845 goto done;
848 change->status = GOT_STATUS_NO_CHANGE;
849 if (id1 == NULL)
850 change->status = GOT_STATUS_ADD;
851 else if (id2 == NULL)
852 change->status = GOT_STATUS_DELETE;
853 else {
854 if (got_object_id_cmp(id1, id2) != 0)
855 change->status = GOT_STATUS_MODIFY;
856 else if (mode1 != mode2)
857 change->status = GOT_STATUS_MODE_CHANGE;
860 err = got_pathlist_append(paths, path, change);
861 done:
862 if (err) {
863 free(path);
864 free(change);
866 return err;
869 const struct got_error *
870 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
871 FILE *f1, FILE *f2, int fd1, int fd2,
872 const char *label1, const char *label2,
873 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
874 int diff_content)
876 const struct got_error *err = NULL;
877 struct got_tree_entry *te1 = NULL;
878 struct got_tree_entry *te2 = NULL;
879 char *l1 = NULL, *l2 = NULL;
880 int tidx1 = 0, tidx2 = 0;
882 if (tree1) {
883 te1 = got_object_tree_get_entry(tree1, 0);
884 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
885 te1->name) == -1)
886 return got_error_from_errno("asprintf");
888 if (tree2) {
889 te2 = got_object_tree_get_entry(tree2, 0);
890 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
891 te2->name) == -1)
892 return got_error_from_errno("asprintf");
895 do {
896 if (te1) {
897 struct got_tree_entry *te = NULL;
898 if (tree2)
899 te = got_object_tree_find_entry(tree2,
900 te1->name);
901 if (te) {
902 free(l2);
903 l2 = NULL;
904 if (te && asprintf(&l2, "%s%s%s", label2,
905 label2[0] ? "/" : "", te->name) == -1)
906 return
907 got_error_from_errno("asprintf");
909 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
910 l1, l2, repo, cb, cb_arg, diff_content);
911 if (err)
912 break;
915 if (te2) {
916 struct got_tree_entry *te = NULL;
917 if (tree1)
918 te = got_object_tree_find_entry(tree1,
919 te2->name);
920 free(l2);
921 if (te) {
922 if (asprintf(&l2, "%s%s%s", label2,
923 label2[0] ? "/" : "", te->name) == -1)
924 return
925 got_error_from_errno("asprintf");
926 } else {
927 if (asprintf(&l2, "%s%s%s", label2,
928 label2[0] ? "/" : "", te2->name) == -1)
929 return
930 got_error_from_errno("asprintf");
932 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
933 repo, cb, cb_arg, diff_content);
934 if (err)
935 break;
938 free(l1);
939 l1 = NULL;
940 if (te1) {
941 tidx1++;
942 te1 = got_object_tree_get_entry(tree1, tidx1);
943 if (te1 &&
944 asprintf(&l1, "%s%s%s", label1,
945 label1[0] ? "/" : "", te1->name) == -1)
946 return got_error_from_errno("asprintf");
948 free(l2);
949 l2 = NULL;
950 if (te2) {
951 tidx2++;
952 te2 = got_object_tree_get_entry(tree2, tidx2);
953 if (te2 &&
954 asprintf(&l2, "%s%s%s", label2,
955 label2[0] ? "/" : "", te2->name) == -1)
956 return got_error_from_errno("asprintf");
958 } while (te1 || te2);
960 return err;
963 const struct got_error *
964 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
965 FILE *f1, FILE *f2, int fd1, int fd2,
966 struct got_object_id *id1, struct got_object_id *id2,
967 const char *label1, const char *label2,
968 enum got_diff_algorithm diff_algo, int diff_context,
969 int ignore_whitespace, int force_text_diff, int show_diffstat,
970 struct got_diffstat_cb_arg *ds, struct got_repository *repo, FILE *outfile)
972 const struct got_error *err;
973 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
975 if (id1 == NULL && id2 == NULL)
976 return got_error(GOT_ERR_NO_OBJ);
978 if (id1) {
979 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
980 if (err)
981 goto done;
983 if (id2) {
984 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
985 if (err)
986 goto done;
988 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
989 diff_algo, diff_context, ignore_whitespace, force_text_diff,
990 show_diffstat, ds, outfile);
991 done:
992 if (blob1)
993 got_object_blob_close(blob1);
994 if (blob2)
995 got_object_blob_close(blob2);
996 return err;
999 static const struct got_error *
1000 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1001 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1002 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1004 const struct got_error *err = NULL;
1005 struct got_pathlist_entry *pe;
1006 struct got_object_id *id1 = NULL, *id2 = NULL;
1007 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1008 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1010 TAILQ_FOREACH(pe, paths, entry) {
1011 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1012 mode_t mode1 = 0, mode2 = 0;
1014 free(id1);
1015 id1 = NULL;
1016 free(id2);
1017 id2 = NULL;
1018 if (subtree1) {
1019 got_object_tree_close(subtree1);
1020 subtree1 = NULL;
1022 if (subtree2) {
1023 got_object_tree_close(subtree2);
1024 subtree2 = NULL;
1026 if (blob1) {
1027 got_object_blob_close(blob1);
1028 blob1 = NULL;
1030 if (blob2) {
1031 got_object_blob_close(blob2);
1032 blob2 = NULL;
1035 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1036 pe->path);
1037 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1038 goto done;
1039 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1040 pe->path);
1041 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1042 goto done;
1043 if (id1 == NULL && id2 == NULL) {
1044 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1045 goto done;
1047 if (id1) {
1048 err = got_object_get_type(&type1, repo, id1);
1049 if (err)
1050 goto done;
1052 if (id2) {
1053 err = got_object_get_type(&type2, repo, id2);
1054 if (err)
1055 goto done;
1057 if (type1 == GOT_OBJ_TYPE_ANY &&
1058 type2 == GOT_OBJ_TYPE_ANY) {
1059 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1060 goto done;
1061 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1062 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1063 err = got_error(GOT_ERR_OBJ_TYPE);
1064 goto done;
1067 if (type1 == GOT_OBJ_TYPE_BLOB ||
1068 type2 == GOT_OBJ_TYPE_BLOB) {
1069 if (id1) {
1070 err = got_object_open_as_blob(&blob1, repo,
1071 id1, 8192, fd1);
1072 if (err)
1073 goto done;
1075 if (id2) {
1076 err = got_object_open_as_blob(&blob2, repo,
1077 id2, 8192, fd2);
1078 if (err)
1079 goto done;
1081 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1082 id1 ? pe->path : "/dev/null",
1083 id2 ? pe->path : "/dev/null",
1084 mode1, mode2, repo);
1085 if (err)
1086 goto done;
1087 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1088 type2 == GOT_OBJ_TYPE_TREE) {
1089 if (id1) {
1090 err = got_object_open_as_tree(&subtree1, repo,
1091 id1);
1092 if (err)
1093 goto done;
1095 if (id2) {
1096 err = got_object_open_as_tree(&subtree2, repo,
1097 id2);
1098 if (err)
1099 goto done;
1101 err = got_diff_tree(subtree1, subtree2, f1, f2,
1102 fd1, fd2,
1103 id1 ? pe->path : "/dev/null",
1104 id2 ? pe->path : "/dev/null",
1105 repo, cb, cb_arg, 1);
1106 if (err)
1107 goto done;
1108 } else {
1109 err = got_error(GOT_ERR_OBJ_TYPE);
1110 goto done;
1113 done:
1114 free(id1);
1115 free(id2);
1116 if (subtree1)
1117 got_object_tree_close(subtree1);
1118 if (subtree2)
1119 got_object_tree_close(subtree2);
1120 if (blob1)
1121 got_object_blob_close(blob1);
1122 if (blob2)
1123 got_object_blob_close(blob2);
1124 return err;
1127 static const struct got_error *
1128 show_object_id(struct got_diff_line **lines, size_t *nlines,
1129 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1131 const struct got_error *err;
1132 int n;
1133 off_t outoff = 0;
1135 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1136 if (n < 0)
1137 return got_error_from_errno("fprintf");
1139 if (lines != NULL && *lines != NULL) {
1140 if (*nlines == 0) {
1141 err = add_line_metadata(lines, nlines, 0,
1142 GOT_DIFF_LINE_META);
1143 if (err)
1144 return err;
1145 } else
1146 outoff = (*lines)[*nlines - 1].offset;
1148 outoff += n;
1149 err = add_line_metadata(lines, nlines, outoff,
1150 GOT_DIFF_LINE_META);
1151 if (err)
1152 return err;
1155 return NULL;
1158 static const struct got_error *
1159 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1160 FILE *f1, FILE *f2, int fd1, int fd2,
1161 struct got_object_id *id1, struct got_object_id *id2,
1162 struct got_pathlist_head *paths, const char *label1, const char *label2,
1163 int diff_context, int ignore_whitespace, int force_text_diff,
1164 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1165 struct got_repository *repo, FILE *outfile,
1166 enum got_diff_algorithm diff_algo)
1168 const struct got_error *err;
1169 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1170 struct got_diff_blob_output_unidiff_arg arg;
1171 int want_linemeta = (lines != NULL && *lines != NULL);
1173 if (id1 == NULL && id2 == NULL)
1174 return got_error(GOT_ERR_NO_OBJ);
1176 if (id1) {
1177 err = got_object_open_as_tree(&tree1, repo, id1);
1178 if (err)
1179 goto done;
1181 if (id2) {
1182 err = got_object_open_as_tree(&tree2, repo, id2);
1183 if (err)
1184 goto done;
1187 arg.diff_algo = diff_algo;
1188 arg.diff_context = diff_context;
1189 arg.ignore_whitespace = ignore_whitespace;
1190 arg.force_text_diff = force_text_diff;
1191 arg.show_diffstat = show_diffstat;
1192 arg.diffstat = dsa;
1193 arg.outfile = outfile;
1194 if (want_linemeta) {
1195 arg.lines = *lines;
1196 arg.nlines = *nlines;
1197 } else {
1198 arg.lines = NULL;
1199 arg.nlines = 0;
1201 if (paths == NULL || TAILQ_EMPTY(paths))
1202 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1203 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1204 else
1205 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1206 got_diff_blob_output_unidiff, &arg);
1207 if (want_linemeta) {
1208 *lines = arg.lines; /* was likely re-allocated */
1209 *nlines = arg.nlines;
1211 done:
1212 if (tree1)
1213 got_object_tree_close(tree1);
1214 if (tree2)
1215 got_object_tree_close(tree2);
1216 return err;
1219 const struct got_error *
1220 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1221 FILE *f1, FILE *f2, int fd1, int fd2,
1222 struct got_object_id *id1, struct got_object_id *id2,
1223 struct got_pathlist_head *paths, const char *label1, const char *label2,
1224 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1225 int force_text_diff, int show_diffstat, struct got_diffstat_cb_arg *dsa,
1226 struct got_repository *repo, FILE *outfile)
1228 const struct got_error *err;
1229 char *idstr = NULL;
1231 if (id1 == NULL && id2 == NULL)
1232 return got_error(GOT_ERR_NO_OBJ);
1234 if (id1) {
1235 err = got_object_id_str(&idstr, id1);
1236 if (err)
1237 goto done;
1238 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1239 if (err)
1240 goto done;
1241 free(idstr);
1242 idstr = NULL;
1243 } else {
1244 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1245 outfile);
1246 if (err)
1247 goto done;
1250 if (id2) {
1251 err = got_object_id_str(&idstr, id2);
1252 if (err)
1253 goto done;
1254 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1255 if (err)
1256 goto done;
1257 free(idstr);
1258 idstr = NULL;
1259 } else {
1260 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1261 outfile);
1262 if (err)
1263 goto done;
1266 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1267 paths, label1, label2, diff_context, ignore_whitespace,
1268 force_text_diff, show_diffstat, dsa, repo, outfile, diff_algo);
1269 done:
1270 free(idstr);
1271 return err;
1274 const struct got_error *
1275 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1276 FILE *f1, FILE *f2, int fd1, int fd2,
1277 struct got_object_id *id1, struct got_object_id *id2,
1278 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1279 int diff_context, int ignore_whitespace, int force_text_diff,
1280 int show_diffstat, struct got_diffstat_cb_arg *dsa,
1281 struct got_repository *repo, FILE *outfile)
1283 const struct got_error *err;
1284 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1285 char *idstr = NULL;
1287 if (id2 == NULL)
1288 return got_error(GOT_ERR_NO_OBJ);
1290 if (id1) {
1291 err = got_object_open_as_commit(&commit1, repo, id1);
1292 if (err)
1293 goto done;
1294 err = got_object_id_str(&idstr, id1);
1295 if (err)
1296 goto done;
1297 err = show_object_id(lines, nlines, "commit", '-', idstr,
1298 outfile);
1299 if (err)
1300 goto done;
1301 free(idstr);
1302 idstr = NULL;
1303 } else {
1304 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1305 outfile);
1306 if (err)
1307 goto done;
1310 err = got_object_open_as_commit(&commit2, repo, id2);
1311 if (err)
1312 goto done;
1314 err = got_object_id_str(&idstr, id2);
1315 if (err)
1316 goto done;
1317 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1318 if (err)
1319 goto done;
1321 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1322 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1323 got_object_commit_get_tree_id(commit2), paths, "", "",
1324 diff_context, ignore_whitespace, force_text_diff, show_diffstat,
1325 dsa, repo, outfile, diff_algo);
1326 done:
1327 if (commit1)
1328 got_object_commit_close(commit1);
1329 if (commit2)
1330 got_object_commit_close(commit2);
1331 free(idstr);
1332 return err;
1335 const struct got_error *
1336 got_diff_files(struct got_diffreg_result **resultp,
1337 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1338 const char *label2, int diff_context, int ignore_whitespace,
1339 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1341 const struct got_error *err = NULL;
1342 struct got_diffreg_result *diffreg_result = NULL;
1344 if (resultp)
1345 *resultp = NULL;
1347 if (outfile) {
1348 fprintf(outfile, "file - %s\n",
1349 f1_exists ? label1 : "/dev/null");
1350 fprintf(outfile, "file + %s\n",
1351 f2_exists ? label2 : "/dev/null");
1354 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1355 ignore_whitespace, force_text_diff);
1356 if (err)
1357 goto done;
1359 if (outfile) {
1360 err = got_diffreg_output(NULL, NULL, diffreg_result,
1361 f1_exists, f2_exists, label1, label2,
1362 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1363 if (err)
1364 goto done;
1367 done:
1368 if (resultp && err == NULL)
1369 *resultp = diffreg_result;
1370 else if (diffreg_result) {
1371 const struct got_error *free_err;
1372 free_err = got_diffreg_result_free(diffreg_result);
1373 if (free_err && err == NULL)
1374 err = free_err;
1377 return err;