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(struct got_tree_entry *te1,
520 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(struct got_tree_entry *te2,
574 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;
605 int tidx1 = 0, tidx2 = 0;
607 if (tree1) {
608 te1 = got_object_tree_get_entry(tree1, 0);
609 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
610 te1->name) == -1)
611 return got_error_from_errno("asprintf");
613 if (tree2) {
614 te2 = got_object_tree_get_entry(tree2, 0);
615 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
616 te2->name) == -1)
617 return got_error_from_errno("asprintf");
620 do {
621 if (te1) {
622 struct got_tree_entry *te = NULL;
623 if (tree2)
624 te = got_object_tree_find_entry(tree2,
625 te1->name);
626 if (te) {
627 free(l2);
628 l2 = NULL;
629 if (te && asprintf(&l2, "%s%s%s", label2,
630 label2[0] ? "/" : "", te->name) == -1)
631 return
632 got_error_from_errno("asprintf");
634 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
635 cb_arg, diff_content);
636 if (err)
637 break;
640 if (te2) {
641 struct got_tree_entry *te = NULL;
642 if (tree1)
643 te = got_object_tree_find_entry(tree1,
644 te2->name);
645 free(l2);
646 if (te) {
647 if (asprintf(&l2, "%s%s%s", label2,
648 label2[0] ? "/" : "", te->name) == -1)
649 return
650 got_error_from_errno("asprintf");
651 } else {
652 if (asprintf(&l2, "%s%s%s", label2,
653 label2[0] ? "/" : "", te2->name) == -1)
654 return
655 got_error_from_errno("asprintf");
657 err = diff_entry_new_old(te2, te, l2, repo,
658 cb, cb_arg, diff_content);
659 if (err)
660 break;
663 free(l1);
664 l1 = NULL;
665 if (te1) {
666 tidx1++;
667 te1 = got_object_tree_get_entry(tree1, tidx1);
668 if (te1 &&
669 asprintf(&l1, "%s%s%s", label1,
670 label1[0] ? "/" : "", te1->name) == -1)
671 return got_error_from_errno("asprintf");
673 free(l2);
674 l2 = NULL;
675 if (te2) {
676 tidx2++;
677 te2 = got_object_tree_get_entry(tree2, tidx2);
678 if (te2 &&
679 asprintf(&l2, "%s%s%s", label2,
680 label2[0] ? "/" : "", te2->name) == -1)
681 return got_error_from_errno("asprintf");
683 } while (te1 || te2);
685 return err;
688 const struct got_error *
689 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
690 const char *label1, const char *label2, int diff_context,
691 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
693 const struct got_error *err;
694 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
696 if (id1 == NULL && id2 == NULL)
697 return got_error(GOT_ERR_NO_OBJ);
699 if (id1) {
700 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
701 if (err)
702 goto done;
704 if (id2) {
705 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
706 if (err)
707 goto done;
709 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
710 ignore_whitespace, outfile);
711 done:
712 if (blob1)
713 got_object_blob_close(blob1);
714 if (blob2)
715 got_object_blob_close(blob2);
716 return err;
719 const struct got_error *
720 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
721 char *label1, char *label2, int diff_context, int ignore_whitespace,
722 struct got_repository *repo, FILE *outfile)
724 const struct got_error *err;
725 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
726 struct got_diff_blob_output_unidiff_arg arg;
728 if (id1 == NULL && id2 == NULL)
729 return got_error(GOT_ERR_NO_OBJ);
731 if (id1) {
732 err = got_object_open_as_tree(&tree1, repo, id1);
733 if (err)
734 goto done;
736 if (id2) {
737 err = got_object_open_as_tree(&tree2, repo, id2);
738 if (err)
739 goto done;
741 arg.diff_context = diff_context;
742 arg.ignore_whitespace = ignore_whitespace;
743 arg.outfile = outfile;
744 err = got_diff_tree(tree1, tree2, label1, label2, repo,
745 got_diff_blob_output_unidiff, &arg, 1);
746 done:
747 if (tree1)
748 got_object_tree_close(tree1);
749 if (tree2)
750 got_object_tree_close(tree2);
751 return err;
754 const struct got_error *
755 got_diff_objects_as_commits(struct got_object_id *id1,
756 struct got_object_id *id2, int diff_context, int ignore_whitespace,
757 struct got_repository *repo, FILE *outfile)
759 const struct got_error *err;
760 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
762 if (id2 == NULL)
763 return got_error(GOT_ERR_NO_OBJ);
765 if (id1) {
766 err = got_object_open_as_commit(&commit1, repo, id1);
767 if (err)
768 goto done;
771 err = got_object_open_as_commit(&commit2, repo, id2);
772 if (err)
773 goto done;
775 err = got_diff_objects_as_trees(
776 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
777 got_object_commit_get_tree_id(commit2), "", "", diff_context,
778 ignore_whitespace, repo, outfile);
779 done:
780 if (commit1)
781 got_object_commit_close(commit1);
782 if (commit2)
783 got_object_commit_close(commit2);
784 return err;
787 const struct got_error *
788 got_diff_files(struct got_diff_changes **changes,
789 struct got_diff_state **ds,
790 struct got_diff_args **args,
791 int *flags,
792 FILE *f1, size_t size1, const char *label1,
793 FILE *f2, size_t size2, const char *label2,
794 int diff_context, FILE *outfile)
796 const struct got_error *err = NULL;
797 int res;
799 *flags = 0;
800 *ds = calloc(1, sizeof(**ds));
801 if (*ds == NULL)
802 return got_error_from_errno("calloc");
803 *args = calloc(1, sizeof(**args));
804 if (*args == NULL) {
805 err = got_error_from_errno("calloc");
806 goto done;
809 if (changes)
810 *changes = NULL;
812 if (f1 == NULL)
813 *flags |= D_EMPTY1;
815 if (f2 == NULL)
816 *flags |= D_EMPTY2;
818 /* XXX should stat buffers be passed in args instead of ds? */
819 (*ds)->stb1.st_mode = S_IFREG;
820 (*ds)->stb1.st_size = size1;
821 (*ds)->stb1.st_mtime = 0; /* XXX */
823 (*ds)->stb2.st_mode = S_IFREG;
824 (*ds)->stb2.st_size = size2;
825 (*ds)->stb2.st_mtime = 0; /* XXX */
827 (*args)->diff_format = D_UNIFIED;
828 (*args)->label[0] = label1;
829 (*args)->label[1] = label2;
830 (*args)->diff_context = diff_context;
831 *flags |= D_PROTOTYPE;
833 if (outfile) {
834 fprintf(outfile, "file - %s\n",
835 f1 == NULL ? "/dev/null" : label1);
836 fprintf(outfile, "file + %s\n",
837 f2 == NULL ? "/dev/null" : label2);
839 if (changes) {
840 err = alloc_changes(changes);
841 if (err)
842 goto done;
844 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
845 changes ? *changes : NULL);
846 done:
847 if (err) {
848 if (*ds) {
849 got_diff_state_free(*ds);
850 free(*ds);
851 *ds = NULL;
853 if (*args) {
854 free(*args);
855 *args = NULL;
857 if (changes) {
858 if (*changes)
859 got_diff_free_changes(*changes);
860 *changes = NULL;
863 return err;