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, FILE *outfile,
42 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;
110 if (outfile) {
111 fprintf(outfile, "blob - %s\n", idstr1);
112 fprintf(outfile, "blob + %s\n", idstr2);
114 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
115 got_diff_state_free(&ds);
116 done:
117 if (f1 && fclose(f1) != 0 && err == NULL)
118 err = got_error_from_errno("fclose");
119 if (f2 && fclose(f2) != 0 && err == NULL)
120 err = got_error_from_errno("fclose");
121 return err;
124 const struct got_error *
125 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
126 struct got_blob_object *blob2, struct got_object_id *id1,
127 struct got_object_id *id2, const char *label1, const char *label2,
128 struct got_repository *repo)
130 struct got_diff_blob_output_unidiff_arg *a = arg;
132 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
133 a->outfile, NULL);
136 const struct got_error *
137 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
138 const char *label1, const char *label2, int diff_context, FILE *outfile)
140 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
141 NULL);
144 static const struct got_error *
145 alloc_changes(struct got_diff_changes **changes)
147 *changes = calloc(1, sizeof(**changes));
148 if (*changes == NULL)
149 return got_error_from_errno("calloc");
150 SIMPLEQ_INIT(&(*changes)->entries);
151 return NULL;
154 static const struct got_error *
155 diff_blob_file(struct got_diff_changes **changes,
156 struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
157 const char *label2, int diff_context, FILE *outfile)
159 struct got_diff_state ds;
160 struct got_diff_args args;
161 const struct got_error *err = NULL;
162 FILE *f1 = NULL;
163 char hex1[SHA1_DIGEST_STRING_LENGTH];
164 char *idstr1 = NULL;
165 size_t size1;
166 int res, flags = 0;
168 if (changes)
169 *changes = NULL;
171 size1 = 0;
172 if (blob1) {
173 f1 = got_opentemp();
174 if (f1 == NULL)
175 return got_error_from_errno("got_opentemp");
176 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
177 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
178 blob1);
179 if (err)
180 goto done;
181 } else {
182 flags |= D_EMPTY1;
183 idstr1 = "/dev/null";
186 if (f2 == NULL)
187 flags |= D_EMPTY2;
189 memset(&ds, 0, sizeof(ds));
190 /* XXX should stat buffers be passed in args instead of ds? */
191 ds.stb1.st_mode = S_IFREG;
192 if (blob1)
193 ds.stb1.st_size = size1;
194 ds.stb1.st_mtime = 0; /* XXX */
196 ds.stb2.st_mode = S_IFREG;
197 ds.stb2.st_size = size2;
198 ds.stb2.st_mtime = 0; /* XXX */
200 memset(&args, 0, sizeof(args));
201 args.diff_format = D_UNIFIED;
202 args.label[0] = label2;
203 args.label[1] = label2;
204 args.diff_context = diff_context;
205 flags |= D_PROTOTYPE;
207 if (outfile) {
208 fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
209 fprintf(outfile, "file + %s\n",
210 f2 == NULL ? "/dev/null" : label2);
212 if (changes) {
213 err = alloc_changes(changes);
214 if (err)
215 return err;
217 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
218 changes ? *changes : NULL);
219 got_diff_state_free(&ds);
220 done:
221 if (f1 && fclose(f1) != 0 && err == NULL)
222 err = got_error_from_errno("fclose");
223 return err;
226 const struct got_error *
227 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
228 FILE *f2, size_t size2, const char *label2, int diff_context,
229 FILE *outfile)
231 return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
232 diff_context, outfile);
235 const struct got_error *
236 got_diff_blob_lines_changed(struct got_diff_changes **changes,
237 struct got_blob_object *blob1, struct got_blob_object *blob2)
239 const struct got_error *err = NULL;
241 err = alloc_changes(changes);
242 if (err)
243 return err;
245 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
246 if (err) {
247 got_diff_free_changes(*changes);
248 *changes = NULL;
250 return err;
253 void
254 got_diff_free_changes(struct got_diff_changes *changes)
256 struct got_diff_change *change;
257 while (!SIMPLEQ_EMPTY(&changes->entries)) {
258 change = SIMPLEQ_FIRST(&changes->entries);
259 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
260 free(change);
262 free(changes);
265 static const struct got_error *
266 diff_added_blob(struct got_object_id *id, const char *label,
267 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
269 const struct got_error *err;
270 struct got_blob_object *blob = NULL;
271 struct got_object *obj = NULL;
273 err = got_object_open(&obj, repo, id);
274 if (err)
275 return err;
277 err = got_object_blob_open(&blob, repo, obj, 8192);
278 if (err)
279 goto done;
280 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
281 done:
282 got_object_close(obj);
283 if (blob)
284 got_object_blob_close(blob);
285 return err;
288 static const struct got_error *
289 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
290 const char *label1, const char *label2, struct got_repository *repo,
291 got_diff_blob_cb cb, void *cb_arg)
293 const struct got_error *err;
294 struct got_object *obj1 = NULL;
295 struct got_object *obj2 = NULL;
296 struct got_blob_object *blob1 = NULL;
297 struct got_blob_object *blob2 = NULL;
299 err = got_object_open(&obj1, repo, id1);
300 if (err)
301 return err;
302 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
304 goto done;
307 err = got_object_open(&obj2, repo, id2);
308 if (err)
309 goto done;
310 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
311 err = got_error(GOT_ERR_BAD_OBJ_DATA);
312 goto done;
315 err = got_object_blob_open(&blob1, repo, obj1, 8192);
316 if (err)
317 goto done;
319 err = got_object_blob_open(&blob2, repo, obj2, 8192);
320 if (err)
321 goto done;
323 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
324 done:
325 if (obj1)
326 got_object_close(obj1);
327 if (obj2)
328 got_object_close(obj2);
329 if (blob1)
330 got_object_blob_close(blob1);
331 if (blob2)
332 got_object_blob_close(blob2);
333 return err;
336 static const struct got_error *
337 diff_deleted_blob(struct got_object_id *id, const char *label,
338 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
340 const struct got_error *err;
341 struct got_blob_object *blob = NULL;
342 struct got_object *obj = NULL;
344 err = got_object_open(&obj, repo, id);
345 if (err)
346 return err;
348 err = got_object_blob_open(&blob, repo, obj, 8192);
349 if (err)
350 goto done;
351 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
352 done:
353 got_object_close(obj);
354 if (blob)
355 got_object_blob_close(blob);
356 return err;
359 static const struct got_error *
360 diff_added_tree(struct got_object_id *id, const char *label,
361 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
362 int diff_content)
364 const struct got_error *err = NULL;
365 struct got_object *treeobj = NULL;
366 struct got_tree_object *tree = NULL;
368 err = got_object_open(&treeobj, repo, id);
369 if (err)
370 goto done;
372 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
373 err = got_error(GOT_ERR_OBJ_TYPE);
374 goto done;
377 err = got_object_tree_open(&tree, repo, treeobj);
378 if (err)
379 goto done;
381 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
382 diff_content);
383 done:
384 if (tree)
385 got_object_tree_close(tree);
386 if (treeobj)
387 got_object_close(treeobj);
388 return err;
391 static const struct got_error *
392 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
393 const char *label1, const char *label2, struct got_repository *repo,
394 got_diff_blob_cb cb, void *cb_arg, int diff_content)
396 const struct got_error *err;
397 struct got_object *treeobj1 = NULL;
398 struct got_object *treeobj2 = NULL;
399 struct got_tree_object *tree1 = NULL;
400 struct got_tree_object *tree2 = NULL;
402 err = got_object_open(&treeobj1, repo, id1);
403 if (err)
404 goto done;
406 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
407 err = got_error(GOT_ERR_OBJ_TYPE);
408 goto done;
411 err = got_object_open(&treeobj2, repo, id2);
412 if (err)
413 goto done;
415 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
416 err = got_error(GOT_ERR_OBJ_TYPE);
417 goto done;
420 err = got_object_tree_open(&tree1, repo, treeobj1);
421 if (err)
422 goto done;
424 err = got_object_tree_open(&tree2, repo, treeobj2);
425 if (err)
426 goto done;
428 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
429 diff_content);
431 done:
432 if (tree1)
433 got_object_tree_close(tree1);
434 if (tree2)
435 got_object_tree_close(tree2);
436 if (treeobj1)
437 got_object_close(treeobj1);
438 if (treeobj2)
439 got_object_close(treeobj2);
440 return err;
443 static const struct got_error *
444 diff_deleted_tree(struct got_object_id *id, const char *label,
445 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
446 int diff_content)
448 const struct got_error *err;
449 struct got_object *treeobj = NULL;
450 struct got_tree_object *tree = NULL;
452 err = got_object_open(&treeobj, repo, id);
453 if (err)
454 goto done;
456 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
457 err = got_error(GOT_ERR_OBJ_TYPE);
458 goto done;
461 err = got_object_tree_open(&tree, repo, treeobj);
462 if (err)
463 goto done;
465 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
466 diff_content);
467 done:
468 if (tree)
469 got_object_tree_close(tree);
470 if (treeobj)
471 got_object_close(treeobj);
472 return err;
475 static const struct got_error *
476 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
477 const char *label1, const char *label2, struct got_repository *repo,
478 got_diff_blob_cb cb, void *cb_arg)
480 /* XXX TODO */
481 return NULL;
484 static const struct got_error *
485 diff_entry_old_new(const struct got_tree_entry *te1,
486 const struct got_tree_entry *te2, const char *label1, const char *label2,
487 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
488 int diff_content)
490 const struct got_error *err = NULL;
491 int id_match;
493 if (te2 == NULL) {
494 if (S_ISDIR(te1->mode))
495 err = diff_deleted_tree(te1->id, label1, repo,
496 cb, cb_arg, diff_content);
497 else {
498 if (diff_content)
499 err = diff_deleted_blob(te1->id, label1, repo,
500 cb, cb_arg);
501 else
502 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
503 label1, NULL, repo);
505 return err;
508 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
509 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
510 if (!id_match)
511 return diff_modified_tree(te1->id, te2->id,
512 label1, label2, repo, cb, cb_arg, diff_content);
513 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
514 if (!id_match) {
515 if (diff_content)
516 return diff_modified_blob(te1->id, te2->id,
517 label1, label2, repo, cb, cb_arg);
518 else
519 return cb(cb_arg, NULL, NULL, te1->id,
520 te2->id, label1, label2, repo);
524 if (id_match)
525 return NULL;
527 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
528 cb, cb_arg);
531 static const struct got_error *
532 diff_entry_new_old(const struct got_tree_entry *te2,
533 const struct got_tree_entry *te1, const char *label2,
534 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
535 int diff_content)
537 if (te1 != NULL) /* handled by diff_entry_old_new() */
538 return NULL;
540 if (S_ISDIR(te2->mode))
541 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
542 diff_content);
544 if (diff_content)
545 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
547 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
550 const struct got_error *
551 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
552 const char *label1, const char *label2, struct got_repository *repo,
553 got_diff_blob_cb cb, void *cb_arg, int diff_content)
555 const struct got_error *err = NULL;
556 struct got_tree_entry *te1 = NULL;
557 struct got_tree_entry *te2 = NULL;
558 char *l1 = NULL, *l2 = NULL;
560 if (tree1) {
561 const struct got_tree_entries *entries;
562 entries = got_object_tree_get_entries(tree1);
563 te1 = SIMPLEQ_FIRST(&entries->head);
564 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
565 te1->name) == -1)
566 return got_error_from_errno("asprintf");
568 if (tree2) {
569 const struct got_tree_entries *entries;
570 entries = got_object_tree_get_entries(tree2);
571 te2 = SIMPLEQ_FIRST(&entries->head);
572 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
573 te2->name) == -1)
574 return got_error_from_errno("asprintf");
577 do {
578 if (te1) {
579 const struct got_tree_entry *te = NULL;
580 if (tree2)
581 te = got_object_tree_find_entry(tree2,
582 te1->name);
583 if (te) {
584 free(l2);
585 l2 = NULL;
586 if (te && asprintf(&l2, "%s%s%s", label2,
587 label2[0] ? "/" : "", te->name) == -1)
588 return
589 got_error_from_errno("asprintf");
591 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
592 cb_arg, diff_content);
593 if (err)
594 break;
597 if (te2) {
598 const struct got_tree_entry *te = NULL;
599 if (tree1)
600 te = got_object_tree_find_entry(tree1,
601 te2->name);
602 free(l2);
603 if (te) {
604 if (asprintf(&l2, "%s%s%s", label2,
605 label2[0] ? "/" : "", te->name) == -1)
606 return
607 got_error_from_errno("asprintf");
608 } else {
609 if (asprintf(&l2, "%s%s%s", label2,
610 label2[0] ? "/" : "", te2->name) == -1)
611 return
612 got_error_from_errno("asprintf");
614 err = diff_entry_new_old(te2, te, l2, repo,
615 cb, cb_arg, diff_content);
616 if (err)
617 break;
620 free(l1);
621 l1 = NULL;
622 if (te1) {
623 te1 = SIMPLEQ_NEXT(te1, entry);
624 if (te1 &&
625 asprintf(&l1, "%s%s%s", label1,
626 label1[0] ? "/" : "", te1->name) == -1)
627 return got_error_from_errno("asprintf");
629 free(l2);
630 l2 = NULL;
631 if (te2) {
632 te2 = SIMPLEQ_NEXT(te2, entry);
633 if (te2 &&
634 asprintf(&l2, "%s%s%s", label2,
635 label2[0] ? "/" : "", te2->name) == -1)
636 return got_error_from_errno("asprintf");
638 } while (te1 || te2);
640 return err;
643 const struct got_error *
644 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
645 const char *label1, const char *label2, int diff_context,
646 struct got_repository *repo, FILE *outfile)
648 const struct got_error *err;
649 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
651 if (id1 == NULL && id2 == NULL)
652 return got_error(GOT_ERR_NO_OBJ);
654 if (id1) {
655 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
656 if (err)
657 goto done;
659 if (id2) {
660 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
661 if (err)
662 goto done;
664 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
665 outfile);
666 done:
667 if (blob1)
668 got_object_blob_close(blob1);
669 if (blob2)
670 got_object_blob_close(blob2);
671 return err;
674 const struct got_error *
675 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
676 char *label1, char *label2, int diff_context, struct got_repository *repo,
677 FILE *outfile)
679 const struct got_error *err;
680 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
681 struct got_diff_blob_output_unidiff_arg arg;
683 if (id1 == NULL && id2 == NULL)
684 return got_error(GOT_ERR_NO_OBJ);
686 if (id1) {
687 err = got_object_open_as_tree(&tree1, repo, id1);
688 if (err)
689 goto done;
691 if (id2) {
692 err = got_object_open_as_tree(&tree2, repo, id2);
693 if (err)
694 goto done;
696 arg.diff_context = diff_context;
697 arg.outfile = outfile;
698 err = got_diff_tree(tree1, tree2, label1, label2, repo,
699 got_diff_blob_output_unidiff, &arg, 1);
700 done:
701 if (tree1)
702 got_object_tree_close(tree1);
703 if (tree2)
704 got_object_tree_close(tree2);
705 return err;
708 const struct got_error *
709 got_diff_objects_as_commits(struct got_object_id *id1,
710 struct got_object_id *id2, int diff_context,
711 struct got_repository *repo, FILE *outfile)
713 const struct got_error *err;
714 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
716 if (id2 == NULL)
717 return got_error(GOT_ERR_NO_OBJ);
719 if (id1) {
720 err = got_object_open_as_commit(&commit1, repo, id1);
721 if (err)
722 goto done;
725 err = got_object_open_as_commit(&commit2, repo, id2);
726 if (err)
727 goto done;
729 err = got_diff_objects_as_trees(
730 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
731 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
732 outfile);
733 done:
734 if (commit1)
735 got_object_commit_close(commit1);
736 if (commit2)
737 got_object_commit_close(commit2);
738 return err;
741 const struct got_error *
742 got_diff_files(struct got_diff_changes **changes,
743 struct got_diff_state **ds,
744 struct got_diff_args **args,
745 int *flags,
746 FILE *f1, size_t size1, const char *label1,
747 FILE *f2, size_t size2, const char *label2,
748 int diff_context, FILE *outfile)
750 const struct got_error *err = NULL;
751 int res;
753 *flags = 0;
754 *ds = calloc(1, sizeof(**ds));
755 if (*ds == NULL)
756 return got_error_from_errno("calloc");
757 *args = calloc(1, sizeof(**args));
758 if (*args == NULL) {
759 err = got_error_from_errno("calloc");
760 goto done;
763 if (changes)
764 *changes = NULL;
766 if (f1 == NULL)
767 *flags |= D_EMPTY1;
769 if (f2 == NULL)
770 *flags |= D_EMPTY2;
772 /* XXX should stat buffers be passed in args instead of ds? */
773 (*ds)->stb1.st_mode = S_IFREG;
774 (*ds)->stb1.st_size = size1;
775 (*ds)->stb1.st_mtime = 0; /* XXX */
777 (*ds)->stb2.st_mode = S_IFREG;
778 (*ds)->stb2.st_size = size2;
779 (*ds)->stb2.st_mtime = 0; /* XXX */
781 (*args)->diff_format = D_UNIFIED;
782 (*args)->label[0] = label1;
783 (*args)->label[1] = label2;
784 (*args)->diff_context = diff_context;
785 *flags |= D_PROTOTYPE;
787 if (outfile) {
788 fprintf(outfile, "file - %s\n",
789 f1 == NULL ? "/dev/null" : label1);
790 fprintf(outfile, "file + %s\n",
791 f2 == NULL ? "/dev/null" : label2);
793 if (changes) {
794 err = alloc_changes(changes);
795 if (err)
796 goto done;
798 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
799 changes ? *changes : NULL);
800 done:
801 if (err) {
802 if (*ds) {
803 got_diff_state_free(*ds);
804 free(*ds);
805 *ds = NULL;
807 if (*args) {
808 free(*args);
809 *args = NULL;
811 if (changes) {
812 if (*changes)
813 got_diff_free_changes(*changes);
814 *changes = NULL;
817 return err;