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_repository.h"
28 #include "got_object.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, f1, blob1);
75 if (err)
76 goto done;
77 } else
78 idstr1 = "/dev/null";
80 size2 = 0;
81 if (blob2) {
82 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
83 err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
84 if (err)
85 goto done;
86 } else
87 idstr2 = "/dev/null";
89 memset(&ds, 0, sizeof(ds));
90 /* XXX should stat buffers be passed in args instead of ds? */
91 ds.stb1.st_mode = S_IFREG;
92 if (blob1)
93 ds.stb1.st_size = size1;
94 ds.stb1.st_mtime = 0; /* XXX */
96 ds.stb2.st_mode = S_IFREG;
97 if (blob2)
98 ds.stb2.st_size = size2;
99 ds.stb2.st_mtime = 0; /* XXX */
101 memset(&args, 0, sizeof(args));
102 args.diff_format = D_UNIFIED;
103 args.label[0] = label1 ? label1 : idstr1;
104 args.label[1] = label2 ? label2 : idstr2;
105 args.diff_context = diff_context;
106 flags |= D_PROTOTYPE;
108 if (outfile) {
109 fprintf(outfile, "blob - %s\n", idstr1);
110 fprintf(outfile, "blob + %s\n", idstr2);
112 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
113 done:
114 if (f1 && fclose(f1) != 0 && err == NULL)
115 err = got_error_from_errno("fclose");
116 if (f2 && fclose(f2) != 0 && err == NULL)
117 err = got_error_from_errno("fclose");
118 return err;
121 const struct got_error *
122 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
123 struct got_blob_object *blob2, struct got_object_id *id1,
124 struct got_object_id *id2, const char *label1, const char *label2,
125 struct got_repository *repo)
127 struct got_diff_blob_output_unidiff_arg *a = arg;
129 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
130 a->outfile, NULL);
133 const struct got_error *
134 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
135 const char *label1, const char *label2, int diff_context, FILE *outfile)
137 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
138 NULL);
141 const struct got_error *
142 got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
143 const char *label2, int diff_context, FILE *outfile)
145 struct got_diff_state ds;
146 struct got_diff_args args;
147 const struct got_error *err = NULL;
148 FILE *f1 = NULL;
149 char hex1[SHA1_DIGEST_STRING_LENGTH];
150 char *idstr1 = NULL;
151 size_t size1;
152 int res, flags = 0;
154 size1 = 0;
155 if (blob1) {
156 f1 = got_opentemp();
157 if (f1 == NULL)
158 return got_error_from_errno("got_opentemp");
159 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
160 err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
161 if (err)
162 goto done;
163 } else {
164 flags |= D_EMPTY1;
165 idstr1 = "/dev/null";
168 if (f2 == NULL)
169 flags |= D_EMPTY2;
171 memset(&ds, 0, sizeof(ds));
172 /* XXX should stat buffers be passed in args instead of ds? */
173 ds.stb1.st_mode = S_IFREG;
174 if (blob1)
175 ds.stb1.st_size = size1;
176 ds.stb1.st_mtime = 0; /* XXX */
178 ds.stb2.st_mode = S_IFREG;
179 ds.stb2.st_size = size2;
180 ds.stb2.st_mtime = 0; /* XXX */
182 memset(&args, 0, sizeof(args));
183 args.diff_format = D_UNIFIED;
184 args.label[0] = label2;
185 args.label[1] = label2;
186 args.diff_context = diff_context;
187 flags |= D_PROTOTYPE;
189 fprintf(outfile, "blob - %s\n", idstr1);
190 fprintf(outfile, "file + %s\n", f2 == NULL ? "/dev/null" : label2);
191 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
192 done:
193 if (f1 && fclose(f1) != 0 && err == NULL)
194 err = got_error_from_errno("fclose");
195 return err;
198 const struct got_error *
199 got_diff_blob_lines_changed(struct got_diff_changes **changes,
200 struct got_blob_object *blob1, struct got_blob_object *blob2)
202 const struct got_error *err = NULL;
204 *changes = calloc(1, sizeof(**changes));
205 if (*changes == NULL)
206 return got_error_from_errno("calloc");
207 SIMPLEQ_INIT(&(*changes)->entries);
209 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
210 if (err) {
211 got_diff_free_changes(*changes);
212 *changes = NULL;
214 return err;
217 void
218 got_diff_free_changes(struct got_diff_changes *changes)
220 struct got_diff_change *change;
221 while (!SIMPLEQ_EMPTY(&changes->entries)) {
222 change = SIMPLEQ_FIRST(&changes->entries);
223 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
224 free(change);
226 free(changes);
229 static const struct got_error *
230 diff_added_blob(struct got_object_id *id, const char *label,
231 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
233 const struct got_error *err;
234 struct got_blob_object *blob = NULL;
235 struct got_object *obj = NULL;
237 err = got_object_open(&obj, repo, id);
238 if (err)
239 return err;
241 err = got_object_blob_open(&blob, repo, obj, 8192);
242 if (err)
243 goto done;
244 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
245 done:
246 got_object_close(obj);
247 if (blob)
248 got_object_blob_close(blob);
249 return err;
252 static const struct got_error *
253 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
254 const char *label1, const char *label2, struct got_repository *repo,
255 got_diff_blob_cb cb, void *cb_arg)
257 const struct got_error *err;
258 struct got_object *obj1 = NULL;
259 struct got_object *obj2 = NULL;
260 struct got_blob_object *blob1 = NULL;
261 struct got_blob_object *blob2 = NULL;
263 err = got_object_open(&obj1, repo, id1);
264 if (err)
265 return err;
266 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
267 err = got_error(GOT_ERR_OBJ_TYPE);
268 goto done;
271 err = got_object_open(&obj2, repo, id2);
272 if (err)
273 goto done;
274 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
275 err = got_error(GOT_ERR_BAD_OBJ_DATA);
276 goto done;
279 err = got_object_blob_open(&blob1, repo, obj1, 8192);
280 if (err)
281 goto done;
283 err = got_object_blob_open(&blob2, repo, obj2, 8192);
284 if (err)
285 goto done;
287 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
288 done:
289 if (obj1)
290 got_object_close(obj1);
291 if (obj2)
292 got_object_close(obj2);
293 if (blob1)
294 got_object_blob_close(blob1);
295 if (blob2)
296 got_object_blob_close(blob2);
297 return err;
300 static const struct got_error *
301 diff_deleted_blob(struct got_object_id *id, const char *label,
302 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
304 const struct got_error *err;
305 struct got_blob_object *blob = NULL;
306 struct got_object *obj = NULL;
308 err = got_object_open(&obj, repo, id);
309 if (err)
310 return err;
312 err = got_object_blob_open(&blob, repo, obj, 8192);
313 if (err)
314 goto done;
315 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
316 done:
317 got_object_close(obj);
318 if (blob)
319 got_object_blob_close(blob);
320 return err;
323 static const struct got_error *
324 diff_added_tree(struct got_object_id *id, const char *label,
325 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
327 const struct got_error *err = NULL;
328 struct got_object *treeobj = NULL;
329 struct got_tree_object *tree = NULL;
331 err = got_object_open(&treeobj, repo, id);
332 if (err)
333 goto done;
335 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
336 err = got_error(GOT_ERR_OBJ_TYPE);
337 goto done;
340 err = got_object_tree_open(&tree, repo, treeobj);
341 if (err)
342 goto done;
344 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg);
345 done:
346 if (tree)
347 got_object_tree_close(tree);
348 if (treeobj)
349 got_object_close(treeobj);
350 return err;
353 static const struct got_error *
354 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
355 const char *label1, const char *label2, struct got_repository *repo,
356 got_diff_blob_cb cb, void *cb_arg)
358 const struct got_error *err;
359 struct got_object *treeobj1 = NULL;
360 struct got_object *treeobj2 = NULL;
361 struct got_tree_object *tree1 = NULL;
362 struct got_tree_object *tree2 = NULL;
364 err = got_object_open(&treeobj1, repo, id1);
365 if (err)
366 goto done;
368 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
369 err = got_error(GOT_ERR_OBJ_TYPE);
370 goto done;
373 err = got_object_open(&treeobj2, repo, id2);
374 if (err)
375 goto done;
377 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
378 err = got_error(GOT_ERR_OBJ_TYPE);
379 goto done;
382 err = got_object_tree_open(&tree1, repo, treeobj1);
383 if (err)
384 goto done;
386 err = got_object_tree_open(&tree2, repo, treeobj2);
387 if (err)
388 goto done;
390 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg);
392 done:
393 if (tree1)
394 got_object_tree_close(tree1);
395 if (tree2)
396 got_object_tree_close(tree2);
397 if (treeobj1)
398 got_object_close(treeobj1);
399 if (treeobj2)
400 got_object_close(treeobj2);
401 return err;
404 static const struct got_error *
405 diff_deleted_tree(struct got_object_id *id, const char *label,
406 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
408 const struct got_error *err;
409 struct got_object *treeobj = NULL;
410 struct got_tree_object *tree = NULL;
412 err = got_object_open(&treeobj, repo, id);
413 if (err)
414 goto done;
416 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
417 err = got_error(GOT_ERR_OBJ_TYPE);
418 goto done;
421 err = got_object_tree_open(&tree, repo, treeobj);
422 if (err)
423 goto done;
425 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg);
426 done:
427 if (tree)
428 got_object_tree_close(tree);
429 if (treeobj)
430 got_object_close(treeobj);
431 return err;
434 static const struct got_error *
435 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
436 const char *label1, const char *label2, struct got_repository *repo,
437 got_diff_blob_cb cb, void *cb_arg)
439 /* XXX TODO */
440 return NULL;
443 static const struct got_error *
444 diff_entry_old_new(const struct got_tree_entry *te1,
445 const struct got_tree_entry *te2, const char *label1, const char *label2,
446 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
448 const struct got_error *err = NULL;
449 int id_match;
451 if (te2 == NULL) {
452 if (S_ISDIR(te1->mode))
453 err = diff_deleted_tree(te1->id, label1, repo,
454 cb, cb_arg);
455 else
456 err = diff_deleted_blob(te1->id, label1, repo,
457 cb, cb_arg);
458 return err;
461 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
462 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
463 if (!id_match)
464 return diff_modified_tree(te1->id, te2->id,
465 label1, label2, repo, cb, cb_arg);
466 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
467 if (!id_match)
468 return diff_modified_blob(te1->id, te2->id,
469 label1, label2, repo, cb, cb_arg);
472 if (id_match)
473 return NULL;
475 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
476 cb, cb_arg);
479 static const struct got_error *
480 diff_entry_new_old(const struct got_tree_entry *te2,
481 const struct got_tree_entry *te1, const char *label2,
482 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
484 if (te1 != NULL) /* handled by diff_entry_old_new() */
485 return NULL;
487 if (S_ISDIR(te2->mode))
488 return diff_added_tree(te2->id, label2, repo, cb, cb_arg);
490 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
493 const struct got_error *
494 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
495 const char *label1, const char *label2, struct got_repository *repo,
496 got_diff_blob_cb cb, void *cb_arg)
498 const struct got_error *err = NULL;
499 struct got_tree_entry *te1 = NULL;
500 struct got_tree_entry *te2 = NULL;
501 char *l1 = NULL, *l2 = NULL;
503 if (tree1) {
504 const struct got_tree_entries *entries;
505 entries = got_object_tree_get_entries(tree1);
506 te1 = SIMPLEQ_FIRST(&entries->head);
507 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
508 te1->name) == -1)
509 return got_error_from_errno("asprintf");
511 if (tree2) {
512 const struct got_tree_entries *entries;
513 entries = got_object_tree_get_entries(tree2);
514 te2 = SIMPLEQ_FIRST(&entries->head);
515 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
516 te2->name) == -1)
517 return got_error_from_errno("asprintf");
520 do {
521 if (te1) {
522 const struct got_tree_entry *te = NULL;
523 if (tree2)
524 te = got_object_tree_find_entry(tree2,
525 te1->name);
526 if (te) {
527 free(l2);
528 l2 = NULL;
529 if (te && asprintf(&l2, "%s%s%s", label2,
530 label2[0] ? "/" : "", te->name) == -1)
531 return
532 got_error_from_errno("asprintf");
534 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
535 cb_arg);
536 if (err)
537 break;
540 if (te2) {
541 const struct got_tree_entry *te = NULL;
542 if (tree1)
543 te = got_object_tree_find_entry(tree1,
544 te2->name);
545 free(l2);
546 if (te) {
547 if (asprintf(&l2, "%s%s%s", label2,
548 label2[0] ? "/" : "", te->name) == -1)
549 return
550 got_error_from_errno("asprintf");
551 } else {
552 if (asprintf(&l2, "%s%s%s", label2,
553 label2[0] ? "/" : "", te2->name) == -1)
554 return
555 got_error_from_errno("asprintf");
557 err = diff_entry_new_old(te2, te, l2, repo, cb, cb_arg);
558 if (err)
559 break;
562 free(l1);
563 l1 = NULL;
564 if (te1) {
565 te1 = SIMPLEQ_NEXT(te1, entry);
566 if (te1 &&
567 asprintf(&l1, "%s%s%s", label1,
568 label1[0] ? "/" : "", te1->name) == -1)
569 return got_error_from_errno("asprintf");
571 free(l2);
572 l2 = NULL;
573 if (te2) {
574 te2 = SIMPLEQ_NEXT(te2, entry);
575 if (te2 &&
576 asprintf(&l2, "%s%s%s", label2,
577 label2[0] ? "/" : "", te2->name) == -1)
578 return got_error_from_errno("asprintf");
580 } while (te1 || te2);
582 return err;
585 const struct got_error *
586 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
587 const char *label1, const char *label2, int diff_context,
588 struct got_repository *repo, FILE *outfile)
590 const struct got_error *err;
591 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
593 if (id1 == NULL && id2 == NULL)
594 return got_error(GOT_ERR_NO_OBJ);
596 if (id1) {
597 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
598 if (err)
599 goto done;
601 if (id2) {
602 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
603 if (err)
604 goto done;
606 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
607 outfile);
608 done:
609 if (blob1)
610 got_object_blob_close(blob1);
611 if (blob2)
612 got_object_blob_close(blob2);
613 return err;
616 const struct got_error *
617 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
618 char *label1, char *label2, int diff_context, struct got_repository *repo,
619 FILE *outfile)
621 const struct got_error *err;
622 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
623 struct got_diff_blob_output_unidiff_arg arg;
625 if (id1 == NULL && id2 == NULL)
626 return got_error(GOT_ERR_NO_OBJ);
628 if (id1) {
629 err = got_object_open_as_tree(&tree1, repo, id1);
630 if (err)
631 goto done;
633 if (id2) {
634 err = got_object_open_as_tree(&tree2, repo, id2);
635 if (err)
636 goto done;
638 arg.diff_context = diff_context;
639 arg.outfile = outfile;
640 err = got_diff_tree(tree1, tree2, label1, label2, repo,
641 got_diff_blob_output_unidiff, &arg);
642 done:
643 if (tree1)
644 got_object_tree_close(tree1);
645 if (tree2)
646 got_object_tree_close(tree2);
647 return err;
650 const struct got_error *
651 got_diff_objects_as_commits(struct got_object_id *id1,
652 struct got_object_id *id2, int diff_context,
653 struct got_repository *repo, FILE *outfile)
655 const struct got_error *err;
656 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
658 if (id2 == NULL)
659 return got_error(GOT_ERR_NO_OBJ);
661 if (id1) {
662 err = got_object_open_as_commit(&commit1, repo, id1);
663 if (err)
664 goto done;
667 err = got_object_open_as_commit(&commit2, repo, id2);
668 if (err)
669 goto done;
671 err = got_diff_objects_as_trees(
672 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
673 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
674 outfile);
675 done:
676 if (commit1)
677 got_object_commit_close(commit1);
678 if (commit2)
679 got_object_commit_close(commit2);
680 return err;