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 done:
116 if (f1 && fclose(f1) != 0 && err == NULL)
117 err = got_error_from_errno("fclose");
118 if (f2 && fclose(f2) != 0 && err == NULL)
119 err = got_error_from_errno("fclose");
120 return err;
123 const struct got_error *
124 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
125 struct got_blob_object *blob2, struct got_object_id *id1,
126 struct got_object_id *id2, const char *label1, const char *label2,
127 struct got_repository *repo)
129 struct got_diff_blob_output_unidiff_arg *a = arg;
131 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
132 a->outfile, NULL);
135 const struct got_error *
136 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
137 const char *label1, const char *label2, int diff_context, FILE *outfile)
139 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
140 NULL);
143 const struct got_error *
144 got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
145 const char *label2, int diff_context, FILE *outfile)
147 struct got_diff_state ds;
148 struct got_diff_args args;
149 const struct got_error *err = NULL;
150 FILE *f1 = NULL;
151 char hex1[SHA1_DIGEST_STRING_LENGTH];
152 char *idstr1 = NULL;
153 size_t size1;
154 int res, flags = 0;
156 size1 = 0;
157 if (blob1) {
158 f1 = got_opentemp();
159 if (f1 == NULL)
160 return got_error_from_errno("got_opentemp");
161 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
162 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
163 blob1);
164 if (err)
165 goto done;
166 } else {
167 flags |= D_EMPTY1;
168 idstr1 = "/dev/null";
171 if (f2 == NULL)
172 flags |= D_EMPTY2;
174 memset(&ds, 0, sizeof(ds));
175 /* XXX should stat buffers be passed in args instead of ds? */
176 ds.stb1.st_mode = S_IFREG;
177 if (blob1)
178 ds.stb1.st_size = size1;
179 ds.stb1.st_mtime = 0; /* XXX */
181 ds.stb2.st_mode = S_IFREG;
182 ds.stb2.st_size = size2;
183 ds.stb2.st_mtime = 0; /* XXX */
185 memset(&args, 0, sizeof(args));
186 args.diff_format = D_UNIFIED;
187 args.label[0] = label2;
188 args.label[1] = label2;
189 args.diff_context = diff_context;
190 flags |= D_PROTOTYPE;
192 fprintf(outfile, "blob - %s\n", idstr1);
193 fprintf(outfile, "file + %s\n", f2 == NULL ? "/dev/null" : label2);
194 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
195 done:
196 if (f1 && fclose(f1) != 0 && err == NULL)
197 err = got_error_from_errno("fclose");
198 return err;
201 const struct got_error *
202 got_diff_blob_lines_changed(struct got_diff_changes **changes,
203 struct got_blob_object *blob1, struct got_blob_object *blob2)
205 const struct got_error *err = NULL;
207 *changes = calloc(1, sizeof(**changes));
208 if (*changes == NULL)
209 return got_error_from_errno("calloc");
210 SIMPLEQ_INIT(&(*changes)->entries);
212 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
213 if (err) {
214 got_diff_free_changes(*changes);
215 *changes = NULL;
217 return err;
220 void
221 got_diff_free_changes(struct got_diff_changes *changes)
223 struct got_diff_change *change;
224 while (!SIMPLEQ_EMPTY(&changes->entries)) {
225 change = SIMPLEQ_FIRST(&changes->entries);
226 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
227 free(change);
229 free(changes);
232 static const struct got_error *
233 diff_added_blob(struct got_object_id *id, const char *label,
234 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
236 const struct got_error *err;
237 struct got_blob_object *blob = NULL;
238 struct got_object *obj = NULL;
240 err = got_object_open(&obj, repo, id);
241 if (err)
242 return err;
244 err = got_object_blob_open(&blob, repo, obj, 8192);
245 if (err)
246 goto done;
247 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
248 done:
249 got_object_close(obj);
250 if (blob)
251 got_object_blob_close(blob);
252 return err;
255 static const struct got_error *
256 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
257 const char *label1, const char *label2, struct got_repository *repo,
258 got_diff_blob_cb cb, void *cb_arg)
260 const struct got_error *err;
261 struct got_object *obj1 = NULL;
262 struct got_object *obj2 = NULL;
263 struct got_blob_object *blob1 = NULL;
264 struct got_blob_object *blob2 = NULL;
266 err = got_object_open(&obj1, repo, id1);
267 if (err)
268 return err;
269 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
270 err = got_error(GOT_ERR_OBJ_TYPE);
271 goto done;
274 err = got_object_open(&obj2, repo, id2);
275 if (err)
276 goto done;
277 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
278 err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 goto done;
282 err = got_object_blob_open(&blob1, repo, obj1, 8192);
283 if (err)
284 goto done;
286 err = got_object_blob_open(&blob2, repo, obj2, 8192);
287 if (err)
288 goto done;
290 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
291 done:
292 if (obj1)
293 got_object_close(obj1);
294 if (obj2)
295 got_object_close(obj2);
296 if (blob1)
297 got_object_blob_close(blob1);
298 if (blob2)
299 got_object_blob_close(blob2);
300 return err;
303 static const struct got_error *
304 diff_deleted_blob(struct got_object_id *id, const char *label,
305 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
307 const struct got_error *err;
308 struct got_blob_object *blob = NULL;
309 struct got_object *obj = NULL;
311 err = got_object_open(&obj, repo, id);
312 if (err)
313 return err;
315 err = got_object_blob_open(&blob, repo, obj, 8192);
316 if (err)
317 goto done;
318 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
319 done:
320 got_object_close(obj);
321 if (blob)
322 got_object_blob_close(blob);
323 return err;
326 static const struct got_error *
327 diff_added_tree(struct got_object_id *id, const char *label,
328 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
330 const struct got_error *err = NULL;
331 struct got_object *treeobj = NULL;
332 struct got_tree_object *tree = NULL;
334 err = got_object_open(&treeobj, repo, id);
335 if (err)
336 goto done;
338 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
339 err = got_error(GOT_ERR_OBJ_TYPE);
340 goto done;
343 err = got_object_tree_open(&tree, repo, treeobj);
344 if (err)
345 goto done;
347 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg);
348 done:
349 if (tree)
350 got_object_tree_close(tree);
351 if (treeobj)
352 got_object_close(treeobj);
353 return err;
356 static const struct got_error *
357 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
358 const char *label1, const char *label2, struct got_repository *repo,
359 got_diff_blob_cb cb, void *cb_arg)
361 const struct got_error *err;
362 struct got_object *treeobj1 = NULL;
363 struct got_object *treeobj2 = NULL;
364 struct got_tree_object *tree1 = NULL;
365 struct got_tree_object *tree2 = NULL;
367 err = got_object_open(&treeobj1, repo, id1);
368 if (err)
369 goto done;
371 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
372 err = got_error(GOT_ERR_OBJ_TYPE);
373 goto done;
376 err = got_object_open(&treeobj2, repo, id2);
377 if (err)
378 goto done;
380 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
381 err = got_error(GOT_ERR_OBJ_TYPE);
382 goto done;
385 err = got_object_tree_open(&tree1, repo, treeobj1);
386 if (err)
387 goto done;
389 err = got_object_tree_open(&tree2, repo, treeobj2);
390 if (err)
391 goto done;
393 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg);
395 done:
396 if (tree1)
397 got_object_tree_close(tree1);
398 if (tree2)
399 got_object_tree_close(tree2);
400 if (treeobj1)
401 got_object_close(treeobj1);
402 if (treeobj2)
403 got_object_close(treeobj2);
404 return err;
407 static const struct got_error *
408 diff_deleted_tree(struct got_object_id *id, const char *label,
409 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
411 const struct got_error *err;
412 struct got_object *treeobj = NULL;
413 struct got_tree_object *tree = NULL;
415 err = got_object_open(&treeobj, repo, id);
416 if (err)
417 goto done;
419 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
420 err = got_error(GOT_ERR_OBJ_TYPE);
421 goto done;
424 err = got_object_tree_open(&tree, repo, treeobj);
425 if (err)
426 goto done;
428 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg);
429 done:
430 if (tree)
431 got_object_tree_close(tree);
432 if (treeobj)
433 got_object_close(treeobj);
434 return err;
437 static const struct got_error *
438 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
439 const char *label1, const char *label2, struct got_repository *repo,
440 got_diff_blob_cb cb, void *cb_arg)
442 /* XXX TODO */
443 return NULL;
446 static const struct got_error *
447 diff_entry_old_new(const struct got_tree_entry *te1,
448 const struct got_tree_entry *te2, const char *label1, const char *label2,
449 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
451 const struct got_error *err = NULL;
452 int id_match;
454 if (te2 == NULL) {
455 if (S_ISDIR(te1->mode))
456 err = diff_deleted_tree(te1->id, label1, repo,
457 cb, cb_arg);
458 else
459 err = diff_deleted_blob(te1->id, label1, repo,
460 cb, cb_arg);
461 return err;
464 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
465 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
466 if (!id_match)
467 return diff_modified_tree(te1->id, te2->id,
468 label1, label2, repo, cb, cb_arg);
469 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
470 if (!id_match)
471 return diff_modified_blob(te1->id, te2->id,
472 label1, label2, repo, cb, cb_arg);
475 if (id_match)
476 return NULL;
478 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
479 cb, cb_arg);
482 static const struct got_error *
483 diff_entry_new_old(const struct got_tree_entry *te2,
484 const struct got_tree_entry *te1, const char *label2,
485 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
487 if (te1 != NULL) /* handled by diff_entry_old_new() */
488 return NULL;
490 if (S_ISDIR(te2->mode))
491 return diff_added_tree(te2->id, label2, repo, cb, cb_arg);
493 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
496 const struct got_error *
497 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
498 const char *label1, const char *label2, struct got_repository *repo,
499 got_diff_blob_cb cb, void *cb_arg)
501 const struct got_error *err = NULL;
502 struct got_tree_entry *te1 = NULL;
503 struct got_tree_entry *te2 = NULL;
504 char *l1 = NULL, *l2 = NULL;
506 if (tree1) {
507 const struct got_tree_entries *entries;
508 entries = got_object_tree_get_entries(tree1);
509 te1 = SIMPLEQ_FIRST(&entries->head);
510 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
511 te1->name) == -1)
512 return got_error_from_errno("asprintf");
514 if (tree2) {
515 const struct got_tree_entries *entries;
516 entries = got_object_tree_get_entries(tree2);
517 te2 = SIMPLEQ_FIRST(&entries->head);
518 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
519 te2->name) == -1)
520 return got_error_from_errno("asprintf");
523 do {
524 if (te1) {
525 const struct got_tree_entry *te = NULL;
526 if (tree2)
527 te = got_object_tree_find_entry(tree2,
528 te1->name);
529 if (te) {
530 free(l2);
531 l2 = NULL;
532 if (te && asprintf(&l2, "%s%s%s", label2,
533 label2[0] ? "/" : "", te->name) == -1)
534 return
535 got_error_from_errno("asprintf");
537 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
538 cb_arg);
539 if (err)
540 break;
543 if (te2) {
544 const struct got_tree_entry *te = NULL;
545 if (tree1)
546 te = got_object_tree_find_entry(tree1,
547 te2->name);
548 free(l2);
549 if (te) {
550 if (asprintf(&l2, "%s%s%s", label2,
551 label2[0] ? "/" : "", te->name) == -1)
552 return
553 got_error_from_errno("asprintf");
554 } else {
555 if (asprintf(&l2, "%s%s%s", label2,
556 label2[0] ? "/" : "", te2->name) == -1)
557 return
558 got_error_from_errno("asprintf");
560 err = diff_entry_new_old(te2, te, l2, repo, cb, cb_arg);
561 if (err)
562 break;
565 free(l1);
566 l1 = NULL;
567 if (te1) {
568 te1 = SIMPLEQ_NEXT(te1, entry);
569 if (te1 &&
570 asprintf(&l1, "%s%s%s", label1,
571 label1[0] ? "/" : "", te1->name) == -1)
572 return got_error_from_errno("asprintf");
574 free(l2);
575 l2 = NULL;
576 if (te2) {
577 te2 = SIMPLEQ_NEXT(te2, entry);
578 if (te2 &&
579 asprintf(&l2, "%s%s%s", label2,
580 label2[0] ? "/" : "", te2->name) == -1)
581 return got_error_from_errno("asprintf");
583 } while (te1 || te2);
585 return err;
588 const struct got_error *
589 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
590 const char *label1, const char *label2, int diff_context,
591 struct got_repository *repo, FILE *outfile)
593 const struct got_error *err;
594 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
596 if (id1 == NULL && id2 == NULL)
597 return got_error(GOT_ERR_NO_OBJ);
599 if (id1) {
600 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
601 if (err)
602 goto done;
604 if (id2) {
605 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
606 if (err)
607 goto done;
609 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
610 outfile);
611 done:
612 if (blob1)
613 got_object_blob_close(blob1);
614 if (blob2)
615 got_object_blob_close(blob2);
616 return err;
619 const struct got_error *
620 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
621 char *label1, char *label2, int diff_context, struct got_repository *repo,
622 FILE *outfile)
624 const struct got_error *err;
625 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
626 struct got_diff_blob_output_unidiff_arg arg;
628 if (id1 == NULL && id2 == NULL)
629 return got_error(GOT_ERR_NO_OBJ);
631 if (id1) {
632 err = got_object_open_as_tree(&tree1, repo, id1);
633 if (err)
634 goto done;
636 if (id2) {
637 err = got_object_open_as_tree(&tree2, repo, id2);
638 if (err)
639 goto done;
641 arg.diff_context = diff_context;
642 arg.outfile = outfile;
643 err = got_diff_tree(tree1, tree2, label1, label2, repo,
644 got_diff_blob_output_unidiff, &arg);
645 done:
646 if (tree1)
647 got_object_tree_close(tree1);
648 if (tree2)
649 got_object_tree_close(tree2);
650 return err;
653 const struct got_error *
654 got_diff_objects_as_commits(struct got_object_id *id1,
655 struct got_object_id *id2, int diff_context,
656 struct got_repository *repo, FILE *outfile)
658 const struct got_error *err;
659 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
661 if (id2 == NULL)
662 return got_error(GOT_ERR_NO_OBJ);
664 if (id1) {
665 err = got_object_open_as_commit(&commit1, repo, id1);
666 if (err)
667 goto done;
670 err = got_object_open_as_commit(&commit2, repo, id2);
671 if (err)
672 goto done;
674 err = got_diff_objects_as_trees(
675 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
676 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
677 outfile);
678 done:
679 if (commit1)
680 got_object_commit_close(commit1);
681 if (commit2)
682 got_object_commit_close(commit2);
683 return err;