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 static const struct got_error *
43 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
44 {
45 off_t *p;
47 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
48 if (p == NULL)
49 return got_error_from_errno("reallocarray");
50 *line_offsets = p;
51 (*line_offsets)[*nlines] = off;
52 (*nlines)++;
53 return NULL;
54 }
56 static const struct got_error *
57 diff_blobs(off_t **line_offsets, size_t *nlines,
58 struct got_diffreg_result **resultp, struct got_blob_object *blob1,
59 struct got_blob_object *blob2, FILE *f1, FILE *f2,
60 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61 int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
62 enum got_diff_algorithm diff_algo)
63 {
64 const struct got_error *err = NULL, *free_err;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
67 const char *idstr1 = NULL, *idstr2 = NULL;
68 off_t size1, size2;
69 struct got_diffreg_result *result;
70 off_t outoff = 0;
71 int n;
73 if (line_offsets && *line_offsets && *nlines > 0)
74 outoff = (*line_offsets)[*nlines - 1];
75 else if (line_offsets) {
76 err = add_line_offset(line_offsets, nlines, 0);
77 if (err)
78 goto done;
79 }
81 if (resultp)
82 *resultp = NULL;
84 if (f1) {
85 err = got_opentemp_truncate(f1);
86 if (err)
87 goto done;
88 }
89 if (f2) {
90 err = got_opentemp_truncate(f2);
91 if (err)
92 goto done;
93 }
95 size1 = 0;
96 if (blob1) {
97 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
98 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
99 blob1);
100 if (err)
101 goto done;
102 } else
103 idstr1 = "/dev/null";
105 size2 = 0;
106 if (blob2) {
107 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
108 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
109 blob2);
110 if (err)
111 goto done;
112 } else
113 idstr2 = "/dev/null";
115 if (outfile) {
116 char *modestr1 = NULL, *modestr2 = NULL;
117 int modebits;
118 if (mode1 && mode1 != mode2) {
119 if (S_ISLNK(mode1))
120 modebits = S_IFLNK;
121 else
122 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
123 if (asprintf(&modestr1, " (mode %o)",
124 mode1 & modebits) == -1) {
125 err = got_error_from_errno("asprintf");
126 goto done;
129 if (mode2 && mode1 != mode2) {
130 if (S_ISLNK(mode2))
131 modebits = S_IFLNK;
132 else
133 modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
134 if (asprintf(&modestr2, " (mode %o)",
135 mode2 & modebits) == -1) {
136 err = got_error_from_errno("asprintf");
137 goto done;
140 n = fprintf(outfile, "blob - %s%s\n", idstr1,
141 modestr1 ? modestr1 : "");
142 if (n < 0)
143 goto done;
144 outoff += n;
145 if (line_offsets) {
146 err = add_line_offset(line_offsets, nlines, outoff);
147 if (err)
148 goto done;
151 n = fprintf(outfile, "blob + %s%s\n", idstr2,
152 modestr2 ? modestr2 : "");
153 if (n < 0)
154 goto done;
155 outoff += n;
156 if (line_offsets) {
157 err = add_line_offset(line_offsets, nlines, outoff);
158 if (err)
159 goto done;
162 free(modestr1);
163 free(modestr2);
165 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
166 force_text_diff);
167 if (err)
168 goto done;
170 if (outfile) {
171 err = got_diffreg_output(line_offsets, nlines, result,
172 blob1 != NULL, blob2 != NULL,
173 label1 ? label1 : idstr1,
174 label2 ? label2 : idstr2,
175 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
176 if (err)
177 goto done;
180 if (resultp && err == NULL)
181 *resultp = result;
182 else {
183 free_err = got_diffreg_result_free(result);
184 if (free_err && err == NULL)
185 err = free_err;
187 done:
188 return err;
191 const struct got_error *
192 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
193 struct got_blob_object *blob2, FILE *f1, FILE *f2,
194 struct got_object_id *id1, struct got_object_id *id2,
195 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
196 struct got_repository *repo)
198 struct got_diff_blob_output_unidiff_arg *a = arg;
200 return diff_blobs(&a->line_offsets, &a->nlines, NULL,
201 blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
202 a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
205 const struct got_error *
206 got_diff_blob(off_t **line_offsets, size_t *nlines,
207 struct got_blob_object *blob1, struct got_blob_object *blob2,
208 FILE *f1, FILE *f2, const char *label1, const char *label2,
209 enum got_diff_algorithm diff_algo, int diff_context,
210 int ignore_whitespace, int force_text_diff, FILE *outfile)
212 return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
213 label1, label2, 0, 0, diff_context, ignore_whitespace,
214 force_text_diff, outfile, diff_algo);
217 static const struct got_error *
218 diff_blob_file(struct got_diffreg_result **resultp,
219 struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
220 FILE *f2, int f2_exists, size_t size2, const char *label2,
221 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
222 int force_text_diff, FILE *outfile)
224 const struct got_error *err = NULL, *free_err;
225 char hex1[SHA1_DIGEST_STRING_LENGTH];
226 const char *idstr1 = NULL;
227 struct got_diffreg_result *result = NULL;
229 if (resultp)
230 *resultp = NULL;
232 if (blob1)
233 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
234 else
235 idstr1 = "/dev/null";
237 if (outfile) {
238 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
239 fprintf(outfile, "file + %s\n",
240 f2_exists ? label2 : "/dev/null");
243 err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
244 force_text_diff);
245 if (err)
246 goto done;
248 if (outfile) {
249 err = got_diffreg_output(NULL, NULL, result,
250 blob1 != NULL, f2_exists,
251 label2, /* show local file's path, not a blob ID */
252 label2, GOT_DIFF_OUTPUT_UNIDIFF,
253 diff_context, outfile);
254 if (err)
255 goto done;
258 if (resultp && err == NULL)
259 *resultp = result;
260 else if (result) {
261 free_err = got_diffreg_result_free(result);
262 if (free_err && err == NULL)
263 err = free_err;
265 done:
266 return err;
269 const struct got_error *
270 got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
271 const char *label1, FILE *f2, int f2_exists, size_t size2,
272 const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
273 int ignore_whitespace, int force_text_diff, FILE *outfile)
275 return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
276 size2, label2, diff_algo, diff_context, ignore_whitespace,
277 force_text_diff, outfile );
280 static const struct got_error *
281 diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
282 const char *label, mode_t mode, struct got_repository *repo,
283 got_diff_blob_cb cb, void *cb_arg)
285 const struct got_error *err;
286 struct got_blob_object *blob = NULL;
287 struct got_object *obj = NULL;
289 err = got_object_open(&obj, repo, id);
290 if (err)
291 return err;
293 err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
294 if (err)
295 goto done;
296 err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
297 NULL, label, 0, mode, repo);
298 done:
299 got_object_close(obj);
300 if (blob)
301 got_object_blob_close(blob);
302 return err;
305 static const struct got_error *
306 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
307 FILE *f1, FILE *f2, int fd1, int fd2,
308 const char *label1, const char *label2,
309 mode_t mode1, mode_t mode2, struct got_repository *repo,
310 got_diff_blob_cb cb, void *cb_arg)
312 const struct got_error *err;
313 struct got_object *obj1 = NULL;
314 struct got_object *obj2 = NULL;
315 struct got_blob_object *blob1 = NULL;
316 struct got_blob_object *blob2 = NULL;
318 err = got_object_open(&obj1, repo, id1);
319 if (err)
320 return err;
322 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
323 err = got_error(GOT_ERR_OBJ_TYPE);
324 goto done;
327 err = got_object_open(&obj2, repo, id2);
328 if (err)
329 goto done;
330 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
331 err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 goto done;
335 err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
336 if (err)
337 goto done;
339 err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
340 if (err)
341 goto done;
343 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
344 mode1, mode2, repo);
345 done:
346 if (obj1)
347 got_object_close(obj1);
348 if (obj2)
349 got_object_close(obj2);
350 if (blob1)
351 got_object_blob_close(blob1);
352 if (blob2)
353 got_object_blob_close(blob2);
354 return err;
357 static const struct got_error *
358 diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
359 FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
360 got_diff_blob_cb cb, void *cb_arg)
362 const struct got_error *err;
363 struct got_blob_object *blob = NULL;
364 struct got_object *obj = NULL;
366 err = got_object_open(&obj, repo, id);
367 if (err)
368 return err;
370 err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
371 if (err)
372 goto done;
373 err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
374 mode, 0, repo);
375 done:
376 got_object_close(obj);
377 if (blob)
378 got_object_blob_close(blob);
379 return err;
382 static const struct got_error *
383 diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
384 const char *label, struct got_repository *repo, got_diff_blob_cb cb,
385 void *cb_arg, int diff_content)
387 const struct got_error *err = NULL;
388 struct got_object *treeobj = NULL;
389 struct got_tree_object *tree = NULL;
391 err = got_object_open(&treeobj, repo, id);
392 if (err)
393 goto done;
395 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
396 err = got_error(GOT_ERR_OBJ_TYPE);
397 goto done;
400 err = got_object_tree_open(&tree, repo, treeobj);
401 if (err)
402 goto done;
404 err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
405 repo, cb, cb_arg, diff_content);
406 done:
407 if (tree)
408 got_object_tree_close(tree);
409 if (treeobj)
410 got_object_close(treeobj);
411 return err;
414 static const struct got_error *
415 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
416 FILE *f1, FILE *f2, int fd1, int fd2,
417 const char *label1, const char *label2,
418 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
419 int diff_content)
421 const struct got_error *err;
422 struct got_object *treeobj1 = NULL;
423 struct got_object *treeobj2 = NULL;
424 struct got_tree_object *tree1 = NULL;
425 struct got_tree_object *tree2 = NULL;
427 err = got_object_open(&treeobj1, repo, id1);
428 if (err)
429 goto done;
431 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
432 err = got_error(GOT_ERR_OBJ_TYPE);
433 goto done;
436 err = got_object_open(&treeobj2, repo, id2);
437 if (err)
438 goto done;
440 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
441 err = got_error(GOT_ERR_OBJ_TYPE);
442 goto done;
445 err = got_object_tree_open(&tree1, repo, treeobj1);
446 if (err)
447 goto done;
449 err = got_object_tree_open(&tree2, repo, treeobj2);
450 if (err)
451 goto done;
453 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
454 label1, label2, repo, cb, cb_arg, diff_content);
456 done:
457 if (tree1)
458 got_object_tree_close(tree1);
459 if (tree2)
460 got_object_tree_close(tree2);
461 if (treeobj1)
462 got_object_close(treeobj1);
463 if (treeobj2)
464 got_object_close(treeobj2);
465 return err;
468 static const struct got_error *
469 diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
470 FILE *f2, const char *label, struct got_repository *repo,
471 got_diff_blob_cb cb, void *cb_arg, int diff_content)
473 const struct got_error *err;
474 struct got_object *treeobj = NULL;
475 struct got_tree_object *tree = NULL;
477 err = got_object_open(&treeobj, repo, id);
478 if (err)
479 goto done;
481 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
482 err = got_error(GOT_ERR_OBJ_TYPE);
483 goto done;
486 err = got_object_tree_open(&tree, repo, treeobj);
487 if (err)
488 goto done;
490 err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
491 repo, cb, cb_arg, diff_content);
492 done:
493 if (tree)
494 got_object_tree_close(tree);
495 if (treeobj)
496 got_object_close(treeobj);
497 return err;
500 static const struct got_error *
501 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
502 const char *label1, const char *label2, struct got_repository *repo,
503 got_diff_blob_cb cb, void *cb_arg)
505 /* XXX TODO */
506 return NULL;
509 static const struct got_error *
510 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
511 FILE *f1, FILE *f2, int fd1, int fd2,
512 const char *label1, const char *label2,
513 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
514 int diff_content)
516 const struct got_error *err = NULL;
517 int id_match;
519 if (got_object_tree_entry_is_submodule(te1))
520 return NULL;
522 if (te2 == NULL) {
523 if (S_ISDIR(te1->mode))
524 err = diff_deleted_tree(&te1->id, f1, fd1, f2,
525 label1, repo, cb, cb_arg, diff_content);
526 else {
527 if (diff_content)
528 err = diff_deleted_blob(&te1->id, f1, fd1,
529 f2, label1, te1->mode, repo, cb, cb_arg);
530 else
531 err = cb(cb_arg, NULL, NULL, NULL, NULL,
532 &te1->id, NULL, label1, NULL,
533 te1->mode, 0, repo);
535 return err;
536 } else if (got_object_tree_entry_is_submodule(te2))
537 return NULL;
539 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
540 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
541 if (!id_match)
542 return diff_modified_tree(&te1->id, &te2->id, f1, f2,
543 fd1, fd2, label1, label2, repo, cb, cb_arg,
544 diff_content);
545 } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
546 (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
547 if (!id_match ||
548 ((te1->mode & (S_IFLNK | S_IXUSR))) !=
549 (te2->mode & (S_IFLNK | S_IXUSR))) {
550 if (diff_content)
551 return diff_modified_blob(&te1->id, &te2->id,
552 f1, f2, fd1, fd2, label1, label2,
553 te1->mode, te2->mode, repo, cb, cb_arg);
554 else
555 return cb(cb_arg, NULL, NULL, NULL, NULL,
556 &te1->id, &te2->id, label1, label2,
557 te1->mode, te2->mode, repo);
561 if (id_match)
562 return NULL;
564 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
565 cb, cb_arg);
568 static const struct got_error *
569 diff_entry_new_old(struct got_tree_entry *te2,
570 struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
571 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
572 int diff_content)
574 if (te1 != NULL) /* handled by diff_entry_old_new() */
575 return NULL;
577 if (got_object_tree_entry_is_submodule(te2))
578 return NULL;
580 if (S_ISDIR(te2->mode))
581 return diff_added_tree(&te2->id, f1, f2, fd2, label2,
582 repo, cb, cb_arg, diff_content);
584 if (diff_content)
585 return diff_added_blob(&te2->id, f1, f2, fd2,
586 label2, te2->mode, repo, cb, cb_arg);
588 return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
589 NULL, label2, 0, te2->mode, repo);
592 const struct got_error *
593 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
594 struct got_blob_object *blob2, FILE *f1, FILE *f2,
595 struct got_object_id *id1, struct got_object_id *id2,
596 const char *label1, const char *label2,
597 mode_t mode1, mode_t mode2, struct got_repository *repo)
599 const struct got_error *err = NULL;
600 struct got_pathlist_head *paths = arg;
601 struct got_diff_changed_path *change = NULL;
602 char *path = NULL;
604 path = strdup(label2 ? label2 : label1);
605 if (path == NULL)
606 return got_error_from_errno("malloc");
608 change = malloc(sizeof(*change));
609 if (change == NULL) {
610 err = got_error_from_errno("malloc");
611 goto done;
614 change->status = GOT_STATUS_NO_CHANGE;
615 if (id1 == NULL)
616 change->status = GOT_STATUS_ADD;
617 else if (id2 == NULL)
618 change->status = GOT_STATUS_DELETE;
619 else {
620 if (got_object_id_cmp(id1, id2) != 0)
621 change->status = GOT_STATUS_MODIFY;
622 else if (mode1 != mode2)
623 change->status = GOT_STATUS_MODE_CHANGE;
626 err = got_pathlist_append(paths, path, change);
627 done:
628 if (err) {
629 free(path);
630 free(change);
632 return err;
635 const struct got_error *
636 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
637 FILE *f1, FILE *f2, int fd1, int fd2,
638 const char *label1, const char *label2,
639 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
640 int diff_content)
642 const struct got_error *err = NULL;
643 struct got_tree_entry *te1 = NULL;
644 struct got_tree_entry *te2 = NULL;
645 char *l1 = NULL, *l2 = NULL;
646 int tidx1 = 0, tidx2 = 0;
648 if (tree1) {
649 te1 = got_object_tree_get_entry(tree1, 0);
650 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
651 te1->name) == -1)
652 return got_error_from_errno("asprintf");
654 if (tree2) {
655 te2 = got_object_tree_get_entry(tree2, 0);
656 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
657 te2->name) == -1)
658 return got_error_from_errno("asprintf");
661 do {
662 if (te1) {
663 struct got_tree_entry *te = NULL;
664 if (tree2)
665 te = got_object_tree_find_entry(tree2,
666 te1->name);
667 if (te) {
668 free(l2);
669 l2 = NULL;
670 if (te && asprintf(&l2, "%s%s%s", label2,
671 label2[0] ? "/" : "", te->name) == -1)
672 return
673 got_error_from_errno("asprintf");
675 err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
676 l1, l2, repo, cb, cb_arg, diff_content);
677 if (err)
678 break;
681 if (te2) {
682 struct got_tree_entry *te = NULL;
683 if (tree1)
684 te = got_object_tree_find_entry(tree1,
685 te2->name);
686 free(l2);
687 if (te) {
688 if (asprintf(&l2, "%s%s%s", label2,
689 label2[0] ? "/" : "", te->name) == -1)
690 return
691 got_error_from_errno("asprintf");
692 } else {
693 if (asprintf(&l2, "%s%s%s", label2,
694 label2[0] ? "/" : "", te2->name) == -1)
695 return
696 got_error_from_errno("asprintf");
698 err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
699 repo, cb, cb_arg, diff_content);
700 if (err)
701 break;
704 free(l1);
705 l1 = NULL;
706 if (te1) {
707 tidx1++;
708 te1 = got_object_tree_get_entry(tree1, tidx1);
709 if (te1 &&
710 asprintf(&l1, "%s%s%s", label1,
711 label1[0] ? "/" : "", te1->name) == -1)
712 return got_error_from_errno("asprintf");
714 free(l2);
715 l2 = NULL;
716 if (te2) {
717 tidx2++;
718 te2 = got_object_tree_get_entry(tree2, tidx2);
719 if (te2 &&
720 asprintf(&l2, "%s%s%s", label2,
721 label2[0] ? "/" : "", te2->name) == -1)
722 return got_error_from_errno("asprintf");
724 } while (te1 || te2);
726 return err;
729 const struct got_error *
730 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
731 FILE *f1, FILE *f2, int fd1, int fd2,
732 struct got_object_id *id1, struct got_object_id *id2,
733 const char *label1, const char *label2,
734 enum got_diff_algorithm diff_algo, int diff_context,
735 int ignore_whitespace, int force_text_diff,
736 struct got_repository *repo, FILE *outfile)
738 const struct got_error *err;
739 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
741 if (id1 == NULL && id2 == NULL)
742 return got_error(GOT_ERR_NO_OBJ);
744 if (id1) {
745 err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
746 if (err)
747 goto done;
749 if (id2) {
750 err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
751 if (err)
752 goto done;
754 err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
755 label1, label2, diff_algo, diff_context, ignore_whitespace,
756 force_text_diff, outfile);
757 done:
758 if (blob1)
759 got_object_blob_close(blob1);
760 if (blob2)
761 got_object_blob_close(blob2);
762 return err;
765 static const struct got_error *
766 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
767 FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
768 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
770 const struct got_error *err = NULL;
771 struct got_pathlist_entry *pe;
772 struct got_object_id *id1 = NULL, *id2 = NULL;
773 struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
774 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
776 TAILQ_FOREACH(pe, paths, entry) {
777 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
778 mode_t mode1 = 0, mode2 = 0;
780 free(id1);
781 id1 = NULL;
782 free(id2);
783 id2 = NULL;
784 if (subtree1) {
785 got_object_tree_close(subtree1);
786 subtree1 = NULL;
788 if (subtree2) {
789 got_object_tree_close(subtree2);
790 subtree2 = NULL;
792 if (blob1) {
793 got_object_blob_close(blob1);
794 blob1 = NULL;
796 if (blob2) {
797 got_object_blob_close(blob2);
798 blob2 = NULL;
801 err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
802 pe->path);
803 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
804 goto done;
805 err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
806 pe->path);
807 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
808 goto done;
809 if (id1 == NULL && id2 == NULL) {
810 err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
811 goto done;
813 if (id1) {
814 err = got_object_get_type(&type1, repo, id1);
815 if (err)
816 goto done;
818 if (id2) {
819 err = got_object_get_type(&type2, repo, id2);
820 if (err)
821 goto done;
823 if (type1 == GOT_OBJ_TYPE_ANY &&
824 type2 == GOT_OBJ_TYPE_ANY) {
825 err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
826 goto done;
827 } else if (type1 != GOT_OBJ_TYPE_ANY &&
828 type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
829 err = got_error(GOT_ERR_OBJ_TYPE);
830 goto done;
833 if (type1 == GOT_OBJ_TYPE_BLOB ||
834 type2 == GOT_OBJ_TYPE_BLOB) {
835 if (id1) {
836 err = got_object_open_as_blob(&blob1, repo,
837 id1, 8192, fd1);
838 if (err)
839 goto done;
841 if (id2) {
842 err = got_object_open_as_blob(&blob2, repo,
843 id2, 8192, fd2);
844 if (err)
845 goto done;
847 err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
848 id1 ? pe->path : "/dev/null",
849 id2 ? pe->path : "/dev/null",
850 mode1, mode2, repo);
851 if (err)
852 goto done;
853 } else if (type1 == GOT_OBJ_TYPE_TREE ||
854 type2 == GOT_OBJ_TYPE_TREE) {
855 if (id1) {
856 err = got_object_open_as_tree(&subtree1, repo,
857 id1);
858 if (err)
859 goto done;
861 if (id2) {
862 err = got_object_open_as_tree(&subtree2, repo,
863 id2);
864 if (err)
865 goto done;
867 err = got_diff_tree(subtree1, subtree2, f1, f2,
868 fd1, fd2,
869 id1 ? pe->path : "/dev/null",
870 id2 ? pe->path : "/dev/null",
871 repo, cb, cb_arg, 1);
872 if (err)
873 goto done;
874 } else {
875 err = got_error(GOT_ERR_OBJ_TYPE);
876 goto done;
879 done:
880 free(id1);
881 free(id2);
882 if (subtree1)
883 got_object_tree_close(subtree1);
884 if (subtree2)
885 got_object_tree_close(subtree2);
886 if (blob1)
887 got_object_blob_close(blob1);
888 if (blob2)
889 got_object_blob_close(blob2);
890 return err;
893 static const struct got_error *
894 show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
895 int ch, const char *id_str, FILE *outfile)
897 const struct got_error *err;
898 int n;
899 off_t outoff = 0;
901 n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
902 if (line_offsets != NULL && *line_offsets != NULL) {
903 if (*nlines == 0) {
904 err = add_line_offset(line_offsets, nlines, 0);
905 if (err)
906 return err;
907 } else
908 outoff = (*line_offsets)[*nlines - 1];
910 outoff += n;
911 err = add_line_offset(line_offsets, nlines, outoff);
912 if (err)
913 return err;
916 return NULL;
919 static const struct got_error *
920 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
921 FILE *f1, FILE *f2, int fd1, int fd2,
922 struct got_object_id *id1, struct got_object_id *id2,
923 struct got_pathlist_head *paths, const char *label1, const char *label2,
924 int diff_context, int ignore_whitespace, int force_text_diff,
925 struct got_repository *repo, FILE *outfile,
926 enum got_diff_algorithm diff_algo)
928 const struct got_error *err;
929 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
930 struct got_diff_blob_output_unidiff_arg arg;
931 int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
933 if (id1 == NULL && id2 == NULL)
934 return got_error(GOT_ERR_NO_OBJ);
936 if (id1) {
937 err = got_object_open_as_tree(&tree1, repo, id1);
938 if (err)
939 goto done;
941 if (id2) {
942 err = got_object_open_as_tree(&tree2, repo, id2);
943 if (err)
944 goto done;
947 arg.diff_algo = diff_algo;
948 arg.diff_context = diff_context;
949 arg.ignore_whitespace = ignore_whitespace;
950 arg.force_text_diff = force_text_diff;
951 arg.outfile = outfile;
952 if (want_lineoffsets) {
953 arg.line_offsets = *line_offsets;
954 arg.nlines = *nlines;
955 } else {
956 arg.line_offsets = NULL;
957 arg.nlines = 0;
959 if (paths == NULL || TAILQ_EMPTY(paths)) {
960 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
961 label1, label2, repo,
962 got_diff_blob_output_unidiff, &arg, 1);
963 } else {
964 err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
965 got_diff_blob_output_unidiff, &arg);
967 if (want_lineoffsets) {
968 *line_offsets = arg.line_offsets; /* was likely re-allocated */
969 *nlines = arg.nlines;
971 done:
972 if (tree1)
973 got_object_tree_close(tree1);
974 if (tree2)
975 got_object_tree_close(tree2);
976 return err;
979 const struct got_error *
980 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
981 FILE *f1, FILE *f2, int fd1, int fd2,
982 struct got_object_id *id1, struct got_object_id *id2,
983 struct got_pathlist_head *paths, const char *label1, const char *label2,
984 enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
985 int force_text_diff, struct got_repository *repo, FILE *outfile)
987 const struct got_error *err;
988 char *idstr = NULL;
990 if (id1 == NULL && id2 == NULL)
991 return got_error(GOT_ERR_NO_OBJ);
993 if (id1) {
994 err = got_object_id_str(&idstr, id1);
995 if (err)
996 goto done;
997 err = show_object_id(line_offsets, nlines, "tree", '-',
998 idstr, outfile);
999 if (err)
1000 goto done;
1001 free(idstr);
1002 idstr = NULL;
1003 } else {
1004 err = show_object_id(line_offsets, nlines, "tree", '-',
1005 "/dev/null", outfile);
1006 if (err)
1007 goto done;
1010 if (id2) {
1011 err = got_object_id_str(&idstr, id2);
1012 if (err)
1013 goto done;
1014 err = show_object_id(line_offsets, nlines, "tree", '+',
1015 idstr, outfile);
1016 if (err)
1017 goto done;
1018 free(idstr);
1019 idstr = NULL;
1020 } else {
1021 err = show_object_id(line_offsets, nlines, "tree", '+',
1022 "/dev/null", outfile);
1023 if (err)
1024 goto done;
1027 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1028 id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1029 force_text_diff, repo, outfile, diff_algo);
1030 done:
1031 free(idstr);
1032 return err;
1035 const struct got_error *
1036 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1037 FILE *f1, FILE *f2, int fd1, int fd2,
1038 struct got_object_id *id1, struct got_object_id *id2,
1039 struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1040 int diff_context, int ignore_whitespace, int force_text_diff,
1041 struct got_repository *repo, FILE *outfile)
1043 const struct got_error *err;
1044 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1045 char *idstr = NULL;
1047 if (id2 == NULL)
1048 return got_error(GOT_ERR_NO_OBJ);
1050 if (id1) {
1051 err = got_object_open_as_commit(&commit1, repo, id1);
1052 if (err)
1053 goto done;
1054 err = got_object_id_str(&idstr, id1);
1055 if (err)
1056 goto done;
1057 err = show_object_id(line_offsets, nlines, "commit", '-',
1058 idstr, outfile);
1059 if (err)
1060 goto done;
1061 free(idstr);
1062 idstr = NULL;
1063 } else {
1064 err = show_object_id(line_offsets, nlines, "commit", '-',
1065 "/dev/null", outfile);
1066 if (err)
1067 goto done;
1070 err = got_object_open_as_commit(&commit2, repo, id2);
1071 if (err)
1072 goto done;
1074 err = got_object_id_str(&idstr, id2);
1075 if (err)
1076 goto done;
1077 err = show_object_id(line_offsets, nlines, "commit", '+',
1078 idstr, outfile);
1079 if (err)
1080 goto done;
1082 err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1083 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1084 got_object_commit_get_tree_id(commit2), paths, "", "",
1085 diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1086 diff_algo);
1087 done:
1088 if (commit1)
1089 got_object_commit_close(commit1);
1090 if (commit2)
1091 got_object_commit_close(commit2);
1092 free(idstr);
1093 return err;
1096 const struct got_error *
1097 got_diff_files(struct got_diffreg_result **resultp,
1098 FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1099 const char *label2, int diff_context, int ignore_whitespace,
1100 int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1102 const struct got_error *err = NULL;
1103 struct got_diffreg_result *diffreg_result = NULL;
1105 if (resultp)
1106 *resultp = NULL;
1108 if (outfile) {
1109 fprintf(outfile, "file - %s\n",
1110 f1_exists ? label1 : "/dev/null");
1111 fprintf(outfile, "file + %s\n",
1112 f2_exists ? label2 : "/dev/null");
1115 err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1116 ignore_whitespace, force_text_diff);
1117 if (err)
1118 goto done;
1120 if (outfile) {
1121 err = got_diffreg_output(NULL, NULL, diffreg_result,
1122 f1_exists, f2_exists, label1, label2,
1123 GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1124 if (err)
1125 goto done;
1128 done:
1129 if (resultp && err == NULL)
1130 *resultp = diffreg_result;
1131 else if (diffreg_result) {
1132 const struct got_error *free_err;
1133 free_err = got_diffreg_result_free(diffreg_result);
1134 if (free_err && err == NULL)
1135 err = free_err;
1138 return err;