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_opentemp.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_worktree.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 static const struct got_error *
42 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
43 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
44 int diff_context, int ignore_whitespace, FILE *outfile,
45 struct got_diff_changes *changes)
46 {
47 struct got_diff_state ds;
48 struct got_diff_args args;
49 const struct got_error *err = NULL;
50 FILE *f1 = NULL, *f2 = NULL;
51 char hex1[SHA1_DIGEST_STRING_LENGTH];
52 char hex2[SHA1_DIGEST_STRING_LENGTH];
53 char *idstr1 = NULL, *idstr2 = NULL;
54 size_t size1, size2;
55 int res, flags = 0;
57 if (blob1) {
58 f1 = got_opentemp();
59 if (f1 == NULL)
60 return got_error_from_errno("got_opentemp");
61 } else
62 flags |= D_EMPTY1;
64 if (blob2) {
65 f2 = got_opentemp();
66 if (f2 == NULL) {
67 err = got_error_from_errno("got_opentemp");
68 fclose(f1);
69 return err;
70 }
71 } else
72 flags |= D_EMPTY2;
74 size1 = 0;
75 if (blob1) {
76 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
77 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
78 blob1);
79 if (err)
80 goto done;
81 } else
82 idstr1 = "/dev/null";
84 size2 = 0;
85 if (blob2) {
86 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
88 blob2);
89 if (err)
90 goto done;
91 } else
92 idstr2 = "/dev/null";
94 memset(&ds, 0, sizeof(ds));
95 /* XXX should stat buffers be passed in args instead of ds? */
96 ds.stb1.st_mode = S_IFREG;
97 if (blob1)
98 ds.stb1.st_size = size1;
99 ds.stb1.st_mtime = 0; /* XXX */
101 ds.stb2.st_mode = S_IFREG;
102 if (blob2)
103 ds.stb2.st_size = size2;
104 ds.stb2.st_mtime = 0; /* XXX */
106 memset(&args, 0, sizeof(args));
107 args.diff_format = D_UNIFIED;
108 args.label[0] = label1 ? label1 : idstr1;
109 args.label[1] = label2 ? label2 : idstr2;
110 args.diff_context = diff_context;
111 flags |= D_PROTOTYPE;
112 if (ignore_whitespace)
113 flags |= D_IGNOREBLANKS;
115 if (outfile) {
116 char *modestr1 = NULL, *modestr2 = NULL;
117 if (mode1 && mode1 != mode2) {
118 if (asprintf(&modestr1, " (mode %o)",
119 mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
120 err = got_error_from_errno("asprintf");
121 goto done;
124 if (mode2 && mode1 != mode2) {
125 if (asprintf(&modestr2, " (mode %o)",
126 mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
127 err = got_error_from_errno("asprintf");
128 goto done;
131 fprintf(outfile, "blob - %s%s\n", idstr1,
132 modestr1 ? modestr1 : "");
133 fprintf(outfile, "blob + %s%s\n", idstr2,
134 modestr2 ? modestr2 : "");
135 free(modestr1);
136 free(modestr2);
138 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
139 got_diff_state_free(&ds);
140 done:
141 if (f1 && fclose(f1) != 0 && err == NULL)
142 err = got_error_from_errno("fclose");
143 if (f2 && fclose(f2) != 0 && err == NULL)
144 err = got_error_from_errno("fclose");
145 return err;
148 const struct got_error *
149 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
150 struct got_blob_object *blob2, struct got_object_id *id1,
151 struct got_object_id *id2, const char *label1, const char *label2,
152 mode_t mode1, mode_t mode2, struct got_repository *repo)
154 struct got_diff_blob_output_unidiff_arg *a = arg;
156 return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
157 a->diff_context, a->ignore_whitespace, a->outfile, NULL);
160 const struct got_error *
161 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
162 const char *label1, const char *label2, int diff_context,
163 int ignore_whitespace, FILE *outfile)
165 return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
166 ignore_whitespace, outfile, NULL);
169 static const struct got_error *
170 alloc_changes(struct got_diff_changes **changes)
172 *changes = calloc(1, sizeof(**changes));
173 if (*changes == NULL)
174 return got_error_from_errno("calloc");
175 SIMPLEQ_INIT(&(*changes)->entries);
176 return NULL;
179 static const struct got_error *
180 diff_blob_file(struct got_diff_changes **changes,
181 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
182 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
184 struct got_diff_state ds;
185 struct got_diff_args args;
186 const struct got_error *err = NULL;
187 FILE *f1 = NULL;
188 char hex1[SHA1_DIGEST_STRING_LENGTH];
189 char *idstr1 = NULL;
190 size_t size1;
191 int res, flags = 0;
193 if (changes)
194 *changes = NULL;
196 size1 = 0;
197 if (blob1) {
198 f1 = got_opentemp();
199 if (f1 == NULL)
200 return got_error_from_errno("got_opentemp");
201 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
202 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
203 blob1);
204 if (err)
205 goto done;
206 } else {
207 flags |= D_EMPTY1;
208 idstr1 = "/dev/null";
211 if (f2 == NULL)
212 flags |= D_EMPTY2;
214 memset(&ds, 0, sizeof(ds));
215 /* XXX should stat buffers be passed in args instead of ds? */
216 ds.stb1.st_mode = S_IFREG;
217 if (blob1)
218 ds.stb1.st_size = size1;
219 ds.stb1.st_mtime = 0; /* XXX */
221 ds.stb2.st_mode = S_IFREG;
222 ds.stb2.st_size = size2;
223 ds.stb2.st_mtime = 0; /* XXX */
225 memset(&args, 0, sizeof(args));
226 args.diff_format = D_UNIFIED;
227 args.label[0] = label2;
228 args.label[1] = label2;
229 args.diff_context = diff_context;
230 flags |= D_PROTOTYPE;
231 if (ignore_whitespace)
232 flags |= D_IGNOREBLANKS;
234 if (outfile) {
235 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 fprintf(outfile, "file + %s\n",
237 f2 == NULL ? "/dev/null" : label2);
239 if (changes) {
240 err = alloc_changes(changes);
241 if (err)
242 return err;
244 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
245 changes ? *changes : NULL);
246 got_diff_state_free(&ds);
247 done:
248 if (f1 && fclose(f1) != 0 && err == NULL)
249 err = got_error_from_errno("fclose");
250 return err;
253 const struct got_error *
254 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
255 FILE *f2, size_t size2, const char *label2, int diff_context,
256 int ignore_whitespace, FILE *outfile)
258 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
259 diff_context, ignore_whitespace, outfile);
262 const struct got_error *
263 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
264 struct got_blob_object *blob1, FILE *f2, size_t size2)
266 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
267 0, 0, NULL);
270 const struct got_error *
271 got_diff_blob_lines_changed(struct got_diff_changes **changes,
272 struct got_blob_object *blob1, struct got_blob_object *blob2)
274 const struct got_error *err = NULL;
276 err = alloc_changes(changes);
277 if (err)
278 return err;
280 err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
281 if (err) {
282 got_diff_free_changes(*changes);
283 *changes = NULL;
285 return err;
288 void
289 got_diff_free_changes(struct got_diff_changes *changes)
291 struct got_diff_change *change;
292 while (!SIMPLEQ_EMPTY(&changes->entries)) {
293 change = SIMPLEQ_FIRST(&changes->entries);
294 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
295 free(change);
297 free(changes);
300 static const struct got_error *
301 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
302 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
304 const struct got_error *err;
305 struct got_blob_object *blob = NULL;
306 struct got_object *obj = NULL;
308 err = got_object_open(&obj, repo, id);
309 if (err)
310 return err;
312 err = got_object_blob_open(&blob, repo, obj, 8192);
313 if (err)
314 goto done;
315 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
316 done:
317 got_object_close(obj);
318 if (blob)
319 got_object_blob_close(blob);
320 return err;
323 static const struct got_error *
324 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
325 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
326 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
328 const struct got_error *err;
329 struct got_object *obj1 = NULL;
330 struct got_object *obj2 = NULL;
331 struct got_blob_object *blob1 = NULL;
332 struct got_blob_object *blob2 = NULL;
334 err = got_object_open(&obj1, repo, id1);
335 if (err)
336 return err;
337 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
339 goto done;
342 err = got_object_open(&obj2, repo, id2);
343 if (err)
344 goto done;
345 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 goto done;
350 err = got_object_blob_open(&blob1, repo, obj1, 8192);
351 if (err)
352 goto done;
354 err = got_object_blob_open(&blob2, repo, obj2, 8192);
355 if (err)
356 goto done;
358 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
359 repo);
360 done:
361 if (obj1)
362 got_object_close(obj1);
363 if (obj2)
364 got_object_close(obj2);
365 if (blob1)
366 got_object_blob_close(blob1);
367 if (blob2)
368 got_object_blob_close(blob2);
369 return err;
372 static const struct got_error *
373 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
374 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
376 const struct got_error *err;
377 struct got_blob_object *blob = NULL;
378 struct got_object *obj = NULL;
380 err = got_object_open(&obj, repo, id);
381 if (err)
382 return err;
384 err = got_object_blob_open(&blob, repo, obj, 8192);
385 if (err)
386 goto done;
387 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
388 done:
389 got_object_close(obj);
390 if (blob)
391 got_object_blob_close(blob);
392 return err;
395 static const struct got_error *
396 diff_added_tree(struct got_object_id *id, const char *label,
397 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
398 int diff_content)
400 const struct got_error *err = NULL;
401 struct got_object *treeobj = NULL;
402 struct got_tree_object *tree = NULL;
404 err = got_object_open(&treeobj, repo, id);
405 if (err)
406 goto done;
408 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
409 err = got_error(GOT_ERR_OBJ_TYPE);
410 goto done;
413 err = got_object_tree_open(&tree, repo, treeobj);
414 if (err)
415 goto done;
417 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
418 diff_content);
419 done:
420 if (tree)
421 got_object_tree_close(tree);
422 if (treeobj)
423 got_object_close(treeobj);
424 return err;
427 static const struct got_error *
428 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
429 const char *label1, const char *label2, struct got_repository *repo,
430 got_diff_blob_cb cb, void *cb_arg, int diff_content)
432 const struct got_error *err;
433 struct got_object *treeobj1 = NULL;
434 struct got_object *treeobj2 = NULL;
435 struct got_tree_object *tree1 = NULL;
436 struct got_tree_object *tree2 = NULL;
438 err = got_object_open(&treeobj1, repo, id1);
439 if (err)
440 goto done;
442 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
443 err = got_error(GOT_ERR_OBJ_TYPE);
444 goto done;
447 err = got_object_open(&treeobj2, repo, id2);
448 if (err)
449 goto done;
451 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
452 err = got_error(GOT_ERR_OBJ_TYPE);
453 goto done;
456 err = got_object_tree_open(&tree1, repo, treeobj1);
457 if (err)
458 goto done;
460 err = got_object_tree_open(&tree2, repo, treeobj2);
461 if (err)
462 goto done;
464 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
465 diff_content);
467 done:
468 if (tree1)
469 got_object_tree_close(tree1);
470 if (tree2)
471 got_object_tree_close(tree2);
472 if (treeobj1)
473 got_object_close(treeobj1);
474 if (treeobj2)
475 got_object_close(treeobj2);
476 return err;
479 static const struct got_error *
480 diff_deleted_tree(struct got_object_id *id, const char *label,
481 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
482 int diff_content)
484 const struct got_error *err;
485 struct got_object *treeobj = NULL;
486 struct got_tree_object *tree = NULL;
488 err = got_object_open(&treeobj, repo, id);
489 if (err)
490 goto done;
492 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
493 err = got_error(GOT_ERR_OBJ_TYPE);
494 goto done;
497 err = got_object_tree_open(&tree, repo, treeobj);
498 if (err)
499 goto done;
501 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
502 diff_content);
503 done:
504 if (tree)
505 got_object_tree_close(tree);
506 if (treeobj)
507 got_object_close(treeobj);
508 return err;
511 static const struct got_error *
512 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
513 const char *label1, const char *label2, struct got_repository *repo,
514 got_diff_blob_cb cb, void *cb_arg)
516 /* XXX TODO */
517 return NULL;
520 static const struct got_error *
521 diff_entry_old_new(struct got_tree_entry *te1,
522 struct got_tree_entry *te2, const char *label1, const char *label2,
523 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
524 int diff_content)
526 const struct got_error *err = NULL;
527 int id_match;
529 if (got_object_tree_entry_is_submodule(te1))
530 return NULL;
532 if (te2 == NULL) {
533 if (S_ISDIR(te1->mode))
534 err = diff_deleted_tree(&te1->id, label1, repo,
535 cb, cb_arg, diff_content);
536 else {
537 if (diff_content)
538 err = diff_deleted_blob(&te1->id, label1,
539 te1->mode, repo, cb, cb_arg);
540 else
541 err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
542 label1, NULL, te1->mode, 0, repo);
544 return err;
545 } else if (got_object_tree_entry_is_submodule(te2))
546 return NULL;
548 id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
549 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
550 if (!id_match)
551 return diff_modified_tree(&te1->id, &te2->id,
552 label1, label2, repo, cb, cb_arg, diff_content);
553 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
554 if (!id_match ||
555 (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
556 if (diff_content)
557 return diff_modified_blob(&te1->id, &te2->id,
558 label1, label2, te1->mode, te2->mode,
559 repo, cb, cb_arg);
560 else
561 return cb(cb_arg, NULL, NULL, &te1->id,
562 &te2->id, label1, label2, te1->mode,
563 te2->mode, repo);
567 if (id_match)
568 return NULL;
570 return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
571 cb, cb_arg);
574 static const struct got_error *
575 diff_entry_new_old(struct got_tree_entry *te2,
576 struct got_tree_entry *te1, const char *label2,
577 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
578 int diff_content)
580 if (te1 != NULL) /* handled by diff_entry_old_new() */
581 return NULL;
583 if (got_object_tree_entry_is_submodule(te2))
584 return NULL;
586 if (S_ISDIR(te2->mode))
587 return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
588 diff_content);
590 if (diff_content)
591 return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
592 cb_arg);
594 return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
595 te2->mode, repo);
598 const struct got_error *
599 got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
600 struct got_blob_object *blob2, struct got_object_id *id1,
601 struct got_object_id *id2, const char *label1, const char *label2,
602 mode_t mode1, mode_t mode2, struct got_repository *repo)
604 const struct got_error *err = NULL;
605 struct got_pathlist_head *paths = arg;
606 struct got_diff_changed_path *change = NULL;
607 char *path = NULL;
609 path = strdup(label2 ? label2 : label1);
610 if (path == NULL)
611 return got_error_from_errno("malloc");
613 change = malloc(sizeof(*change));
614 if (change == NULL) {
615 err = got_error_from_errno("malloc");
616 goto done;
619 change->status = GOT_STATUS_NO_CHANGE;
620 if (id1 == NULL)
621 change->status = GOT_STATUS_ADD;
622 else if (id2 == NULL)
623 change->status = GOT_STATUS_DELETE;
624 else {
625 if (got_object_id_cmp(id1, id2) != 0)
626 change->status = GOT_STATUS_MODIFY;
627 else if (mode1 != mode2)
628 change->status = GOT_STATUS_MODE_CHANGE;
631 err = got_pathlist_insert(NULL, paths, path, change);
632 done:
633 if (err) {
634 free(path);
635 free(change);
637 return err;
640 const struct got_error *
641 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
642 const char *label1, const char *label2, struct got_repository *repo,
643 got_diff_blob_cb cb, void *cb_arg, int diff_content)
645 const struct got_error *err = NULL;
646 struct got_tree_entry *te1 = NULL;
647 struct got_tree_entry *te2 = NULL;
648 char *l1 = NULL, *l2 = NULL;
649 int tidx1 = 0, tidx2 = 0;
651 if (tree1) {
652 te1 = got_object_tree_get_entry(tree1, 0);
653 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
654 te1->name) == -1)
655 return got_error_from_errno("asprintf");
657 if (tree2) {
658 te2 = got_object_tree_get_entry(tree2, 0);
659 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
660 te2->name) == -1)
661 return got_error_from_errno("asprintf");
664 do {
665 if (te1) {
666 struct got_tree_entry *te = NULL;
667 if (tree2)
668 te = got_object_tree_find_entry(tree2,
669 te1->name);
670 if (te) {
671 free(l2);
672 l2 = NULL;
673 if (te && asprintf(&l2, "%s%s%s", label2,
674 label2[0] ? "/" : "", te->name) == -1)
675 return
676 got_error_from_errno("asprintf");
678 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
679 cb_arg, diff_content);
680 if (err)
681 break;
684 if (te2) {
685 struct got_tree_entry *te = NULL;
686 if (tree1)
687 te = got_object_tree_find_entry(tree1,
688 te2->name);
689 free(l2);
690 if (te) {
691 if (asprintf(&l2, "%s%s%s", label2,
692 label2[0] ? "/" : "", te->name) == -1)
693 return
694 got_error_from_errno("asprintf");
695 } else {
696 if (asprintf(&l2, "%s%s%s", label2,
697 label2[0] ? "/" : "", te2->name) == -1)
698 return
699 got_error_from_errno("asprintf");
701 err = diff_entry_new_old(te2, te, l2, repo,
702 cb, cb_arg, diff_content);
703 if (err)
704 break;
707 free(l1);
708 l1 = NULL;
709 if (te1) {
710 tidx1++;
711 te1 = got_object_tree_get_entry(tree1, tidx1);
712 if (te1 &&
713 asprintf(&l1, "%s%s%s", label1,
714 label1[0] ? "/" : "", te1->name) == -1)
715 return got_error_from_errno("asprintf");
717 free(l2);
718 l2 = NULL;
719 if (te2) {
720 tidx2++;
721 te2 = got_object_tree_get_entry(tree2, tidx2);
722 if (te2 &&
723 asprintf(&l2, "%s%s%s", label2,
724 label2[0] ? "/" : "", te2->name) == -1)
725 return got_error_from_errno("asprintf");
727 } while (te1 || te2);
729 return err;
732 const struct got_error *
733 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
734 const char *label1, const char *label2, int diff_context,
735 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
737 const struct got_error *err;
738 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
740 if (id1 == NULL && id2 == NULL)
741 return got_error(GOT_ERR_NO_OBJ);
743 if (id1) {
744 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
745 if (err)
746 goto done;
748 if (id2) {
749 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
750 if (err)
751 goto done;
753 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
754 ignore_whitespace, outfile);
755 done:
756 if (blob1)
757 got_object_blob_close(blob1);
758 if (blob2)
759 got_object_blob_close(blob2);
760 return err;
763 const struct got_error *
764 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
765 char *label1, char *label2, int diff_context, int ignore_whitespace,
766 struct got_repository *repo, FILE *outfile)
768 const struct got_error *err;
769 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
770 struct got_diff_blob_output_unidiff_arg arg;
772 if (id1 == NULL && id2 == NULL)
773 return got_error(GOT_ERR_NO_OBJ);
775 if (id1) {
776 err = got_object_open_as_tree(&tree1, repo, id1);
777 if (err)
778 goto done;
780 if (id2) {
781 err = got_object_open_as_tree(&tree2, repo, id2);
782 if (err)
783 goto done;
785 arg.diff_context = diff_context;
786 arg.ignore_whitespace = ignore_whitespace;
787 arg.outfile = outfile;
788 err = got_diff_tree(tree1, tree2, label1, label2, repo,
789 got_diff_blob_output_unidiff, &arg, 1);
790 done:
791 if (tree1)
792 got_object_tree_close(tree1);
793 if (tree2)
794 got_object_tree_close(tree2);
795 return err;
798 const struct got_error *
799 got_diff_objects_as_commits(struct got_object_id *id1,
800 struct got_object_id *id2, int diff_context, int ignore_whitespace,
801 struct got_repository *repo, FILE *outfile)
803 const struct got_error *err;
804 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
806 if (id2 == NULL)
807 return got_error(GOT_ERR_NO_OBJ);
809 if (id1) {
810 err = got_object_open_as_commit(&commit1, repo, id1);
811 if (err)
812 goto done;
815 err = got_object_open_as_commit(&commit2, repo, id2);
816 if (err)
817 goto done;
819 err = got_diff_objects_as_trees(
820 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
821 got_object_commit_get_tree_id(commit2), "", "", diff_context,
822 ignore_whitespace, repo, outfile);
823 done:
824 if (commit1)
825 got_object_commit_close(commit1);
826 if (commit2)
827 got_object_commit_close(commit2);
828 return err;
831 const struct got_error *
832 got_diff_files(struct got_diff_changes **changes,
833 struct got_diff_state **ds,
834 struct got_diff_args **args,
835 int *flags,
836 FILE *f1, size_t size1, const char *label1,
837 FILE *f2, size_t size2, const char *label2,
838 int diff_context, FILE *outfile)
840 const struct got_error *err = NULL;
841 int res;
843 *flags = 0;
844 *ds = calloc(1, sizeof(**ds));
845 if (*ds == NULL)
846 return got_error_from_errno("calloc");
847 *args = calloc(1, sizeof(**args));
848 if (*args == NULL) {
849 err = got_error_from_errno("calloc");
850 goto done;
853 if (changes)
854 *changes = NULL;
856 if (f1 == NULL)
857 *flags |= D_EMPTY1;
859 if (f2 == NULL)
860 *flags |= D_EMPTY2;
862 /* XXX should stat buffers be passed in args instead of ds? */
863 (*ds)->stb1.st_mode = S_IFREG;
864 (*ds)->stb1.st_size = size1;
865 (*ds)->stb1.st_mtime = 0; /* XXX */
867 (*ds)->stb2.st_mode = S_IFREG;
868 (*ds)->stb2.st_size = size2;
869 (*ds)->stb2.st_mtime = 0; /* XXX */
871 (*args)->diff_format = D_UNIFIED;
872 (*args)->label[0] = label1;
873 (*args)->label[1] = label2;
874 (*args)->diff_context = diff_context;
875 *flags |= D_PROTOTYPE;
877 if (outfile) {
878 fprintf(outfile, "file - %s\n",
879 f1 == NULL ? "/dev/null" : label1);
880 fprintf(outfile, "file + %s\n",
881 f2 == NULL ? "/dev/null" : label2);
883 if (changes) {
884 err = alloc_changes(changes);
885 if (err)
886 goto done;
888 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
889 changes ? *changes : NULL);
890 done:
891 if (err) {
892 if (*ds) {
893 got_diff_state_free(*ds);
894 free(*ds);
895 *ds = NULL;
897 if (*args) {
898 free(*args);
899 *args = NULL;
901 if (changes) {
902 if (*changes)
903 got_diff_free_changes(*changes);
904 *changes = NULL;
907 return err;