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