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"
33 #include "got_lib_diff.h"
34 #include "got_lib_path.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();
58 } else
59 flags |= D_EMPTY1;
61 if (blob2) {
62 f2 = got_opentemp();
63 if (f2 == NULL) {
64 fclose(f1);
65 return got_error_from_errno();
66 }
67 } else
68 flags |= D_EMPTY2;
70 size1 = 0;
71 if (blob1) {
72 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
73 err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
74 if (err)
75 goto done;
76 } else
77 idstr1 = "/dev/null";
79 size2 = 0;
80 if (blob2) {
81 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
82 err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
83 if (err)
84 goto done;
85 } else
86 idstr2 = "/dev/null";
88 memset(&ds, 0, sizeof(ds));
89 /* XXX should stat buffers be passed in args instead of ds? */
90 ds.stb1.st_mode = S_IFREG;
91 if (blob1)
92 ds.stb1.st_size = size1;
93 ds.stb1.st_mtime = 0; /* XXX */
95 ds.stb2.st_mode = S_IFREG;
96 if (blob2)
97 ds.stb2.st_size = size2;
98 ds.stb2.st_mtime = 0; /* XXX */
100 memset(&args, 0, sizeof(args));
101 args.diff_format = D_UNIFIED;
102 args.label[0] = label1 ? label1 : idstr1;
103 args.label[1] = label2 ? label2 : idstr2;
104 args.diff_context = diff_context;
105 flags |= D_PROTOTYPE;
107 if (outfile) {
108 fprintf(outfile, "blob - %s\n", idstr1);
109 fprintf(outfile, "blob + %s\n", idstr2);
111 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
112 done:
113 if (f1)
114 fclose(f1);
115 if (f2)
116 fclose(f2);
117 return err;
120 const struct got_error *
121 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
122 const char *label1, const char *label2, int diff_context, FILE *outfile)
124 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
125 NULL);
128 const struct got_error *
129 got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
130 const char *label2, int diff_context, FILE *outfile)
132 struct got_diff_state ds;
133 struct got_diff_args args;
134 const struct got_error *err = NULL;
135 FILE *f1 = NULL;
136 char hex1[SHA1_DIGEST_STRING_LENGTH];
137 char *idstr1 = NULL;
138 size_t size1;
139 int res, flags = 0;
141 size1 = 0;
142 if (blob1) {
143 f1 = got_opentemp();
144 if (f1 == NULL)
145 return got_error_from_errno();
146 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
147 err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
148 if (err)
149 goto done;
150 } else {
151 flags |= D_EMPTY1;
152 idstr1 = "/dev/null";
155 if (f2 == NULL)
156 flags |= D_EMPTY2;
158 memset(&ds, 0, sizeof(ds));
159 /* XXX should stat buffers be passed in args instead of ds? */
160 ds.stb1.st_mode = S_IFREG;
161 if (blob1)
162 ds.stb1.st_size = size1;
163 ds.stb1.st_mtime = 0; /* XXX */
165 ds.stb2.st_mode = S_IFREG;
166 ds.stb2.st_size = size2;
167 ds.stb2.st_mtime = 0; /* XXX */
169 memset(&args, 0, sizeof(args));
170 args.diff_format = D_UNIFIED;
171 args.label[0] = label2;
172 args.label[1] = label2;
173 args.diff_context = diff_context;
174 flags |= D_PROTOTYPE;
176 fprintf(outfile, "blob - %s\n", idstr1);
177 fprintf(outfile, "file + %s\n", label2);
178 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
179 done:
180 if (f1)
181 fclose(f1);
182 return err;
185 const struct got_error *
186 got_diff_blob_lines_changed(struct got_diff_changes **changes,
187 struct got_blob_object *blob1, struct got_blob_object *blob2)
189 const struct got_error *err = NULL;
191 *changes = calloc(1, sizeof(**changes));
192 if (*changes == NULL)
193 return got_error_from_errno();
194 SIMPLEQ_INIT(&(*changes)->entries);
196 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
197 if (err) {
198 got_diff_free_changes(*changes);
199 *changes = NULL;
201 return err;
204 void
205 got_diff_free_changes(struct got_diff_changes *changes)
207 struct got_diff_change *change;
208 while (!SIMPLEQ_EMPTY(&changes->entries)) {
209 change = SIMPLEQ_FIRST(&changes->entries);
210 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
211 free(change);
213 free(changes);
216 struct got_tree_entry *
217 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
219 struct got_tree_entry *te2;
220 const struct got_tree_entries *entries2;
222 entries2 = got_object_tree_get_entries(tree2);
223 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
224 if (strcmp(te1->name, te2->name) == 0)
225 return te2;
227 return NULL;
230 static const struct got_error *
231 diff_added_blob(struct got_object_id *id, const char *label,
232 int diff_context, struct got_repository *repo, FILE *outfile)
234 const struct got_error *err;
235 struct got_blob_object *blob = NULL;
236 struct got_object *obj = NULL;
238 err = got_object_open(&obj, repo, id);
239 if (err)
240 return err;
242 err = got_object_blob_open(&blob, repo, obj, 8192);
243 if (err)
244 goto done;
245 err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
246 done:
247 got_object_close(obj);
248 if (blob)
249 got_object_blob_close(blob);
250 return err;
253 static const struct got_error *
254 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
255 const char *label1, const char *label2, int diff_context,
256 struct got_repository *repo, FILE *outfile)
258 const struct got_error *err;
259 struct got_object *obj1 = NULL;
260 struct got_object *obj2 = NULL;
261 struct got_blob_object *blob1 = NULL;
262 struct got_blob_object *blob2 = NULL;
264 err = got_object_open(&obj1, repo, id1);
265 if (err)
266 return err;
267 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
268 err = got_error(GOT_ERR_OBJ_TYPE);
269 goto done;
272 err = got_object_open(&obj2, repo, id2);
273 if (err)
274 goto done;
275 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
276 err = got_error(GOT_ERR_BAD_OBJ_DATA);
277 goto done;
280 err = got_object_blob_open(&blob1, repo, obj1, 8192);
281 if (err)
282 goto done;
284 err = got_object_blob_open(&blob2, repo, obj2, 8192);
285 if (err)
286 goto done;
288 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
289 outfile);
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 int diff_context, struct got_repository *repo, FILE *outfile)
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 = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
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 int diff_context, struct got_repository *repo, FILE *outfile)
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, diff_context, repo,
348 outfile);
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, int diff_context,
361 struct got_repository *repo, FILE *outfile)
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, diff_context, repo,
396 outfile);
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 int diff_context, struct got_repository *repo, FILE *outfile)
414 const struct got_error *err;
415 struct got_object *treeobj = NULL;
416 struct got_tree_object *tree = NULL;
418 err = got_object_open(&treeobj, repo, id);
419 if (err)
420 goto done;
422 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
423 err = got_error(GOT_ERR_OBJ_TYPE);
424 goto done;
427 err = got_object_tree_open(&tree, repo, treeobj);
428 if (err)
429 goto done;
431 err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
432 outfile);
433 done:
434 if (tree)
435 got_object_tree_close(tree);
436 if (treeobj)
437 got_object_close(treeobj);
438 return err;
441 static const struct got_error *
442 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
443 const char *label1, const char *label2, FILE *outfile)
445 /* XXX TODO */
446 return NULL;
449 static const struct got_error *
450 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
451 const char *label1, const char *label2, int diff_context,
452 struct got_repository *repo, FILE *outfile)
454 const struct got_error *err = NULL;
455 int id_match;
457 if (te2 == NULL) {
458 if (S_ISDIR(te1->mode))
459 err = diff_deleted_tree(te1->id, label1, diff_context,
460 repo, outfile);
461 else
462 err = diff_deleted_blob(te1->id, label1, diff_context,
463 repo, outfile);
464 return err;
467 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
468 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
469 if (!id_match)
470 return diff_modified_tree(te1->id, te2->id,
471 label1, label2, diff_context, repo, outfile);
472 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
473 if (!id_match)
474 return diff_modified_blob(te1->id, te2->id,
475 label1, label2, diff_context, repo, outfile);
478 if (id_match)
479 return NULL;
481 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
484 static const struct got_error *
485 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
486 const char *label2, int diff_context, struct got_repository *repo,
487 FILE *outfile)
489 if (te1 != NULL) /* handled by diff_entry_old_new() */
490 return NULL;
492 if (S_ISDIR(te2->mode))
493 return diff_added_tree(te2->id, label2, diff_context, repo,
494 outfile);
496 return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
499 const struct got_error *
500 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
501 const char *label1, const char *label2, int diff_context,
502 struct got_repository *repo, FILE *outfile)
504 const struct got_error *err = NULL;
505 struct got_tree_entry *te1 = NULL;
506 struct got_tree_entry *te2 = NULL;
507 char *l1 = NULL, *l2 = NULL;
509 if (tree1) {
510 const struct got_tree_entries *entries;
511 entries = got_object_tree_get_entries(tree1);
512 te1 = SIMPLEQ_FIRST(&entries->head);
513 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
514 te1->name) == -1)
515 return got_error_from_errno();
517 if (tree2) {
518 const struct got_tree_entries *entries;
519 entries = got_object_tree_get_entries(tree2);
520 te2 = SIMPLEQ_FIRST(&entries->head);
521 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
522 te2->name) == -1)
523 return got_error_from_errno();
526 do {
527 if (te1) {
528 struct got_tree_entry *te = NULL;
529 if (tree2)
530 te = match_entry_by_name(te1, tree2);
531 if (te) {
532 free(l2);
533 l2 = NULL;
534 if (te && asprintf(&l2, "%s%s%s", label2,
535 label2[0] ? "/" : "", te->name) == -1)
536 return got_error_from_errno();
538 err = diff_entry_old_new(te1, te, l1, l2, diff_context,
539 repo, outfile);
540 if (err)
541 break;
544 if (te2) {
545 struct got_tree_entry *te = NULL;
546 if (tree1)
547 te = match_entry_by_name(te2, tree1);
548 free(l2);
549 if (te) {
550 if (asprintf(&l2, "%s%s%s", label2,
551 label2[0] ? "/" : "", te->name) == -1)
552 return got_error_from_errno();
553 } else {
554 if (asprintf(&l2, "%s%s%s", label2,
555 label2[0] ? "/" : "", te2->name) == -1)
556 return got_error_from_errno();
558 err = diff_entry_new_old(te2, te, l2, diff_context,
559 repo, outfile);
560 if (err)
561 break;
564 free(l1);
565 l1 = NULL;
566 if (te1) {
567 te1 = SIMPLEQ_NEXT(te1, entry);
568 if (te1 &&
569 asprintf(&l1, "%s%s%s", label1,
570 label1[0] ? "/" : "", te1->name) == -1)
571 return got_error_from_errno();
573 free(l2);
574 l2 = NULL;
575 if (te2) {
576 te2 = SIMPLEQ_NEXT(te2, entry);
577 if (te2 &&
578 asprintf(&l2, "%s%s%s", label2,
579 label2[0] ? "/" : "", te2->name) == -1)
580 return got_error_from_errno();
582 } while (te1 || te2);
584 return err;
587 const struct got_error *
588 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
589 const char *label1, const char *label2, int diff_context,
590 struct got_repository *repo, FILE *outfile)
592 const struct got_error *err;
593 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
595 if (id1 == NULL && id2 == NULL)
596 return got_error(GOT_ERR_NO_OBJ);
598 if (id1) {
599 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
600 if (err)
601 goto done;
603 if (id2) {
604 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
605 if (err)
606 goto done;
608 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
609 outfile);
610 done:
611 if (blob1)
612 got_object_blob_close(blob1);
613 if (blob2)
614 got_object_blob_close(blob2);
615 return err;
618 const struct got_error *
619 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
620 char *label1, char *label2, int diff_context, struct got_repository *repo,
621 FILE *outfile)
623 const struct got_error *err;
624 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
626 if (id1 == NULL && id2 == NULL)
627 return got_error(GOT_ERR_NO_OBJ);
629 if (id1) {
630 err = got_object_open_as_tree(&tree1, repo, id1);
631 if (err)
632 goto done;
634 if (id2) {
635 err = got_object_open_as_tree(&tree2, repo, id2);
636 if (err)
637 goto done;
639 err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
640 repo, outfile);
641 done:
642 if (tree1)
643 got_object_tree_close(tree1);
644 if (tree2)
645 got_object_tree_close(tree2);
646 return err;
649 const struct got_error *
650 got_diff_objects_as_commits(struct got_object_id *id1,
651 struct got_object_id *id2, int diff_context,
652 struct got_repository *repo, FILE *outfile)
654 const struct got_error *err;
655 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
657 if (id2 == NULL)
658 return got_error(GOT_ERR_NO_OBJ);
660 if (id1) {
661 err = got_object_open_as_commit(&commit1, repo, id1);
662 if (err)
663 goto done;
666 err = got_object_open_as_commit(&commit2, repo, id2);
667 if (err)
668 goto done;
670 err = got_diff_objects_as_trees(
671 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
672 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
673 outfile);
674 done:
675 if (commit1)
676 got_object_commit_close(commit1);
677 if (commit2)
678 got_object_commit_close(commit2);
679 return err;