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"
34 #include "got_lib_diff.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 static const struct got_error *
40 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
41 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
42 int diff_context, int ignore_whitespace, FILE *outfile,
43 struct got_diff_changes *changes)
44 {
45 struct got_diff_state ds;
46 struct got_diff_args args;
47 const struct got_error *err = NULL;
48 FILE *f1 = NULL, *f2 = NULL;
49 char hex1[SHA1_DIGEST_STRING_LENGTH];
50 char hex2[SHA1_DIGEST_STRING_LENGTH];
51 char *idstr1 = NULL, *idstr2 = NULL;
52 size_t size1, size2;
53 int res, flags = 0;
55 if (blob1) {
56 f1 = got_opentemp();
57 if (f1 == NULL)
58 return got_error_from_errno("got_opentemp");
59 } else
60 flags |= D_EMPTY1;
62 if (blob2) {
63 f2 = got_opentemp();
64 if (f2 == NULL) {
65 err = got_error_from_errno("got_opentemp");
66 fclose(f1);
67 return err;
68 }
69 } else
70 flags |= D_EMPTY2;
72 size1 = 0;
73 if (blob1) {
74 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
75 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
76 blob1);
77 if (err)
78 goto done;
79 } else
80 idstr1 = "/dev/null";
82 size2 = 0;
83 if (blob2) {
84 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
85 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
86 blob2);
87 if (err)
88 goto done;
89 } else
90 idstr2 = "/dev/null";
92 memset(&ds, 0, sizeof(ds));
93 /* XXX should stat buffers be passed in args instead of ds? */
94 ds.stb1.st_mode = S_IFREG;
95 if (blob1)
96 ds.stb1.st_size = size1;
97 ds.stb1.st_mtime = 0; /* XXX */
99 ds.stb2.st_mode = S_IFREG;
100 if (blob2)
101 ds.stb2.st_size = size2;
102 ds.stb2.st_mtime = 0; /* XXX */
104 memset(&args, 0, sizeof(args));
105 args.diff_format = D_UNIFIED;
106 args.label[0] = label1 ? label1 : idstr1;
107 args.label[1] = label2 ? label2 : idstr2;
108 args.diff_context = diff_context;
109 flags |= D_PROTOTYPE;
110 if (ignore_whitespace)
111 flags |= D_IGNOREBLANKS;
113 if (outfile) {
114 char *modestr1 = NULL, *modestr2 = NULL;
115 if (mode1 && mode1 != mode2) {
116 if (asprintf(&modestr1, " (mode %o)",
117 mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
118 err = got_error_from_errno("asprintf");
119 goto done;
122 if (mode2 && mode1 != mode2) {
123 if (asprintf(&modestr2, " (mode %o)",
124 mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
125 err = got_error_from_errno("asprintf");
126 goto done;
129 fprintf(outfile, "blob - %s%s\n", idstr1,
130 modestr1 ? modestr1 : "");
131 fprintf(outfile, "blob + %s%s\n", idstr2,
132 modestr2 ? modestr2 : "");
133 free(modestr1);
134 free(modestr2);
136 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
137 got_diff_state_free(&ds);
138 done:
139 if (f1 && fclose(f1) != 0 && err == NULL)
140 err = got_error_from_errno("fclose");
141 if (f2 && fclose(f2) != 0 && err == NULL)
142 err = got_error_from_errno("fclose");
143 return err;
146 const struct got_error *
147 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
148 struct got_blob_object *blob2, struct got_object_id *id1,
149 struct got_object_id *id2, const char *label1, const char *label2,
150 mode_t mode1, mode_t mode2, struct got_repository *repo)
152 struct got_diff_blob_output_unidiff_arg *a = arg;
154 return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
155 a->diff_context, a->ignore_whitespace, a->outfile, NULL);
158 const struct got_error *
159 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
160 const char *label1, const char *label2, int diff_context,
161 int ignore_whitespace, FILE *outfile)
163 return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
164 ignore_whitespace, outfile, NULL);
167 static const struct got_error *
168 alloc_changes(struct got_diff_changes **changes)
170 *changes = calloc(1, sizeof(**changes));
171 if (*changes == NULL)
172 return got_error_from_errno("calloc");
173 SIMPLEQ_INIT(&(*changes)->entries);
174 return NULL;
177 static const struct got_error *
178 diff_blob_file(struct got_diff_changes **changes,
179 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
180 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
182 struct got_diff_state ds;
183 struct got_diff_args args;
184 const struct got_error *err = NULL;
185 FILE *f1 = NULL;
186 char hex1[SHA1_DIGEST_STRING_LENGTH];
187 char *idstr1 = NULL;
188 size_t size1;
189 int res, flags = 0;
191 if (changes)
192 *changes = NULL;
194 size1 = 0;
195 if (blob1) {
196 f1 = got_opentemp();
197 if (f1 == NULL)
198 return got_error_from_errno("got_opentemp");
199 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
200 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
201 blob1);
202 if (err)
203 goto done;
204 } else {
205 flags |= D_EMPTY1;
206 idstr1 = "/dev/null";
209 if (f2 == NULL)
210 flags |= D_EMPTY2;
212 memset(&ds, 0, sizeof(ds));
213 /* XXX should stat buffers be passed in args instead of ds? */
214 ds.stb1.st_mode = S_IFREG;
215 if (blob1)
216 ds.stb1.st_size = size1;
217 ds.stb1.st_mtime = 0; /* XXX */
219 ds.stb2.st_mode = S_IFREG;
220 ds.stb2.st_size = size2;
221 ds.stb2.st_mtime = 0; /* XXX */
223 memset(&args, 0, sizeof(args));
224 args.diff_format = D_UNIFIED;
225 args.label[0] = label2;
226 args.label[1] = label2;
227 args.diff_context = diff_context;
228 flags |= D_PROTOTYPE;
229 if (ignore_whitespace)
230 flags |= D_IGNOREBLANKS;
232 if (outfile) {
233 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
234 fprintf(outfile, "file + %s\n",
235 f2 == NULL ? "/dev/null" : label2);
237 if (changes) {
238 err = alloc_changes(changes);
239 if (err)
240 return err;
242 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
243 changes ? *changes : NULL);
244 got_diff_state_free(&ds);
245 done:
246 if (f1 && fclose(f1) != 0 && err == NULL)
247 err = got_error_from_errno("fclose");
248 return err;
251 const struct got_error *
252 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
253 FILE *f2, size_t size2, const char *label2, int diff_context,
254 int ignore_whitespace, FILE *outfile)
256 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
257 diff_context, ignore_whitespace, outfile);
260 const struct got_error *
261 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
262 struct got_blob_object *blob1, FILE *f2, size_t size2)
264 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
265 0, 0, NULL);
268 const struct got_error *
269 got_diff_blob_lines_changed(struct got_diff_changes **changes,
270 struct got_blob_object *blob1, struct got_blob_object *blob2)
272 const struct got_error *err = NULL;
274 err = alloc_changes(changes);
275 if (err)
276 return err;
278 err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
279 if (err) {
280 got_diff_free_changes(*changes);
281 *changes = NULL;
283 return err;
286 void
287 got_diff_free_changes(struct got_diff_changes *changes)
289 struct got_diff_change *change;
290 while (!SIMPLEQ_EMPTY(&changes->entries)) {
291 change = SIMPLEQ_FIRST(&changes->entries);
292 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
293 free(change);
295 free(changes);
298 static const struct got_error *
299 diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
300 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
302 const struct got_error *err;
303 struct got_blob_object *blob = NULL;
304 struct got_object *obj = NULL;
306 err = got_object_open(&obj, repo, id);
307 if (err)
308 return err;
310 err = got_object_blob_open(&blob, repo, obj, 8192);
311 if (err)
312 goto done;
313 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
314 done:
315 got_object_close(obj);
316 if (blob)
317 got_object_blob_close(blob);
318 return err;
321 static const struct got_error *
322 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
323 const char *label1, const char *label2, mode_t mode1, mode_t mode2,
324 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
326 const struct got_error *err;
327 struct got_object *obj1 = NULL;
328 struct got_object *obj2 = NULL;
329 struct got_blob_object *blob1 = NULL;
330 struct got_blob_object *blob2 = NULL;
332 err = got_object_open(&obj1, repo, id1);
333 if (err)
334 return err;
335 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
336 err = got_error(GOT_ERR_OBJ_TYPE);
337 goto done;
340 err = got_object_open(&obj2, repo, id2);
341 if (err)
342 goto done;
343 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
344 err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 goto done;
348 err = got_object_blob_open(&blob1, repo, obj1, 8192);
349 if (err)
350 goto done;
352 err = got_object_blob_open(&blob2, repo, obj2, 8192);
353 if (err)
354 goto done;
356 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
357 repo);
358 done:
359 if (obj1)
360 got_object_close(obj1);
361 if (obj2)
362 got_object_close(obj2);
363 if (blob1)
364 got_object_blob_close(blob1);
365 if (blob2)
366 got_object_blob_close(blob2);
367 return err;
370 static const struct got_error *
371 diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
372 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
374 const struct got_error *err;
375 struct got_blob_object *blob = NULL;
376 struct got_object *obj = NULL;
378 err = got_object_open(&obj, repo, id);
379 if (err)
380 return err;
382 err = got_object_blob_open(&blob, repo, obj, 8192);
383 if (err)
384 goto done;
385 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
386 done:
387 got_object_close(obj);
388 if (blob)
389 got_object_blob_close(blob);
390 return err;
393 static const struct got_error *
394 diff_added_tree(struct got_object_id *id, const char *label,
395 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
396 int diff_content)
398 const struct got_error *err = NULL;
399 struct got_object *treeobj = NULL;
400 struct got_tree_object *tree = NULL;
402 err = got_object_open(&treeobj, repo, id);
403 if (err)
404 goto done;
406 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
407 err = got_error(GOT_ERR_OBJ_TYPE);
408 goto done;
411 err = got_object_tree_open(&tree, repo, treeobj);
412 if (err)
413 goto done;
415 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
416 diff_content);
417 done:
418 if (tree)
419 got_object_tree_close(tree);
420 if (treeobj)
421 got_object_close(treeobj);
422 return err;
425 static const struct got_error *
426 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
427 const char *label1, const char *label2, struct got_repository *repo,
428 got_diff_blob_cb cb, void *cb_arg, int diff_content)
430 const struct got_error *err;
431 struct got_object *treeobj1 = NULL;
432 struct got_object *treeobj2 = NULL;
433 struct got_tree_object *tree1 = NULL;
434 struct got_tree_object *tree2 = NULL;
436 err = got_object_open(&treeobj1, repo, id1);
437 if (err)
438 goto done;
440 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
441 err = got_error(GOT_ERR_OBJ_TYPE);
442 goto done;
445 err = got_object_open(&treeobj2, repo, id2);
446 if (err)
447 goto done;
449 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
450 err = got_error(GOT_ERR_OBJ_TYPE);
451 goto done;
454 err = got_object_tree_open(&tree1, repo, treeobj1);
455 if (err)
456 goto done;
458 err = got_object_tree_open(&tree2, repo, treeobj2);
459 if (err)
460 goto done;
462 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
463 diff_content);
465 done:
466 if (tree1)
467 got_object_tree_close(tree1);
468 if (tree2)
469 got_object_tree_close(tree2);
470 if (treeobj1)
471 got_object_close(treeobj1);
472 if (treeobj2)
473 got_object_close(treeobj2);
474 return err;
477 static const struct got_error *
478 diff_deleted_tree(struct got_object_id *id, const char *label,
479 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
480 int diff_content)
482 const struct got_error *err;
483 struct got_object *treeobj = NULL;
484 struct got_tree_object *tree = NULL;
486 err = got_object_open(&treeobj, repo, id);
487 if (err)
488 goto done;
490 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
491 err = got_error(GOT_ERR_OBJ_TYPE);
492 goto done;
495 err = got_object_tree_open(&tree, repo, treeobj);
496 if (err)
497 goto done;
499 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
500 diff_content);
501 done:
502 if (tree)
503 got_object_tree_close(tree);
504 if (treeobj)
505 got_object_close(treeobj);
506 return err;
509 static const struct got_error *
510 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
511 const char *label1, const char *label2, struct got_repository *repo,
512 got_diff_blob_cb cb, void *cb_arg)
514 /* XXX TODO */
515 return NULL;
518 static const struct got_error *
519 diff_entry_old_new(const struct got_tree_entry *te1,
520 const struct got_tree_entry *te2, const char *label1, const char *label2,
521 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
522 int diff_content)
524 const struct got_error *err = NULL;
525 int id_match;
527 if (got_object_tree_entry_is_submodule(te1))
528 return NULL;
530 if (te2 == NULL) {
531 if (S_ISDIR(te1->mode))
532 err = diff_deleted_tree(te1->id, label1, repo,
533 cb, cb_arg, diff_content);
534 else {
535 if (diff_content)
536 err = diff_deleted_blob(te1->id, label1,
537 te1->mode, repo, cb, cb_arg);
538 else
539 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
540 label1, NULL, te1->mode, 0, repo);
542 return err;
543 } else if (got_object_tree_entry_is_submodule(te2))
544 return NULL;
546 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
547 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
548 if (!id_match)
549 return diff_modified_tree(te1->id, te2->id,
550 label1, label2, repo, cb, cb_arg, diff_content);
551 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
552 if (!id_match ||
553 (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
554 if (diff_content)
555 return diff_modified_blob(te1->id, te2->id,
556 label1, label2, te1->mode, te2->mode,
557 repo, cb, cb_arg);
558 else
559 return cb(cb_arg, NULL, NULL, te1->id,
560 te2->id, label1, label2, te1->mode,
561 te2->mode, repo);
565 if (id_match)
566 return NULL;
568 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
569 cb, cb_arg);
572 static const struct got_error *
573 diff_entry_new_old(const struct got_tree_entry *te2,
574 const struct got_tree_entry *te1, const char *label2,
575 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
576 int diff_content)
578 if (te1 != NULL) /* handled by diff_entry_old_new() */
579 return NULL;
581 if (got_object_tree_entry_is_submodule(te2))
582 return NULL;
584 if (S_ISDIR(te2->mode))
585 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
586 diff_content);
588 if (diff_content)
589 return diff_added_blob(te2->id, label2, te2->mode, repo, cb,
590 cb_arg);
592 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, 0,
593 te2->mode, repo);
596 const struct got_error *
597 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
598 const char *label1, const char *label2, struct got_repository *repo,
599 got_diff_blob_cb cb, void *cb_arg, int diff_content)
601 const struct got_error *err = NULL;
602 struct got_tree_entry *te1 = NULL;
603 struct got_tree_entry *te2 = NULL;
604 char *l1 = NULL, *l2 = NULL;
606 if (tree1) {
607 const struct got_tree_entries *entries;
608 entries = got_object_tree_get_entries(tree1);
609 te1 = SIMPLEQ_FIRST(&entries->head);
610 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
611 te1->name) == -1)
612 return got_error_from_errno("asprintf");
614 if (tree2) {
615 const struct got_tree_entries *entries;
616 entries = got_object_tree_get_entries(tree2);
617 te2 = SIMPLEQ_FIRST(&entries->head);
618 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
619 te2->name) == -1)
620 return got_error_from_errno("asprintf");
623 do {
624 if (te1) {
625 const struct got_tree_entry *te = NULL;
626 if (tree2)
627 te = got_object_tree_find_entry(tree2,
628 te1->name);
629 if (te) {
630 free(l2);
631 l2 = NULL;
632 if (te && asprintf(&l2, "%s%s%s", label2,
633 label2[0] ? "/" : "", te->name) == -1)
634 return
635 got_error_from_errno("asprintf");
637 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
638 cb_arg, diff_content);
639 if (err)
640 break;
643 if (te2) {
644 const struct got_tree_entry *te = NULL;
645 if (tree1)
646 te = got_object_tree_find_entry(tree1,
647 te2->name);
648 free(l2);
649 if (te) {
650 if (asprintf(&l2, "%s%s%s", label2,
651 label2[0] ? "/" : "", te->name) == -1)
652 return
653 got_error_from_errno("asprintf");
654 } else {
655 if (asprintf(&l2, "%s%s%s", label2,
656 label2[0] ? "/" : "", te2->name) == -1)
657 return
658 got_error_from_errno("asprintf");
660 err = diff_entry_new_old(te2, te, l2, repo,
661 cb, cb_arg, diff_content);
662 if (err)
663 break;
666 free(l1);
667 l1 = NULL;
668 if (te1) {
669 te1 = SIMPLEQ_NEXT(te1, entry);
670 if (te1 &&
671 asprintf(&l1, "%s%s%s", label1,
672 label1[0] ? "/" : "", te1->name) == -1)
673 return got_error_from_errno("asprintf");
675 free(l2);
676 l2 = NULL;
677 if (te2) {
678 te2 = SIMPLEQ_NEXT(te2, entry);
679 if (te2 &&
680 asprintf(&l2, "%s%s%s", label2,
681 label2[0] ? "/" : "", te2->name) == -1)
682 return got_error_from_errno("asprintf");
684 } while (te1 || te2);
686 return err;
689 const struct got_error *
690 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
691 const char *label1, const char *label2, int diff_context,
692 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
694 const struct got_error *err;
695 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
697 if (id1 == NULL && id2 == NULL)
698 return got_error(GOT_ERR_NO_OBJ);
700 if (id1) {
701 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
702 if (err)
703 goto done;
705 if (id2) {
706 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
707 if (err)
708 goto done;
710 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
711 ignore_whitespace, outfile);
712 done:
713 if (blob1)
714 got_object_blob_close(blob1);
715 if (blob2)
716 got_object_blob_close(blob2);
717 return err;
720 const struct got_error *
721 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
722 char *label1, char *label2, int diff_context, int ignore_whitespace,
723 struct got_repository *repo, FILE *outfile)
725 const struct got_error *err;
726 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
727 struct got_diff_blob_output_unidiff_arg arg;
729 if (id1 == NULL && id2 == NULL)
730 return got_error(GOT_ERR_NO_OBJ);
732 if (id1) {
733 err = got_object_open_as_tree(&tree1, repo, id1);
734 if (err)
735 goto done;
737 if (id2) {
738 err = got_object_open_as_tree(&tree2, repo, id2);
739 if (err)
740 goto done;
742 arg.diff_context = diff_context;
743 arg.ignore_whitespace = ignore_whitespace;
744 arg.outfile = outfile;
745 err = got_diff_tree(tree1, tree2, label1, label2, repo,
746 got_diff_blob_output_unidiff, &arg, 1);
747 done:
748 if (tree1)
749 got_object_tree_close(tree1);
750 if (tree2)
751 got_object_tree_close(tree2);
752 return err;
755 const struct got_error *
756 got_diff_objects_as_commits(struct got_object_id *id1,
757 struct got_object_id *id2, int diff_context, int ignore_whitespace,
758 struct got_repository *repo, FILE *outfile)
760 const struct got_error *err;
761 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
763 if (id2 == NULL)
764 return got_error(GOT_ERR_NO_OBJ);
766 if (id1) {
767 err = got_object_open_as_commit(&commit1, repo, id1);
768 if (err)
769 goto done;
772 err = got_object_open_as_commit(&commit2, repo, id2);
773 if (err)
774 goto done;
776 err = got_diff_objects_as_trees(
777 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
778 got_object_commit_get_tree_id(commit2), "", "", diff_context,
779 ignore_whitespace, repo, outfile);
780 done:
781 if (commit1)
782 got_object_commit_close(commit1);
783 if (commit2)
784 got_object_commit_close(commit2);
785 return err;
788 const struct got_error *
789 got_diff_files(struct got_diff_changes **changes,
790 struct got_diff_state **ds,
791 struct got_diff_args **args,
792 int *flags,
793 FILE *f1, size_t size1, const char *label1,
794 FILE *f2, size_t size2, const char *label2,
795 int diff_context, FILE *outfile)
797 const struct got_error *err = NULL;
798 int res;
800 *flags = 0;
801 *ds = calloc(1, sizeof(**ds));
802 if (*ds == NULL)
803 return got_error_from_errno("calloc");
804 *args = calloc(1, sizeof(**args));
805 if (*args == NULL) {
806 err = got_error_from_errno("calloc");
807 goto done;
810 if (changes)
811 *changes = NULL;
813 if (f1 == NULL)
814 *flags |= D_EMPTY1;
816 if (f2 == NULL)
817 *flags |= D_EMPTY2;
819 /* XXX should stat buffers be passed in args instead of ds? */
820 (*ds)->stb1.st_mode = S_IFREG;
821 (*ds)->stb1.st_size = size1;
822 (*ds)->stb1.st_mtime = 0; /* XXX */
824 (*ds)->stb2.st_mode = S_IFREG;
825 (*ds)->stb2.st_size = size2;
826 (*ds)->stb2.st_mtime = 0; /* XXX */
828 (*args)->diff_format = D_UNIFIED;
829 (*args)->label[0] = label1;
830 (*args)->label[1] = label2;
831 (*args)->diff_context = diff_context;
832 *flags |= D_PROTOTYPE;
834 if (outfile) {
835 fprintf(outfile, "file - %s\n",
836 f1 == NULL ? "/dev/null" : label1);
837 fprintf(outfile, "file + %s\n",
838 f2 == NULL ? "/dev/null" : label2);
840 if (changes) {
841 err = alloc_changes(changes);
842 if (err)
843 goto done;
845 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
846 changes ? *changes : NULL);
847 done:
848 if (err) {
849 if (*ds) {
850 got_diff_state_free(*ds);
851 free(*ds);
852 *ds = NULL;
854 if (*args) {
855 free(*args);
856 *args = NULL;
858 if (changes) {
859 if (*changes)
860 got_diff_free_changes(*changes);
861 *changes = NULL;
864 return err;