Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <zlib.h>
27 #include "got_object.h"
28 #include "got_repository.h"
29 #include "got_error.h"
30 #include "got_diff.h"
31 #include "got_path.h"
32 #include "got_cancel.h"
33 #include "got_worktree.h"
34 #include "got_opentemp.h"
36 #include "got_lib_diff.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
41 #ifndef MAX
42 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
43 #endif
45 static const struct got_error *
46 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
47 off_t off, uint8_t type)
48 {
49 struct got_diff_line *p;
51 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
52 if (p == NULL)
53 return got_error_from_errno("reallocarray");
54 *lines = p;
55 (*lines)[*nlines].offset = off;
56 (*lines)[*nlines].type = type;
57 (*nlines)++;
59 return NULL;
60 }
62 static void
63 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
64 uint32_t add, uint32_t rm)
65 {
66 int d1 = 1, d2 = 1;
68 if (maxlen)
69 *maxlen = MAX(*maxlen, len);
71 while (add /= 10)
72 ++d1;
73 *add_cols = MAX(*add_cols, d1);
75 while (rm /= 10)
76 ++d2;
77 *rm_cols = MAX(*rm_cols, d2);
78 }
80 static const struct got_error *
81 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
82 struct diff_result *r, int force_text, int status)
83 {
84 const struct got_error *err;
85 struct got_pathlist_entry *pe;
86 struct got_diff_changed_path *change = NULL;
87 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
88 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
89 int i;
91 change = calloc(1, sizeof(*change));
92 if (change == NULL)
93 return got_error_from_errno("calloc");
95 if (!isbin || force_text) {
96 for (i = 0; i < r->chunks.len; ++i) {
97 struct diff_chunk *c;
98 int clc, crc;
100 c = diff_chunk_get(r, i);
101 clc = diff_chunk_get_left_count(c);
102 crc = diff_chunk_get_right_count(c);
104 if (crc && !clc)
105 change->add += crc;
106 if (clc && !crc)
107 change->rm += clc;
111 change->status = status;
112 ds->ins += change->add;
113 ds->del += change->rm;
114 ++ds->nfiles;
116 err = got_pathlist_append(ds->paths, path, change);
117 if (err) {
118 free(change);
119 return err;
122 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
123 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
124 pe->path_len, change->add, change->rm);
126 return NULL;
129 static const struct got_error *
130 diff_blobs(struct got_diff_line **lines, size_t *nlines,
131 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
132 struct got_blob_object *blob2, FILE *f1, FILE *f2,
133 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
134 int diff_context, int ignore_whitespace, int force_text_diff,
135 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
136 enum got_diff_algorithm diff_algo)
138 const struct got_error *err = NULL, *free_err;
139 char hex1[SHA1_DIGEST_STRING_LENGTH];
140 char hex2[SHA1_DIGEST_STRING_LENGTH];
141 const char *idstr1 = NULL, *idstr2 = NULL;
142 char *modestr1 = NULL, *modestr2 = NULL;
143 off_t size1, size2;
144 struct got_diffreg_result *result = NULL;
145 off_t outoff = 0;
146 int n;
148 if (lines && *lines && *nlines > 0)
149 outoff = (*lines)[*nlines - 1].offset;
150 else if (lines) {
151 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
152 if (err)
153 goto done;
156 if (resultp)
157 *resultp = NULL;
159 if (f1) {
160 err = got_opentemp_truncate(f1);
161 if (err)
162 goto done;
164 if (f2) {
165 err = got_opentemp_truncate(f2);
166 if (err)
167 goto done;
170 size1 = 0;
171 if (blob1) {
172 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
173 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
174 blob1);
175 if (err)
176 goto done;
177 } else
178 idstr1 = "/dev/null";
180 size2 = 0;
181 if (blob2) {
182 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
183 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
184 blob2);
185 if (err)
186 goto done;
187 } else
188 idstr2 = "/dev/null";
190 if (outfile) {
191 int modebits;
193 if (mode1 && mode1 != mode2) {
194 if (S_ISLNK(mode1))
195 modebits = S_IFLNK;
196 else
197 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
198 if (asprintf(&modestr1, " (mode %o)",
199 mode1 & modebits) == -1) {
200 err = got_error_from_errno("asprintf");
201 goto done;
204 if (mode2 && mode1 != mode2) {
205 if (S_ISLNK(mode2))
206 modebits = S_IFLNK;
207 else
208 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
209 if (asprintf(&modestr2, " (mode %o)",
210 mode2 & modebits) == -1) {
211 err = got_error_from_errno("asprintf");
212 goto done;
215 n = fprintf(outfile, "blob - %s%s\n", idstr1,
216 modestr1 ? modestr1 : "");
217 if (n < 0)
218 goto done;
219 outoff += n;
220 if (lines) {
221 err = add_line_metadata(lines, nlines, outoff,
222 GOT_DIFF_LINE_BLOB_MIN);
223 if (err)
224 goto done;
227 n = fprintf(outfile, "blob + %s%s\n", idstr2,
228 modestr2 ? modestr2 : "");
229 if (n < 0)
230 goto done;
231 outoff += n;
232 if (lines) {
233 err = add_line_metadata(lines, nlines, outoff,
234 GOT_DIFF_LINE_BLOB_PLUS);
235 if (err)
236 goto done;
240 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
241 force_text_diff);
242 if (err)
243 goto done;
245 if (diffstat) {
246 char *path = NULL;
247 int status = GOT_STATUS_NO_CHANGE;
249 if (blob1 == NULL)
250 status = GOT_STATUS_ADD;
251 else if (blob2 == NULL)
252 status = GOT_STATUS_DELETE;
253 else {
254 if (strcmp(idstr1, idstr2) != 0)
255 status = GOT_STATUS_MODIFY;
256 else if (mode1 != mode2)
257 status = GOT_STATUS_MODE_CHANGE;
260 if (label1 == NULL && label2 == NULL) {
261 /* diffstat of blobs, show hash instead of path */
262 if (asprintf(&path, "%.10s -> %.10s",
263 idstr1, idstr2) == -1) {
264 err = got_error_from_errno("asprintf");
265 goto done;
267 } else {
268 if (label2 != NULL &&
269 (status != GOT_STATUS_DELETE || label1 == NULL))
270 path = strdup(label2);
271 else
272 path = strdup(label1);
273 if (path == NULL) {
274 err = got_error_from_errno("strdup");
275 goto done;
279 err = get_diffstat(diffstat, path, result->result,
280 force_text_diff, status);
281 if (err) {
282 free(path);
283 goto done;
287 if (outfile) {
288 err = got_diffreg_output(lines, nlines, result,
289 blob1 != NULL, blob2 != NULL,
290 label1 ? label1 : idstr1,
291 label2 ? label2 : idstr2,
292 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
293 if (err)
294 goto done;
297 done:
298 free(modestr1);
299 free(modestr2);
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->diffstat, a->outfile,
323 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,
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, 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, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
346 const struct got_error *err = NULL, *free_err;
347 char hex1[SHA1_DIGEST_STRING_LENGTH];
348 const char *idstr1 = NULL;
349 struct got_diffreg_result *result = NULL;
351 if (resultp)
352 *resultp = NULL;
354 if (blob1)
355 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
356 else
357 idstr1 = "/dev/null";
359 if (outfile) {
360 char *mode = NULL;
362 /* display file mode for new added files only */
363 if (f2_exists && blob1 == NULL) {
364 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
366 if (S_ISLNK(sb2->st_mode))
367 mmask = S_IFLNK;
368 if (asprintf(&mode, " (mode %o)",
369 sb2->st_mode & mmask) == -1)
370 return got_error_from_errno("asprintf");
372 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
373 fprintf(outfile, "file + %s%s\n",
374 f2_exists ? label2 : "/dev/null", mode ? mode : "");
375 free(mode);
378 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
379 force_text_diff);
380 if (err)
381 goto done;
383 if (outfile) {
384 err = got_diffreg_output(NULL, NULL, result,
385 blob1 != NULL, f2_exists,
386 label2, /* show local file's path, not a blob ID */
387 label2, GOT_DIFF_OUTPUT_UNIDIFF,
388 diff_context, outfile);
389 if (err)
390 goto done;
393 if (diffstat) {
394 char *path = NULL;
395 int status = GOT_STATUS_NO_CHANGE;
397 /*
398 * Ignore 'm'ode status change: if there's no accompanying
399 * content change, there'll be no diffstat, and if there
400 * are actual changes, 'M'odified takes precedence.
401 */
402 if (blob1 == NULL)
403 status = GOT_STATUS_ADD;
404 else if (!f2_exists)
405 status = GOT_STATUS_DELETE;
406 else
407 status = GOT_STATUS_MODIFY;
409 if (label2 != NULL &&
410 (status != GOT_STATUS_DELETE || label1 == NULL))
411 path = strdup(label2);
412 else
413 path = strdup(label1);
414 if (path == NULL) {
415 err = got_error_from_errno("strdup");
416 goto done;
419 err = get_diffstat(diffstat, path, result->result,
420 force_text_diff, status);
421 if (err) {
422 free(path);
423 goto done;
427 done:
428 if (resultp && err == NULL)
429 *resultp = result;
430 else if (result) {
431 free_err = got_diffreg_result_free(result);
432 if (free_err && err == NULL)
433 err = free_err;
435 return err;
438 const struct got_error *
439 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
440 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
441 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
442 int ignore_whitespace, int force_text_diff,
443 struct got_diffstat_cb_arg *ds, FILE *outfile)
445 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
446 sb2, label2, diff_algo, diff_context, ignore_whitespace,
447 force_text_diff, ds, outfile);
450 static const struct got_error *
451 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
452 const char *label, mode_t mode, struct got_repository *repo,
453 got_diff_blob_cb cb, void *cb_arg)
455 const struct got_error *err;
456 struct got_blob_object *blob = NULL;
457 struct got_object *obj = NULL;
459 err = got_object_open(&obj, repo, id);
460 if (err)
461 return err;
463 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
464 if (err)
465 goto done;
466 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
467 NULL, label, 0, mode, repo);
468 done:
469 got_object_close(obj);
470 if (blob)
471 got_object_blob_close(blob);
472 return err;
475 static const struct got_error *
476 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
477 FILE *f1, FILE *f2, int fd1, int fd2,
478 const char *label1, const char *label2,
479 mode_t mode1, mode_t mode2, struct got_repository *repo,
480 got_diff_blob_cb cb, void *cb_arg)
482 const struct got_error *err;
483 struct got_object *obj1 = NULL;
484 struct got_object *obj2 = NULL;
485 struct got_blob_object *blob1 = NULL;
486 struct got_blob_object *blob2 = NULL;
488 err = got_object_open(&obj1, repo, id1);
489 if (err)
490 return err;
492 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
493 err = got_error(GOT_ERR_OBJ_TYPE);
494 goto done;
497 err = got_object_open(&obj2, repo, id2);
498 if (err)
499 goto done;
500 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
501 err = got_error(GOT_ERR_BAD_OBJ_DATA);
502 goto done;
505 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
506 if (err)
507 goto done;
509 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
510 if (err)
511 goto done;
513 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
514 mode1, mode2, repo);
515 done:
516 if (obj1)
517 got_object_close(obj1);
518 if (obj2)
519 got_object_close(obj2);
520 if (blob1)
521 got_object_blob_close(blob1);
522 if (blob2)
523 got_object_blob_close(blob2);
524 return err;
527 static const struct got_error *
528 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
529 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
530 got_diff_blob_cb cb, void *cb_arg)
532 const struct got_error *err;
533 struct got_blob_object *blob = NULL;
534 struct got_object *obj = NULL;
536 err = got_object_open(&obj, repo, id);
537 if (err)
538 return err;
540 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
541 if (err)
542 goto done;
543 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
544 mode, 0, repo);
545 done:
546 got_object_close(obj);
547 if (blob)
548 got_object_blob_close(blob);
549 return err;
552 static const struct got_error *
553 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
554 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
555 void *cb_arg, int diff_content)
557 const struct got_error *err = NULL;
558 struct got_object *treeobj = NULL;
559 struct got_tree_object *tree = NULL;
561 err = got_object_open(&treeobj, repo, id);
562 if (err)
563 goto done;
565 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
566 err = got_error(GOT_ERR_OBJ_TYPE);
567 goto done;
570 err = got_object_tree_open(&tree, repo, treeobj);
571 if (err)
572 goto done;
574 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
575 repo, cb, cb_arg, diff_content);
576 done:
577 if (tree)
578 got_object_tree_close(tree);
579 if (treeobj)
580 got_object_close(treeobj);
581 return err;
584 static const struct got_error *
585 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
586 FILE *f1, FILE *f2, int fd1, int fd2,
587 const char *label1, const char *label2,
588 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
589 int diff_content)
591 const struct got_error *err;
592 struct got_object *treeobj1 = NULL;
593 struct got_object *treeobj2 = NULL;
594 struct got_tree_object *tree1 = NULL;
595 struct got_tree_object *tree2 = NULL;
597 err = got_object_open(&treeobj1, repo, id1);
598 if (err)
599 goto done;
601 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
602 err = got_error(GOT_ERR_OBJ_TYPE);
603 goto done;
606 err = got_object_open(&treeobj2, repo, id2);
607 if (err)
608 goto done;
610 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
611 err = got_error(GOT_ERR_OBJ_TYPE);
612 goto done;
615 err = got_object_tree_open(&tree1, repo, treeobj1);
616 if (err)
617 goto done;
619 err = got_object_tree_open(&tree2, repo, treeobj2);
620 if (err)
621 goto done;
623 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
624 label1, label2, repo, cb, cb_arg, diff_content);
626 done:
627 if (tree1)
628 got_object_tree_close(tree1);
629 if (tree2)
630 got_object_tree_close(tree2);
631 if (treeobj1)
632 got_object_close(treeobj1);
633 if (treeobj2)
634 got_object_close(treeobj2);
635 return err;
638 static const struct got_error *
639 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
640 FILE *f2, const char *label, struct got_repository *repo,
641 got_diff_blob_cb cb, void *cb_arg, int diff_content)
643 const struct got_error *err;
644 struct got_object *treeobj = NULL;
645 struct got_tree_object *tree = NULL;
647 err = got_object_open(&treeobj, repo, id);
648 if (err)
649 goto done;
651 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
652 err = got_error(GOT_ERR_OBJ_TYPE);
653 goto done;
656 err = got_object_tree_open(&tree, repo, treeobj);
657 if (err)
658 goto done;
660 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
661 repo, cb, cb_arg, diff_content);
662 done:
663 if (tree)
664 got_object_tree_close(tree);
665 if (treeobj)
666 got_object_close(treeobj);
667 return err;
670 static const struct got_error *
671 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
672 const char *label1, const char *label2, struct got_repository *repo,
673 got_diff_blob_cb cb, void *cb_arg)
675 /* XXX TODO */
676 return NULL;
679 static const struct got_error *
680 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
681 FILE *f1, FILE *f2, int fd1, int fd2,
682 const char *label1, const char *label2,
683 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
684 int diff_content)
686 const struct got_error *err = NULL;
687 int id_match;
689 if (got_object_tree_entry_is_submodule(te1))
690 return NULL;
692 if (te2 == NULL) {
693 if (S_ISDIR(te1->mode))
694 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
695 label1, repo, cb, cb_arg, diff_content);
696 else {
697 if (diff_content)
698 err = diff_deleted_blob(&te1->id, f1, fd1,
699 f2, label1, te1->mode, repo, cb, cb_arg);
700 else
701 err = cb(cb_arg, NULL, NULL, NULL, NULL,
702 &te1->id, NULL, label1, NULL,
703 te1->mode, 0, repo);
705 return err;
706 } else if (got_object_tree_entry_is_submodule(te2))
707 return NULL;
709 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
710 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
711 if (!id_match)
712 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
713 fd1, fd2, label1, label2, repo, cb, cb_arg,
714 diff_content);
715 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
716 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
717 if (!id_match ||
718 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
719 (te2->mode & (S_IFLNK | S_IXUSR))) {
720 if (diff_content)
721 return diff_modified_blob(&te1->id, &te2->id,
722 f1, f2, fd1, fd2, label1, label2,
723 te1->mode, te2->mode, repo, cb, cb_arg);
724 else
725 return cb(cb_arg, NULL, NULL, NULL, NULL,
726 &te1->id, &te2->id, label1, label2,
727 te1->mode, te2->mode, repo);
731 if (id_match)
732 return NULL;
734 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
735 cb, cb_arg);
738 static const struct got_error *
739 diff_entry_new_old(struct got_tree_entry *te2,
740 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
741 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
742 int diff_content)
744 if (te1 != NULL) /* handled by diff_entry_old_new() */
745 return NULL;
747 if (got_object_tree_entry_is_submodule(te2))
748 return NULL;
750 if (S_ISDIR(te2->mode))
751 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
752 repo, cb, cb_arg, diff_content);
754 if (diff_content)
755 return diff_added_blob(&te2->id, f1, f2, fd2,
756 label2, te2->mode, repo, cb, cb_arg);
758 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
759 NULL, label2, 0, te2->mode, repo);
762 const struct got_error *
763 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
764 struct got_blob_object *blob2, FILE *f1, FILE *f2,
765 struct got_object_id *id1, struct got_object_id *id2,
766 const char *label1, const char *label2,
767 mode_t mode1, mode_t mode2, struct got_repository *repo)
769 const struct got_error *err = NULL;
770 struct got_diffreg_result *result = NULL;
771 struct got_diffstat_cb_arg *a = arg;
772 char *path = NULL;
773 int status = GOT_STATUS_NO_CHANGE;
775 path = strdup(label2 ? label2 : label1);
776 if (path == NULL)
777 return got_error_from_errno("strdup");
779 if (id1 == NULL)
780 status = GOT_STATUS_ADD;
781 else if (id2 == NULL)
782 status = GOT_STATUS_DELETE;
783 else {
784 if (got_object_id_cmp(id1, id2) != 0)
785 status = GOT_STATUS_MODIFY;
786 else if (mode1 != mode2)
787 status = GOT_STATUS_MODE_CHANGE;
790 if (f1) {
791 err = got_opentemp_truncate(f1);
792 if (err)
793 goto done;
795 if (f2) {
796 err = got_opentemp_truncate(f2);
797 if (err)
798 goto done;
801 if (blob1) {
802 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
803 blob1);
804 if (err)
805 goto done;
807 if (blob2) {
808 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
809 blob2);
810 if (err)
811 goto done;
814 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
815 a->force_text);
816 if (err)
817 goto done;
819 err = get_diffstat(a, path, result->result, a->force_text, status);
821 done:
822 if (result) {
823 const struct got_error *free_err;
825 free_err = got_diffreg_result_free(result);
826 if (free_err && err == NULL)
827 err = free_err;
829 if (err)
830 free(path);
831 return err;
834 const struct got_error *
835 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
836 struct got_blob_object *blob2, FILE *f1, FILE *f2,
837 struct got_object_id *id1, struct got_object_id *id2,
838 const char *label1, const char *label2,
839 mode_t mode1, mode_t mode2, struct got_repository *repo)
841 const struct got_error *err = NULL;
842 struct got_pathlist_head *paths = arg;
843 struct got_diff_changed_path *change = NULL;
844 char *path = NULL;
846 path = strdup(label2 ? label2 : label1);
847 if (path == NULL)
848 return got_error_from_errno("strdup");
850 change = malloc(sizeof(*change));
851 if (change == NULL) {
852 err = got_error_from_errno("malloc");
853 goto done;
856 change->status = GOT_STATUS_NO_CHANGE;
857 if (id1 == NULL)
858 change->status = GOT_STATUS_ADD;
859 else if (id2 == NULL)
860 change->status = GOT_STATUS_DELETE;
861 else {
862 if (got_object_id_cmp(id1, id2) != 0)
863 change->status = GOT_STATUS_MODIFY;
864 else if (mode1 != mode2)
865 change->status = GOT_STATUS_MODE_CHANGE;
868 err = got_pathlist_append(paths, path, change);
869 done:
870 if (err) {
871 free(path);
872 free(change);
874 return err;
877 const struct got_error *
878 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
879 FILE *f1, FILE *f2, int fd1, int fd2,
880 const char *label1, const char *label2,
881 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
882 int diff_content)
884 const struct got_error *err = NULL;
885 struct got_tree_entry *te1 = NULL;
886 struct got_tree_entry *te2 = NULL;
887 char *l1 = NULL, *l2 = NULL;
888 int tidx1 = 0, tidx2 = 0;
890 if (tree1) {
891 te1 = got_object_tree_get_entry(tree1, 0);
892 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
893 te1->name) == -1)
894 return got_error_from_errno("asprintf");
896 if (tree2) {
897 te2 = got_object_tree_get_entry(tree2, 0);
898 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
899 te2->name) == -1) {
900 err = got_error_from_errno("asprintf");
901 goto done;
905 do {
906 if (te1) {
907 struct got_tree_entry *te = NULL;
909 if (tree2)
910 te = got_object_tree_find_entry(tree2,
911 te1->name);
912 if (te) {
913 free(l2);
914 l2 = NULL;
915 if (te && asprintf(&l2, "%s%s%s", label2,
916 label2[0] ? "/" : "", te->name) == -1) {
917 err = got_error_from_errno("asprintf");
918 goto done;
922 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
923 l1, l2, repo, cb, cb_arg, diff_content);
924 if (err)
925 break;
928 if (te2) {
929 struct got_tree_entry *te = NULL;
931 if (tree1)
932 te = got_object_tree_find_entry(tree1,
933 te2->name);
935 free(l2);
936 l2 = NULL;
937 if (te) {
938 if (asprintf(&l2, "%s%s%s", label2,
939 label2[0] ? "/" : "", te->name) == -1) {
940 err = got_error_from_errno("asprintf");
941 goto done;
943 } else {
944 if (asprintf(&l2, "%s%s%s", label2,
945 label2[0] ? "/" : "", te2->name) == -1) {
946 err = got_error_from_errno("asprintf");
947 goto done;
951 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
952 repo, cb, cb_arg, diff_content);
953 if (err)
954 break;
957 free(l1);
958 l1 = NULL;
959 if (te1) {
960 tidx1++;
961 te1 = got_object_tree_get_entry(tree1, tidx1);
962 if (te1 &&
963 asprintf(&l1, "%s%s%s", label1,
964 label1[0] ? "/" : "", te1->name) == -1) {
965 err = got_error_from_errno("asprintf");
966 goto done;
970 free(l2);
971 l2 = NULL;
972 if (te2) {
973 tidx2++;
974 te2 = got_object_tree_get_entry(tree2, tidx2);
975 if (te2 &&
976 asprintf(&l2, "%s%s%s", label2,
977 label2[0] ? "/" : "", te2->name) == -1) {
978 err = got_error_from_errno("asprintf");
979 goto done;
982 } while (te1 || te2);
984 done:
985 free(l1);
986 free(l2);
987 return err;
990 const struct got_error *
991 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
992 FILE *f1, FILE *f2, int fd1, int fd2,
993 struct got_object_id *id1, struct got_object_id *id2,
994 const char *label1, const char *label2,
995 enum got_diff_algorithm diff_algo, int diff_context,
996 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
997 struct got_repository *repo, FILE *outfile)
999 const struct got_error *err;
1000 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1002 if (id1 == NULL && id2 == NULL)
1003 return got_error(GOT_ERR_NO_OBJ);
1005 if (id1) {
1006 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1007 if (err)
1008 goto done;
1010 if (id2) {
1011 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1012 if (err)
1013 goto done;
1015 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1016 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1017 ds, outfile);
1018 done:
1019 if (blob1)
1020 got_object_blob_close(blob1);
1021 if (blob2)
1022 got_object_blob_close(blob2);
1023 return err;
1026 static const struct got_error *
1027 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1028 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1029 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1031 const struct got_error *err = NULL;
1032 struct got_pathlist_entry *pe;
1033 struct got_object_id *id1 = NULL, *id2 = NULL;
1034 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1035 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1037 TAILQ_FOREACH(pe, paths, entry) {
1038 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1039 mode_t mode1 = 0, mode2 = 0;
1041 free(id1);
1042 id1 = NULL;
1043 free(id2);
1044 id2 = NULL;
1045 if (subtree1) {
1046 got_object_tree_close(subtree1);
1047 subtree1 = NULL;
1049 if (subtree2) {
1050 got_object_tree_close(subtree2);
1051 subtree2 = NULL;
1053 if (blob1) {
1054 got_object_blob_close(blob1);
1055 blob1 = NULL;
1057 if (blob2) {
1058 got_object_blob_close(blob2);
1059 blob2 = NULL;
1062 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1063 pe->path);
1064 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1065 goto done;
1066 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1067 pe->path);
1068 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1069 goto done;
1070 if (id1 == NULL && id2 == NULL) {
1071 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1072 goto done;
1074 if (id1) {
1075 err = got_object_get_type(&type1, repo, id1);
1076 if (err)
1077 goto done;
1079 if (id2) {
1080 err = got_object_get_type(&type2, repo, id2);
1081 if (err)
1082 goto done;
1084 if (type1 == GOT_OBJ_TYPE_ANY &&
1085 type2 == GOT_OBJ_TYPE_ANY) {
1086 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1087 goto done;
1088 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1089 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1090 err = got_error(GOT_ERR_OBJ_TYPE);
1091 goto done;
1094 if (type1 == GOT_OBJ_TYPE_BLOB ||
1095 type2 == GOT_OBJ_TYPE_BLOB) {
1096 if (id1) {
1097 err = got_object_open_as_blob(&blob1, repo,
1098 id1, 8192, fd1);
1099 if (err)
1100 goto done;
1102 if (id2) {
1103 err = got_object_open_as_blob(&blob2, repo,
1104 id2, 8192, fd2);
1105 if (err)
1106 goto done;
1108 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1109 id1 ? pe->path : "/dev/null",
1110 id2 ? pe->path : "/dev/null",
1111 mode1, mode2, repo);
1112 if (err)
1113 goto done;
1114 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1115 type2 == GOT_OBJ_TYPE_TREE) {
1116 if (id1) {
1117 err = got_object_open_as_tree(&subtree1, repo,
1118 id1);
1119 if (err)
1120 goto done;
1122 if (id2) {
1123 err = got_object_open_as_tree(&subtree2, repo,
1124 id2);
1125 if (err)
1126 goto done;
1128 err = got_diff_tree(subtree1, subtree2, f1, f2,
1129 fd1, fd2,
1130 id1 ? pe->path : "/dev/null",
1131 id2 ? pe->path : "/dev/null",
1132 repo, cb, cb_arg, 1);
1133 if (err)
1134 goto done;
1135 } else {
1136 err = got_error(GOT_ERR_OBJ_TYPE);
1137 goto done;
1140 done:
1141 free(id1);
1142 free(id2);
1143 if (subtree1)
1144 got_object_tree_close(subtree1);
1145 if (subtree2)
1146 got_object_tree_close(subtree2);
1147 if (blob1)
1148 got_object_blob_close(blob1);
1149 if (blob2)
1150 got_object_blob_close(blob2);
1151 return err;
1154 static const struct got_error *
1155 show_object_id(struct got_diff_line **lines, size_t *nlines,
1156 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1158 const struct got_error *err;
1159 int n;
1160 off_t outoff = 0;
1162 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1163 if (n < 0)
1164 return got_error_from_errno("fprintf");
1166 if (lines != NULL && *lines != NULL) {
1167 if (*nlines == 0) {
1168 err = add_line_metadata(lines, nlines, 0,
1169 GOT_DIFF_LINE_META);
1170 if (err)
1171 return err;
1172 } else
1173 outoff = (*lines)[*nlines - 1].offset;
1175 outoff += n;
1176 err = add_line_metadata(lines, nlines, outoff,
1177 GOT_DIFF_LINE_META);
1178 if (err)
1179 return err;
1182 return NULL;
1185 static const struct got_error *
1186 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1187 FILE *f1, FILE *f2, int fd1, int fd2,
1188 struct got_object_id *id1, struct got_object_id *id2,
1189 struct got_pathlist_head *paths, const char *label1, const char *label2,
1190 int diff_context, int ignore_whitespace, int force_text_diff,
1191 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1192 FILE *outfile, enum got_diff_algorithm diff_algo)
1194 const struct got_error *err;
1195 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1196 struct got_diff_blob_output_unidiff_arg arg;
1197 int want_linemeta = (lines != NULL && *lines != NULL);
1199 if (id1 == NULL && id2 == NULL)
1200 return got_error(GOT_ERR_NO_OBJ);
1202 if (id1) {
1203 err = got_object_open_as_tree(&tree1, repo, id1);
1204 if (err)
1205 goto done;
1207 if (id2) {
1208 err = got_object_open_as_tree(&tree2, repo, id2);
1209 if (err)
1210 goto done;
1213 arg.diff_algo = diff_algo;
1214 arg.diff_context = diff_context;
1215 arg.ignore_whitespace = ignore_whitespace;
1216 arg.force_text_diff = force_text_diff;
1217 arg.diffstat = dsa;
1218 arg.outfile = outfile;
1219 if (want_linemeta) {
1220 arg.lines = *lines;
1221 arg.nlines = *nlines;
1222 } else {
1223 arg.lines = NULL;
1224 arg.nlines = 0;
1226 if (paths == NULL || TAILQ_EMPTY(paths))
1227 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1228 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1229 else
1230 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1231 got_diff_blob_output_unidiff, &arg);
1232 if (want_linemeta) {
1233 *lines = arg.lines; /* was likely re-allocated */
1234 *nlines = arg.nlines;
1236 done:
1237 if (tree1)
1238 got_object_tree_close(tree1);
1239 if (tree2)
1240 got_object_tree_close(tree2);
1241 return err;
1244 const struct got_error *
1245 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1246 FILE *f1, FILE *f2, int fd1, int fd2,
1247 struct got_object_id *id1, struct got_object_id *id2,
1248 struct got_pathlist_head *paths, const char *label1, const char *label2,
1249 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1250 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1251 struct got_repository *repo, FILE *outfile)
1253 const struct got_error *err;
1254 char *idstr = NULL;
1256 if (id1 == NULL && id2 == NULL)
1257 return got_error(GOT_ERR_NO_OBJ);
1259 if (id1) {
1260 err = got_object_id_str(&idstr, id1);
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 if (id2) {
1276 err = got_object_id_str(&idstr, id2);
1277 if (err)
1278 goto done;
1279 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1280 if (err)
1281 goto done;
1282 free(idstr);
1283 idstr = NULL;
1284 } else {
1285 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1286 outfile);
1287 if (err)
1288 goto done;
1291 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1292 paths, label1, label2, diff_context, ignore_whitespace,
1293 force_text_diff, dsa, repo, outfile, diff_algo);
1294 done:
1295 free(idstr);
1296 return err;
1299 const struct got_error *
1300 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1301 FILE *f1, FILE *f2, int fd1, int fd2,
1302 struct got_object_id *id1, struct got_object_id *id2,
1303 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1304 int diff_context, int ignore_whitespace, int force_text_diff,
1305 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1307 const struct got_error *err;
1308 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1309 char *idstr = NULL;
1311 if (id2 == NULL)
1312 return got_error(GOT_ERR_NO_OBJ);
1314 if (id1) {
1315 err = got_object_open_as_commit(&commit1, repo, id1);
1316 if (err)
1317 goto done;
1318 err = got_object_id_str(&idstr, id1);
1319 if (err)
1320 goto done;
1321 err = show_object_id(lines, nlines, "commit", '-', idstr,
1322 outfile);
1323 if (err)
1324 goto done;
1325 free(idstr);
1326 idstr = NULL;
1327 } else {
1328 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1329 outfile);
1330 if (err)
1331 goto done;
1334 err = got_object_open_as_commit(&commit2, repo, id2);
1335 if (err)
1336 goto done;
1338 err = got_object_id_str(&idstr, id2);
1339 if (err)
1340 goto done;
1341 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1342 if (err)
1343 goto done;
1345 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1346 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1347 got_object_commit_get_tree_id(commit2), paths, "", "",
1348 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1349 outfile, diff_algo);
1350 done:
1351 if (commit1)
1352 got_object_commit_close(commit1);
1353 if (commit2)
1354 got_object_commit_close(commit2);
1355 free(idstr);
1356 return err;
1359 const struct got_error *
1360 got_diff_files(struct got_diffreg_result **resultp,
1361 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1362 const char *label2, int diff_context, int ignore_whitespace,
1363 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1365 const struct got_error *err = NULL;
1366 struct got_diffreg_result *diffreg_result = NULL;
1368 if (resultp)
1369 *resultp = NULL;
1371 if (outfile) {
1372 fprintf(outfile, "file - %s\n",
1373 f1_exists ? label1 : "/dev/null");
1374 fprintf(outfile, "file + %s\n",
1375 f2_exists ? label2 : "/dev/null");
1378 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1379 ignore_whitespace, force_text_diff);
1380 if (err)
1381 goto done;
1383 if (outfile) {
1384 err = got_diffreg_output(NULL, NULL, diffreg_result,
1385 f1_exists, f2_exists, label1, label2,
1386 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1387 if (err)
1388 goto done;
1391 done:
1392 if (resultp && err == NULL)
1393 *resultp = diffreg_result;
1394 else if (diffreg_result) {
1395 const struct got_error *free_err;
1397 free_err = got_diffreg_result_free(diffreg_result);
1398 if (free_err && err == NULL)
1399 err = free_err;
1402 return err;