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,
329 int diff_content)
331 const struct got_error *err = NULL;
332 struct got_object *treeobj = NULL;
333 struct got_tree_object *tree = NULL;
335 err = got_object_open(&treeobj, repo, id);
336 if (err)
337 goto done;
339 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
340 err = got_error(GOT_ERR_OBJ_TYPE);
341 goto done;
344 err = got_object_tree_open(&tree, repo, treeobj);
345 if (err)
346 goto done;
348 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
349 diff_content);
350 done:
351 if (tree)
352 got_object_tree_close(tree);
353 if (treeobj)
354 got_object_close(treeobj);
355 return err;
358 static const struct got_error *
359 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
360 const char *label1, const char *label2, struct got_repository *repo,
361 got_diff_blob_cb cb, void *cb_arg, int diff_content)
363 const struct got_error *err;
364 struct got_object *treeobj1 = NULL;
365 struct got_object *treeobj2 = NULL;
366 struct got_tree_object *tree1 = NULL;
367 struct got_tree_object *tree2 = NULL;
369 err = got_object_open(&treeobj1, repo, id1);
370 if (err)
371 goto done;
373 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
374 err = got_error(GOT_ERR_OBJ_TYPE);
375 goto done;
378 err = got_object_open(&treeobj2, repo, id2);
379 if (err)
380 goto done;
382 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
383 err = got_error(GOT_ERR_OBJ_TYPE);
384 goto done;
387 err = got_object_tree_open(&tree1, repo, treeobj1);
388 if (err)
389 goto done;
391 err = got_object_tree_open(&tree2, repo, treeobj2);
392 if (err)
393 goto done;
395 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
396 diff_content);
398 done:
399 if (tree1)
400 got_object_tree_close(tree1);
401 if (tree2)
402 got_object_tree_close(tree2);
403 if (treeobj1)
404 got_object_close(treeobj1);
405 if (treeobj2)
406 got_object_close(treeobj2);
407 return err;
410 static const struct got_error *
411 diff_deleted_tree(struct got_object_id *id, const char *label,
412 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
413 int diff_content)
415 const struct got_error *err;
416 struct got_object *treeobj = NULL;
417 struct got_tree_object *tree = NULL;
419 err = got_object_open(&treeobj, repo, id);
420 if (err)
421 goto done;
423 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
424 err = got_error(GOT_ERR_OBJ_TYPE);
425 goto done;
428 err = got_object_tree_open(&tree, repo, treeobj);
429 if (err)
430 goto done;
432 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
433 diff_content);
434 done:
435 if (tree)
436 got_object_tree_close(tree);
437 if (treeobj)
438 got_object_close(treeobj);
439 return err;
442 static const struct got_error *
443 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
444 const char *label1, const char *label2, struct got_repository *repo,
445 got_diff_blob_cb cb, void *cb_arg)
447 /* XXX TODO */
448 return NULL;
451 static const struct got_error *
452 diff_entry_old_new(const struct got_tree_entry *te1,
453 const struct got_tree_entry *te2, const char *label1, const char *label2,
454 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
455 int diff_content)
457 const struct got_error *err = NULL;
458 int id_match;
460 if (te2 == NULL) {
461 if (S_ISDIR(te1->mode))
462 err = diff_deleted_tree(te1->id, label1, repo,
463 cb, cb_arg, diff_content);
464 else {
465 if (diff_content)
466 err = diff_deleted_blob(te1->id, label1, repo,
467 cb, cb_arg);
468 else
469 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
470 label1, NULL, repo);
472 return err;
475 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
476 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
477 if (!id_match)
478 return diff_modified_tree(te1->id, te2->id,
479 label1, label2, repo, cb, cb_arg, diff_content);
480 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
481 if (!id_match) {
482 if (diff_content)
483 return diff_modified_blob(te1->id, te2->id,
484 label1, label2, repo, cb, cb_arg);
485 else
486 return cb(cb_arg, NULL, NULL, te1->id,
487 te2->id, label1, label2, repo);
491 if (id_match)
492 return NULL;
494 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
495 cb, cb_arg);
498 static const struct got_error *
499 diff_entry_new_old(const struct got_tree_entry *te2,
500 const struct got_tree_entry *te1, const char *label2,
501 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
502 int diff_content)
504 if (te1 != NULL) /* handled by diff_entry_old_new() */
505 return NULL;
507 if (S_ISDIR(te2->mode))
508 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
509 diff_content);
511 if (diff_content)
512 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
514 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
517 const struct got_error *
518 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
519 const char *label1, const char *label2, struct got_repository *repo,
520 got_diff_blob_cb cb, void *cb_arg, int diff_content)
522 const struct got_error *err = NULL;
523 struct got_tree_entry *te1 = NULL;
524 struct got_tree_entry *te2 = NULL;
525 char *l1 = NULL, *l2 = NULL;
527 if (tree1) {
528 const struct got_tree_entries *entries;
529 entries = got_object_tree_get_entries(tree1);
530 te1 = SIMPLEQ_FIRST(&entries->head);
531 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
532 te1->name) == -1)
533 return got_error_from_errno("asprintf");
535 if (tree2) {
536 const struct got_tree_entries *entries;
537 entries = got_object_tree_get_entries(tree2);
538 te2 = SIMPLEQ_FIRST(&entries->head);
539 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
540 te2->name) == -1)
541 return got_error_from_errno("asprintf");
544 do {
545 if (te1) {
546 const struct got_tree_entry *te = NULL;
547 if (tree2)
548 te = got_object_tree_find_entry(tree2,
549 te1->name);
550 if (te) {
551 free(l2);
552 l2 = NULL;
553 if (te && asprintf(&l2, "%s%s%s", label2,
554 label2[0] ? "/" : "", te->name) == -1)
555 return
556 got_error_from_errno("asprintf");
558 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
559 cb_arg, diff_content);
560 if (err)
561 break;
564 if (te2) {
565 const struct got_tree_entry *te = NULL;
566 if (tree1)
567 te = got_object_tree_find_entry(tree1,
568 te2->name);
569 free(l2);
570 if (te) {
571 if (asprintf(&l2, "%s%s%s", label2,
572 label2[0] ? "/" : "", te->name) == -1)
573 return
574 got_error_from_errno("asprintf");
575 } else {
576 if (asprintf(&l2, "%s%s%s", label2,
577 label2[0] ? "/" : "", te2->name) == -1)
578 return
579 got_error_from_errno("asprintf");
581 err = diff_entry_new_old(te2, te, l2, repo,
582 cb, cb_arg, diff_content);
583 if (err)
584 break;
587 free(l1);
588 l1 = NULL;
589 if (te1) {
590 te1 = SIMPLEQ_NEXT(te1, entry);
591 if (te1 &&
592 asprintf(&l1, "%s%s%s", label1,
593 label1[0] ? "/" : "", te1->name) == -1)
594 return got_error_from_errno("asprintf");
596 free(l2);
597 l2 = NULL;
598 if (te2) {
599 te2 = SIMPLEQ_NEXT(te2, entry);
600 if (te2 &&
601 asprintf(&l2, "%s%s%s", label2,
602 label2[0] ? "/" : "", te2->name) == -1)
603 return got_error_from_errno("asprintf");
605 } while (te1 || te2);
607 return err;
610 const struct got_error *
611 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
612 const char *label1, const char *label2, int diff_context,
613 struct got_repository *repo, FILE *outfile)
615 const struct got_error *err;
616 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
618 if (id1 == NULL && id2 == NULL)
619 return got_error(GOT_ERR_NO_OBJ);
621 if (id1) {
622 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
623 if (err)
624 goto done;
626 if (id2) {
627 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
628 if (err)
629 goto done;
631 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
632 outfile);
633 done:
634 if (blob1)
635 got_object_blob_close(blob1);
636 if (blob2)
637 got_object_blob_close(blob2);
638 return err;
641 const struct got_error *
642 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
643 char *label1, char *label2, int diff_context, struct got_repository *repo,
644 FILE *outfile)
646 const struct got_error *err;
647 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
648 struct got_diff_blob_output_unidiff_arg arg;
650 if (id1 == NULL && id2 == NULL)
651 return got_error(GOT_ERR_NO_OBJ);
653 if (id1) {
654 err = got_object_open_as_tree(&tree1, repo, id1);
655 if (err)
656 goto done;
658 if (id2) {
659 err = got_object_open_as_tree(&tree2, repo, id2);
660 if (err)
661 goto done;
663 arg.diff_context = diff_context;
664 arg.outfile = outfile;
665 err = got_diff_tree(tree1, tree2, label1, label2, repo,
666 got_diff_blob_output_unidiff, &arg, 1);
667 done:
668 if (tree1)
669 got_object_tree_close(tree1);
670 if (tree2)
671 got_object_tree_close(tree2);
672 return err;
675 const struct got_error *
676 got_diff_objects_as_commits(struct got_object_id *id1,
677 struct got_object_id *id2, int diff_context,
678 struct got_repository *repo, FILE *outfile)
680 const struct got_error *err;
681 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
683 if (id2 == NULL)
684 return got_error(GOT_ERR_NO_OBJ);
686 if (id1) {
687 err = got_object_open_as_commit(&commit1, repo, id1);
688 if (err)
689 goto done;
692 err = got_object_open_as_commit(&commit2, repo, id2);
693 if (err)
694 goto done;
696 err = got_diff_objects_as_trees(
697 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
698 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
699 outfile);
700 done:
701 if (commit1)
702 got_object_commit_close(commit1);
703 if (commit2)
704 got_object_commit_close(commit2);
705 return err;