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(GOT_ERR_FILE_OPEN);
58 } else
59 flags |= D_EMPTY1;
61 if (blob2) {
62 f2 = got_opentemp();
63 if (f2 == NULL) {
64 fclose(f1);
65 return got_error(GOT_ERR_FILE_OPEN);
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_lines_changed(struct got_diff_changes **changes,
130 struct got_blob_object *blob1, struct got_blob_object *blob2)
132 const struct got_error *err = NULL;
134 *changes = calloc(1, sizeof(**changes));
135 if (*changes == NULL)
136 return got_error_from_errno();
137 SIMPLEQ_INIT(&(*changes)->entries);
139 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
140 if (err) {
141 got_diff_free_changes(*changes);
142 *changes = NULL;
144 return err;
147 void
148 got_diff_free_changes(struct got_diff_changes *changes)
150 struct got_diff_change *change;
151 while (!SIMPLEQ_EMPTY(&changes->entries)) {
152 change = SIMPLEQ_FIRST(&changes->entries);
153 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
154 free(change);
156 free(changes);
159 struct got_tree_entry *
160 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
162 struct got_tree_entry *te2;
163 const struct got_tree_entries *entries2;
165 entries2 = got_object_tree_get_entries(tree2);
166 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
167 if (strcmp(te1->name, te2->name) == 0)
168 return te2;
170 return NULL;
173 static const struct got_error *
174 diff_added_blob(struct got_object_id *id, const char *label,
175 int diff_context, struct got_repository *repo, FILE *outfile)
177 const struct got_error *err;
178 struct got_blob_object *blob = NULL;
179 struct got_object *obj = NULL;
181 err = got_object_open(&obj, repo, id);
182 if (err)
183 return err;
185 err = got_object_blob_open(&blob, repo, obj, 8192);
186 if (err)
187 goto done;
188 err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
189 done:
190 got_object_close(obj);
191 if (blob)
192 got_object_blob_close(blob);
193 return err;
196 static const struct got_error *
197 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
198 const char *label1, const char *label2, int diff_context,
199 struct got_repository *repo, FILE *outfile)
201 const struct got_error *err;
202 struct got_object *obj1 = NULL;
203 struct got_object *obj2 = NULL;
204 struct got_blob_object *blob1 = NULL;
205 struct got_blob_object *blob2 = NULL;
207 err = got_object_open(&obj1, repo, id1);
208 if (err)
209 return err;
210 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
211 err = got_error(GOT_ERR_OBJ_TYPE);
212 goto done;
215 err = got_object_open(&obj2, repo, id2);
216 if (err)
217 goto done;
218 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
219 err = got_error(GOT_ERR_BAD_OBJ_DATA);
220 goto done;
223 err = got_object_blob_open(&blob1, repo, obj1, 8192);
224 if (err)
225 goto done;
227 err = got_object_blob_open(&blob2, repo, obj2, 8192);
228 if (err)
229 goto done;
231 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
232 outfile);
234 done:
235 if (obj1)
236 got_object_close(obj1);
237 if (obj2)
238 got_object_close(obj2);
239 if (blob1)
240 got_object_blob_close(blob1);
241 if (blob2)
242 got_object_blob_close(blob2);
243 return err;
246 static const struct got_error *
247 diff_deleted_blob(struct got_object_id *id, const char *label,
248 int diff_context, struct got_repository *repo, FILE *outfile)
250 const struct got_error *err;
251 struct got_blob_object *blob = NULL;
252 struct got_object *obj = NULL;
254 err = got_object_open(&obj, repo, id);
255 if (err)
256 return err;
258 err = got_object_blob_open(&blob, repo, obj, 8192);
259 if (err)
260 goto done;
261 err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
262 done:
263 got_object_close(obj);
264 if (blob)
265 got_object_blob_close(blob);
266 return err;
269 static const struct got_error *
270 diff_added_tree(struct got_object_id *id, const char *label,
271 int diff_context, struct got_repository *repo, FILE *outfile)
273 const struct got_error *err = NULL;
274 struct got_object *treeobj = NULL;
275 struct got_tree_object *tree = NULL;
277 err = got_object_open(&treeobj, repo, id);
278 if (err)
279 goto done;
281 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
282 err = got_error(GOT_ERR_OBJ_TYPE);
283 goto done;
286 err = got_object_tree_open(&tree, repo, treeobj);
287 if (err)
288 goto done;
290 err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo,
291 outfile);
293 done:
294 if (tree)
295 got_object_tree_close(tree);
296 if (treeobj)
297 got_object_close(treeobj);
298 return err;
301 static const struct got_error *
302 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
303 const char *label1, const char *label2, int diff_context,
304 struct got_repository *repo, FILE *outfile)
306 const struct got_error *err;
307 struct got_object *treeobj1 = NULL;
308 struct got_object *treeobj2 = NULL;
309 struct got_tree_object *tree1 = NULL;
310 struct got_tree_object *tree2 = NULL;
312 err = got_object_open(&treeobj1, repo, id1);
313 if (err)
314 goto done;
316 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
317 err = got_error(GOT_ERR_OBJ_TYPE);
318 goto done;
321 err = got_object_open(&treeobj2, repo, id2);
322 if (err)
323 goto done;
325 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
326 err = got_error(GOT_ERR_OBJ_TYPE);
327 goto done;
330 err = got_object_tree_open(&tree1, repo, treeobj1);
331 if (err)
332 goto done;
334 err = got_object_tree_open(&tree2, repo, treeobj2);
335 if (err)
336 goto done;
338 err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo,
339 outfile);
341 done:
342 if (tree1)
343 got_object_tree_close(tree1);
344 if (tree2)
345 got_object_tree_close(tree2);
346 if (treeobj1)
347 got_object_close(treeobj1);
348 if (treeobj2)
349 got_object_close(treeobj2);
350 return err;
353 static const struct got_error *
354 diff_deleted_tree(struct got_object_id *id, const char *label,
355 int diff_context, struct got_repository *repo, FILE *outfile)
357 const struct got_error *err;
358 struct got_object *treeobj = NULL;
359 struct got_tree_object *tree = NULL;
361 err = got_object_open(&treeobj, repo, id);
362 if (err)
363 goto done;
365 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
367 goto done;
370 err = got_object_tree_open(&tree, repo, treeobj);
371 if (err)
372 goto done;
374 err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
375 outfile);
376 done:
377 if (tree)
378 got_object_tree_close(tree);
379 if (treeobj)
380 got_object_close(treeobj);
381 return err;
384 static const struct got_error *
385 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
386 const char *label1, const char *label2, FILE *outfile)
388 /* XXX TODO */
389 return NULL;
392 static const struct got_error *
393 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
394 const char *label1, const char *label2, int diff_context,
395 struct got_repository *repo, FILE *outfile)
397 const struct got_error *err = NULL;
398 int id_match;
400 if (te2 == NULL) {
401 if (S_ISDIR(te1->mode))
402 err = diff_deleted_tree(te1->id, label1, diff_context,
403 repo, outfile);
404 else
405 err = diff_deleted_blob(te1->id, label1, diff_context,
406 repo, outfile);
407 return err;
410 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
411 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
412 if (!id_match)
413 return diff_modified_tree(te1->id, te2->id,
414 label1, label2, diff_context, repo, outfile);
415 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
416 if (!id_match)
417 return diff_modified_blob(te1->id, te2->id,
418 label1, label2, diff_context, repo, outfile);
421 if (id_match)
422 return NULL;
424 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
427 static const struct got_error *
428 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
429 const char *label2, int diff_context, struct got_repository *repo,
430 FILE *outfile)
432 if (te1 != NULL) /* handled by diff_entry_old_new() */
433 return NULL;
435 if (S_ISDIR(te2->mode))
436 return diff_added_tree(te2->id, label2, diff_context, repo,
437 outfile);
439 return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
442 const struct got_error *
443 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
444 const char *label1, const char *label2, int diff_context,
445 struct got_repository *repo, FILE *outfile)
447 const struct got_error *err = NULL;
448 struct got_tree_entry *te1 = NULL;
449 struct got_tree_entry *te2 = NULL;
450 char *l1 = NULL, *l2 = NULL;
452 if (tree1) {
453 const struct got_tree_entries *entries;
454 entries = got_object_tree_get_entries(tree1);
455 te1 = SIMPLEQ_FIRST(&entries->head);
456 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
457 te1->name) == -1)
458 return got_error_from_errno();
460 if (tree2) {
461 const struct got_tree_entries *entries;
462 entries = got_object_tree_get_entries(tree2);
463 te2 = SIMPLEQ_FIRST(&entries->head);
464 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
465 te2->name) == -1)
466 return got_error_from_errno();
469 do {
470 if (te1) {
471 struct got_tree_entry *te = NULL;
472 if (tree2)
473 te = match_entry_by_name(te1, tree2);
474 if (te) {
475 free(l2);
476 l2 = NULL;
477 if (te && asprintf(&l2, "%s%s%s", label2,
478 label2[0] ? "/" : "", te->name) == -1)
479 return got_error_from_errno();
481 err = diff_entry_old_new(te1, te, l1, l2, diff_context,
482 repo, outfile);
483 if (err)
484 break;
487 if (te2) {
488 struct got_tree_entry *te = NULL;
489 if (tree1)
490 te = match_entry_by_name(te2, tree1);
491 free(l2);
492 if (te) {
493 if (asprintf(&l2, "%s%s%s", label2,
494 label2[0] ? "/" : "", te->name) == -1)
495 return got_error_from_errno();
496 } else {
497 if (asprintf(&l2, "%s%s%s", label2,
498 label2[0] ? "/" : "", te2->name) == -1)
499 return got_error_from_errno();
501 err = diff_entry_new_old(te2, te, l2, diff_context,
502 repo, outfile);
503 if (err)
504 break;
507 free(l1);
508 l1 = NULL;
509 if (te1) {
510 te1 = SIMPLEQ_NEXT(te1, entry);
511 if (te1 &&
512 asprintf(&l1, "%s%s%s", label1,
513 label1[0] ? "/" : "", te1->name) == -1)
514 return got_error_from_errno();
516 free(l2);
517 l2 = NULL;
518 if (te2) {
519 te2 = SIMPLEQ_NEXT(te2, entry);
520 if (te2 &&
521 asprintf(&l2, "%s%s%s", label2,
522 label2[0] ? "/" : "", te2->name) == -1)
523 return got_error_from_errno();
525 } while (te1 || te2);
527 return err;
530 const struct got_error *
531 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
532 const char *label1, const char *label2, int diff_context,
533 struct got_repository *repo, FILE *outfile)
535 const struct got_error *err;
536 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
538 if (id1 == NULL && id2 == NULL)
539 return got_error(GOT_ERR_NO_OBJ);
541 if (id1) {
542 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
543 if (err)
544 goto done;
546 if (id2) {
547 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
548 if (err)
549 goto done;
551 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
552 outfile);
553 done:
554 if (blob1)
555 got_object_blob_close(blob1);
556 if (blob2)
557 got_object_blob_close(blob2);
558 return err;
561 const struct got_error *
562 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
563 char *label1, char *label2, int diff_context, struct got_repository *repo,
564 FILE *outfile)
566 const struct got_error *err;
567 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
569 if (id1 == NULL && id2 == NULL)
570 return got_error(GOT_ERR_NO_OBJ);
572 if (id1) {
573 err = got_object_open_as_tree(&tree1, repo, id1);
574 if (err)
575 goto done;
577 if (id2) {
578 err = got_object_open_as_tree(&tree2, repo, id2);
579 if (err)
580 goto done;
582 err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
583 repo, outfile);
584 done:
585 if (tree1)
586 got_object_tree_close(tree1);
587 if (tree2)
588 got_object_tree_close(tree2);
589 return err;
592 const struct got_error *
593 got_diff_objects_as_commits(struct got_object_id *id1,
594 struct got_object_id *id2, int diff_context,
595 struct got_repository *repo, FILE *outfile)
597 const struct got_error *err;
598 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
600 if (id2 == NULL)
601 return got_error(GOT_ERR_NO_OBJ);
603 if (id1) {
604 err = got_object_open_as_commit(&commit1, repo, id1);
605 if (err)
606 goto done;
609 err = got_object_open_as_commit(&commit2, repo, id2);
610 if (err)
611 goto done;
613 err = got_diff_objects_as_trees(
614 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
615 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
616 outfile);
617 done:
618 if (commit1)
619 got_object_commit_close(commit1);
620 if (commit2)
621 got_object_commit_close(commit2);
622 return err;