Blob


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