Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <sha2.h>
26 #include <zlib.h>
28 #include "got_compat.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_error.h"
33 #include "got_diff.h"
34 #include "got_path.h"
35 #include "got_cancel.h"
36 #include "got_worktree.h"
37 #include "got_opentemp.h"
39 #include "got_lib_diff.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
44 #ifndef MAX
45 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 #endif
48 static const struct got_error *
49 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
50 off_t off, uint8_t type)
51 {
52 struct got_diff_line *p;
54 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
55 if (p == NULL)
56 return got_error_from_errno("reallocarray");
57 *lines = p;
58 (*lines)[*nlines].offset = off;
59 (*lines)[*nlines].type = type;
60 (*nlines)++;
62 return NULL;
63 }
65 static void
66 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
67 uint32_t add, uint32_t rm)
68 {
69 int d1 = 1, d2 = 1;
71 if (maxlen)
72 *maxlen = MAX(*maxlen, len);
74 while (add /= 10)
75 ++d1;
76 *add_cols = MAX(*add_cols, d1);
78 while (rm /= 10)
79 ++d2;
80 *rm_cols = MAX(*rm_cols, d2);
81 }
83 static const struct got_error *
84 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
85 struct diff_result *r, int force_text, int status)
86 {
87 const struct got_error *err;
88 struct got_pathlist_entry *pe;
89 struct got_diff_changed_path *change = NULL;
90 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
91 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
92 int i;
94 change = calloc(1, sizeof(*change));
95 if (change == NULL)
96 return got_error_from_errno("calloc");
98 if (!isbin || force_text) {
99 for (i = 0; i < r->chunks.len; ++i) {
100 struct diff_chunk *c;
101 int clc, crc;
103 c = diff_chunk_get(r, i);
104 clc = diff_chunk_get_left_count(c);
105 crc = diff_chunk_get_right_count(c);
107 if (crc && !clc)
108 change->add += crc;
109 if (clc && !crc)
110 change->rm += clc;
114 change->status = status;
115 ds->ins += change->add;
116 ds->del += change->rm;
117 ++ds->nfiles;
119 err = got_pathlist_append(ds->paths, path, change);
120 if (err) {
121 free(change);
122 return err;
125 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
126 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
127 pe->path_len, change->add, change->rm);
129 return NULL;
132 static const struct got_error *
133 diff_blobs(struct got_diff_line **lines, size_t *nlines,
134 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
135 struct got_blob_object *blob2, FILE *f1, FILE *f2,
136 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
137 int diff_context, int ignore_whitespace, int force_text_diff,
138 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
139 enum got_diff_algorithm diff_algo)
141 const struct got_error *err = NULL, *free_err;
142 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
143 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
144 const char *idstr1 = NULL, *idstr2 = NULL;
145 char *modestr1 = NULL, *modestr2 = NULL;
146 off_t size1, size2;
147 struct got_diffreg_result *result = NULL;
148 off_t outoff = 0;
149 int n;
151 if (lines && *lines && *nlines > 0)
152 outoff = (*lines)[*nlines - 1].offset;
153 else if (lines) {
154 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
155 if (err)
156 goto done;
159 if (resultp)
160 *resultp = NULL;
162 if (f1) {
163 err = got_opentemp_truncate(f1);
164 if (err)
165 goto done;
167 if (f2) {
168 err = got_opentemp_truncate(f2);
169 if (err)
170 goto done;
173 size1 = 0;
174 if (blob1) {
175 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
176 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
177 blob1);
178 if (err)
179 goto done;
180 } else
181 idstr1 = "/dev/null";
183 size2 = 0;
184 if (blob2) {
185 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
186 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
187 blob2);
188 if (err)
189 goto done;
190 } else
191 idstr2 = "/dev/null";
193 if (outfile) {
194 int modebits;
196 if (mode1 && mode1 != mode2) {
197 if (S_ISLNK(mode1))
198 modebits = S_IFLNK;
199 else
200 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
201 if (asprintf(&modestr1, " (mode %o)",
202 mode1 & modebits) == -1) {
203 err = got_error_from_errno("asprintf");
204 goto done;
207 if (mode2 && mode1 != mode2) {
208 if (S_ISLNK(mode2))
209 modebits = S_IFLNK;
210 else
211 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
212 if (asprintf(&modestr2, " (mode %o)",
213 mode2 & modebits) == -1) {
214 err = got_error_from_errno("asprintf");
215 goto done;
218 n = fprintf(outfile, "blob - %s%s\n", idstr1,
219 modestr1 ? modestr1 : "");
220 if (n < 0)
221 goto done;
222 outoff += n;
223 if (lines) {
224 err = add_line_metadata(lines, nlines, outoff,
225 GOT_DIFF_LINE_BLOB_MIN);
226 if (err)
227 goto done;
230 n = fprintf(outfile, "blob + %s%s\n", idstr2,
231 modestr2 ? modestr2 : "");
232 if (n < 0)
233 goto done;
234 outoff += n;
235 if (lines) {
236 err = add_line_metadata(lines, nlines, outoff,
237 GOT_DIFF_LINE_BLOB_PLUS);
238 if (err)
239 goto done;
243 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
244 force_text_diff);
245 if (err)
246 goto done;
248 if (diffstat) {
249 char *path = NULL;
250 int status = GOT_STATUS_NO_CHANGE;
252 if (blob1 == NULL)
253 status = GOT_STATUS_ADD;
254 else if (blob2 == NULL)
255 status = GOT_STATUS_DELETE;
256 else {
257 if (strcmp(idstr1, idstr2) != 0)
258 status = GOT_STATUS_MODIFY;
259 else if (mode1 != mode2)
260 status = GOT_STATUS_MODE_CHANGE;
263 if (label1 == NULL && label2 == NULL) {
264 /* diffstat of blobs, show hash instead of path */
265 if (asprintf(&path, "%.10s -> %.10s",
266 idstr1, idstr2) == -1) {
267 err = got_error_from_errno("asprintf");
268 goto done;
270 } else {
271 if (label2 != NULL &&
272 (status != GOT_STATUS_DELETE || label1 == NULL))
273 path = strdup(label2);
274 else
275 path = strdup(label1);
276 if (path == NULL) {
277 err = got_error_from_errno("strdup");
278 goto done;
282 err = get_diffstat(diffstat, path, result->result,
283 force_text_diff, status);
284 if (err) {
285 free(path);
286 goto done;
290 if (outfile) {
291 err = got_diffreg_output(lines, nlines, result,
292 blob1 != NULL, blob2 != NULL,
293 label1 ? label1 : idstr1,
294 label2 ? label2 : idstr2,
295 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
296 if (err)
297 goto done;
300 done:
301 free(modestr1);
302 free(modestr2);
303 if (resultp && err == NULL)
304 *resultp = result;
305 else if (result) {
306 free_err = got_diffreg_result_free(result);
307 if (free_err && err == NULL)
308 err = free_err;
311 return err;
314 const struct got_error *
315 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
316 struct got_blob_object *blob2, FILE *f1, FILE *f2,
317 struct got_object_id *id1, struct got_object_id *id2,
318 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
319 struct got_repository *repo)
321 struct got_diff_blob_output_unidiff_arg *a = arg;
323 return diff_blobs(&a->lines, &a->nlines, NULL,
324 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
325 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
326 a->diff_algo);
329 const struct got_error *
330 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
331 struct got_blob_object *blob1, struct got_blob_object *blob2,
332 FILE *f1, FILE *f2, const char *label1, const char *label2,
333 enum got_diff_algorithm diff_algo, int diff_context,
334 int ignore_whitespace, int force_text_diff,
335 struct got_diffstat_cb_arg *ds, FILE *outfile)
337 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
338 label1, label2, 0, 0, diff_context, ignore_whitespace,
339 force_text_diff, ds, outfile, diff_algo);
342 static const struct got_error *
343 diff_blob_file(struct got_diffreg_result **resultp,
344 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
345 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
346 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
347 int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
349 const struct got_error *err = NULL, *free_err;
350 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
351 const char *idstr1 = NULL;
352 struct got_diffreg_result *result = NULL;
354 if (resultp)
355 *resultp = NULL;
357 if (blob1)
358 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
359 else
360 idstr1 = "/dev/null";
362 if (outfile) {
363 char *mode = NULL;
365 /* display file mode for new added files only */
366 if (f2_exists && blob1 == NULL) {
367 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
369 if (S_ISLNK(sb2->st_mode))
370 mmask = S_IFLNK;
371 if (asprintf(&mode, " (mode %o)",
372 sb2->st_mode & mmask) == -1)
373 return got_error_from_errno("asprintf");
375 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
376 fprintf(outfile, "file + %s%s\n",
377 f2_exists ? label2 : "/dev/null", mode ? mode : "");
378 free(mode);
381 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
382 force_text_diff);
383 if (err)
384 goto done;
386 if (outfile) {
387 err = got_diffreg_output(NULL, NULL, result,
388 blob1 != NULL, f2_exists,
389 label2, /* show local file's path, not a blob ID */
390 label2, GOT_DIFF_OUTPUT_UNIDIFF,
391 diff_context, outfile);
392 if (err)
393 goto done;
396 if (diffstat) {
397 char *path = NULL;
398 int status = GOT_STATUS_NO_CHANGE;
400 /*
401 * Ignore 'm'ode status change: if there's no accompanying
402 * content change, there'll be no diffstat, and if there
403 * are actual changes, 'M'odified takes precedence.
404 */
405 if (blob1 == NULL)
406 status = GOT_STATUS_ADD;
407 else if (!f2_exists)
408 status = GOT_STATUS_DELETE;
409 else
410 status = GOT_STATUS_MODIFY;
412 if (label2 != NULL &&
413 (status != GOT_STATUS_DELETE || label1 == NULL))
414 path = strdup(label2);
415 else
416 path = strdup(label1);
417 if (path == NULL) {
418 err = got_error_from_errno("strdup");
419 goto done;
422 err = get_diffstat(diffstat, path, result->result,
423 force_text_diff, status);
424 if (err) {
425 free(path);
426 goto done;
430 done:
431 if (resultp && err == NULL)
432 *resultp = result;
433 else if (result) {
434 free_err = got_diffreg_result_free(result);
435 if (free_err && err == NULL)
436 err = free_err;
438 return err;
441 const struct got_error *
442 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
443 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
444 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
445 int ignore_whitespace, int force_text_diff,
446 struct got_diffstat_cb_arg *ds, FILE *outfile)
448 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
449 sb2, label2, diff_algo, diff_context, ignore_whitespace,
450 force_text_diff, ds, outfile);
453 static const struct got_error *
454 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
455 const char *label, mode_t mode, struct got_repository *repo,
456 got_diff_blob_cb cb, void *cb_arg)
458 const struct got_error *err;
459 struct got_blob_object *blob = NULL;
460 struct got_object *obj = NULL;
462 err = got_object_open(&obj, repo, id);
463 if (err)
464 return err;
466 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
467 if (err)
468 goto done;
469 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
470 NULL, label, 0, mode, repo);
471 done:
472 got_object_close(obj);
473 if (blob)
474 got_object_blob_close(blob);
475 return err;
478 static const struct got_error *
479 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
480 FILE *f1, FILE *f2, int fd1, int fd2,
481 const char *label1, const char *label2,
482 mode_t mode1, mode_t mode2, struct got_repository *repo,
483 got_diff_blob_cb cb, void *cb_arg)
485 const struct got_error *err;
486 struct got_object *obj1 = NULL;
487 struct got_object *obj2 = NULL;
488 struct got_blob_object *blob1 = NULL;
489 struct got_blob_object *blob2 = NULL;
491 err = got_object_open(&obj1, repo, id1);
492 if (err)
493 return err;
495 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
496 err = got_error(GOT_ERR_OBJ_TYPE);
497 goto done;
500 err = got_object_open(&obj2, repo, id2);
501 if (err)
502 goto done;
503 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 goto done;
508 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
509 if (err)
510 goto done;
512 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
513 if (err)
514 goto done;
516 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
517 mode1, mode2, repo);
518 done:
519 if (obj1)
520 got_object_close(obj1);
521 if (obj2)
522 got_object_close(obj2);
523 if (blob1)
524 got_object_blob_close(blob1);
525 if (blob2)
526 got_object_blob_close(blob2);
527 return err;
530 static const struct got_error *
531 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
532 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
533 got_diff_blob_cb cb, void *cb_arg)
535 const struct got_error *err;
536 struct got_blob_object *blob = NULL;
537 struct got_object *obj = NULL;
539 err = got_object_open(&obj, repo, id);
540 if (err)
541 return err;
543 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
544 if (err)
545 goto done;
546 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
547 mode, 0, repo);
548 done:
549 got_object_close(obj);
550 if (blob)
551 got_object_blob_close(blob);
552 return err;
555 static const struct got_error *
556 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
557 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
558 void *cb_arg, int diff_content)
560 const struct got_error *err = NULL;
561 struct got_object *treeobj = NULL;
562 struct got_tree_object *tree = NULL;
564 err = got_object_open(&treeobj, repo, id);
565 if (err)
566 goto done;
568 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
570 goto done;
573 err = got_object_tree_open(&tree, repo, treeobj);
574 if (err)
575 goto done;
577 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
578 repo, cb, cb_arg, diff_content);
579 done:
580 if (tree)
581 got_object_tree_close(tree);
582 if (treeobj)
583 got_object_close(treeobj);
584 return err;
587 static const struct got_error *
588 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
589 FILE *f1, FILE *f2, int fd1, int fd2,
590 const char *label1, const char *label2,
591 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
592 int diff_content)
594 const struct got_error *err;
595 struct got_object *treeobj1 = NULL;
596 struct got_object *treeobj2 = NULL;
597 struct got_tree_object *tree1 = NULL;
598 struct got_tree_object *tree2 = NULL;
600 err = got_object_open(&treeobj1, repo, id1);
601 if (err)
602 goto done;
604 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
605 err = got_error(GOT_ERR_OBJ_TYPE);
606 goto done;
609 err = got_object_open(&treeobj2, repo, id2);
610 if (err)
611 goto done;
613 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
614 err = got_error(GOT_ERR_OBJ_TYPE);
615 goto done;
618 err = got_object_tree_open(&tree1, repo, treeobj1);
619 if (err)
620 goto done;
622 err = got_object_tree_open(&tree2, repo, treeobj2);
623 if (err)
624 goto done;
626 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
627 label1, label2, repo, cb, cb_arg, diff_content);
629 done:
630 if (tree1)
631 got_object_tree_close(tree1);
632 if (tree2)
633 got_object_tree_close(tree2);
634 if (treeobj1)
635 got_object_close(treeobj1);
636 if (treeobj2)
637 got_object_close(treeobj2);
638 return err;
641 static const struct got_error *
642 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
643 FILE *f2, const char *label, struct got_repository *repo,
644 got_diff_blob_cb cb, void *cb_arg, int diff_content)
646 const struct got_error *err;
647 struct got_object *treeobj = NULL;
648 struct got_tree_object *tree = NULL;
650 err = got_object_open(&treeobj, repo, id);
651 if (err)
652 goto done;
654 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
655 err = got_error(GOT_ERR_OBJ_TYPE);
656 goto done;
659 err = got_object_tree_open(&tree, repo, treeobj);
660 if (err)
661 goto done;
663 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
664 repo, cb, cb_arg, diff_content);
665 done:
666 if (tree)
667 got_object_tree_close(tree);
668 if (treeobj)
669 got_object_close(treeobj);
670 return err;
673 static const struct got_error *
674 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
675 const char *label1, const char *label2, struct got_repository *repo,
676 got_diff_blob_cb cb, void *cb_arg)
678 /* XXX TODO */
679 return NULL;
682 static const struct got_error *
683 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
684 FILE *f1, FILE *f2, int fd1, int fd2,
685 const char *label1, const char *label2,
686 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
687 int diff_content)
689 const struct got_error *err = NULL;
690 int id_match;
692 if (got_object_tree_entry_is_submodule(te1))
693 return NULL;
695 if (te2 == NULL) {
696 if (S_ISDIR(te1->mode))
697 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
698 label1, repo, cb, cb_arg, diff_content);
699 else {
700 if (diff_content)
701 err = diff_deleted_blob(&te1->id, f1, fd1,
702 f2, label1, te1->mode, repo, cb, cb_arg);
703 else
704 err = cb(cb_arg, NULL, NULL, NULL, NULL,
705 &te1->id, NULL, label1, NULL,
706 te1->mode, 0, repo);
708 return err;
709 } else if (got_object_tree_entry_is_submodule(te2))
710 return NULL;
712 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
713 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
714 if (!id_match)
715 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
716 fd1, fd2, label1, label2, repo, cb, cb_arg,
717 diff_content);
718 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
719 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
720 if (!id_match ||
721 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
722 (te2->mode & (S_IFLNK | S_IXUSR))) {
723 if (diff_content)
724 return diff_modified_blob(&te1->id, &te2->id,
725 f1, f2, fd1, fd2, label1, label2,
726 te1->mode, te2->mode, repo, cb, cb_arg);
727 else
728 return cb(cb_arg, NULL, NULL, NULL, NULL,
729 &te1->id, &te2->id, label1, label2,
730 te1->mode, te2->mode, repo);
734 if (id_match)
735 return NULL;
737 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
738 cb, cb_arg);
741 static const struct got_error *
742 diff_entry_new_old(struct got_tree_entry *te2,
743 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
744 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
745 int diff_content)
747 if (te1 != NULL) /* handled by diff_entry_old_new() */
748 return NULL;
750 if (got_object_tree_entry_is_submodule(te2))
751 return NULL;
753 if (S_ISDIR(te2->mode))
754 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
755 repo, cb, cb_arg, diff_content);
757 if (diff_content)
758 return diff_added_blob(&te2->id, f1, f2, fd2,
759 label2, te2->mode, repo, cb, cb_arg);
761 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
762 NULL, label2, 0, te2->mode, repo);
765 const struct got_error *
766 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
767 struct got_blob_object *blob2, FILE *f1, FILE *f2,
768 struct got_object_id *id1, struct got_object_id *id2,
769 const char *label1, const char *label2,
770 mode_t mode1, mode_t mode2, struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_diffreg_result *result = NULL;
774 struct got_diffstat_cb_arg *a = arg;
775 char *path = NULL;
776 int status = GOT_STATUS_NO_CHANGE;
778 path = strdup(label2 ? label2 : label1);
779 if (path == NULL)
780 return got_error_from_errno("strdup");
782 if (id1 == NULL)
783 status = GOT_STATUS_ADD;
784 else if (id2 == NULL)
785 status = GOT_STATUS_DELETE;
786 else {
787 if (got_object_id_cmp(id1, id2) != 0)
788 status = GOT_STATUS_MODIFY;
789 else if (mode1 != mode2)
790 status = GOT_STATUS_MODE_CHANGE;
793 if (f1) {
794 err = got_opentemp_truncate(f1);
795 if (err)
796 goto done;
798 if (f2) {
799 err = got_opentemp_truncate(f2);
800 if (err)
801 goto done;
804 if (blob1) {
805 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
806 blob1);
807 if (err)
808 goto done;
810 if (blob2) {
811 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
812 blob2);
813 if (err)
814 goto done;
817 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
818 a->force_text);
819 if (err)
820 goto done;
822 err = get_diffstat(a, path, result->result, a->force_text, status);
824 done:
825 if (result) {
826 const struct got_error *free_err;
828 free_err = got_diffreg_result_free(result);
829 if (free_err && err == NULL)
830 err = free_err;
832 if (err)
833 free(path);
834 return err;
837 const struct got_error *
838 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
839 struct got_blob_object *blob2, FILE *f1, FILE *f2,
840 struct got_object_id *id1, struct got_object_id *id2,
841 const char *label1, const char *label2,
842 mode_t mode1, mode_t mode2, struct got_repository *repo)
844 const struct got_error *err = NULL;
845 struct got_pathlist_head *paths = arg;
846 struct got_diff_changed_path *change = NULL;
847 char *path = NULL;
849 path = strdup(label2 ? label2 : label1);
850 if (path == NULL)
851 return got_error_from_errno("strdup");
853 change = malloc(sizeof(*change));
854 if (change == NULL) {
855 err = got_error_from_errno("malloc");
856 goto done;
859 change->status = GOT_STATUS_NO_CHANGE;
860 if (id1 == NULL)
861 change->status = GOT_STATUS_ADD;
862 else if (id2 == NULL)
863 change->status = GOT_STATUS_DELETE;
864 else {
865 if (got_object_id_cmp(id1, id2) != 0)
866 change->status = GOT_STATUS_MODIFY;
867 else if (mode1 != mode2)
868 change->status = GOT_STATUS_MODE_CHANGE;
871 err = got_pathlist_append(paths, path, change);
872 done:
873 if (err) {
874 free(path);
875 free(change);
877 return err;
880 const struct got_error *
881 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
882 FILE *f1, FILE *f2, int fd1, int fd2,
883 const char *label1, const char *label2,
884 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
885 int diff_content)
887 const struct got_error *err = NULL;
888 struct got_tree_entry *te1 = NULL;
889 struct got_tree_entry *te2 = NULL;
890 char *l1 = NULL, *l2 = NULL;
891 int tidx1 = 0, tidx2 = 0;
893 if (tree1) {
894 te1 = got_object_tree_get_entry(tree1, 0);
895 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
896 te1->name) == -1)
897 return got_error_from_errno("asprintf");
899 if (tree2) {
900 te2 = got_object_tree_get_entry(tree2, 0);
901 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
902 te2->name) == -1) {
903 err = got_error_from_errno("asprintf");
904 goto done;
908 do {
909 if (te1) {
910 struct got_tree_entry *te = NULL;
912 if (tree2)
913 te = got_object_tree_find_entry(tree2,
914 te1->name);
915 if (te) {
916 free(l2);
917 l2 = NULL;
918 if (te && asprintf(&l2, "%s%s%s", label2,
919 label2[0] ? "/" : "", te->name) == -1) {
920 err = got_error_from_errno("asprintf");
921 goto done;
925 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
926 l1, l2, repo, cb, cb_arg, diff_content);
927 if (err)
928 break;
931 if (te2) {
932 struct got_tree_entry *te = NULL;
934 if (tree1)
935 te = got_object_tree_find_entry(tree1,
936 te2->name);
938 free(l2);
939 l2 = NULL;
940 if (te) {
941 if (asprintf(&l2, "%s%s%s", label2,
942 label2[0] ? "/" : "", te->name) == -1) {
943 err = got_error_from_errno("asprintf");
944 goto done;
946 } else {
947 if (asprintf(&l2, "%s%s%s", label2,
948 label2[0] ? "/" : "", te2->name) == -1) {
949 err = got_error_from_errno("asprintf");
950 goto done;
954 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
955 repo, cb, cb_arg, diff_content);
956 if (err)
957 break;
960 free(l1);
961 l1 = NULL;
962 if (te1) {
963 tidx1++;
964 te1 = got_object_tree_get_entry(tree1, tidx1);
965 if (te1 &&
966 asprintf(&l1, "%s%s%s", label1,
967 label1[0] ? "/" : "", te1->name) == -1) {
968 err = got_error_from_errno("asprintf");
969 goto done;
973 free(l2);
974 l2 = NULL;
975 if (te2) {
976 tidx2++;
977 te2 = got_object_tree_get_entry(tree2, tidx2);
978 if (te2 &&
979 asprintf(&l2, "%s%s%s", label2,
980 label2[0] ? "/" : "", te2->name) == -1) {
981 err = got_error_from_errno("asprintf");
982 goto done;
985 } while (te1 || te2);
987 done:
988 free(l1);
989 free(l2);
990 return err;
993 const struct got_error *
994 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
995 FILE *f1, FILE *f2, int fd1, int fd2,
996 struct got_object_id *id1, struct got_object_id *id2,
997 const char *label1, const char *label2,
998 enum got_diff_algorithm diff_algo, int diff_context,
999 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1000 struct got_repository *repo, FILE *outfile)
1002 const struct got_error *err;
1003 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1005 if (id1 == NULL && id2 == NULL)
1006 return got_error(GOT_ERR_NO_OBJ);
1008 if (id1) {
1009 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1010 if (err)
1011 goto done;
1013 if (id2) {
1014 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1015 if (err)
1016 goto done;
1018 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1019 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1020 ds, outfile);
1021 done:
1022 if (blob1)
1023 got_object_blob_close(blob1);
1024 if (blob2)
1025 got_object_blob_close(blob2);
1026 return err;
1029 static const struct got_error *
1030 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1031 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1032 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1034 const struct got_error *err = NULL;
1035 struct got_pathlist_entry *pe;
1036 struct got_object_id *id1 = NULL, *id2 = NULL;
1037 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1038 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1040 TAILQ_FOREACH(pe, paths, entry) {
1041 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1042 mode_t mode1 = 0, mode2 = 0;
1044 free(id1);
1045 id1 = NULL;
1046 free(id2);
1047 id2 = NULL;
1048 if (subtree1) {
1049 got_object_tree_close(subtree1);
1050 subtree1 = NULL;
1052 if (subtree2) {
1053 got_object_tree_close(subtree2);
1054 subtree2 = NULL;
1056 if (blob1) {
1057 got_object_blob_close(blob1);
1058 blob1 = NULL;
1060 if (blob2) {
1061 got_object_blob_close(blob2);
1062 blob2 = NULL;
1065 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1066 pe->path);
1067 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1068 goto done;
1069 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1070 pe->path);
1071 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1072 goto done;
1073 if (id1 == NULL && id2 == NULL) {
1074 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1075 goto done;
1077 if (id1) {
1078 err = got_object_get_type(&type1, repo, id1);
1079 if (err)
1080 goto done;
1082 if (id2) {
1083 err = got_object_get_type(&type2, repo, id2);
1084 if (err)
1085 goto done;
1087 if (type1 == GOT_OBJ_TYPE_ANY &&
1088 type2 == GOT_OBJ_TYPE_ANY) {
1089 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1090 goto done;
1091 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1092 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1093 err = got_error(GOT_ERR_OBJ_TYPE);
1094 goto done;
1097 if (type1 == GOT_OBJ_TYPE_BLOB ||
1098 type2 == GOT_OBJ_TYPE_BLOB) {
1099 if (id1) {
1100 err = got_object_open_as_blob(&blob1, repo,
1101 id1, 8192, fd1);
1102 if (err)
1103 goto done;
1105 if (id2) {
1106 err = got_object_open_as_blob(&blob2, repo,
1107 id2, 8192, fd2);
1108 if (err)
1109 goto done;
1111 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1112 id1 ? pe->path : "/dev/null",
1113 id2 ? pe->path : "/dev/null",
1114 mode1, mode2, repo);
1115 if (err)
1116 goto done;
1117 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1118 type2 == GOT_OBJ_TYPE_TREE) {
1119 if (id1) {
1120 err = got_object_open_as_tree(&subtree1, repo,
1121 id1);
1122 if (err)
1123 goto done;
1125 if (id2) {
1126 err = got_object_open_as_tree(&subtree2, repo,
1127 id2);
1128 if (err)
1129 goto done;
1131 err = got_diff_tree(subtree1, subtree2, f1, f2,
1132 fd1, fd2,
1133 id1 ? pe->path : "/dev/null",
1134 id2 ? pe->path : "/dev/null",
1135 repo, cb, cb_arg, 1);
1136 if (err)
1137 goto done;
1138 } else {
1139 err = got_error(GOT_ERR_OBJ_TYPE);
1140 goto done;
1143 done:
1144 free(id1);
1145 free(id2);
1146 if (subtree1)
1147 got_object_tree_close(subtree1);
1148 if (subtree2)
1149 got_object_tree_close(subtree2);
1150 if (blob1)
1151 got_object_blob_close(blob1);
1152 if (blob2)
1153 got_object_blob_close(blob2);
1154 return err;
1157 static const struct got_error *
1158 show_object_id(struct got_diff_line **lines, size_t *nlines,
1159 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1161 const struct got_error *err;
1162 int n;
1163 off_t outoff = 0;
1165 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1166 if (n < 0)
1167 return got_error_from_errno("fprintf");
1169 if (lines != NULL && *lines != NULL) {
1170 if (*nlines == 0) {
1171 err = add_line_metadata(lines, nlines, 0,
1172 GOT_DIFF_LINE_META);
1173 if (err)
1174 return err;
1175 } else
1176 outoff = (*lines)[*nlines - 1].offset;
1178 outoff += n;
1179 err = add_line_metadata(lines, nlines, outoff,
1180 GOT_DIFF_LINE_META);
1181 if (err)
1182 return err;
1185 return NULL;
1188 static const struct got_error *
1189 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1190 FILE *f1, FILE *f2, int fd1, int fd2,
1191 struct got_object_id *id1, struct got_object_id *id2,
1192 struct got_pathlist_head *paths, const char *label1, const char *label2,
1193 int diff_context, int ignore_whitespace, int force_text_diff,
1194 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1195 FILE *outfile, enum got_diff_algorithm diff_algo)
1197 const struct got_error *err;
1198 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1199 struct got_diff_blob_output_unidiff_arg arg;
1200 int want_linemeta = (lines != NULL && *lines != NULL);
1202 if (id1 == NULL && id2 == NULL)
1203 return got_error(GOT_ERR_NO_OBJ);
1205 if (id1) {
1206 err = got_object_open_as_tree(&tree1, repo, id1);
1207 if (err)
1208 goto done;
1210 if (id2) {
1211 err = got_object_open_as_tree(&tree2, repo, id2);
1212 if (err)
1213 goto done;
1216 arg.diff_algo = diff_algo;
1217 arg.diff_context = diff_context;
1218 arg.ignore_whitespace = ignore_whitespace;
1219 arg.force_text_diff = force_text_diff;
1220 arg.diffstat = dsa;
1221 arg.outfile = outfile;
1222 if (want_linemeta) {
1223 arg.lines = *lines;
1224 arg.nlines = *nlines;
1225 } else {
1226 arg.lines = NULL;
1227 arg.nlines = 0;
1229 if (paths == NULL || TAILQ_EMPTY(paths))
1230 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1231 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1232 else
1233 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1234 got_diff_blob_output_unidiff, &arg);
1235 if (want_linemeta) {
1236 *lines = arg.lines; /* was likely re-allocated */
1237 *nlines = arg.nlines;
1239 done:
1240 if (tree1)
1241 got_object_tree_close(tree1);
1242 if (tree2)
1243 got_object_tree_close(tree2);
1244 return err;
1247 const struct got_error *
1248 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1249 FILE *f1, FILE *f2, int fd1, int fd2,
1250 struct got_object_id *id1, struct got_object_id *id2,
1251 struct got_pathlist_head *paths, const char *label1, const char *label2,
1252 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1253 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1254 struct got_repository *repo, FILE *outfile)
1256 const struct got_error *err;
1257 char *idstr = NULL;
1259 if (id1 == NULL && id2 == NULL)
1260 return got_error(GOT_ERR_NO_OBJ);
1262 if (id1) {
1263 err = got_object_id_str(&idstr, id1);
1264 if (err)
1265 goto done;
1266 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1267 if (err)
1268 goto done;
1269 free(idstr);
1270 idstr = NULL;
1271 } else {
1272 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1273 outfile);
1274 if (err)
1275 goto done;
1278 if (id2) {
1279 err = got_object_id_str(&idstr, id2);
1280 if (err)
1281 goto done;
1282 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1283 if (err)
1284 goto done;
1285 free(idstr);
1286 idstr = NULL;
1287 } else {
1288 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1289 outfile);
1290 if (err)
1291 goto done;
1294 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1295 paths, label1, label2, diff_context, ignore_whitespace,
1296 force_text_diff, dsa, repo, outfile, diff_algo);
1297 done:
1298 free(idstr);
1299 return err;
1302 const struct got_error *
1303 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1304 FILE *f1, FILE *f2, int fd1, int fd2,
1305 struct got_object_id *id1, struct got_object_id *id2,
1306 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1307 int diff_context, int ignore_whitespace, int force_text_diff,
1308 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1310 const struct got_error *err;
1311 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1312 char *idstr = NULL;
1314 if (id2 == NULL)
1315 return got_error(GOT_ERR_NO_OBJ);
1317 if (id1) {
1318 err = got_object_open_as_commit(&commit1, repo, id1);
1319 if (err)
1320 goto done;
1321 err = got_object_id_str(&idstr, id1);
1322 if (err)
1323 goto done;
1324 err = show_object_id(lines, nlines, "commit", '-', idstr,
1325 outfile);
1326 if (err)
1327 goto done;
1328 free(idstr);
1329 idstr = NULL;
1330 } else {
1331 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1332 outfile);
1333 if (err)
1334 goto done;
1337 err = got_object_open_as_commit(&commit2, repo, id2);
1338 if (err)
1339 goto done;
1341 err = got_object_id_str(&idstr, id2);
1342 if (err)
1343 goto done;
1344 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1345 if (err)
1346 goto done;
1348 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1349 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1350 got_object_commit_get_tree_id(commit2), paths, "", "",
1351 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1352 outfile, diff_algo);
1353 done:
1354 if (commit1)
1355 got_object_commit_close(commit1);
1356 if (commit2)
1357 got_object_commit_close(commit2);
1358 free(idstr);
1359 return err;
1362 const struct got_error *
1363 got_diff_files(struct got_diffreg_result **resultp,
1364 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1365 const char *label2, int diff_context, int ignore_whitespace,
1366 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1368 const struct got_error *err = NULL;
1369 struct got_diffreg_result *diffreg_result = NULL;
1371 if (resultp)
1372 *resultp = NULL;
1374 if (outfile) {
1375 fprintf(outfile, "file - %s\n",
1376 f1_exists ? label1 : "/dev/null");
1377 fprintf(outfile, "file + %s\n",
1378 f2_exists ? label2 : "/dev/null");
1381 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1382 ignore_whitespace, force_text_diff);
1383 if (err)
1384 goto done;
1386 if (outfile) {
1387 err = got_diffreg_output(NULL, NULL, diffreg_result,
1388 f1_exists, f2_exists, label1, label2,
1389 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1390 if (err)
1391 goto done;
1394 done:
1395 if (resultp && err == NULL)
1396 *resultp = diffreg_result;
1397 else if (diffreg_result) {
1398 const struct got_error *free_err;
1400 free_err = got_diffreg_result_free(diffreg_result);
1401 if (free_err && err == NULL)
1402 err = free_err;
1405 return err;