Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <sha2.h>
26 #include <zlib.h>
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_error.h"
31 #include "got_diff.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_worktree.h"
35 #include "got_opentemp.h"
37 #include "got_lib_diff.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
42 #ifndef MAX
43 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
44 #endif
46 static const struct got_error *
47 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 off_t off, uint8_t type)
49 {
50 struct got_diff_line *p;
52 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 if (p == NULL)
54 return got_error_from_errno("reallocarray");
55 *lines = p;
56 (*lines)[*nlines].offset = off;
57 (*lines)[*nlines].type = type;
58 (*nlines)++;
60 return NULL;
61 }
63 static void
64 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
65 uint32_t add, uint32_t rm)
66 {
67 int d1 = 1, d2 = 1;
69 if (maxlen)
70 *maxlen = MAX(*maxlen, len);
72 while (add /= 10)
73 ++d1;
74 *add_cols = MAX(*add_cols, d1);
76 while (rm /= 10)
77 ++d2;
78 *rm_cols = MAX(*rm_cols, d2);
79 }
81 static const struct got_error *
82 get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
83 struct diff_result *r, int force_text, int status)
84 {
85 const struct got_error *err;
86 struct got_pathlist_entry *pe;
87 struct got_diff_changed_path *change = NULL;
88 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
89 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
90 int i;
92 change = calloc(1, sizeof(*change));
93 if (change == NULL)
94 return got_error_from_errno("calloc");
96 if (!isbin || force_text) {
97 for (i = 0; i < r->chunks.len; ++i) {
98 struct diff_chunk *c;
99 int clc, crc;
101 c = diff_chunk_get(r, i);
102 clc = diff_chunk_get_left_count(c);
103 crc = diff_chunk_get_right_count(c);
105 if (crc && !clc)
106 change->add += crc;
107 if (clc && !crc)
108 change->rm += clc;
112 change->status = status;
113 ds->ins += change->add;
114 ds->del += change->rm;
115 ++ds->nfiles;
117 err = got_pathlist_append(ds->paths, path, change);
118 if (err) {
119 free(change);
120 return err;
123 pe = TAILQ_LAST(ds->paths, got_pathlist_head);
124 diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
125 pe->path_len, change->add, change->rm);
127 return NULL;
130 static const struct got_error *
131 diff_blobs(struct got_diff_line **lines, size_t *nlines,
132 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
133 struct got_blob_object *blob2, FILE *f1, FILE *f2,
134 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
135 int diff_context, int ignore_whitespace, int force_text_diff,
136 struct got_diffstat_cb_arg *diffstat, FILE *outfile,
137 enum got_diff_algorithm diff_algo)
139 const struct got_error *err = NULL, *free_err;
140 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
141 char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
142 const char *idstr1 = NULL, *idstr2 = NULL;
143 char *modestr1 = NULL, *modestr2 = NULL;
144 off_t size1, size2;
145 struct got_diffreg_result *result = NULL;
146 off_t outoff = 0;
147 int n;
149 if (lines && *lines) {
150 if (*nlines > 0)
151 outoff = (*lines)[*nlines - 1].offset;
152 else {
153 err = add_line_metadata(lines, nlines,
154 0, GOT_DIFF_LINE_NONE);
155 if (err != NULL)
156 goto done;
160 if (resultp)
161 *resultp = NULL;
163 if (f1) {
164 err = got_opentemp_truncate(f1);
165 if (err)
166 goto done;
168 if (f2) {
169 err = got_opentemp_truncate(f2);
170 if (err)
171 goto done;
174 size1 = 0;
175 if (blob1) {
176 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
177 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
178 blob1);
179 if (err)
180 goto done;
181 } else
182 idstr1 = "/dev/null";
184 size2 = 0;
185 if (blob2) {
186 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
187 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
188 blob2);
189 if (err)
190 goto done;
191 } else
192 idstr2 = "/dev/null";
194 if (outfile) {
195 int modebits;
197 if (mode1 && mode1 != mode2) {
198 if (S_ISLNK(mode1))
199 modebits = S_IFLNK;
200 else
201 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
202 if (asprintf(&modestr1, " (mode %o)",
203 mode1 & modebits) == -1) {
204 err = got_error_from_errno("asprintf");
205 goto done;
208 if (mode2 && mode1 != mode2) {
209 if (S_ISLNK(mode2))
210 modebits = S_IFLNK;
211 else
212 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
213 if (asprintf(&modestr2, " (mode %o)",
214 mode2 & modebits) == -1) {
215 err = got_error_from_errno("asprintf");
216 goto done;
219 n = fprintf(outfile, "blob - %s%s\n", idstr1,
220 modestr1 ? modestr1 : "");
221 if (n < 0)
222 goto done;
223 outoff += n;
224 if (lines && *lines) {
225 err = add_line_metadata(lines, nlines, outoff,
226 GOT_DIFF_LINE_BLOB_MIN);
227 if (err)
228 goto done;
231 n = fprintf(outfile, "blob + %s%s\n", idstr2,
232 modestr2 ? modestr2 : "");
233 if (n < 0)
234 goto done;
235 outoff += n;
236 if (lines && *lines) {
237 err = add_line_metadata(lines, nlines, outoff,
238 GOT_DIFF_LINE_BLOB_PLUS);
239 if (err)
240 goto done;
244 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
245 force_text_diff);
246 if (err)
247 goto done;
249 if (diffstat) {
250 char *path = NULL;
251 int status = GOT_STATUS_NO_CHANGE;
253 if (blob1 == NULL)
254 status = GOT_STATUS_ADD;
255 else if (blob2 == NULL)
256 status = GOT_STATUS_DELETE;
257 else {
258 if (strcmp(idstr1, idstr2) != 0)
259 status = GOT_STATUS_MODIFY;
260 else if (mode1 != mode2)
261 status = GOT_STATUS_MODE_CHANGE;
264 if (label1 == NULL && label2 == NULL) {
265 /* diffstat of blobs, show hash instead of path */
266 if (asprintf(&path, "%.10s -> %.10s",
267 idstr1, idstr2) == -1) {
268 err = got_error_from_errno("asprintf");
269 goto done;
271 } else {
272 if (label2 != NULL &&
273 (status != GOT_STATUS_DELETE || label1 == NULL))
274 path = strdup(label2);
275 else
276 path = strdup(label1);
277 if (path == NULL) {
278 err = got_error_from_errno("strdup");
279 goto done;
283 err = get_diffstat(diffstat, path, result->result,
284 force_text_diff, status);
285 if (err) {
286 free(path);
287 goto done;
291 if (outfile) {
292 err = got_diffreg_output(lines, nlines, result,
293 blob1 != NULL, blob2 != NULL,
294 label1 ? label1 : idstr1,
295 label2 ? label2 : idstr2,
296 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
297 if (err)
298 goto done;
301 done:
302 free(modestr1);
303 free(modestr2);
304 if (resultp && err == NULL)
305 *resultp = result;
306 else if (result) {
307 free_err = got_diffreg_result_free(result);
308 if (free_err && err == NULL)
309 err = free_err;
312 return err;
315 const struct got_error *
316 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
317 struct got_blob_object *blob2, FILE *f1, FILE *f2,
318 struct got_object_id *id1, struct got_object_id *id2,
319 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
320 struct got_repository *repo)
322 struct got_diff_blob_output_unidiff_arg *a = arg;
324 return diff_blobs(&a->lines, &a->nlines, NULL,
325 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
326 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
327 a->diff_algo);
330 const struct got_error *
331 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
332 struct got_blob_object *blob1, struct got_blob_object *blob2,
333 FILE *f1, FILE *f2, const char *label1, const char *label2,
334 enum got_diff_algorithm diff_algo, int diff_context,
335 int ignore_whitespace, int force_text_diff,
336 struct got_diffstat_cb_arg *ds, FILE *outfile)
338 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
339 label1, label2, 0, 0, diff_context, ignore_whitespace,
340 force_text_diff, ds, outfile, diff_algo);
343 static const struct got_error *
344 diff_blob_file(struct got_diffreg_result **resultp,
345 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
346 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
347 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
348 int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
350 const struct got_error *err = NULL, *free_err;
351 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
352 const char *idstr1 = NULL;
353 struct got_diffreg_result *result = NULL;
355 if (resultp)
356 *resultp = NULL;
358 if (blob1)
359 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
360 else
361 idstr1 = "/dev/null";
363 if (outfile) {
364 char *mode = NULL;
366 /* display file mode for new added files only */
367 if (f2_exists && blob1 == NULL) {
368 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
370 if (S_ISLNK(sb2->st_mode))
371 mmask = S_IFLNK;
372 if (asprintf(&mode, " (mode %o)",
373 sb2->st_mode & mmask) == -1)
374 return got_error_from_errno("asprintf");
376 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
377 fprintf(outfile, "file + %s%s\n",
378 f2_exists ? label2 : "/dev/null", mode ? mode : "");
379 free(mode);
382 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
383 force_text_diff);
384 if (err) {
385 char msg[GOT_ERR_MAX_MSG_SIZE];
386 if (snprintf(msg, sizeof(msg), "%s vs %s: %s",
387 label1 ? label1 : idstr1,
388 f2_exists ? label2 : "/dev/null", err->msg) >= 0) {
389 err = got_error_msg(err->code, msg);
391 goto done;
394 if (outfile) {
395 err = got_diffreg_output(NULL, NULL, result,
396 blob1 != NULL, f2_exists,
397 label2, /* show local file's path, not a blob ID */
398 label2, GOT_DIFF_OUTPUT_UNIDIFF,
399 diff_context, outfile);
400 if (err)
401 goto done;
404 if (diffstat) {
405 char *path = NULL;
406 int status = GOT_STATUS_NO_CHANGE;
408 /*
409 * Ignore 'm'ode status change: if there's no accompanying
410 * content change, there'll be no diffstat, and if there
411 * are actual changes, 'M'odified takes precedence.
412 */
413 if (blob1 == NULL)
414 status = GOT_STATUS_ADD;
415 else if (!f2_exists)
416 status = GOT_STATUS_DELETE;
417 else
418 status = GOT_STATUS_MODIFY;
420 if (label2 != NULL &&
421 (status != GOT_STATUS_DELETE || label1 == NULL))
422 path = strdup(label2);
423 else
424 path = strdup(label1);
425 if (path == NULL) {
426 err = got_error_from_errno("strdup");
427 goto done;
430 err = get_diffstat(diffstat, path, result->result,
431 force_text_diff, status);
432 if (err) {
433 free(path);
434 goto done;
438 done:
439 if (resultp && err == NULL)
440 *resultp = result;
441 else if (result) {
442 free_err = got_diffreg_result_free(result);
443 if (free_err && err == NULL)
444 err = free_err;
446 return err;
449 const struct got_error *
450 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
451 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
452 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
453 int ignore_whitespace, int force_text_diff,
454 struct got_diffstat_cb_arg *ds, FILE *outfile)
456 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
457 sb2, label2, diff_algo, diff_context, ignore_whitespace,
458 force_text_diff, ds, outfile);
461 static const struct got_error *
462 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
463 const char *label, mode_t mode, struct got_repository *repo,
464 got_diff_blob_cb cb, void *cb_arg)
466 const struct got_error *err;
467 struct got_blob_object *blob = NULL;
468 struct got_object *obj = NULL;
470 err = got_object_open(&obj, repo, id);
471 if (err)
472 return err;
474 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
475 if (err)
476 goto done;
477 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
478 NULL, label, 0, mode, repo);
479 done:
480 got_object_close(obj);
481 if (blob)
482 got_object_blob_close(blob);
483 return err;
486 static const struct got_error *
487 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
488 FILE *f1, FILE *f2, int fd1, int fd2,
489 const char *label1, const char *label2,
490 mode_t mode1, mode_t mode2, struct got_repository *repo,
491 got_diff_blob_cb cb, void *cb_arg)
493 const struct got_error *err;
494 struct got_object *obj1 = NULL;
495 struct got_object *obj2 = NULL;
496 struct got_blob_object *blob1 = NULL;
497 struct got_blob_object *blob2 = NULL;
499 err = got_object_open(&obj1, repo, id1);
500 if (err)
501 return err;
503 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
504 err = got_error(GOT_ERR_OBJ_TYPE);
505 goto done;
508 err = got_object_open(&obj2, repo, id2);
509 if (err)
510 goto done;
511 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
512 err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 goto done;
516 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
517 if (err)
518 goto done;
520 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
521 if (err)
522 goto done;
524 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
525 mode1, mode2, repo);
526 done:
527 if (obj1)
528 got_object_close(obj1);
529 if (obj2)
530 got_object_close(obj2);
531 if (blob1)
532 got_object_blob_close(blob1);
533 if (blob2)
534 got_object_blob_close(blob2);
535 return err;
538 static const struct got_error *
539 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
540 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
541 got_diff_blob_cb cb, void *cb_arg)
543 const struct got_error *err;
544 struct got_blob_object *blob = NULL;
545 struct got_object *obj = NULL;
547 err = got_object_open(&obj, repo, id);
548 if (err)
549 return err;
551 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
552 if (err)
553 goto done;
554 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
555 mode, 0, repo);
556 done:
557 got_object_close(obj);
558 if (blob)
559 got_object_blob_close(blob);
560 return err;
563 static const struct got_error *
564 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
565 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
566 void *cb_arg, int diff_content)
568 const struct got_error *err = NULL;
569 struct got_object *treeobj = NULL;
570 struct got_tree_object *tree = NULL;
572 err = got_object_open(&treeobj, repo, id);
573 if (err)
574 goto done;
576 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
577 err = got_error(GOT_ERR_OBJ_TYPE);
578 goto done;
581 err = got_object_tree_open(&tree, repo, treeobj);
582 if (err)
583 goto done;
585 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
586 repo, cb, cb_arg, diff_content);
587 done:
588 if (tree)
589 got_object_tree_close(tree);
590 if (treeobj)
591 got_object_close(treeobj);
592 return err;
595 static const struct got_error *
596 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
597 FILE *f1, FILE *f2, int fd1, int fd2,
598 const char *label1, const char *label2,
599 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
600 int diff_content)
602 const struct got_error *err;
603 struct got_object *treeobj1 = NULL;
604 struct got_object *treeobj2 = NULL;
605 struct got_tree_object *tree1 = NULL;
606 struct got_tree_object *tree2 = NULL;
608 err = got_object_open(&treeobj1, repo, id1);
609 if (err)
610 goto done;
612 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
613 err = got_error(GOT_ERR_OBJ_TYPE);
614 goto done;
617 err = got_object_open(&treeobj2, repo, id2);
618 if (err)
619 goto done;
621 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
622 err = got_error(GOT_ERR_OBJ_TYPE);
623 goto done;
626 err = got_object_tree_open(&tree1, repo, treeobj1);
627 if (err)
628 goto done;
630 err = got_object_tree_open(&tree2, repo, treeobj2);
631 if (err)
632 goto done;
634 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
635 label1, label2, repo, cb, cb_arg, diff_content);
637 done:
638 if (tree1)
639 got_object_tree_close(tree1);
640 if (tree2)
641 got_object_tree_close(tree2);
642 if (treeobj1)
643 got_object_close(treeobj1);
644 if (treeobj2)
645 got_object_close(treeobj2);
646 return err;
649 static const struct got_error *
650 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
651 FILE *f2, const char *label, struct got_repository *repo,
652 got_diff_blob_cb cb, void *cb_arg, int diff_content)
654 const struct got_error *err;
655 struct got_object *treeobj = NULL;
656 struct got_tree_object *tree = NULL;
658 err = got_object_open(&treeobj, repo, id);
659 if (err)
660 goto done;
662 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
663 err = got_error(GOT_ERR_OBJ_TYPE);
664 goto done;
667 err = got_object_tree_open(&tree, repo, treeobj);
668 if (err)
669 goto done;
671 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
672 repo, cb, cb_arg, diff_content);
673 done:
674 if (tree)
675 got_object_tree_close(tree);
676 if (treeobj)
677 got_object_close(treeobj);
678 return err;
681 static const struct got_error *
682 diff_kind_mismatch(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, struct got_repository *repo,
685 got_diff_blob_cb cb, void *cb_arg, int diff_content)
687 const struct got_error *err = NULL;
689 /*
690 * Handle files changing into directories and vice-versa.
691 * Disregard edge cases with FIFOs, device nodes, etc for now.
692 */
693 if (!S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
694 if (S_ISREG(te1->mode)) {
695 if (diff_content) {
696 err = diff_deleted_blob(&te1->id, f1, fd1,
697 f2, label1, te1->mode, repo, cb, cb_arg);
698 } else {
699 err = cb(cb_arg, NULL, NULL, NULL, NULL,
700 &te1->id, NULL, label1, NULL,
701 te1->mode, 0, repo);
703 if (err)
704 return err;
706 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
707 repo, cb, cb_arg, diff_content);
708 } else if (S_ISDIR(te1->mode) && !S_ISDIR(te2->mode)) {
709 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
710 label1, repo, cb, cb_arg, diff_content);
711 if (err)
712 return err;
713 if (S_ISREG(te2->mode)) {
714 if (diff_content) {
715 err = diff_added_blob(&te2->id, f1, f2, fd2,
716 label2, te2->mode, repo, cb, cb_arg);
717 } else {
718 err = cb(cb_arg, NULL, NULL, NULL, NULL, NULL,
719 &te2->id, NULL, label2, 0, te2->mode, repo);
721 if (err)
722 return err;
726 return NULL;
729 static const struct got_error *
730 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
731 FILE *f1, FILE *f2, int fd1, int fd2,
732 const char *label1, const char *label2,
733 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
734 int diff_content)
736 const struct got_error *err = NULL;
737 int id_match;
739 if (got_object_tree_entry_is_submodule(te1))
740 return NULL;
742 if (te2 == NULL) {
743 if (S_ISDIR(te1->mode))
744 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
745 label1, repo, cb, cb_arg, diff_content);
746 else {
747 if (diff_content)
748 err = diff_deleted_blob(&te1->id, f1, fd1,
749 f2, label1, te1->mode, repo, cb, cb_arg);
750 else
751 err = cb(cb_arg, NULL, NULL, NULL, NULL,
752 &te1->id, NULL, label1, NULL,
753 te1->mode, 0, repo);
755 return err;
756 } else if (got_object_tree_entry_is_submodule(te2))
757 return NULL;
759 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
760 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
761 if (!id_match)
762 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
763 fd1, fd2, label1, label2, repo, cb, cb_arg,
764 diff_content);
765 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
766 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
767 if (!id_match ||
768 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
769 (te2->mode & (S_IFLNK | S_IXUSR))) {
770 if (diff_content)
771 return diff_modified_blob(&te1->id, &te2->id,
772 f1, f2, fd1, fd2, label1, label2,
773 te1->mode, te2->mode, repo, cb, cb_arg);
774 else
775 return cb(cb_arg, NULL, NULL, NULL, NULL,
776 &te1->id, &te2->id, label1, label2,
777 te1->mode, te2->mode, repo);
781 if (id_match)
782 return NULL;
784 return diff_kind_mismatch(te1, te2, f1, f2, fd1, fd2,
785 label1, label2, repo, cb, cb_arg, diff_content);
788 static const struct got_error *
789 diff_entry_new_old(struct got_tree_entry *te2,
790 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
791 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
792 int diff_content)
794 if (te1 != NULL) /* handled by diff_entry_old_new() */
795 return NULL;
797 if (got_object_tree_entry_is_submodule(te2))
798 return NULL;
800 if (S_ISDIR(te2->mode))
801 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
802 repo, cb, cb_arg, diff_content);
804 if (diff_content)
805 return diff_added_blob(&te2->id, f1, f2, fd2,
806 label2, te2->mode, repo, cb, cb_arg);
808 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
809 NULL, label2, 0, te2->mode, repo);
812 const struct got_error *
813 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
814 struct got_blob_object *blob2, FILE *f1, FILE *f2,
815 struct got_object_id *id1, struct got_object_id *id2,
816 const char *label1, const char *label2,
817 mode_t mode1, mode_t mode2, struct got_repository *repo)
819 const struct got_error *err = NULL;
820 struct got_diffreg_result *result = NULL;
821 struct got_diffstat_cb_arg *a = arg;
822 char *path = NULL;
823 int status = GOT_STATUS_NO_CHANGE;
825 path = strdup(label2 ? label2 : label1);
826 if (path == NULL)
827 return got_error_from_errno("strdup");
829 if (id1 == NULL)
830 status = GOT_STATUS_ADD;
831 else if (id2 == NULL)
832 status = GOT_STATUS_DELETE;
833 else {
834 if (got_object_id_cmp(id1, id2) != 0)
835 status = GOT_STATUS_MODIFY;
836 else if (mode1 != mode2)
837 status = GOT_STATUS_MODE_CHANGE;
840 if (f1) {
841 err = got_opentemp_truncate(f1);
842 if (err)
843 goto done;
845 if (f2) {
846 err = got_opentemp_truncate(f2);
847 if (err)
848 goto done;
851 if (blob1) {
852 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
853 blob1);
854 if (err)
855 goto done;
857 if (blob2) {
858 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
859 blob2);
860 if (err)
861 goto done;
864 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
865 a->force_text);
866 if (err)
867 goto done;
869 err = get_diffstat(a, path, result->result, a->force_text, status);
871 done:
872 if (result) {
873 const struct got_error *free_err;
875 free_err = got_diffreg_result_free(result);
876 if (free_err && err == NULL)
877 err = free_err;
879 if (err)
880 free(path);
881 return err;
884 const struct got_error *
885 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
886 struct got_blob_object *blob2, FILE *f1, FILE *f2,
887 struct got_object_id *id1, struct got_object_id *id2,
888 const char *label1, const char *label2,
889 mode_t mode1, mode_t mode2, struct got_repository *repo)
891 const struct got_error *err = NULL;
892 struct got_pathlist_head *paths = arg;
893 struct got_diff_changed_path *change = NULL;
894 char *path = NULL;
896 path = strdup(label2 ? label2 : label1);
897 if (path == NULL)
898 return got_error_from_errno("strdup");
900 change = malloc(sizeof(*change));
901 if (change == NULL) {
902 err = got_error_from_errno("malloc");
903 goto done;
906 change->status = GOT_STATUS_NO_CHANGE;
907 if (id1 == NULL)
908 change->status = GOT_STATUS_ADD;
909 else if (id2 == NULL)
910 change->status = GOT_STATUS_DELETE;
911 else {
912 if (got_object_id_cmp(id1, id2) != 0)
913 change->status = GOT_STATUS_MODIFY;
914 else if (mode1 != mode2)
915 change->status = GOT_STATUS_MODE_CHANGE;
918 err = got_pathlist_append(paths, path, change);
919 done:
920 if (err) {
921 free(path);
922 free(change);
924 return err;
927 const struct got_error *
928 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
929 FILE *f1, FILE *f2, int fd1, int fd2,
930 const char *label1, const char *label2,
931 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
932 int diff_content)
934 const struct got_error *err = NULL;
935 struct got_tree_entry *te1 = NULL;
936 struct got_tree_entry *te2 = NULL;
937 char *l1 = NULL, *l2 = NULL;
938 int tidx1 = 0, tidx2 = 0;
940 if (tree1) {
941 te1 = got_object_tree_get_entry(tree1, 0);
942 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
943 te1->name) == -1)
944 return got_error_from_errno("asprintf");
946 if (tree2) {
947 te2 = got_object_tree_get_entry(tree2, 0);
948 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
949 te2->name) == -1) {
950 err = got_error_from_errno("asprintf");
951 goto done;
955 do {
956 if (te1) {
957 struct got_tree_entry *te = NULL;
959 if (tree2)
960 te = got_object_tree_find_entry(tree2,
961 te1->name);
962 if (te) {
963 free(l2);
964 l2 = NULL;
965 if (te && asprintf(&l2, "%s%s%s", label2,
966 label2[0] ? "/" : "", te->name) == -1) {
967 err = got_error_from_errno("asprintf");
968 goto done;
972 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
973 l1, l2, repo, cb, cb_arg, diff_content);
974 if (err)
975 break;
978 if (te2) {
979 struct got_tree_entry *te = NULL;
981 if (tree1)
982 te = got_object_tree_find_entry(tree1,
983 te2->name);
985 free(l2);
986 l2 = NULL;
987 if (te) {
988 if (asprintf(&l2, "%s%s%s", label2,
989 label2[0] ? "/" : "", te->name) == -1) {
990 err = got_error_from_errno("asprintf");
991 goto done;
993 } else {
994 if (asprintf(&l2, "%s%s%s", label2,
995 label2[0] ? "/" : "", te2->name) == -1) {
996 err = got_error_from_errno("asprintf");
997 goto done;
1001 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
1002 repo, cb, cb_arg, diff_content);
1003 if (err)
1004 break;
1007 free(l1);
1008 l1 = NULL;
1009 if (te1) {
1010 tidx1++;
1011 te1 = got_object_tree_get_entry(tree1, tidx1);
1012 if (te1 &&
1013 asprintf(&l1, "%s%s%s", label1,
1014 label1[0] ? "/" : "", te1->name) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 goto done;
1020 free(l2);
1021 l2 = NULL;
1022 if (te2) {
1023 tidx2++;
1024 te2 = got_object_tree_get_entry(tree2, tidx2);
1025 if (te2 &&
1026 asprintf(&l2, "%s%s%s", label2,
1027 label2[0] ? "/" : "", te2->name) == -1) {
1028 err = got_error_from_errno("asprintf");
1029 goto done;
1032 } while (te1 || te2);
1034 done:
1035 free(l1);
1036 free(l2);
1037 return err;
1040 const struct got_error *
1041 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
1042 FILE *f1, FILE *f2, int fd1, int fd2,
1043 struct got_object_id *id1, struct got_object_id *id2,
1044 const char *label1, const char *label2,
1045 enum got_diff_algorithm diff_algo, int diff_context,
1046 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1047 struct got_repository *repo, FILE *outfile)
1049 const struct got_error *err;
1050 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1052 if (id1 == NULL && id2 == NULL)
1053 return got_error(GOT_ERR_NO_OBJ);
1055 if (id1) {
1056 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1057 if (err)
1058 goto done;
1060 if (id2) {
1061 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1062 if (err)
1063 goto done;
1065 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1066 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1067 ds, outfile);
1068 done:
1069 if (blob1)
1070 got_object_blob_close(blob1);
1071 if (blob2)
1072 got_object_blob_close(blob2);
1073 return err;
1076 static const struct got_error *
1077 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1078 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1079 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1081 const struct got_error *err = NULL;
1082 struct got_pathlist_entry *pe;
1083 struct got_object_id *id1 = NULL, *id2 = NULL;
1084 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1085 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1087 TAILQ_FOREACH(pe, paths, entry) {
1088 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1089 mode_t mode1 = 0, mode2 = 0;
1091 free(id1);
1092 id1 = NULL;
1093 free(id2);
1094 id2 = NULL;
1095 if (subtree1) {
1096 got_object_tree_close(subtree1);
1097 subtree1 = NULL;
1099 if (subtree2) {
1100 got_object_tree_close(subtree2);
1101 subtree2 = NULL;
1103 if (blob1) {
1104 got_object_blob_close(blob1);
1105 blob1 = NULL;
1107 if (blob2) {
1108 got_object_blob_close(blob2);
1109 blob2 = NULL;
1111 if (tree1) {
1112 err = got_object_tree_find_path(&id1, &mode1, repo,
1113 tree1, pe->path);
1114 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1115 goto done;
1117 if (tree2) {
1118 err = got_object_tree_find_path(&id2, &mode2, repo,
1119 tree2, pe->path);
1120 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1121 goto done;
1123 if (id1 == NULL && id2 == NULL) {
1124 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1125 goto done;
1127 if (id1) {
1128 err = got_object_get_type(&type1, repo, id1);
1129 if (err)
1130 goto done;
1132 if (id2) {
1133 err = got_object_get_type(&type2, repo, id2);
1134 if (err)
1135 goto done;
1137 if (type1 == GOT_OBJ_TYPE_ANY &&
1138 type2 == GOT_OBJ_TYPE_ANY) {
1139 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1140 goto done;
1141 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1142 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1143 err = got_error(GOT_ERR_OBJ_TYPE);
1144 goto done;
1147 if (type1 == GOT_OBJ_TYPE_BLOB ||
1148 type2 == GOT_OBJ_TYPE_BLOB) {
1149 if (id1) {
1150 err = got_object_open_as_blob(&blob1, repo,
1151 id1, 8192, fd1);
1152 if (err)
1153 goto done;
1155 if (id2) {
1156 err = got_object_open_as_blob(&blob2, repo,
1157 id2, 8192, fd2);
1158 if (err)
1159 goto done;
1161 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1162 id1 ? pe->path : "/dev/null",
1163 id2 ? pe->path : "/dev/null",
1164 mode1, mode2, repo);
1165 if (err)
1166 goto done;
1167 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1168 type2 == GOT_OBJ_TYPE_TREE) {
1169 if (id1) {
1170 err = got_object_open_as_tree(&subtree1, repo,
1171 id1);
1172 if (err)
1173 goto done;
1175 if (id2) {
1176 err = got_object_open_as_tree(&subtree2, repo,
1177 id2);
1178 if (err)
1179 goto done;
1181 err = got_diff_tree(subtree1, subtree2, f1, f2,
1182 fd1, fd2,
1183 id1 ? pe->path : "/dev/null",
1184 id2 ? pe->path : "/dev/null",
1185 repo, cb, cb_arg, 1);
1186 if (err)
1187 goto done;
1188 } else {
1189 err = got_error(GOT_ERR_OBJ_TYPE);
1190 goto done;
1193 done:
1194 free(id1);
1195 free(id2);
1196 if (subtree1)
1197 got_object_tree_close(subtree1);
1198 if (subtree2)
1199 got_object_tree_close(subtree2);
1200 if (blob1)
1201 got_object_blob_close(blob1);
1202 if (blob2)
1203 got_object_blob_close(blob2);
1204 return err;
1207 static const struct got_error *
1208 show_object_id(struct got_diff_line **lines, size_t *nlines,
1209 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1211 const struct got_error *err;
1212 int n;
1213 off_t outoff = 0;
1215 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1216 if (n < 0)
1217 return got_error_from_errno("fprintf");
1219 if (lines != NULL && *lines != NULL) {
1220 if (*nlines == 0) {
1221 err = add_line_metadata(lines, nlines, 0,
1222 GOT_DIFF_LINE_META);
1223 if (err)
1224 return err;
1225 } else
1226 outoff = (*lines)[*nlines - 1].offset;
1228 outoff += n;
1229 err = add_line_metadata(lines, nlines, outoff,
1230 GOT_DIFF_LINE_META);
1231 if (err)
1232 return err;
1235 return NULL;
1238 static const struct got_error *
1239 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1240 FILE *f1, FILE *f2, int fd1, int fd2,
1241 struct got_object_id *id1, struct got_object_id *id2,
1242 struct got_pathlist_head *paths, const char *label1, const char *label2,
1243 int diff_context, int ignore_whitespace, int force_text_diff,
1244 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1245 FILE *outfile, enum got_diff_algorithm diff_algo)
1247 const struct got_error *err;
1248 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1249 struct got_diff_blob_output_unidiff_arg arg;
1250 int want_linemeta = (lines != NULL && *lines != NULL);
1252 if (id1 == NULL && id2 == NULL)
1253 return got_error(GOT_ERR_NO_OBJ);
1255 if (id1) {
1256 err = got_object_open_as_tree(&tree1, repo, id1);
1257 if (err)
1258 goto done;
1260 if (id2) {
1261 err = got_object_open_as_tree(&tree2, repo, id2);
1262 if (err)
1263 goto done;
1266 arg.diff_algo = diff_algo;
1267 arg.diff_context = diff_context;
1268 arg.ignore_whitespace = ignore_whitespace;
1269 arg.force_text_diff = force_text_diff;
1270 arg.diffstat = dsa;
1271 arg.outfile = outfile;
1272 if (want_linemeta) {
1273 arg.lines = *lines;
1274 arg.nlines = *nlines;
1275 } else {
1276 arg.lines = NULL;
1277 arg.nlines = 0;
1279 if (paths == NULL || TAILQ_EMPTY(paths))
1280 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1281 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1282 else
1283 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1284 got_diff_blob_output_unidiff, &arg);
1285 if (want_linemeta) {
1286 *lines = arg.lines; /* was likely re-allocated */
1287 *nlines = arg.nlines;
1289 done:
1290 if (tree1)
1291 got_object_tree_close(tree1);
1292 if (tree2)
1293 got_object_tree_close(tree2);
1294 return err;
1297 const struct got_error *
1298 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1299 FILE *f1, FILE *f2, int fd1, int fd2,
1300 struct got_object_id *id1, struct got_object_id *id2,
1301 struct got_pathlist_head *paths, const char *label1, const char *label2,
1302 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1303 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1304 struct got_repository *repo, FILE *outfile)
1306 const struct got_error *err;
1307 char *idstr = NULL;
1309 if (id1 == NULL && id2 == NULL)
1310 return got_error(GOT_ERR_NO_OBJ);
1312 if (id1) {
1313 err = got_object_id_str(&idstr, id1);
1314 if (err)
1315 goto done;
1316 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1317 if (err)
1318 goto done;
1319 free(idstr);
1320 idstr = NULL;
1321 } else {
1322 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1323 outfile);
1324 if (err)
1325 goto done;
1328 if (id2) {
1329 err = got_object_id_str(&idstr, id2);
1330 if (err)
1331 goto done;
1332 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1333 if (err)
1334 goto done;
1335 free(idstr);
1336 idstr = NULL;
1337 } else {
1338 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1339 outfile);
1340 if (err)
1341 goto done;
1344 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1345 paths, label1, label2, diff_context, ignore_whitespace,
1346 force_text_diff, dsa, repo, outfile, diff_algo);
1347 done:
1348 free(idstr);
1349 return err;
1352 const struct got_error *
1353 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1354 FILE *f1, FILE *f2, int fd1, int fd2,
1355 struct got_object_id *id1, struct got_object_id *id2,
1356 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1357 int diff_context, int ignore_whitespace, int force_text_diff,
1358 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1360 const struct got_error *err;
1361 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1362 char *idstr = NULL;
1364 if (id2 == NULL)
1365 return got_error(GOT_ERR_NO_OBJ);
1367 if (id1) {
1368 err = got_object_open_as_commit(&commit1, repo, id1);
1369 if (err)
1370 goto done;
1371 err = got_object_id_str(&idstr, id1);
1372 if (err)
1373 goto done;
1374 err = show_object_id(lines, nlines, "commit", '-', idstr,
1375 outfile);
1376 if (err)
1377 goto done;
1378 free(idstr);
1379 idstr = NULL;
1380 } else {
1381 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1382 outfile);
1383 if (err)
1384 goto done;
1387 err = got_object_open_as_commit(&commit2, repo, id2);
1388 if (err)
1389 goto done;
1391 err = got_object_id_str(&idstr, id2);
1392 if (err)
1393 goto done;
1394 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1395 if (err)
1396 goto done;
1398 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1399 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1400 got_object_commit_get_tree_id(commit2), paths, "", "",
1401 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1402 outfile, diff_algo);
1403 done:
1404 if (commit1)
1405 got_object_commit_close(commit1);
1406 if (commit2)
1407 got_object_commit_close(commit2);
1408 free(idstr);
1409 return err;
1412 const struct got_error *
1413 got_diff_files(struct got_diffreg_result **resultp,
1414 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1415 const char *label2, int diff_context, int ignore_whitespace,
1416 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1418 const struct got_error *err = NULL;
1419 struct got_diffreg_result *diffreg_result = NULL;
1421 if (resultp)
1422 *resultp = NULL;
1424 if (outfile) {
1425 fprintf(outfile, "file - %s\n",
1426 f1_exists ? label1 : "/dev/null");
1427 fprintf(outfile, "file + %s\n",
1428 f2_exists ? label2 : "/dev/null");
1431 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1432 ignore_whitespace, force_text_diff);
1433 if (err)
1434 goto done;
1436 if (outfile) {
1437 err = got_diffreg_output(NULL, NULL, diffreg_result,
1438 f1_exists, f2_exists, label1, label2,
1439 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1440 if (err)
1441 goto done;
1444 done:
1445 if (resultp && err == NULL)
1446 *resultp = diffreg_result;
1447 else if (diffreg_result) {
1448 const struct got_error *free_err;
1450 free_err = got_diffreg_result_free(diffreg_result);
1451 if (free_err && err == NULL)
1452 err = free_err;
1455 return err;