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 <zlib.h>
26 #include "got_compat.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 const struct got_error *
64 diff_blobs(struct got_diff_line **lines, size_t *nlines,
65 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
66 struct got_blob_object *blob2, FILE *f1, FILE *f2,
67 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
68 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
69 enum got_diff_algorithm diff_algo)
70 {
71 const struct got_error *err = NULL, *free_err;
72 char hex1[SHA1_DIGEST_STRING_LENGTH];
73 char hex2[SHA1_DIGEST_STRING_LENGTH];
74 const char *idstr1 = NULL, *idstr2 = NULL;
75 off_t size1, size2;
76 struct got_diffreg_result *result = NULL;
77 off_t outoff = 0;
78 int n;
80 if (lines && *lines && *nlines > 0)
81 outoff = (*lines)[*nlines - 1].offset;
82 else if (lines) {
83 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
84 if (err)
85 goto done;
86 }
88 if (resultp)
89 *resultp = NULL;
91 if (f1) {
92 err = got_opentemp_truncate(f1);
93 if (err)
94 goto done;
95 }
96 if (f2) {
97 err = got_opentemp_truncate(f2);
98 if (err)
99 goto done;
102 size1 = 0;
103 if (blob1) {
104 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
105 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
106 blob1);
107 if (err)
108 goto done;
109 } else
110 idstr1 = "/dev/null";
112 size2 = 0;
113 if (blob2) {
114 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
115 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
116 blob2);
117 if (err)
118 goto done;
119 } else
120 idstr2 = "/dev/null";
122 if (outfile) {
123 char *modestr1 = NULL, *modestr2 = NULL;
124 int modebits;
125 if (mode1 && mode1 != mode2) {
126 if (S_ISLNK(mode1))
127 modebits = S_IFLNK;
128 else
129 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
130 if (asprintf(&modestr1, " (mode %o)",
131 mode1 & modebits) == -1) {
132 err = got_error_from_errno("asprintf");
133 goto done;
136 if (mode2 && mode1 != mode2) {
137 if (S_ISLNK(mode2))
138 modebits = S_IFLNK;
139 else
140 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
141 if (asprintf(&modestr2, " (mode %o)",
142 mode2 & modebits) == -1) {
143 err = got_error_from_errno("asprintf");
144 goto done;
147 n = fprintf(outfile, "blob - %s%s\n", idstr1,
148 modestr1 ? modestr1 : "");
149 if (n < 0)
150 goto done;
151 outoff += n;
152 if (lines) {
153 err = add_line_metadata(lines, nlines, outoff,
154 GOT_DIFF_LINE_BLOB_MIN);
155 if (err)
156 goto done;
159 n = fprintf(outfile, "blob + %s%s\n", idstr2,
160 modestr2 ? modestr2 : "");
161 if (n < 0)
162 goto done;
163 outoff += n;
164 if (lines) {
165 err = add_line_metadata(lines, nlines, outoff,
166 GOT_DIFF_LINE_BLOB_PLUS);
167 if (err)
168 goto done;
171 free(modestr1);
172 free(modestr2);
174 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
175 force_text_diff);
176 if (err)
177 goto done;
179 if (outfile) {
180 err = got_diffreg_output(lines, nlines, result,
181 blob1 != NULL, blob2 != NULL,
182 label1 ? label1 : idstr1,
183 label2 ? label2 : idstr2,
184 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
185 if (err)
186 goto done;
189 done:
190 if (resultp && err == NULL)
191 *resultp = result;
192 else if (result) {
193 free_err = got_diffreg_result_free(result);
194 if (free_err && err == NULL)
195 err = free_err;
198 return err;
201 const struct got_error *
202 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
203 struct got_blob_object *blob2, FILE *f1, FILE *f2,
204 struct got_object_id *id1, struct got_object_id *id2,
205 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
206 struct got_repository *repo)
208 struct got_diff_blob_output_unidiff_arg *a = arg;
210 return diff_blobs(&a->lines, &a->nlines, NULL,
211 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
212 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
215 const struct got_error *
216 got_diff_blob(struct got_diff_line **lines, size_t*nlines,
217 struct got_blob_object *blob1, struct got_blob_object *blob2,
218 FILE *f1, FILE *f2, const char *label1, const char *label2,
219 enum got_diff_algorithm diff_algo, int diff_context,
220 int ignore_whitespace, int force_text_diff, FILE *outfile)
222 return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
223 label1, label2, 0, 0, diff_context, ignore_whitespace,
224 force_text_diff, outfile, diff_algo);
227 static const struct got_error *
228 diff_blob_file(struct got_diffreg_result **resultp,
229 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
230 FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
231 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
232 int force_text_diff, FILE *outfile)
234 const struct got_error *err = NULL, *free_err;
235 char hex1[SHA1_DIGEST_STRING_LENGTH];
236 const char *idstr1 = NULL;
237 struct got_diffreg_result *result = NULL;
239 if (resultp)
240 *resultp = NULL;
242 if (blob1)
243 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
244 else
245 idstr1 = "/dev/null";
247 if (outfile) {
248 char *mode = NULL;
250 /* display file mode for new added files only */
251 if (f2_exists && blob1 == NULL) {
252 int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
254 if (S_ISLNK(sb2->st_mode))
255 mmask = S_IFLNK;
256 if (asprintf(&mode, " (mode %o)",
257 sb2->st_mode & mmask) == -1)
258 return got_error_from_errno("asprintf");
260 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
261 fprintf(outfile, "file + %s%s\n",
262 f2_exists ? label2 : "/dev/null", mode ? mode : "");
263 free(mode);
266 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
267 force_text_diff);
268 if (err)
269 goto done;
271 if (outfile) {
272 err = got_diffreg_output(NULL, NULL, result,
273 blob1 != NULL, f2_exists,
274 label2, /* show local file's path, not a blob ID */
275 label2, GOT_DIFF_OUTPUT_UNIDIFF,
276 diff_context, outfile);
277 if (err)
278 goto done;
281 done:
282 if (resultp && err == NULL)
283 *resultp = result;
284 else if (result) {
285 free_err = got_diffreg_result_free(result);
286 if (free_err && err == NULL)
287 err = free_err;
289 return err;
292 const struct got_error *
293 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
294 const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
295 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
296 int ignore_whitespace, int force_text_diff, FILE *outfile)
298 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
299 sb2, label2, diff_algo, diff_context, ignore_whitespace,
300 force_text_diff, outfile);
303 static const struct got_error *
304 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
305 const char *label, mode_t mode, struct got_repository *repo,
306 got_diff_blob_cb cb, void *cb_arg)
308 const struct got_error *err;
309 struct got_blob_object *blob = NULL;
310 struct got_object *obj = NULL;
312 err = got_object_open(&obj, repo, id);
313 if (err)
314 return err;
316 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
317 if (err)
318 goto done;
319 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
320 NULL, label, 0, mode, repo);
321 done:
322 got_object_close(obj);
323 if (blob)
324 got_object_blob_close(blob);
325 return err;
328 static const struct got_error *
329 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
330 FILE *f1, FILE *f2, int fd1, int fd2,
331 const char *label1, const char *label2,
332 mode_t mode1, mode_t mode2, struct got_repository *repo,
333 got_diff_blob_cb cb, void *cb_arg)
335 const struct got_error *err;
336 struct got_object *obj1 = NULL;
337 struct got_object *obj2 = NULL;
338 struct got_blob_object *blob1 = NULL;
339 struct got_blob_object *blob2 = NULL;
341 err = got_object_open(&obj1, repo, id1);
342 if (err)
343 return err;
345 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
346 err = got_error(GOT_ERR_OBJ_TYPE);
347 goto done;
350 err = got_object_open(&obj2, repo, id2);
351 if (err)
352 goto done;
353 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
354 err = got_error(GOT_ERR_BAD_OBJ_DATA);
355 goto done;
358 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
359 if (err)
360 goto done;
362 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
363 if (err)
364 goto done;
366 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
367 mode1, mode2, repo);
368 done:
369 if (obj1)
370 got_object_close(obj1);
371 if (obj2)
372 got_object_close(obj2);
373 if (blob1)
374 got_object_blob_close(blob1);
375 if (blob2)
376 got_object_blob_close(blob2);
377 return err;
380 static const struct got_error *
381 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
382 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
383 got_diff_blob_cb cb, void *cb_arg)
385 const struct got_error *err;
386 struct got_blob_object *blob = NULL;
387 struct got_object *obj = NULL;
389 err = got_object_open(&obj, repo, id);
390 if (err)
391 return err;
393 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
394 if (err)
395 goto done;
396 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
397 mode, 0, repo);
398 done:
399 got_object_close(obj);
400 if (blob)
401 got_object_blob_close(blob);
402 return err;
405 static const struct got_error *
406 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
407 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
408 void *cb_arg, int diff_content)
410 const struct got_error *err = NULL;
411 struct got_object *treeobj = NULL;
412 struct got_tree_object *tree = NULL;
414 err = got_object_open(&treeobj, repo, id);
415 if (err)
416 goto done;
418 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
419 err = got_error(GOT_ERR_OBJ_TYPE);
420 goto done;
423 err = got_object_tree_open(&tree, repo, treeobj);
424 if (err)
425 goto done;
427 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
428 repo, cb, cb_arg, diff_content);
429 done:
430 if (tree)
431 got_object_tree_close(tree);
432 if (treeobj)
433 got_object_close(treeobj);
434 return err;
437 static const struct got_error *
438 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
439 FILE *f1, FILE *f2, int fd1, int fd2,
440 const char *label1, const char *label2,
441 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
442 int diff_content)
444 const struct got_error *err;
445 struct got_object *treeobj1 = NULL;
446 struct got_object *treeobj2 = NULL;
447 struct got_tree_object *tree1 = NULL;
448 struct got_tree_object *tree2 = NULL;
450 err = got_object_open(&treeobj1, repo, id1);
451 if (err)
452 goto done;
454 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
455 err = got_error(GOT_ERR_OBJ_TYPE);
456 goto done;
459 err = got_object_open(&treeobj2, repo, id2);
460 if (err)
461 goto done;
463 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
464 err = got_error(GOT_ERR_OBJ_TYPE);
465 goto done;
468 err = got_object_tree_open(&tree1, repo, treeobj1);
469 if (err)
470 goto done;
472 err = got_object_tree_open(&tree2, repo, treeobj2);
473 if (err)
474 goto done;
476 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
477 label1, label2, repo, cb, cb_arg, diff_content);
479 done:
480 if (tree1)
481 got_object_tree_close(tree1);
482 if (tree2)
483 got_object_tree_close(tree2);
484 if (treeobj1)
485 got_object_close(treeobj1);
486 if (treeobj2)
487 got_object_close(treeobj2);
488 return err;
491 static const struct got_error *
492 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
493 FILE *f2, const char *label, struct got_repository *repo,
494 got_diff_blob_cb cb, void *cb_arg, int diff_content)
496 const struct got_error *err;
497 struct got_object *treeobj = NULL;
498 struct got_tree_object *tree = NULL;
500 err = got_object_open(&treeobj, repo, id);
501 if (err)
502 goto done;
504 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
505 err = got_error(GOT_ERR_OBJ_TYPE);
506 goto done;
509 err = got_object_tree_open(&tree, repo, treeobj);
510 if (err)
511 goto done;
513 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
514 repo, cb, cb_arg, diff_content);
515 done:
516 if (tree)
517 got_object_tree_close(tree);
518 if (treeobj)
519 got_object_close(treeobj);
520 return err;
523 static const struct got_error *
524 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
525 const char *label1, const char *label2, struct got_repository *repo,
526 got_diff_blob_cb cb, void *cb_arg)
528 /* XXX TODO */
529 return NULL;
532 static const struct got_error *
533 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
534 FILE *f1, FILE *f2, int fd1, int fd2,
535 const char *label1, const char *label2,
536 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
537 int diff_content)
539 const struct got_error *err = NULL;
540 int id_match;
542 if (got_object_tree_entry_is_submodule(te1))
543 return NULL;
545 if (te2 == NULL) {
546 if (S_ISDIR(te1->mode))
547 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
548 label1, repo, cb, cb_arg, diff_content);
549 else {
550 if (diff_content)
551 err = diff_deleted_blob(&te1->id, f1, fd1,
552 f2, label1, te1->mode, repo, cb, cb_arg);
553 else
554 err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 &te1->id, NULL, label1, NULL,
556 te1->mode, 0, repo);
558 return err;
559 } else if (got_object_tree_entry_is_submodule(te2))
560 return NULL;
562 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
563 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
564 if (!id_match)
565 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
566 fd1, fd2, label1, label2, repo, cb, cb_arg,
567 diff_content);
568 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
569 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
570 if (!id_match ||
571 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
572 (te2->mode & (S_IFLNK | S_IXUSR))) {
573 if (diff_content)
574 return diff_modified_blob(&te1->id, &te2->id,
575 f1, f2, fd1, fd2, label1, label2,
576 te1->mode, te2->mode, repo, cb, cb_arg);
577 else
578 return cb(cb_arg, NULL, NULL, NULL, NULL,
579 &te1->id, &te2->id, label1, label2,
580 te1->mode, te2->mode, repo);
584 if (id_match)
585 return NULL;
587 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
588 cb, cb_arg);
591 static const struct got_error *
592 diff_entry_new_old(struct got_tree_entry *te2,
593 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
594 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
595 int diff_content)
597 if (te1 != NULL) /* handled by diff_entry_old_new() */
598 return NULL;
600 if (got_object_tree_entry_is_submodule(te2))
601 return NULL;
603 if (S_ISDIR(te2->mode))
604 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
605 repo, cb, cb_arg, diff_content);
607 if (diff_content)
608 return diff_added_blob(&te2->id, f1, f2, fd2,
609 label2, te2->mode, repo, cb, cb_arg);
611 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
612 NULL, label2, 0, te2->mode, repo);
615 static void
616 diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
617 uint32_t add, uint32_t rm)
619 int d1 = 1, d2 = 1;
621 *maxlen = MAX(*maxlen, len);
623 while (add /= 10)
624 ++d1;
625 *add_cols = MAX(*add_cols, d1);
627 while (rm /= 10)
628 ++d2;
629 *rm_cols = MAX(*rm_cols, d2);
632 const struct got_error *
633 got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
634 struct got_blob_object *blob2, FILE *f1, FILE *f2,
635 struct got_object_id *id1, struct got_object_id *id2,
636 const char *label1, const char *label2,
637 mode_t mode1, mode_t mode2, struct got_repository *repo)
639 const struct got_error *err = NULL;
640 struct got_diffreg_result *result = NULL;
641 struct diff_result *r;
642 struct got_diff_changed_path *change = NULL;
643 struct got_diffstat_cb_arg *a = arg;
644 struct got_pathlist_entry *pe;
645 char *path = NULL;
646 int i;
648 path = strdup(label2 ? label2 : label1);
649 if (path == NULL)
650 return got_error_from_errno("malloc");
652 change = malloc(sizeof(*change));
653 if (change == NULL) {
654 err = got_error_from_errno("malloc");
655 goto done;
658 change->add = 0;
659 change->rm = 0;
660 change->status = GOT_STATUS_NO_CHANGE;
661 if (id1 == NULL)
662 change->status = GOT_STATUS_ADD;
663 else if (id2 == NULL)
664 change->status = GOT_STATUS_DELETE;
665 else {
666 if (got_object_id_cmp(id1, id2) != 0)
667 change->status = GOT_STATUS_MODIFY;
668 else if (mode1 != mode2)
669 change->status = GOT_STATUS_MODE_CHANGE;
672 if (f1) {
673 err = got_opentemp_truncate(f1);
674 if (err)
675 goto done;
677 if (f2) {
678 err = got_opentemp_truncate(f2);
679 if (err)
680 goto done;
683 if (blob1) {
684 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
685 blob1);
686 if (err)
687 goto done;
689 if (blob2) {
690 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
691 blob2);
692 if (err)
693 goto done;
696 err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
697 a->force_text);
698 if (err)
699 goto done;
701 for (i = 0, r = result->result; i < r->chunks.len; ++i) {
702 int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
703 int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
705 if (!isbin || a->force_text) {
706 struct diff_chunk *c;
707 int clc, crc;
709 c = diff_chunk_get(r, i);
710 clc = diff_chunk_get_left_count(c);
711 crc = diff_chunk_get_right_count(c);
713 if (clc && !crc)
714 change->rm += clc;
715 else if (crc && !clc)
716 change->add += crc;
720 err = got_pathlist_append(a->paths, path, change);
721 if (err)
722 goto done;
724 pe = TAILQ_LAST(a->paths, got_pathlist_head);
725 diffstat_field_width(&a->max_path_len, &a->add_cols, &a->rm_cols,
726 pe->path_len, change->add, change->rm);
727 a->ins += change->add;
728 a->del += change->rm;
729 ++a->nfiles;
731 done:
732 if (result) {
733 const struct got_error *free_err;
735 free_err = got_diffreg_result_free(result);
736 if (free_err && err == NULL)
737 err = free_err;
739 if (err) {
740 free(path);
741 free(change);
743 return err;
746 const struct got_error *
747 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
748 struct got_blob_object *blob2, FILE *f1, FILE *f2,
749 struct got_object_id *id1, struct got_object_id *id2,
750 const char *label1, const char *label2,
751 mode_t mode1, mode_t mode2, struct got_repository *repo)
753 const struct got_error *err = NULL;
754 struct got_pathlist_head *paths = arg;
755 struct got_diff_changed_path *change = NULL;
756 char *path = NULL;
758 path = strdup(label2 ? label2 : label1);
759 if (path == NULL)
760 return got_error_from_errno("malloc");
762 change = malloc(sizeof(*change));
763 if (change == NULL) {
764 err = got_error_from_errno("malloc");
765 goto done;
768 change->status = GOT_STATUS_NO_CHANGE;
769 if (id1 == NULL)
770 change->status = GOT_STATUS_ADD;
771 else if (id2 == NULL)
772 change->status = GOT_STATUS_DELETE;
773 else {
774 if (got_object_id_cmp(id1, id2) != 0)
775 change->status = GOT_STATUS_MODIFY;
776 else if (mode1 != mode2)
777 change->status = GOT_STATUS_MODE_CHANGE;
780 err = got_pathlist_append(paths, path, change);
781 done:
782 if (err) {
783 free(path);
784 free(change);
786 return err;
789 const struct got_error *
790 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
791 FILE *f1, FILE *f2, int fd1, int fd2,
792 const char *label1, const char *label2,
793 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
794 int diff_content)
796 const struct got_error *err = NULL;
797 struct got_tree_entry *te1 = NULL;
798 struct got_tree_entry *te2 = NULL;
799 char *l1 = NULL, *l2 = NULL;
800 int tidx1 = 0, tidx2 = 0;
802 if (tree1) {
803 te1 = got_object_tree_get_entry(tree1, 0);
804 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
805 te1->name) == -1)
806 return got_error_from_errno("asprintf");
808 if (tree2) {
809 te2 = got_object_tree_get_entry(tree2, 0);
810 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
811 te2->name) == -1)
812 return got_error_from_errno("asprintf");
815 do {
816 if (te1) {
817 struct got_tree_entry *te = NULL;
818 if (tree2)
819 te = got_object_tree_find_entry(tree2,
820 te1->name);
821 if (te) {
822 free(l2);
823 l2 = NULL;
824 if (te && asprintf(&l2, "%s%s%s", label2,
825 label2[0] ? "/" : "", te->name) == -1)
826 return
827 got_error_from_errno("asprintf");
829 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
830 l1, l2, repo, cb, cb_arg, diff_content);
831 if (err)
832 break;
835 if (te2) {
836 struct got_tree_entry *te = NULL;
837 if (tree1)
838 te = got_object_tree_find_entry(tree1,
839 te2->name);
840 free(l2);
841 if (te) {
842 if (asprintf(&l2, "%s%s%s", label2,
843 label2[0] ? "/" : "", te->name) == -1)
844 return
845 got_error_from_errno("asprintf");
846 } else {
847 if (asprintf(&l2, "%s%s%s", label2,
848 label2[0] ? "/" : "", te2->name) == -1)
849 return
850 got_error_from_errno("asprintf");
852 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
853 repo, cb, cb_arg, diff_content);
854 if (err)
855 break;
858 free(l1);
859 l1 = NULL;
860 if (te1) {
861 tidx1++;
862 te1 = got_object_tree_get_entry(tree1, tidx1);
863 if (te1 &&
864 asprintf(&l1, "%s%s%s", label1,
865 label1[0] ? "/" : "", te1->name) == -1)
866 return got_error_from_errno("asprintf");
868 free(l2);
869 l2 = NULL;
870 if (te2) {
871 tidx2++;
872 te2 = got_object_tree_get_entry(tree2, tidx2);
873 if (te2 &&
874 asprintf(&l2, "%s%s%s", label2,
875 label2[0] ? "/" : "", te2->name) == -1)
876 return got_error_from_errno("asprintf");
878 } while (te1 || te2);
880 return err;
883 const struct got_error *
884 got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
885 FILE *f1, FILE *f2, int fd1, int fd2,
886 struct got_object_id *id1, struct got_object_id *id2,
887 const char *label1, const char *label2,
888 enum got_diff_algorithm diff_algo, int diff_context,
889 int ignore_whitespace, int force_text_diff,
890 struct got_repository *repo, FILE *outfile)
892 const struct got_error *err;
893 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
895 if (id1 == NULL && id2 == NULL)
896 return got_error(GOT_ERR_NO_OBJ);
898 if (id1) {
899 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
900 if (err)
901 goto done;
903 if (id2) {
904 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
905 if (err)
906 goto done;
908 err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
909 diff_algo, diff_context, ignore_whitespace, force_text_diff,
910 outfile);
911 done:
912 if (blob1)
913 got_object_blob_close(blob1);
914 if (blob2)
915 got_object_blob_close(blob2);
916 return err;
919 static const struct got_error *
920 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
921 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
922 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
924 const struct got_error *err = NULL;
925 struct got_pathlist_entry *pe;
926 struct got_object_id *id1 = NULL, *id2 = NULL;
927 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
928 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
930 TAILQ_FOREACH(pe, paths, entry) {
931 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
932 mode_t mode1 = 0, mode2 = 0;
934 free(id1);
935 id1 = NULL;
936 free(id2);
937 id2 = NULL;
938 if (subtree1) {
939 got_object_tree_close(subtree1);
940 subtree1 = NULL;
942 if (subtree2) {
943 got_object_tree_close(subtree2);
944 subtree2 = NULL;
946 if (blob1) {
947 got_object_blob_close(blob1);
948 blob1 = NULL;
950 if (blob2) {
951 got_object_blob_close(blob2);
952 blob2 = NULL;
955 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
956 pe->path);
957 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
958 goto done;
959 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
960 pe->path);
961 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
962 goto done;
963 if (id1 == NULL && id2 == NULL) {
964 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
965 goto done;
967 if (id1) {
968 err = got_object_get_type(&type1, repo, id1);
969 if (err)
970 goto done;
972 if (id2) {
973 err = got_object_get_type(&type2, repo, id2);
974 if (err)
975 goto done;
977 if (type1 == GOT_OBJ_TYPE_ANY &&
978 type2 == GOT_OBJ_TYPE_ANY) {
979 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
980 goto done;
981 } else if (type1 != GOT_OBJ_TYPE_ANY &&
982 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
983 err = got_error(GOT_ERR_OBJ_TYPE);
984 goto done;
987 if (type1 == GOT_OBJ_TYPE_BLOB ||
988 type2 == GOT_OBJ_TYPE_BLOB) {
989 if (id1) {
990 err = got_object_open_as_blob(&blob1, repo,
991 id1, 8192, fd1);
992 if (err)
993 goto done;
995 if (id2) {
996 err = got_object_open_as_blob(&blob2, repo,
997 id2, 8192, fd2);
998 if (err)
999 goto done;
1001 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1002 id1 ? pe->path : "/dev/null",
1003 id2 ? pe->path : "/dev/null",
1004 mode1, mode2, repo);
1005 if (err)
1006 goto done;
1007 } else if (type1 == GOT_OBJ_TYPE_TREE ||
1008 type2 == GOT_OBJ_TYPE_TREE) {
1009 if (id1) {
1010 err = got_object_open_as_tree(&subtree1, repo,
1011 id1);
1012 if (err)
1013 goto done;
1015 if (id2) {
1016 err = got_object_open_as_tree(&subtree2, repo,
1017 id2);
1018 if (err)
1019 goto done;
1021 err = got_diff_tree(subtree1, subtree2, f1, f2,
1022 fd1, fd2,
1023 id1 ? pe->path : "/dev/null",
1024 id2 ? pe->path : "/dev/null",
1025 repo, cb, cb_arg, 1);
1026 if (err)
1027 goto done;
1028 } else {
1029 err = got_error(GOT_ERR_OBJ_TYPE);
1030 goto done;
1033 done:
1034 free(id1);
1035 free(id2);
1036 if (subtree1)
1037 got_object_tree_close(subtree1);
1038 if (subtree2)
1039 got_object_tree_close(subtree2);
1040 if (blob1)
1041 got_object_blob_close(blob1);
1042 if (blob2)
1043 got_object_blob_close(blob2);
1044 return err;
1047 static const struct got_error *
1048 show_object_id(struct got_diff_line **lines, size_t *nlines,
1049 const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1051 const struct got_error *err;
1052 int n;
1053 off_t outoff = 0;
1055 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1056 if (n < 0)
1057 return got_error_from_errno("fprintf");
1059 if (lines != NULL && *lines != NULL) {
1060 if (*nlines == 0) {
1061 err = add_line_metadata(lines, nlines, 0,
1062 GOT_DIFF_LINE_META);
1063 if (err)
1064 return err;
1065 } else
1066 outoff = (*lines)[*nlines - 1].offset;
1068 outoff += n;
1069 err = add_line_metadata(lines, nlines, outoff,
1070 GOT_DIFF_LINE_META);
1071 if (err)
1072 return err;
1075 return NULL;
1078 static const struct got_error *
1079 diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1080 FILE *f1, FILE *f2, int fd1, int fd2,
1081 struct got_object_id *id1, struct got_object_id *id2,
1082 struct got_pathlist_head *paths, const char *label1, const char *label2,
1083 int diff_context, int ignore_whitespace, int force_text_diff,
1084 struct got_repository *repo, FILE *outfile,
1085 enum got_diff_algorithm diff_algo)
1087 const struct got_error *err;
1088 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1089 struct got_diff_blob_output_unidiff_arg arg;
1090 int want_linemeta = (lines != NULL && *lines != NULL);
1092 if (id1 == NULL && id2 == NULL)
1093 return got_error(GOT_ERR_NO_OBJ);
1095 if (id1) {
1096 err = got_object_open_as_tree(&tree1, repo, id1);
1097 if (err)
1098 goto done;
1100 if (id2) {
1101 err = got_object_open_as_tree(&tree2, repo, id2);
1102 if (err)
1103 goto done;
1106 arg.diff_algo = diff_algo;
1107 arg.diff_context = diff_context;
1108 arg.ignore_whitespace = ignore_whitespace;
1109 arg.force_text_diff = force_text_diff;
1110 arg.outfile = outfile;
1111 if (want_linemeta) {
1112 arg.lines = *lines;
1113 arg.nlines = *nlines;
1114 } else {
1115 arg.lines = NULL;
1116 arg.nlines = 0;
1118 if (paths == NULL || TAILQ_EMPTY(paths)) {
1119 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
1120 label1, label2, repo,
1121 got_diff_blob_output_unidiff, &arg, 1);
1122 } else {
1123 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1124 got_diff_blob_output_unidiff, &arg);
1126 if (want_linemeta) {
1127 *lines = arg.lines; /* was likely re-allocated */
1128 *nlines = arg.nlines;
1130 done:
1131 if (tree1)
1132 got_object_tree_close(tree1);
1133 if (tree2)
1134 got_object_tree_close(tree2);
1135 return err;
1138 const struct got_error *
1139 got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1140 FILE *f1, FILE *f2, int fd1, int fd2,
1141 struct got_object_id *id1, struct got_object_id *id2,
1142 struct got_pathlist_head *paths, const char *label1, const char *label2,
1143 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1144 int force_text_diff, struct got_repository *repo, FILE *outfile)
1146 const struct got_error *err;
1147 char *idstr = NULL;
1149 if (id1 == NULL && id2 == NULL)
1150 return got_error(GOT_ERR_NO_OBJ);
1152 if (id1) {
1153 err = got_object_id_str(&idstr, id1);
1154 if (err)
1155 goto done;
1156 err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1157 if (err)
1158 goto done;
1159 free(idstr);
1160 idstr = NULL;
1161 } else {
1162 err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1163 outfile);
1164 if (err)
1165 goto done;
1168 if (id2) {
1169 err = got_object_id_str(&idstr, id2);
1170 if (err)
1171 goto done;
1172 err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1173 if (err)
1174 goto done;
1175 free(idstr);
1176 idstr = NULL;
1177 } else {
1178 err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1179 outfile);
1180 if (err)
1181 goto done;
1184 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1185 paths, label1, label2, diff_context, ignore_whitespace,
1186 force_text_diff, repo, outfile, diff_algo);
1187 done:
1188 free(idstr);
1189 return err;
1192 const struct got_error *
1193 got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1194 FILE *f1, FILE *f2, int fd1, int fd2,
1195 struct got_object_id *id1, struct got_object_id *id2,
1196 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1197 int diff_context, int ignore_whitespace, int force_text_diff,
1198 struct got_repository *repo, FILE *outfile)
1200 const struct got_error *err;
1201 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1202 char *idstr = NULL;
1204 if (id2 == NULL)
1205 return got_error(GOT_ERR_NO_OBJ);
1207 if (id1) {
1208 err = got_object_open_as_commit(&commit1, repo, id1);
1209 if (err)
1210 goto done;
1211 err = got_object_id_str(&idstr, id1);
1212 if (err)
1213 goto done;
1214 err = show_object_id(lines, nlines, "commit", '-', idstr,
1215 outfile);
1216 if (err)
1217 goto done;
1218 free(idstr);
1219 idstr = NULL;
1220 } else {
1221 err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1222 outfile);
1223 if (err)
1224 goto done;
1227 err = got_object_open_as_commit(&commit2, repo, id2);
1228 if (err)
1229 goto done;
1231 err = got_object_id_str(&idstr, id2);
1232 if (err)
1233 goto done;
1234 err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1235 if (err)
1236 goto done;
1238 err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1239 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1240 got_object_commit_get_tree_id(commit2), paths, "", "",
1241 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1242 diff_algo);
1243 done:
1244 if (commit1)
1245 got_object_commit_close(commit1);
1246 if (commit2)
1247 got_object_commit_close(commit2);
1248 free(idstr);
1249 return err;
1252 const struct got_error *
1253 got_diff_files(struct got_diffreg_result **resultp,
1254 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1255 const char *label2, int diff_context, int ignore_whitespace,
1256 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1258 const struct got_error *err = NULL;
1259 struct got_diffreg_result *diffreg_result = NULL;
1261 if (resultp)
1262 *resultp = NULL;
1264 if (outfile) {
1265 fprintf(outfile, "file - %s\n",
1266 f1_exists ? label1 : "/dev/null");
1267 fprintf(outfile, "file + %s\n",
1268 f2_exists ? label2 : "/dev/null");
1271 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1272 ignore_whitespace, force_text_diff);
1273 if (err)
1274 goto done;
1276 if (outfile) {
1277 err = got_diffreg_output(NULL, NULL, diffreg_result,
1278 f1_exists, f2_exists, label1, label2,
1279 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1280 if (err)
1281 goto done;
1284 done:
1285 if (resultp && err == NULL)
1286 *resultp = diffreg_result;
1287 else if (diffreg_result) {
1288 const struct got_error *free_err;
1289 free_err = got_diffreg_result_free(diffreg_result);
1290 if (free_err && err == NULL)
1291 err = free_err;
1294 return err;