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 && *nlines > 0)
150 outoff = (*lines)[*nlines - 1].offset;
151 else if (lines) {
152 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
153 if (err)
154 goto done;
157 if (resultp)
158 *resultp = NULL;
160 if (f1) {
161 err = got_opentemp_truncate(f1);
162 if (err)
163 goto done;
165 if (f2) {
166 err = got_opentemp_truncate(f2);
167 if (err)
168 goto done;
171 size1 = 0;
172 if (blob1) {
173 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
174 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
175 blob1);
176 if (err)
177 goto done;
178 } else
179 idstr1 = "/dev/null";
181 size2 = 0;
182 if (blob2) {
183 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
184 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
185 blob2);
186 if (err)
187 goto done;
188 } else
189 idstr2 = "/dev/null";
191 if (outfile) {
192 int modebits;
194 if (mode1 && mode1 != mode2) {
195 if (S_ISLNK(mode1))
196 modebits = S_IFLNK;
197 else
198 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
199 if (asprintf(&modestr1, " (mode %o)",
200 mode1 & modebits) == -1) {
201 err = got_error_from_errno("asprintf");
202 goto done;
205 if (mode2 && mode1 != mode2) {
206 if (S_ISLNK(mode2))
207 modebits = S_IFLNK;
208 else
209 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
210 if (asprintf(&modestr2, " (mode %o)",
211 mode2 & modebits) == -1) {
212 err = got_error_from_errno("asprintf");
213 goto done;
216 n = fprintf(outfile, "blob - %s%s\n", idstr1,
217 modestr1 ? modestr1 : "");
218 if (n < 0)
219 goto done;
220 outoff += n;
221 if (lines) {
222 err = add_line_metadata(lines, nlines, outoff,
223 GOT_DIFF_LINE_BLOB_MIN);
224 if (err)
225 goto done;
228 n = fprintf(outfile, "blob + %s%s\n", idstr2,
229 modestr2 ? modestr2 : "");
230 if (n < 0)
231 goto done;
232 outoff += n;
233 if (lines) {
234 err = add_line_metadata(lines, nlines, outoff,
235 GOT_DIFF_LINE_BLOB_PLUS);
236 if (err)
237 goto done;
241 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
242 force_text_diff);
243 if (err)
244 goto done;
246 if (diffstat) {
247 char *path = NULL;
248 int status = GOT_STATUS_NO_CHANGE;
250 if (blob1 == NULL)
251 status = GOT_STATUS_ADD;
252 else if (blob2 == NULL)
253 status = GOT_STATUS_DELETE;
254 else {
255 if (strcmp(idstr1, idstr2) != 0)
256 status = GOT_STATUS_MODIFY;
257 else if (mode1 != mode2)
258 status = GOT_STATUS_MODE_CHANGE;
261 if (label1 == NULL && label2 == NULL) {
262 /* diffstat of blobs, show hash instead of path */
263 if (asprintf(&path, "%.10s -> %.10s",
264 idstr1, idstr2) == -1) {
265 err = got_error_from_errno("asprintf");
266 goto done;
268 } else {
269 if (label2 != NULL &&
270 (status != GOT_STATUS_DELETE || label1 == NULL))
271 path = strdup(label2);
272 else
273 path = strdup(label1);
274 if (path == NULL) {
275 err = got_error_from_errno("strdup");
276 goto done;
280 err = get_diffstat(diffstat, path, result->result,
281 force_text_diff, status);
282 if (err) {
283 free(path);
284 goto done;
288 if (outfile) {
289 err = got_diffreg_output(lines, nlines, result,
290 blob1 != NULL, blob2 != NULL,
291 label1 ? label1 : idstr1,
292 label2 ? label2 : idstr2,
293 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
294 if (err)
295 goto done;
298 done:
299 free(modestr1);
300 free(modestr2);
301 if (resultp && err == NULL)
302 *resultp = result;
303 else if (result) {
304 free_err = got_diffreg_result_free(result);
305 if (free_err && err == NULL)
306 err = free_err;
309 return err;
312 const struct got_error *
313 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
314 struct got_blob_object *blob2, FILE *f1, FILE *f2,
315 struct got_object_id *id1, struct got_object_id *id2,
316 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 struct got_repository *repo)
319 struct got_diff_blob_output_unidiff_arg *a = arg;
321 return diff_blobs(&a->lines, &a->nlines, NULL,
322 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
323 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
324 a->diff_algo);
327 const struct got_error *
328 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
329 struct got_blob_object *blob1, struct got_blob_object *blob2,
330 FILE *f1, FILE *f2, const char *label1, const char *label2,
331 enum got_diff_algorithm diff_algo, int diff_context,
332 int ignore_whitespace, int force_text_diff,
333 struct got_diffstat_cb_arg *ds, FILE *outfile)
335 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
336 label1, label2, 0, 0, diff_context, ignore_whitespace,
337 force_text_diff, ds, outfile, diff_algo);
340 static const struct got_error *
341 diff_blob_file(struct got_diffreg_result **resultp,
342 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
343 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
344 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
345 int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
347 const struct got_error *err = NULL, *free_err;
348 char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
349 const char *idstr1 = NULL;
350 struct got_diffreg_result *result = NULL;
352 if (resultp)
353 *resultp = NULL;
355 if (blob1)
356 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
357 else
358 idstr1 = "/dev/null";
360 if (outfile) {
361 char *mode = NULL;
363 /* display file mode for new added files only */
364 if (f2_exists && blob1 == NULL) {
365 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
367 if (S_ISLNK(sb2->st_mode))
368 mmask = S_IFLNK;
369 if (asprintf(&mode, " (mode %o)",
370 sb2->st_mode & mmask) == -1)
371 return got_error_from_errno("asprintf");
373 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
374 fprintf(outfile, "file + %s%s\n",
375 f2_exists ? label2 : "/dev/null", mode ? mode : "");
376 free(mode);
379 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
380 force_text_diff);
381 if (err) {
382 char msg[GOT_ERR_MAX_MSG_SIZE];
383 if (snprintf(msg, sizeof(msg), "%s vs %s: %s",
384 label1 ? label1 : idstr1,
385 f2_exists ? label2 : "/dev/null", err->msg) >= 0) {
386 err = got_error_msg(err->code, msg);
388 goto done;
391 if (outfile) {
392 err = got_diffreg_output(NULL, NULL, result,
393 blob1 != NULL, f2_exists,
394 label2, /* show local file's path, not a blob ID */
395 label2, GOT_DIFF_OUTPUT_UNIDIFF,
396 diff_context, outfile);
397 if (err)
398 goto done;
401 if (diffstat) {
402 char *path = NULL;
403 int status = GOT_STATUS_NO_CHANGE;
405 /*
406 * Ignore 'm'ode status change: if there's no accompanying
407 * content change, there'll be no diffstat, and if there
408 * are actual changes, 'M'odified takes precedence.
409 */
410 if (blob1 == NULL)
411 status = GOT_STATUS_ADD;
412 else if (!f2_exists)
413 status = GOT_STATUS_DELETE;
414 else
415 status = GOT_STATUS_MODIFY;
417 if (label2 != NULL &&
418 (status != GOT_STATUS_DELETE || label1 == NULL))
419 path = strdup(label2);
420 else
421 path = strdup(label1);
422 if (path == NULL) {
423 err = got_error_from_errno("strdup");
424 goto done;
427 err = get_diffstat(diffstat, path, result->result,
428 force_text_diff, status);
429 if (err) {
430 free(path);
431 goto done;
435 done:
436 if (resultp && err == NULL)
437 *resultp = result;
438 else if (result) {
439 free_err = got_diffreg_result_free(result);
440 if (free_err && err == NULL)
441 err = free_err;
443 return err;
446 const struct got_error *
447 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
448 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
449 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
450 int ignore_whitespace, int force_text_diff,
451 struct got_diffstat_cb_arg *ds, FILE *outfile)
453 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
454 sb2, label2, diff_algo, diff_context, ignore_whitespace,
455 force_text_diff, ds, outfile);
458 static const struct got_error *
459 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
460 const char *label, mode_t mode, struct got_repository *repo,
461 got_diff_blob_cb cb, void *cb_arg)
463 const struct got_error *err;
464 struct got_blob_object *blob = NULL;
465 struct got_object *obj = NULL;
467 err = got_object_open(&obj, repo, id);
468 if (err)
469 return err;
471 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
472 if (err)
473 goto done;
474 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
475 NULL, label, 0, mode, repo);
476 done:
477 got_object_close(obj);
478 if (blob)
479 got_object_blob_close(blob);
480 return err;
483 static const struct got_error *
484 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
485 FILE *f1, FILE *f2, int fd1, int fd2,
486 const char *label1, const char *label2,
487 mode_t mode1, mode_t mode2, struct got_repository *repo,
488 got_diff_blob_cb cb, void *cb_arg)
490 const struct got_error *err;
491 struct got_object *obj1 = NULL;
492 struct got_object *obj2 = NULL;
493 struct got_blob_object *blob1 = NULL;
494 struct got_blob_object *blob2 = NULL;
496 err = got_object_open(&obj1, repo, id1);
497 if (err)
498 return err;
500 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
501 err = got_error(GOT_ERR_OBJ_TYPE);
502 goto done;
505 err = got_object_open(&obj2, repo, id2);
506 if (err)
507 goto done;
508 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 goto done;
513 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
514 if (err)
515 goto done;
517 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
518 if (err)
519 goto done;
521 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
522 mode1, mode2, repo);
523 done:
524 if (obj1)
525 got_object_close(obj1);
526 if (obj2)
527 got_object_close(obj2);
528 if (blob1)
529 got_object_blob_close(blob1);
530 if (blob2)
531 got_object_blob_close(blob2);
532 return err;
535 static const struct got_error *
536 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
537 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
538 got_diff_blob_cb cb, void *cb_arg)
540 const struct got_error *err;
541 struct got_blob_object *blob = NULL;
542 struct got_object *obj = NULL;
544 err = got_object_open(&obj, repo, id);
545 if (err)
546 return err;
548 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
549 if (err)
550 goto done;
551 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
552 mode, 0, repo);
553 done:
554 got_object_close(obj);
555 if (blob)
556 got_object_blob_close(blob);
557 return err;
560 static const struct got_error *
561 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
562 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
563 void *cb_arg, int diff_content)
565 const struct got_error *err = NULL;
566 struct got_object *treeobj = NULL;
567 struct got_tree_object *tree = NULL;
569 err = got_object_open(&treeobj, repo, id);
570 if (err)
571 goto done;
573 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
574 err = got_error(GOT_ERR_OBJ_TYPE);
575 goto done;
578 err = got_object_tree_open(&tree, repo, treeobj);
579 if (err)
580 goto done;
582 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
583 repo, cb, cb_arg, diff_content);
584 done:
585 if (tree)
586 got_object_tree_close(tree);
587 if (treeobj)
588 got_object_close(treeobj);
589 return err;
592 static const struct got_error *
593 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
594 FILE *f1, FILE *f2, int fd1, int fd2,
595 const char *label1, const char *label2,
596 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
597 int diff_content)
599 const struct got_error *err;
600 struct got_object *treeobj1 = NULL;
601 struct got_object *treeobj2 = NULL;
602 struct got_tree_object *tree1 = NULL;
603 struct got_tree_object *tree2 = NULL;
605 err = got_object_open(&treeobj1, repo, id1);
606 if (err)
607 goto done;
609 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 err = got_object_open(&treeobj2, repo, id2);
615 if (err)
616 goto done;
618 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
619 err = got_error(GOT_ERR_OBJ_TYPE);
620 goto done;
623 err = got_object_tree_open(&tree1, repo, treeobj1);
624 if (err)
625 goto done;
627 err = got_object_tree_open(&tree2, repo, treeobj2);
628 if (err)
629 goto done;
631 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
632 label1, label2, repo, cb, cb_arg, diff_content);
634 done:
635 if (tree1)
636 got_object_tree_close(tree1);
637 if (tree2)
638 got_object_tree_close(tree2);
639 if (treeobj1)
640 got_object_close(treeobj1);
641 if (treeobj2)
642 got_object_close(treeobj2);
643 return err;
646 static const struct got_error *
647 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
648 FILE *f2, const char *label, struct got_repository *repo,
649 got_diff_blob_cb cb, void *cb_arg, int diff_content)
651 const struct got_error *err;
652 struct got_object *treeobj = NULL;
653 struct got_tree_object *tree = NULL;
655 err = got_object_open(&treeobj, repo, id);
656 if (err)
657 goto done;
659 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
660 err = got_error(GOT_ERR_OBJ_TYPE);
661 goto done;
664 err = got_object_tree_open(&tree, repo, treeobj);
665 if (err)
666 goto done;
668 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
669 repo, cb, cb_arg, diff_content);
670 done:
671 if (tree)
672 got_object_tree_close(tree);
673 if (treeobj)
674 got_object_close(treeobj);
675 return err;
678 static const struct got_error *
679 diff_kind_mismatch(struct got_tree_entry *te1, struct got_tree_entry *te2,
680 FILE *f1, FILE *f2, int fd1, int fd2,
681 const char *label1, const char *label2, struct got_repository *repo,
682 got_diff_blob_cb cb, void *cb_arg, int diff_content)
684 const struct got_error *err = NULL;
686 /*
687 * Handle files changing into directories and vice-versa.
688 * Disregard edge cases with FIFOs, device nodes, etc for now.
689 */
690 if (!S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
691 if (S_ISREG(te1->mode)) {
692 if (diff_content) {
693 err = diff_deleted_blob(&te1->id, f1, fd1,
694 f2, label1, te1->mode, repo, cb, cb_arg);
695 } else {
696 err = cb(cb_arg, NULL, NULL, NULL, NULL,
697 &te1->id, NULL, label1, NULL,
698 te1->mode, 0, repo);
700 if (err)
701 return err;
703 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
704 repo, cb, cb_arg, diff_content);
705 } else if (S_ISDIR(te1->mode) && !S_ISDIR(te2->mode)) {
706 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
707 label1, repo, cb, cb_arg, diff_content);
708 if (err)
709 return err;
710 if (S_ISREG(te2->mode)) {
711 if (diff_content) {
712 err = diff_added_blob(&te2->id, f1, f2, fd2,
713 label2, te2->mode, repo, cb, cb_arg);
714 } else {
715 err = cb(cb_arg, NULL, NULL, NULL, NULL, NULL,
716 &te2->id, NULL, label2, 0, te2->mode, repo);
718 if (err)
719 return err;
723 return NULL;
726 static const struct got_error *
727 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
728 FILE *f1, FILE *f2, int fd1, int fd2,
729 const char *label1, const char *label2,
730 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
731 int diff_content)
733 const struct got_error *err = NULL;
734 int id_match;
736 if (got_object_tree_entry_is_submodule(te1))
737 return NULL;
739 if (te2 == NULL) {
740 if (S_ISDIR(te1->mode))
741 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
742 label1, repo, cb, cb_arg, diff_content);
743 else {
744 if (diff_content)
745 err = diff_deleted_blob(&te1->id, f1, fd1,
746 f2, label1, te1->mode, repo, cb, cb_arg);
747 else
748 err = cb(cb_arg, NULL, NULL, NULL, NULL,
749 &te1->id, NULL, label1, NULL,
750 te1->mode, 0, repo);
752 return err;
753 } else if (got_object_tree_entry_is_submodule(te2))
754 return NULL;
756 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
757 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
758 if (!id_match)
759 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
760 fd1, fd2, label1, label2, repo, cb, cb_arg,
761 diff_content);
762 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
763 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
764 if (!id_match ||
765 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
766 (te2->mode & (S_IFLNK | S_IXUSR))) {
767 if (diff_content)
768 return diff_modified_blob(&te1->id, &te2->id,
769 f1, f2, fd1, fd2, label1, label2,
770 te1->mode, te2->mode, repo, cb, cb_arg);
771 else
772 return cb(cb_arg, NULL, NULL, NULL, NULL,
773 &te1->id, &te2->id, label1, label2,
774 te1->mode, te2->mode, repo);
778 if (id_match)
779 return NULL;
781 return diff_kind_mismatch(te1, te2, f1, f2, fd1, fd2,
782 label1, label2, repo, cb, cb_arg, diff_content);
785 static const struct got_error *
786 diff_entry_new_old(struct got_tree_entry *te2,
787 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
788 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
789 int diff_content)
791 if (te1 != NULL) /* handled by diff_entry_old_new() */
792 return NULL;
794 if (got_object_tree_entry_is_submodule(te2))
795 return NULL;
797 if (S_ISDIR(te2->mode))
798 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
799 repo, cb, cb_arg, diff_content);
801 if (diff_content)
802 return diff_added_blob(&te2->id, f1, f2, fd2,
803 label2, te2->mode, repo, cb, cb_arg);
805 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
806 NULL, label2, 0, te2->mode, repo);
809 const struct got_error *
810 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
811 struct got_blob_object *blob2, FILE *f1, FILE *f2,
812 struct got_object_id *id1, struct got_object_id *id2,
813 const char *label1, const char *label2,
814 mode_t mode1, mode_t mode2, struct got_repository *repo)
816 const struct got_error *err = NULL;
817 struct got_diffreg_result *result = NULL;
818 struct got_diffstat_cb_arg *a = arg;
819 char *path = NULL;
820 int status = GOT_STATUS_NO_CHANGE;
822 path = strdup(label2 ? label2 : label1);
823 if (path == NULL)
824 return got_error_from_errno("strdup");
826 if (id1 == NULL)
827 status = GOT_STATUS_ADD;
828 else if (id2 == NULL)
829 status = GOT_STATUS_DELETE;
830 else {
831 if (got_object_id_cmp(id1, id2) != 0)
832 status = GOT_STATUS_MODIFY;
833 else if (mode1 != mode2)
834 status = GOT_STATUS_MODE_CHANGE;
837 if (f1) {
838 err = got_opentemp_truncate(f1);
839 if (err)
840 goto done;
842 if (f2) {
843 err = got_opentemp_truncate(f2);
844 if (err)
845 goto done;
848 if (blob1) {
849 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
850 blob1);
851 if (err)
852 goto done;
854 if (blob2) {
855 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
856 blob2);
857 if (err)
858 goto done;
861 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
862 a->force_text);
863 if (err)
864 goto done;
866 err = get_diffstat(a, path, result->result, a->force_text, status);
868 done:
869 if (result) {
870 const struct got_error *free_err;
872 free_err = got_diffreg_result_free(result);
873 if (free_err && err == NULL)
874 err = free_err;
876 if (err)
877 free(path);
878 return err;
881 const struct got_error *
882 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
883 struct got_blob_object *blob2, FILE *f1, FILE *f2,
884 struct got_object_id *id1, struct got_object_id *id2,
885 const char *label1, const char *label2,
886 mode_t mode1, mode_t mode2, struct got_repository *repo)
888 const struct got_error *err = NULL;
889 struct got_pathlist_head *paths = arg;
890 struct got_diff_changed_path *change = NULL;
891 char *path = NULL;
893 path = strdup(label2 ? label2 : label1);
894 if (path == NULL)
895 return got_error_from_errno("strdup");
897 change = malloc(sizeof(*change));
898 if (change == NULL) {
899 err = got_error_from_errno("malloc");
900 goto done;
903 change->status = GOT_STATUS_NO_CHANGE;
904 if (id1 == NULL)
905 change->status = GOT_STATUS_ADD;
906 else if (id2 == NULL)
907 change->status = GOT_STATUS_DELETE;
908 else {
909 if (got_object_id_cmp(id1, id2) != 0)
910 change->status = GOT_STATUS_MODIFY;
911 else if (mode1 != mode2)
912 change->status = GOT_STATUS_MODE_CHANGE;
915 err = got_pathlist_append(paths, path, change);
916 done:
917 if (err) {
918 free(path);
919 free(change);
921 return err;
924 const struct got_error *
925 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
926 FILE *f1, FILE *f2, int fd1, int fd2,
927 const char *label1, const char *label2,
928 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
929 int diff_content)
931 const struct got_error *err = NULL;
932 struct got_tree_entry *te1 = NULL;
933 struct got_tree_entry *te2 = NULL;
934 char *l1 = NULL, *l2 = NULL;
935 int tidx1 = 0, tidx2 = 0;
937 if (tree1) {
938 te1 = got_object_tree_get_entry(tree1, 0);
939 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
940 te1->name) == -1)
941 return got_error_from_errno("asprintf");
943 if (tree2) {
944 te2 = got_object_tree_get_entry(tree2, 0);
945 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
946 te2->name) == -1) {
947 err = got_error_from_errno("asprintf");
948 goto done;
952 do {
953 if (te1) {
954 struct got_tree_entry *te = NULL;
956 if (tree2)
957 te = got_object_tree_find_entry(tree2,
958 te1->name);
959 if (te) {
960 free(l2);
961 l2 = NULL;
962 if (te && asprintf(&l2, "%s%s%s", label2,
963 label2[0] ? "/" : "", te->name) == -1) {
964 err = got_error_from_errno("asprintf");
965 goto done;
969 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
970 l1, l2, repo, cb, cb_arg, diff_content);
971 if (err)
972 break;
975 if (te2) {
976 struct got_tree_entry *te = NULL;
978 if (tree1)
979 te = got_object_tree_find_entry(tree1,
980 te2->name);
982 free(l2);
983 l2 = NULL;
984 if (te) {
985 if (asprintf(&l2, "%s%s%s", label2,
986 label2[0] ? "/" : "", te->name) == -1) {
987 err = got_error_from_errno("asprintf");
988 goto done;
990 } else {
991 if (asprintf(&l2, "%s%s%s", label2,
992 label2[0] ? "/" : "", te2->name) == -1) {
993 err = got_error_from_errno("asprintf");
994 goto done;
998 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
999 repo, cb, cb_arg, diff_content);
1000 if (err)
1001 break;
1004 free(l1);
1005 l1 = NULL;
1006 if (te1) {
1007 tidx1++;
1008 te1 = got_object_tree_get_entry(tree1, tidx1);
1009 if (te1 &&
1010 asprintf(&l1, "%s%s%s", label1,
1011 label1[0] ? "/" : "", te1->name) == -1) {
1012 err = got_error_from_errno("asprintf");
1013 goto done;
1017 free(l2);
1018 l2 = NULL;
1019 if (te2) {
1020 tidx2++;
1021 te2 = got_object_tree_get_entry(tree2, tidx2);
1022 if (te2 &&
1023 asprintf(&l2, "%s%s%s", label2,
1024 label2[0] ? "/" : "", te2->name) == -1) {
1025 err = got_error_from_errno("asprintf");
1026 goto done;
1029 } while (te1 || te2);
1031 done:
1032 free(l1);
1033 free(l2);
1034 return err;
1037 const struct got_error *
1038 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
1039 FILE *f1, FILE *f2, int fd1, int fd2,
1040 struct got_object_id *id1, struct got_object_id *id2,
1041 const char *label1, const char *label2,
1042 enum got_diff_algorithm diff_algo, int diff_context,
1043 int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1044 struct got_repository *repo, FILE *outfile)
1046 const struct got_error *err;
1047 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1049 if (id1 == NULL && id2 == NULL)
1050 return got_error(GOT_ERR_NO_OBJ);
1052 if (id1) {
1053 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1054 if (err)
1055 goto done;
1057 if (id2) {
1058 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1059 if (err)
1060 goto done;
1062 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1063 diff_algo, diff_context, ignore_whitespace, force_text_diff,
1064 ds, outfile);
1065 done:
1066 if (blob1)
1067 got_object_blob_close(blob1);
1068 if (blob2)
1069 got_object_blob_close(blob2);
1070 return err;
1073 static const struct got_error *
1074 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1075 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1076 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1078 const struct got_error *err = NULL;
1079 struct got_pathlist_entry *pe;
1080 struct got_object_id *id1 = NULL, *id2 = NULL;
1081 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1082 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1084 TAILQ_FOREACH(pe, paths, entry) {
1085 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1086 mode_t mode1 = 0, mode2 = 0;
1088 free(id1);
1089 id1 = NULL;
1090 free(id2);
1091 id2 = NULL;
1092 if (subtree1) {
1093 got_object_tree_close(subtree1);
1094 subtree1 = NULL;
1096 if (subtree2) {
1097 got_object_tree_close(subtree2);
1098 subtree2 = NULL;
1100 if (blob1) {
1101 got_object_blob_close(blob1);
1102 blob1 = NULL;
1104 if (blob2) {
1105 got_object_blob_close(blob2);
1106 blob2 = NULL;
1108 if (tree1) {
1109 err = got_object_tree_find_path(&id1, &mode1, repo,
1110 tree1, pe->path);
1111 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1112 goto done;
1114 if (tree2) {
1115 err = got_object_tree_find_path(&id2, &mode2, repo,
1116 tree2, pe->path);
1117 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1118 goto done;
1120 if (id1 == NULL && id2 == NULL) {
1121 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1122 goto done;
1124 if (id1) {
1125 err = got_object_get_type(&type1, repo, id1);
1126 if (err)
1127 goto done;
1129 if (id2) {
1130 err = got_object_get_type(&type2, repo, id2);
1131 if (err)
1132 goto done;
1134 if (type1 == GOT_OBJ_TYPE_ANY &&
1135 type2 == GOT_OBJ_TYPE_ANY) {
1136 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1137 goto done;
1138 } else if (type1 != GOT_OBJ_TYPE_ANY &&
1139 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1140 err = got_error(GOT_ERR_OBJ_TYPE);
1141 goto done;
1144 if (type1 == GOT_OBJ_TYPE_BLOB ||
1145 type2 == GOT_OBJ_TYPE_BLOB) {
1146 if (id1) {
1147 err = got_object_open_as_blob(&blob1, repo,
1148 id1, 8192, fd1);
1149 if (err)
1150 goto done;
1152 if (id2) {
1153 err = got_object_open_as_blob(&blob2, repo,
1154 id2, 8192, fd2);
1155 if (err)
1156 goto done;
1158 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1159 id1 ? pe->path : "/dev/null",
1160 id2 ? pe->path : "/dev/null",
1161 mode1, mode2, repo);
1162 if (err)
1163 goto done;
1164 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1165 type2 == GOT_OBJ_TYPE_TREE) {
1166 if (id1) {
1167 err = got_object_open_as_tree(&subtree1, repo,
1168 id1);
1169 if (err)
1170 goto done;
1172 if (id2) {
1173 err = got_object_open_as_tree(&subtree2, repo,
1174 id2);
1175 if (err)
1176 goto done;
1178 err = got_diff_tree(subtree1, subtree2, f1, f2,
1179 fd1, fd2,
1180 id1 ? pe->path : "/dev/null",
1181 id2 ? pe->path : "/dev/null",
1182 repo, cb, cb_arg, 1);
1183 if (err)
1184 goto done;
1185 } else {
1186 err = got_error(GOT_ERR_OBJ_TYPE);
1187 goto done;
1190 done:
1191 free(id1);
1192 free(id2);
1193 if (subtree1)
1194 got_object_tree_close(subtree1);
1195 if (subtree2)
1196 got_object_tree_close(subtree2);
1197 if (blob1)
1198 got_object_blob_close(blob1);
1199 if (blob2)
1200 got_object_blob_close(blob2);
1201 return err;
1204 static const struct got_error *
1205 show_object_id(struct got_diff_line **lines, size_t *nlines,
1206 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1208 const struct got_error *err;
1209 int n;
1210 off_t outoff = 0;
1212 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1213 if (n < 0)
1214 return got_error_from_errno("fprintf");
1216 if (lines != NULL && *lines != NULL) {
1217 if (*nlines == 0) {
1218 err = add_line_metadata(lines, nlines, 0,
1219 GOT_DIFF_LINE_META);
1220 if (err)
1221 return err;
1222 } else
1223 outoff = (*lines)[*nlines - 1].offset;
1225 outoff += n;
1226 err = add_line_metadata(lines, nlines, outoff,
1227 GOT_DIFF_LINE_META);
1228 if (err)
1229 return err;
1232 return NULL;
1235 static const struct got_error *
1236 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1237 FILE *f1, FILE *f2, int fd1, int fd2,
1238 struct got_object_id *id1, struct got_object_id *id2,
1239 struct got_pathlist_head *paths, const char *label1, const char *label2,
1240 int diff_context, int ignore_whitespace, int force_text_diff,
1241 struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1242 FILE *outfile, enum got_diff_algorithm diff_algo)
1244 const struct got_error *err;
1245 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1246 struct got_diff_blob_output_unidiff_arg arg;
1247 int want_linemeta = (lines != NULL && *lines != NULL);
1249 if (id1 == NULL && id2 == NULL)
1250 return got_error(GOT_ERR_NO_OBJ);
1252 if (id1) {
1253 err = got_object_open_as_tree(&tree1, repo, id1);
1254 if (err)
1255 goto done;
1257 if (id2) {
1258 err = got_object_open_as_tree(&tree2, repo, id2);
1259 if (err)
1260 goto done;
1263 arg.diff_algo = diff_algo;
1264 arg.diff_context = diff_context;
1265 arg.ignore_whitespace = ignore_whitespace;
1266 arg.force_text_diff = force_text_diff;
1267 arg.diffstat = dsa;
1268 arg.outfile = outfile;
1269 if (want_linemeta) {
1270 arg.lines = *lines;
1271 arg.nlines = *nlines;
1272 } else {
1273 arg.lines = NULL;
1274 arg.nlines = 0;
1276 if (paths == NULL || TAILQ_EMPTY(paths))
1277 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1278 label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1279 else
1280 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1281 got_diff_blob_output_unidiff, &arg);
1282 if (want_linemeta) {
1283 *lines = arg.lines; /* was likely re-allocated */
1284 *nlines = arg.nlines;
1286 done:
1287 if (tree1)
1288 got_object_tree_close(tree1);
1289 if (tree2)
1290 got_object_tree_close(tree2);
1291 return err;
1294 const struct got_error *
1295 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1296 FILE *f1, FILE *f2, int fd1, int fd2,
1297 struct got_object_id *id1, struct got_object_id *id2,
1298 struct got_pathlist_head *paths, const char *label1, const char *label2,
1299 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1300 int force_text_diff, struct got_diffstat_cb_arg *dsa,
1301 struct got_repository *repo, FILE *outfile)
1303 const struct got_error *err;
1304 char *idstr = NULL;
1306 if (id1 == NULL && id2 == NULL)
1307 return got_error(GOT_ERR_NO_OBJ);
1309 if (id1) {
1310 err = got_object_id_str(&idstr, id1);
1311 if (err)
1312 goto done;
1313 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1314 if (err)
1315 goto done;
1316 free(idstr);
1317 idstr = NULL;
1318 } else {
1319 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1320 outfile);
1321 if (err)
1322 goto done;
1325 if (id2) {
1326 err = got_object_id_str(&idstr, id2);
1327 if (err)
1328 goto done;
1329 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1330 if (err)
1331 goto done;
1332 free(idstr);
1333 idstr = NULL;
1334 } else {
1335 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1336 outfile);
1337 if (err)
1338 goto done;
1341 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1342 paths, label1, label2, diff_context, ignore_whitespace,
1343 force_text_diff, dsa, repo, outfile, diff_algo);
1344 done:
1345 free(idstr);
1346 return err;
1349 const struct got_error *
1350 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1351 FILE *f1, FILE *f2, int fd1, int fd2,
1352 struct got_object_id *id1, struct got_object_id *id2,
1353 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1354 int diff_context, int ignore_whitespace, int force_text_diff,
1355 struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1357 const struct got_error *err;
1358 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1359 char *idstr = NULL;
1361 if (id2 == NULL)
1362 return got_error(GOT_ERR_NO_OBJ);
1364 if (id1) {
1365 err = got_object_open_as_commit(&commit1, repo, id1);
1366 if (err)
1367 goto done;
1368 err = got_object_id_str(&idstr, id1);
1369 if (err)
1370 goto done;
1371 err = show_object_id(lines, nlines, "commit", '-', idstr,
1372 outfile);
1373 if (err)
1374 goto done;
1375 free(idstr);
1376 idstr = NULL;
1377 } else {
1378 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1379 outfile);
1380 if (err)
1381 goto done;
1384 err = got_object_open_as_commit(&commit2, repo, id2);
1385 if (err)
1386 goto done;
1388 err = got_object_id_str(&idstr, id2);
1389 if (err)
1390 goto done;
1391 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1392 if (err)
1393 goto done;
1395 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1396 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1397 got_object_commit_get_tree_id(commit2), paths, "", "",
1398 diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1399 outfile, diff_algo);
1400 done:
1401 if (commit1)
1402 got_object_commit_close(commit1);
1403 if (commit2)
1404 got_object_commit_close(commit2);
1405 free(idstr);
1406 return err;
1409 const struct got_error *
1410 got_diff_files(struct got_diffreg_result **resultp,
1411 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1412 const char *label2, int diff_context, int ignore_whitespace,
1413 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1415 const struct got_error *err = NULL;
1416 struct got_diffreg_result *diffreg_result = NULL;
1418 if (resultp)
1419 *resultp = NULL;
1421 if (outfile) {
1422 fprintf(outfile, "file - %s\n",
1423 f1_exists ? label1 : "/dev/null");
1424 fprintf(outfile, "file + %s\n",
1425 f2_exists ? label2 : "/dev/null");
1428 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1429 ignore_whitespace, force_text_diff);
1430 if (err)
1431 goto done;
1433 if (outfile) {
1434 err = got_diffreg_output(NULL, NULL, diffreg_result,
1435 f1_exists, f2_exists, label1, label2,
1436 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1437 if (err)
1438 goto done;
1441 done:
1442 if (resultp && err == NULL)
1443 *resultp = diffreg_result;
1444 else if (diffreg_result) {
1445 const struct got_error *free_err;
1447 free_err = got_diffreg_result_free(diffreg_result);
1448 if (free_err && err == NULL)
1449 err = free_err;
1452 return err;