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, int diff_context,
42 int ignore_whitespace, FILE *outfile, struct got_diff_changes *changes)
43 {
44 struct got_diff_state ds;
45 struct got_diff_args args;
46 const struct got_error *err = NULL;
47 FILE *f1 = NULL, *f2 = NULL;
48 char hex1[SHA1_DIGEST_STRING_LENGTH];
49 char hex2[SHA1_DIGEST_STRING_LENGTH];
50 char *idstr1 = NULL, *idstr2 = NULL;
51 size_t size1, size2;
52 int res, flags = 0;
54 if (blob1) {
55 f1 = got_opentemp();
56 if (f1 == NULL)
57 return got_error_from_errno("got_opentemp");
58 } else
59 flags |= D_EMPTY1;
61 if (blob2) {
62 f2 = got_opentemp();
63 if (f2 == NULL) {
64 err = got_error_from_errno("got_opentemp");
65 fclose(f1);
66 return err;
67 }
68 } else
69 flags |= D_EMPTY2;
71 size1 = 0;
72 if (blob1) {
73 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
74 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
75 blob1);
76 if (err)
77 goto done;
78 } else
79 idstr1 = "/dev/null";
81 size2 = 0;
82 if (blob2) {
83 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
84 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
85 blob2);
86 if (err)
87 goto done;
88 } else
89 idstr2 = "/dev/null";
91 memset(&ds, 0, sizeof(ds));
92 /* XXX should stat buffers be passed in args instead of ds? */
93 ds.stb1.st_mode = S_IFREG;
94 if (blob1)
95 ds.stb1.st_size = size1;
96 ds.stb1.st_mtime = 0; /* XXX */
98 ds.stb2.st_mode = S_IFREG;
99 if (blob2)
100 ds.stb2.st_size = size2;
101 ds.stb2.st_mtime = 0; /* XXX */
103 memset(&args, 0, sizeof(args));
104 args.diff_format = D_UNIFIED;
105 args.label[0] = label1 ? label1 : idstr1;
106 args.label[1] = label2 ? label2 : idstr2;
107 args.diff_context = diff_context;
108 flags |= D_PROTOTYPE;
109 if (ignore_whitespace)
110 flags |= D_IGNOREBLANKS;
112 if (outfile) {
113 fprintf(outfile, "blob - %s\n", idstr1);
114 fprintf(outfile, "blob + %s\n", idstr2);
116 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
117 got_diff_state_free(&ds);
118 done:
119 if (f1 && fclose(f1) != 0 && err == NULL)
120 err = got_error_from_errno("fclose");
121 if (f2 && fclose(f2) != 0 && err == NULL)
122 err = got_error_from_errno("fclose");
123 return err;
126 const struct got_error *
127 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
128 struct got_blob_object *blob2, struct got_object_id *id1,
129 struct got_object_id *id2, const char *label1, const char *label2,
130 struct got_repository *repo)
132 struct got_diff_blob_output_unidiff_arg *a = arg;
134 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
135 a->ignore_whitespace, a->outfile, NULL);
138 const struct got_error *
139 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
140 const char *label1, const char *label2, int diff_context,
141 int ignore_whitespace, FILE *outfile)
143 return diff_blobs(blob1, blob2, label1, label2, diff_context,
144 ignore_whitespace, outfile, NULL);
147 static const struct got_error *
148 alloc_changes(struct got_diff_changes **changes)
150 *changes = calloc(1, sizeof(**changes));
151 if (*changes == NULL)
152 return got_error_from_errno("calloc");
153 SIMPLEQ_INIT(&(*changes)->entries);
154 return NULL;
157 static const struct got_error *
158 diff_blob_file(struct got_diff_changes **changes,
159 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
160 const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
162 struct got_diff_state ds;
163 struct got_diff_args args;
164 const struct got_error *err = NULL;
165 FILE *f1 = NULL;
166 char hex1[SHA1_DIGEST_STRING_LENGTH];
167 char *idstr1 = NULL;
168 size_t size1;
169 int res, flags = 0;
171 if (changes)
172 *changes = NULL;
174 size1 = 0;
175 if (blob1) {
176 f1 = got_opentemp();
177 if (f1 == NULL)
178 return got_error_from_errno("got_opentemp");
179 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
180 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
181 blob1);
182 if (err)
183 goto done;
184 } else {
185 flags |= D_EMPTY1;
186 idstr1 = "/dev/null";
189 if (f2 == NULL)
190 flags |= D_EMPTY2;
192 memset(&ds, 0, sizeof(ds));
193 /* XXX should stat buffers be passed in args instead of ds? */
194 ds.stb1.st_mode = S_IFREG;
195 if (blob1)
196 ds.stb1.st_size = size1;
197 ds.stb1.st_mtime = 0; /* XXX */
199 ds.stb2.st_mode = S_IFREG;
200 ds.stb2.st_size = size2;
201 ds.stb2.st_mtime = 0; /* XXX */
203 memset(&args, 0, sizeof(args));
204 args.diff_format = D_UNIFIED;
205 args.label[0] = label2;
206 args.label[1] = label2;
207 args.diff_context = diff_context;
208 flags |= D_PROTOTYPE;
209 if (ignore_whitespace)
210 flags |= D_IGNOREBLANKS;
212 if (outfile) {
213 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
214 fprintf(outfile, "file + %s\n",
215 f2 == NULL ? "/dev/null" : label2);
217 if (changes) {
218 err = alloc_changes(changes);
219 if (err)
220 return err;
222 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
223 changes ? *changes : NULL);
224 got_diff_state_free(&ds);
225 done:
226 if (f1 && fclose(f1) != 0 && err == NULL)
227 err = got_error_from_errno("fclose");
228 return err;
231 const struct got_error *
232 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
233 FILE *f2, size_t size2, const char *label2, int diff_context,
234 int ignore_whitespace, FILE *outfile)
236 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
237 diff_context, ignore_whitespace, outfile);
240 const struct got_error *
241 got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
242 struct got_blob_object *blob1, FILE *f2, size_t size2)
244 return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
245 0, 0, NULL);
248 const struct got_error *
249 got_diff_blob_lines_changed(struct got_diff_changes **changes,
250 struct got_blob_object *blob1, struct got_blob_object *blob2)
252 const struct got_error *err = NULL;
254 err = alloc_changes(changes);
255 if (err)
256 return err;
258 err = diff_blobs(blob1, blob2, NULL, NULL, 3, 0, NULL, *changes);
259 if (err) {
260 got_diff_free_changes(*changes);
261 *changes = NULL;
263 return err;
266 void
267 got_diff_free_changes(struct got_diff_changes *changes)
269 struct got_diff_change *change;
270 while (!SIMPLEQ_EMPTY(&changes->entries)) {
271 change = SIMPLEQ_FIRST(&changes->entries);
272 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
273 free(change);
275 free(changes);
278 static const struct got_error *
279 diff_added_blob(struct got_object_id *id, const char *label,
280 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
282 const struct got_error *err;
283 struct got_blob_object *blob = NULL;
284 struct got_object *obj = NULL;
286 err = got_object_open(&obj, repo, id);
287 if (err)
288 return err;
290 err = got_object_blob_open(&blob, repo, obj, 8192);
291 if (err)
292 goto done;
293 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
294 done:
295 got_object_close(obj);
296 if (blob)
297 got_object_blob_close(blob);
298 return err;
301 static const struct got_error *
302 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
303 const char *label1, const char *label2, struct got_repository *repo,
304 got_diff_blob_cb cb, void *cb_arg)
306 const struct got_error *err;
307 struct got_object *obj1 = NULL;
308 struct got_object *obj2 = NULL;
309 struct got_blob_object *blob1 = NULL;
310 struct got_blob_object *blob2 = NULL;
312 err = got_object_open(&obj1, repo, id1);
313 if (err)
314 return err;
315 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
316 err = got_error(GOT_ERR_OBJ_TYPE);
317 goto done;
320 err = got_object_open(&obj2, repo, id2);
321 if (err)
322 goto done;
323 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
324 err = got_error(GOT_ERR_BAD_OBJ_DATA);
325 goto done;
328 err = got_object_blob_open(&blob1, repo, obj1, 8192);
329 if (err)
330 goto done;
332 err = got_object_blob_open(&blob2, repo, obj2, 8192);
333 if (err)
334 goto done;
336 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
337 done:
338 if (obj1)
339 got_object_close(obj1);
340 if (obj2)
341 got_object_close(obj2);
342 if (blob1)
343 got_object_blob_close(blob1);
344 if (blob2)
345 got_object_blob_close(blob2);
346 return err;
349 static const struct got_error *
350 diff_deleted_blob(struct got_object_id *id, const char *label,
351 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
353 const struct got_error *err;
354 struct got_blob_object *blob = NULL;
355 struct got_object *obj = NULL;
357 err = got_object_open(&obj, repo, id);
358 if (err)
359 return err;
361 err = got_object_blob_open(&blob, repo, obj, 8192);
362 if (err)
363 goto done;
364 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
365 done:
366 got_object_close(obj);
367 if (blob)
368 got_object_blob_close(blob);
369 return err;
372 static const struct got_error *
373 diff_added_tree(struct got_object_id *id, const char *label,
374 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
375 int diff_content)
377 const struct got_error *err = NULL;
378 struct got_object *treeobj = NULL;
379 struct got_tree_object *tree = NULL;
381 err = got_object_open(&treeobj, repo, id);
382 if (err)
383 goto done;
385 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
386 err = got_error(GOT_ERR_OBJ_TYPE);
387 goto done;
390 err = got_object_tree_open(&tree, repo, treeobj);
391 if (err)
392 goto done;
394 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
395 diff_content);
396 done:
397 if (tree)
398 got_object_tree_close(tree);
399 if (treeobj)
400 got_object_close(treeobj);
401 return err;
404 static const struct got_error *
405 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
406 const char *label1, const char *label2, struct got_repository *repo,
407 got_diff_blob_cb cb, void *cb_arg, int diff_content)
409 const struct got_error *err;
410 struct got_object *treeobj1 = NULL;
411 struct got_object *treeobj2 = NULL;
412 struct got_tree_object *tree1 = NULL;
413 struct got_tree_object *tree2 = NULL;
415 err = got_object_open(&treeobj1, repo, id1);
416 if (err)
417 goto done;
419 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
420 err = got_error(GOT_ERR_OBJ_TYPE);
421 goto done;
424 err = got_object_open(&treeobj2, repo, id2);
425 if (err)
426 goto done;
428 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
429 err = got_error(GOT_ERR_OBJ_TYPE);
430 goto done;
433 err = got_object_tree_open(&tree1, repo, treeobj1);
434 if (err)
435 goto done;
437 err = got_object_tree_open(&tree2, repo, treeobj2);
438 if (err)
439 goto done;
441 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
442 diff_content);
444 done:
445 if (tree1)
446 got_object_tree_close(tree1);
447 if (tree2)
448 got_object_tree_close(tree2);
449 if (treeobj1)
450 got_object_close(treeobj1);
451 if (treeobj2)
452 got_object_close(treeobj2);
453 return err;
456 static const struct got_error *
457 diff_deleted_tree(struct got_object_id *id, const char *label,
458 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
459 int diff_content)
461 const struct got_error *err;
462 struct got_object *treeobj = NULL;
463 struct got_tree_object *tree = NULL;
465 err = got_object_open(&treeobj, repo, id);
466 if (err)
467 goto done;
469 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
470 err = got_error(GOT_ERR_OBJ_TYPE);
471 goto done;
474 err = got_object_tree_open(&tree, repo, treeobj);
475 if (err)
476 goto done;
478 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
479 diff_content);
480 done:
481 if (tree)
482 got_object_tree_close(tree);
483 if (treeobj)
484 got_object_close(treeobj);
485 return err;
488 static const struct got_error *
489 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
490 const char *label1, const char *label2, struct got_repository *repo,
491 got_diff_blob_cb cb, void *cb_arg)
493 /* XXX TODO */
494 return NULL;
497 static const struct got_error *
498 diff_entry_old_new(const struct got_tree_entry *te1,
499 const struct got_tree_entry *te2, const char *label1, const char *label2,
500 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
501 int diff_content)
503 const struct got_error *err = NULL;
504 int id_match;
506 if (got_object_tree_entry_is_submodule(te1))
507 return NULL;
509 if (te2 == NULL) {
510 if (S_ISDIR(te1->mode))
511 err = diff_deleted_tree(te1->id, label1, repo,
512 cb, cb_arg, diff_content);
513 else {
514 if (diff_content)
515 err = diff_deleted_blob(te1->id, label1, repo,
516 cb, cb_arg);
517 else
518 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
519 label1, NULL, repo);
521 return err;
522 } else if (got_object_tree_entry_is_submodule(te2))
523 return NULL;
525 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
526 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
527 if (!id_match)
528 return diff_modified_tree(te1->id, te2->id,
529 label1, label2, repo, cb, cb_arg, diff_content);
530 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
531 if (!id_match) {
532 if (diff_content)
533 return diff_modified_blob(te1->id, te2->id,
534 label1, label2, repo, cb, cb_arg);
535 else
536 return cb(cb_arg, NULL, NULL, te1->id,
537 te2->id, label1, label2, repo);
541 if (id_match)
542 return NULL;
544 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
545 cb, cb_arg);
548 static const struct got_error *
549 diff_entry_new_old(const struct got_tree_entry *te2,
550 const struct got_tree_entry *te1, const char *label2,
551 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
552 int diff_content)
554 if (te1 != NULL) /* handled by diff_entry_old_new() */
555 return NULL;
557 if (got_object_tree_entry_is_submodule(te2))
558 return NULL;
560 if (S_ISDIR(te2->mode))
561 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
562 diff_content);
564 if (diff_content)
565 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
567 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
570 const struct got_error *
571 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
572 const char *label1, const char *label2, struct got_repository *repo,
573 got_diff_blob_cb cb, void *cb_arg, int diff_content)
575 const struct got_error *err = NULL;
576 struct got_tree_entry *te1 = NULL;
577 struct got_tree_entry *te2 = NULL;
578 char *l1 = NULL, *l2 = NULL;
580 if (tree1) {
581 const struct got_tree_entries *entries;
582 entries = got_object_tree_get_entries(tree1);
583 te1 = SIMPLEQ_FIRST(&entries->head);
584 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
585 te1->name) == -1)
586 return got_error_from_errno("asprintf");
588 if (tree2) {
589 const struct got_tree_entries *entries;
590 entries = got_object_tree_get_entries(tree2);
591 te2 = SIMPLEQ_FIRST(&entries->head);
592 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
593 te2->name) == -1)
594 return got_error_from_errno("asprintf");
597 do {
598 if (te1) {
599 const struct got_tree_entry *te = NULL;
600 if (tree2)
601 te = got_object_tree_find_entry(tree2,
602 te1->name);
603 if (te) {
604 free(l2);
605 l2 = NULL;
606 if (te && asprintf(&l2, "%s%s%s", label2,
607 label2[0] ? "/" : "", te->name) == -1)
608 return
609 got_error_from_errno("asprintf");
611 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
612 cb_arg, diff_content);
613 if (err)
614 break;
617 if (te2) {
618 const struct got_tree_entry *te = NULL;
619 if (tree1)
620 te = got_object_tree_find_entry(tree1,
621 te2->name);
622 free(l2);
623 if (te) {
624 if (asprintf(&l2, "%s%s%s", label2,
625 label2[0] ? "/" : "", te->name) == -1)
626 return
627 got_error_from_errno("asprintf");
628 } else {
629 if (asprintf(&l2, "%s%s%s", label2,
630 label2[0] ? "/" : "", te2->name) == -1)
631 return
632 got_error_from_errno("asprintf");
634 err = diff_entry_new_old(te2, te, l2, repo,
635 cb, cb_arg, diff_content);
636 if (err)
637 break;
640 free(l1);
641 l1 = NULL;
642 if (te1) {
643 te1 = SIMPLEQ_NEXT(te1, entry);
644 if (te1 &&
645 asprintf(&l1, "%s%s%s", label1,
646 label1[0] ? "/" : "", te1->name) == -1)
647 return got_error_from_errno("asprintf");
649 free(l2);
650 l2 = NULL;
651 if (te2) {
652 te2 = SIMPLEQ_NEXT(te2, entry);
653 if (te2 &&
654 asprintf(&l2, "%s%s%s", label2,
655 label2[0] ? "/" : "", te2->name) == -1)
656 return got_error_from_errno("asprintf");
658 } while (te1 || te2);
660 return err;
663 const struct got_error *
664 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
665 const char *label1, const char *label2, int diff_context,
666 int ignore_whitespace, struct got_repository *repo, FILE *outfile)
668 const struct got_error *err;
669 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
671 if (id1 == NULL && id2 == NULL)
672 return got_error(GOT_ERR_NO_OBJ);
674 if (id1) {
675 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
676 if (err)
677 goto done;
679 if (id2) {
680 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
681 if (err)
682 goto done;
684 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
685 ignore_whitespace, outfile);
686 done:
687 if (blob1)
688 got_object_blob_close(blob1);
689 if (blob2)
690 got_object_blob_close(blob2);
691 return err;
694 const struct got_error *
695 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
696 char *label1, char *label2, int diff_context, int ignore_whitespace,
697 struct got_repository *repo, FILE *outfile)
699 const struct got_error *err;
700 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
701 struct got_diff_blob_output_unidiff_arg arg;
703 if (id1 == NULL && id2 == NULL)
704 return got_error(GOT_ERR_NO_OBJ);
706 if (id1) {
707 err = got_object_open_as_tree(&tree1, repo, id1);
708 if (err)
709 goto done;
711 if (id2) {
712 err = got_object_open_as_tree(&tree2, repo, id2);
713 if (err)
714 goto done;
716 arg.diff_context = diff_context;
717 arg.ignore_whitespace = ignore_whitespace;
718 arg.outfile = outfile;
719 err = got_diff_tree(tree1, tree2, label1, label2, repo,
720 got_diff_blob_output_unidiff, &arg, 1);
721 done:
722 if (tree1)
723 got_object_tree_close(tree1);
724 if (tree2)
725 got_object_tree_close(tree2);
726 return err;
729 const struct got_error *
730 got_diff_objects_as_commits(struct got_object_id *id1,
731 struct got_object_id *id2, int diff_context, int ignore_whitespace,
732 struct got_repository *repo, FILE *outfile)
734 const struct got_error *err;
735 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
737 if (id2 == NULL)
738 return got_error(GOT_ERR_NO_OBJ);
740 if (id1) {
741 err = got_object_open_as_commit(&commit1, repo, id1);
742 if (err)
743 goto done;
746 err = got_object_open_as_commit(&commit2, repo, id2);
747 if (err)
748 goto done;
750 err = got_diff_objects_as_trees(
751 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
752 got_object_commit_get_tree_id(commit2), "", "", diff_context,
753 ignore_whitespace, repo, outfile);
754 done:
755 if (commit1)
756 got_object_commit_close(commit1);
757 if (commit2)
758 got_object_commit_close(commit2);
759 return err;
762 const struct got_error *
763 got_diff_files(struct got_diff_changes **changes,
764 struct got_diff_state **ds,
765 struct got_diff_args **args,
766 int *flags,
767 FILE *f1, size_t size1, const char *label1,
768 FILE *f2, size_t size2, const char *label2,
769 int diff_context, FILE *outfile)
771 const struct got_error *err = NULL;
772 int res;
774 *flags = 0;
775 *ds = calloc(1, sizeof(**ds));
776 if (*ds == NULL)
777 return got_error_from_errno("calloc");
778 *args = calloc(1, sizeof(**args));
779 if (*args == NULL) {
780 err = got_error_from_errno("calloc");
781 goto done;
784 if (changes)
785 *changes = NULL;
787 if (f1 == NULL)
788 *flags |= D_EMPTY1;
790 if (f2 == NULL)
791 *flags |= D_EMPTY2;
793 /* XXX should stat buffers be passed in args instead of ds? */
794 (*ds)->stb1.st_mode = S_IFREG;
795 (*ds)->stb1.st_size = size1;
796 (*ds)->stb1.st_mtime = 0; /* XXX */
798 (*ds)->stb2.st_mode = S_IFREG;
799 (*ds)->stb2.st_size = size2;
800 (*ds)->stb2.st_mtime = 0; /* XXX */
802 (*args)->diff_format = D_UNIFIED;
803 (*args)->label[0] = label1;
804 (*args)->label[1] = label2;
805 (*args)->diff_context = diff_context;
806 *flags |= D_PROTOTYPE;
808 if (outfile) {
809 fprintf(outfile, "file - %s\n",
810 f1 == NULL ? "/dev/null" : label1);
811 fprintf(outfile, "file + %s\n",
812 f2 == NULL ? "/dev/null" : label2);
814 if (changes) {
815 err = alloc_changes(changes);
816 if (err)
817 goto done;
819 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
820 changes ? *changes : NULL);
821 done:
822 if (err) {
823 if (*ds) {
824 got_diff_state_free(*ds);
825 free(*ds);
826 *ds = NULL;
828 if (*args) {
829 free(*args);
830 *args = NULL;
832 if (changes) {
833 if (*changes)
834 got_diff_free_changes(*changes);
835 *changes = NULL;
838 return err;