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, 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", 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, FILE *f2, size_t size2,
228 const char *label2, int diff_context, FILE *outfile)
230 return diff_blob_file(NULL, blob1, f2, size2, label2, diff_context,
231 outfile);
234 const struct got_error *
235 got_diff_blob_lines_changed(struct got_diff_changes **changes,
236 struct got_blob_object *blob1, struct got_blob_object *blob2)
238 const struct got_error *err = NULL;
240 err = alloc_changes(changes);
241 if (err)
242 return err;
244 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
245 if (err) {
246 got_diff_free_changes(*changes);
247 *changes = NULL;
249 return err;
252 void
253 got_diff_free_changes(struct got_diff_changes *changes)
255 struct got_diff_change *change;
256 while (!SIMPLEQ_EMPTY(&changes->entries)) {
257 change = SIMPLEQ_FIRST(&changes->entries);
258 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
259 free(change);
261 free(changes);
264 static const struct got_error *
265 diff_added_blob(struct got_object_id *id, const char *label,
266 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
268 const struct got_error *err;
269 struct got_blob_object *blob = NULL;
270 struct got_object *obj = NULL;
272 err = got_object_open(&obj, repo, id);
273 if (err)
274 return err;
276 err = got_object_blob_open(&blob, repo, obj, 8192);
277 if (err)
278 goto done;
279 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
280 done:
281 got_object_close(obj);
282 if (blob)
283 got_object_blob_close(blob);
284 return err;
287 static const struct got_error *
288 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
289 const char *label1, const char *label2, struct got_repository *repo,
290 got_diff_blob_cb cb, void *cb_arg)
292 const struct got_error *err;
293 struct got_object *obj1 = NULL;
294 struct got_object *obj2 = NULL;
295 struct got_blob_object *blob1 = NULL;
296 struct got_blob_object *blob2 = NULL;
298 err = got_object_open(&obj1, repo, id1);
299 if (err)
300 return err;
301 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
302 err = got_error(GOT_ERR_OBJ_TYPE);
303 goto done;
306 err = got_object_open(&obj2, repo, id2);
307 if (err)
308 goto done;
309 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
310 err = got_error(GOT_ERR_BAD_OBJ_DATA);
311 goto done;
314 err = got_object_blob_open(&blob1, repo, obj1, 8192);
315 if (err)
316 goto done;
318 err = got_object_blob_open(&blob2, repo, obj2, 8192);
319 if (err)
320 goto done;
322 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
323 done:
324 if (obj1)
325 got_object_close(obj1);
326 if (obj2)
327 got_object_close(obj2);
328 if (blob1)
329 got_object_blob_close(blob1);
330 if (blob2)
331 got_object_blob_close(blob2);
332 return err;
335 static const struct got_error *
336 diff_deleted_blob(struct got_object_id *id, const char *label,
337 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
339 const struct got_error *err;
340 struct got_blob_object *blob = NULL;
341 struct got_object *obj = NULL;
343 err = got_object_open(&obj, repo, id);
344 if (err)
345 return err;
347 err = got_object_blob_open(&blob, repo, obj, 8192);
348 if (err)
349 goto done;
350 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
351 done:
352 got_object_close(obj);
353 if (blob)
354 got_object_blob_close(blob);
355 return err;
358 static const struct got_error *
359 diff_added_tree(struct got_object_id *id, const char *label,
360 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
361 int diff_content)
363 const struct got_error *err = NULL;
364 struct got_object *treeobj = NULL;
365 struct got_tree_object *tree = NULL;
367 err = got_object_open(&treeobj, repo, id);
368 if (err)
369 goto done;
371 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
372 err = got_error(GOT_ERR_OBJ_TYPE);
373 goto done;
376 err = got_object_tree_open(&tree, repo, treeobj);
377 if (err)
378 goto done;
380 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
381 diff_content);
382 done:
383 if (tree)
384 got_object_tree_close(tree);
385 if (treeobj)
386 got_object_close(treeobj);
387 return err;
390 static const struct got_error *
391 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
392 const char *label1, const char *label2, struct got_repository *repo,
393 got_diff_blob_cb cb, void *cb_arg, int diff_content)
395 const struct got_error *err;
396 struct got_object *treeobj1 = NULL;
397 struct got_object *treeobj2 = NULL;
398 struct got_tree_object *tree1 = NULL;
399 struct got_tree_object *tree2 = NULL;
401 err = got_object_open(&treeobj1, repo, id1);
402 if (err)
403 goto done;
405 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
406 err = got_error(GOT_ERR_OBJ_TYPE);
407 goto done;
410 err = got_object_open(&treeobj2, repo, id2);
411 if (err)
412 goto done;
414 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
415 err = got_error(GOT_ERR_OBJ_TYPE);
416 goto done;
419 err = got_object_tree_open(&tree1, repo, treeobj1);
420 if (err)
421 goto done;
423 err = got_object_tree_open(&tree2, repo, treeobj2);
424 if (err)
425 goto done;
427 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
428 diff_content);
430 done:
431 if (tree1)
432 got_object_tree_close(tree1);
433 if (tree2)
434 got_object_tree_close(tree2);
435 if (treeobj1)
436 got_object_close(treeobj1);
437 if (treeobj2)
438 got_object_close(treeobj2);
439 return err;
442 static const struct got_error *
443 diff_deleted_tree(struct got_object_id *id, const char *label,
444 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
445 int diff_content)
447 const struct got_error *err;
448 struct got_object *treeobj = NULL;
449 struct got_tree_object *tree = NULL;
451 err = got_object_open(&treeobj, repo, id);
452 if (err)
453 goto done;
455 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
456 err = got_error(GOT_ERR_OBJ_TYPE);
457 goto done;
460 err = got_object_tree_open(&tree, repo, treeobj);
461 if (err)
462 goto done;
464 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
465 diff_content);
466 done:
467 if (tree)
468 got_object_tree_close(tree);
469 if (treeobj)
470 got_object_close(treeobj);
471 return err;
474 static const struct got_error *
475 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
476 const char *label1, const char *label2, struct got_repository *repo,
477 got_diff_blob_cb cb, void *cb_arg)
479 /* XXX TODO */
480 return NULL;
483 static const struct got_error *
484 diff_entry_old_new(const struct got_tree_entry *te1,
485 const struct got_tree_entry *te2, const char *label1, const char *label2,
486 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
487 int diff_content)
489 const struct got_error *err = NULL;
490 int id_match;
492 if (te2 == NULL) {
493 if (S_ISDIR(te1->mode))
494 err = diff_deleted_tree(te1->id, label1, repo,
495 cb, cb_arg, diff_content);
496 else {
497 if (diff_content)
498 err = diff_deleted_blob(te1->id, label1, repo,
499 cb, cb_arg);
500 else
501 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
502 label1, NULL, repo);
504 return err;
507 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
508 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
509 if (!id_match)
510 return diff_modified_tree(te1->id, te2->id,
511 label1, label2, repo, cb, cb_arg, diff_content);
512 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
513 if (!id_match) {
514 if (diff_content)
515 return diff_modified_blob(te1->id, te2->id,
516 label1, label2, repo, cb, cb_arg);
517 else
518 return cb(cb_arg, NULL, NULL, te1->id,
519 te2->id, label1, label2, repo);
523 if (id_match)
524 return NULL;
526 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
527 cb, cb_arg);
530 static const struct got_error *
531 diff_entry_new_old(const struct got_tree_entry *te2,
532 const struct got_tree_entry *te1, const char *label2,
533 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
534 int diff_content)
536 if (te1 != NULL) /* handled by diff_entry_old_new() */
537 return NULL;
539 if (S_ISDIR(te2->mode))
540 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
541 diff_content);
543 if (diff_content)
544 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
546 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
549 const struct got_error *
550 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
551 const char *label1, const char *label2, struct got_repository *repo,
552 got_diff_blob_cb cb, void *cb_arg, int diff_content)
554 const struct got_error *err = NULL;
555 struct got_tree_entry *te1 = NULL;
556 struct got_tree_entry *te2 = NULL;
557 char *l1 = NULL, *l2 = NULL;
559 if (tree1) {
560 const struct got_tree_entries *entries;
561 entries = got_object_tree_get_entries(tree1);
562 te1 = SIMPLEQ_FIRST(&entries->head);
563 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
564 te1->name) == -1)
565 return got_error_from_errno("asprintf");
567 if (tree2) {
568 const struct got_tree_entries *entries;
569 entries = got_object_tree_get_entries(tree2);
570 te2 = SIMPLEQ_FIRST(&entries->head);
571 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
572 te2->name) == -1)
573 return got_error_from_errno("asprintf");
576 do {
577 if (te1) {
578 const struct got_tree_entry *te = NULL;
579 if (tree2)
580 te = got_object_tree_find_entry(tree2,
581 te1->name);
582 if (te) {
583 free(l2);
584 l2 = NULL;
585 if (te && asprintf(&l2, "%s%s%s", label2,
586 label2[0] ? "/" : "", te->name) == -1)
587 return
588 got_error_from_errno("asprintf");
590 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
591 cb_arg, diff_content);
592 if (err)
593 break;
596 if (te2) {
597 const struct got_tree_entry *te = NULL;
598 if (tree1)
599 te = got_object_tree_find_entry(tree1,
600 te2->name);
601 free(l2);
602 if (te) {
603 if (asprintf(&l2, "%s%s%s", label2,
604 label2[0] ? "/" : "", te->name) == -1)
605 return
606 got_error_from_errno("asprintf");
607 } else {
608 if (asprintf(&l2, "%s%s%s", label2,
609 label2[0] ? "/" : "", te2->name) == -1)
610 return
611 got_error_from_errno("asprintf");
613 err = diff_entry_new_old(te2, te, l2, repo,
614 cb, cb_arg, diff_content);
615 if (err)
616 break;
619 free(l1);
620 l1 = NULL;
621 if (te1) {
622 te1 = SIMPLEQ_NEXT(te1, entry);
623 if (te1 &&
624 asprintf(&l1, "%s%s%s", label1,
625 label1[0] ? "/" : "", te1->name) == -1)
626 return got_error_from_errno("asprintf");
628 free(l2);
629 l2 = NULL;
630 if (te2) {
631 te2 = SIMPLEQ_NEXT(te2, entry);
632 if (te2 &&
633 asprintf(&l2, "%s%s%s", label2,
634 label2[0] ? "/" : "", te2->name) == -1)
635 return got_error_from_errno("asprintf");
637 } while (te1 || te2);
639 return err;
642 const struct got_error *
643 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
644 const char *label1, const char *label2, int diff_context,
645 struct got_repository *repo, FILE *outfile)
647 const struct got_error *err;
648 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
650 if (id1 == NULL && id2 == NULL)
651 return got_error(GOT_ERR_NO_OBJ);
653 if (id1) {
654 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
655 if (err)
656 goto done;
658 if (id2) {
659 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
660 if (err)
661 goto done;
663 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
664 outfile);
665 done:
666 if (blob1)
667 got_object_blob_close(blob1);
668 if (blob2)
669 got_object_blob_close(blob2);
670 return err;
673 const struct got_error *
674 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
675 char *label1, char *label2, int diff_context, struct got_repository *repo,
676 FILE *outfile)
678 const struct got_error *err;
679 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
680 struct got_diff_blob_output_unidiff_arg arg;
682 if (id1 == NULL && id2 == NULL)
683 return got_error(GOT_ERR_NO_OBJ);
685 if (id1) {
686 err = got_object_open_as_tree(&tree1, repo, id1);
687 if (err)
688 goto done;
690 if (id2) {
691 err = got_object_open_as_tree(&tree2, repo, id2);
692 if (err)
693 goto done;
695 arg.diff_context = diff_context;
696 arg.outfile = outfile;
697 err = got_diff_tree(tree1, tree2, label1, label2, repo,
698 got_diff_blob_output_unidiff, &arg, 1);
699 done:
700 if (tree1)
701 got_object_tree_close(tree1);
702 if (tree2)
703 got_object_tree_close(tree2);
704 return err;
707 const struct got_error *
708 got_diff_objects_as_commits(struct got_object_id *id1,
709 struct got_object_id *id2, int diff_context,
710 struct got_repository *repo, FILE *outfile)
712 const struct got_error *err;
713 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
715 if (id2 == NULL)
716 return got_error(GOT_ERR_NO_OBJ);
718 if (id1) {
719 err = got_object_open_as_commit(&commit1, repo, id1);
720 if (err)
721 goto done;
724 err = got_object_open_as_commit(&commit2, repo, id2);
725 if (err)
726 goto done;
728 err = got_diff_objects_as_trees(
729 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
730 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
731 outfile);
732 done:
733 if (commit1)
734 got_object_commit_close(commit1);
735 if (commit2)
736 got_object_commit_close(commit2);
737 return err;
740 const struct got_error *
741 got_diff_files(struct got_diff_changes **changes,
742 struct got_diff_state **ds,
743 struct got_diff_args **args,
744 int *flags,
745 FILE *f1, size_t size1, const char *label1,
746 FILE *f2, size_t size2, const char *label2,
747 int diff_context, FILE *outfile)
749 const struct got_error *err = NULL;
750 int res;
752 *flags = 0;
753 *ds = calloc(1, sizeof(**ds));
754 if (*ds == NULL)
755 return got_error_from_errno("calloc");
756 *args = calloc(1, sizeof(**args));
757 if (*args == NULL) {
758 err = got_error_from_errno("calloc");
759 goto done;
762 if (changes)
763 *changes = NULL;
765 if (f1 == NULL)
766 *flags |= D_EMPTY1;
768 if (f2 == NULL)
769 *flags |= D_EMPTY2;
771 /* XXX should stat buffers be passed in args instead of ds? */
772 (*ds)->stb1.st_mode = S_IFREG;
773 (*ds)->stb1.st_size = size1;
774 (*ds)->stb1.st_mtime = 0; /* XXX */
776 (*ds)->stb2.st_mode = S_IFREG;
777 (*ds)->stb2.st_size = size2;
778 (*ds)->stb2.st_mtime = 0; /* XXX */
780 (*args)->diff_format = D_UNIFIED;
781 (*args)->label[0] = label1;
782 (*args)->label[1] = label2;
783 (*args)->diff_context = diff_context;
784 *flags |= D_PROTOTYPE;
786 if (outfile) {
787 fprintf(outfile, "file - %s\n",
788 f1 == NULL ? "/dev/null" : label1);
789 fprintf(outfile, "file + %s\n",
790 f2 == NULL ? "/dev/null" : label2);
792 if (changes) {
793 err = alloc_changes(changes);
794 if (err)
795 goto done;
797 err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
798 changes ? *changes : NULL);
799 done:
800 if (err) {
801 if (*ds) {
802 got_diff_state_free(*ds);
803 free(*ds);
804 *ds = NULL;
806 if (*args) {
807 free(*args);
808 *args = NULL;
810 if (changes) {
811 if (*changes)
812 got_diff_free_changes(*changes);
813 *changes = NULL;
816 return err;