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 (label1 && strcmp(label1, idstr1) != 0)
108 fprintf(outfile, "blob - %s\n", idstr1);
109 if (label2 && strcmp(label2, idstr2) != 0)
110 fprintf(outfile, "blob + %s\n", idstr2);
112 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
113 done:
114 if (f1)
115 fclose(f1);
116 if (f2)
117 fclose(f2);
118 return err;
121 const struct got_error *
122 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
123 const char *label1, const char *label2, int diff_context, FILE *outfile)
125 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
126 NULL);
129 const struct got_error *
130 got_diff_blob_lines_changed(struct got_diff_changes **changes,
131 struct got_blob_object *blob1, struct got_blob_object *blob2)
133 const struct got_error *err = NULL;
135 *changes = calloc(1, sizeof(**changes));
136 if (*changes == NULL)
137 return got_error_from_errno();
138 SIMPLEQ_INIT(&(*changes)->entries);
140 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
141 if (err) {
142 got_diff_free_changes(*changes);
143 *changes = NULL;
145 return err;
148 void
149 got_diff_free_changes(struct got_diff_changes *changes)
151 struct got_diff_change *change;
152 while (!SIMPLEQ_EMPTY(&changes->entries)) {
153 change = SIMPLEQ_FIRST(&changes->entries);
154 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
155 free(change);
157 free(changes);
160 struct got_tree_entry *
161 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
163 struct got_tree_entry *te2;
164 const struct got_tree_entries *entries2;
166 entries2 = got_object_tree_get_entries(tree2);
167 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
168 if (strcmp(te1->name, te2->name) == 0)
169 return te2;
171 return NULL;
174 static const struct got_error *
175 diff_added_blob(struct got_object_id *id, const char *label,
176 int diff_context, struct got_repository *repo, FILE *outfile)
178 const struct got_error *err;
179 struct got_blob_object *blob = NULL;
180 struct got_object *obj = NULL;
182 err = got_object_open(&obj, repo, id);
183 if (err)
184 return err;
186 err = got_object_blob_open(&blob, repo, obj, 8192);
187 if (err)
188 goto done;
189 err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
190 done:
191 got_object_close(obj);
192 if (blob)
193 got_object_blob_close(blob);
194 return err;
197 static const struct got_error *
198 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
199 const char *label1, const char *label2, int diff_context,
200 struct got_repository *repo, FILE *outfile)
202 const struct got_error *err;
203 struct got_object *obj1 = NULL;
204 struct got_object *obj2 = NULL;
205 struct got_blob_object *blob1 = NULL;
206 struct got_blob_object *blob2 = NULL;
208 err = got_object_open(&obj1, repo, id1);
209 if (err)
210 return err;
211 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
212 err = got_error(GOT_ERR_OBJ_TYPE);
213 goto done;
216 err = got_object_open(&obj2, repo, id2);
217 if (err)
218 goto done;
219 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
220 err = got_error(GOT_ERR_BAD_OBJ_DATA);
221 goto done;
224 err = got_object_blob_open(&blob1, repo, obj1, 8192);
225 if (err)
226 goto done;
228 err = got_object_blob_open(&blob2, repo, obj2, 8192);
229 if (err)
230 goto done;
232 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
233 outfile);
235 done:
236 if (obj1)
237 got_object_close(obj1);
238 if (obj2)
239 got_object_close(obj2);
240 if (blob1)
241 got_object_blob_close(blob1);
242 if (blob2)
243 got_object_blob_close(blob2);
244 return err;
247 static const struct got_error *
248 diff_deleted_blob(struct got_object_id *id, const char *label,
249 int diff_context, struct got_repository *repo, FILE *outfile)
251 const struct got_error *err;
252 struct got_blob_object *blob = NULL;
253 struct got_object *obj = NULL;
255 err = got_object_open(&obj, repo, id);
256 if (err)
257 return err;
259 err = got_object_blob_open(&blob, repo, obj, 8192);
260 if (err)
261 goto done;
262 err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
263 done:
264 got_object_close(obj);
265 if (blob)
266 got_object_blob_close(blob);
267 return err;
270 static const struct got_error *
271 diff_added_tree(struct got_object_id *id, const char *label,
272 int diff_context, struct got_repository *repo, FILE *outfile)
274 const struct got_error *err = NULL;
275 struct got_object *treeobj = NULL;
276 struct got_tree_object *tree = NULL;
278 err = got_object_open(&treeobj, repo, id);
279 if (err)
280 goto done;
282 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
283 err = got_error(GOT_ERR_OBJ_TYPE);
284 goto done;
287 err = got_object_tree_open(&tree, repo, treeobj);
288 if (err)
289 goto done;
291 err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo,
292 outfile);
294 done:
295 if (tree)
296 got_object_tree_close(tree);
297 if (treeobj)
298 got_object_close(treeobj);
299 return err;
302 static const struct got_error *
303 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
304 const char *label1, const char *label2, int diff_context,
305 struct got_repository *repo, FILE *outfile)
307 const struct got_error *err;
308 struct got_object *treeobj1 = NULL;
309 struct got_object *treeobj2 = NULL;
310 struct got_tree_object *tree1 = NULL;
311 struct got_tree_object *tree2 = NULL;
313 err = got_object_open(&treeobj1, repo, id1);
314 if (err)
315 goto done;
317 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
319 goto done;
322 err = got_object_open(&treeobj2, repo, id2);
323 if (err)
324 goto done;
326 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
327 err = got_error(GOT_ERR_OBJ_TYPE);
328 goto done;
331 err = got_object_tree_open(&tree1, repo, treeobj1);
332 if (err)
333 goto done;
335 err = got_object_tree_open(&tree2, repo, treeobj2);
336 if (err)
337 goto done;
339 err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo,
340 outfile);
342 done:
343 if (tree1)
344 got_object_tree_close(tree1);
345 if (tree2)
346 got_object_tree_close(tree2);
347 if (treeobj1)
348 got_object_close(treeobj1);
349 if (treeobj2)
350 got_object_close(treeobj2);
351 return err;
354 static const struct got_error *
355 diff_deleted_tree(struct got_object_id *id, const char *label,
356 int diff_context, struct got_repository *repo, FILE *outfile)
358 const struct got_error *err;
359 struct got_object *treeobj = NULL;
360 struct got_tree_object *tree = NULL;
362 err = got_object_open(&treeobj, repo, id);
363 if (err)
364 goto done;
366 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
367 err = got_error(GOT_ERR_OBJ_TYPE);
368 goto done;
371 err = got_object_tree_open(&tree, repo, treeobj);
372 if (err)
373 goto done;
375 err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
376 outfile);
377 done:
378 if (tree)
379 got_object_tree_close(tree);
380 if (treeobj)
381 got_object_close(treeobj);
382 return err;
385 static const struct got_error *
386 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
387 const char *label1, const char *label2, FILE *outfile)
389 /* XXX TODO */
390 return NULL;
393 static const struct got_error *
394 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
395 const char *label1, const char *label2, int diff_context,
396 struct got_repository *repo, FILE *outfile)
398 const struct got_error *err = NULL;
399 int id_match;
401 if (te2 == NULL) {
402 if (S_ISDIR(te1->mode))
403 err = diff_deleted_tree(te1->id, label1, diff_context,
404 repo, outfile);
405 else
406 err = diff_deleted_blob(te1->id, label1, diff_context,
407 repo, outfile);
408 return err;
411 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
412 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
413 if (!id_match)
414 return diff_modified_tree(te1->id, te2->id,
415 label1, label2, diff_context, repo, outfile);
416 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
417 if (!id_match)
418 return diff_modified_blob(te1->id, te2->id,
419 label1, label2, diff_context, repo, outfile);
422 if (id_match)
423 return NULL;
425 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
428 static const struct got_error *
429 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
430 const char *label2, int diff_context, struct got_repository *repo,
431 FILE *outfile)
433 if (te1 != NULL) /* handled by diff_entry_old_new() */
434 return NULL;
436 if (S_ISDIR(te2->mode))
437 return diff_added_tree(te2->id, label2, diff_context, repo,
438 outfile);
440 return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
443 const struct got_error *
444 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
445 const char *label1, const char *label2, int diff_context,
446 struct got_repository *repo, FILE *outfile)
448 const struct got_error *err = NULL;
449 struct got_tree_entry *te1 = NULL;
450 struct got_tree_entry *te2 = NULL;
451 char *l1 = NULL, *l2 = NULL;
453 if (tree1) {
454 const struct got_tree_entries *entries;
455 entries = got_object_tree_get_entries(tree1);
456 te1 = SIMPLEQ_FIRST(&entries->head);
457 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
458 te1->name) == -1)
459 return got_error_from_errno();
461 if (tree2) {
462 const struct got_tree_entries *entries;
463 entries = got_object_tree_get_entries(tree2);
464 te2 = SIMPLEQ_FIRST(&entries->head);
465 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
466 te2->name) == -1)
467 return got_error_from_errno();
470 do {
471 if (te1) {
472 struct got_tree_entry *te = NULL;
473 if (tree2)
474 te = match_entry_by_name(te1, tree2);
475 if (te) {
476 free(l2);
477 l2 = NULL;
478 if (te && asprintf(&l2, "%s%s%s", label2,
479 label2[0] ? "/" : "", te->name) == -1)
480 return got_error_from_errno();
482 err = diff_entry_old_new(te1, te, l1, l2, diff_context,
483 repo, outfile);
484 if (err)
485 break;
488 if (te2) {
489 struct got_tree_entry *te = NULL;
490 if (tree1)
491 te = match_entry_by_name(te2, tree1);
492 free(l2);
493 if (te) {
494 if (asprintf(&l2, "%s%s%s", label2,
495 label2[0] ? "/" : "", te->name) == -1)
496 return got_error_from_errno();
497 } else {
498 if (asprintf(&l2, "%s%s%s", label2,
499 label2[0] ? "/" : "", te2->name) == -1)
500 return got_error_from_errno();
502 err = diff_entry_new_old(te2, te, l2, diff_context,
503 repo, outfile);
504 if (err)
505 break;
508 free(l1);
509 l1 = NULL;
510 if (te1) {
511 te1 = SIMPLEQ_NEXT(te1, entry);
512 if (te1 &&
513 asprintf(&l1, "%s%s%s", label1,
514 label1[0] ? "/" : "", te1->name) == -1)
515 return got_error_from_errno();
517 free(l2);
518 l2 = NULL;
519 if (te2) {
520 te2 = SIMPLEQ_NEXT(te2, entry);
521 if (te2 &&
522 asprintf(&l2, "%s%s%s", label2,
523 label2[0] ? "/" : "", te2->name) == -1)
524 return got_error_from_errno();
526 } while (te1 || te2);
528 return err;
531 const struct got_error *
532 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
533 const char *label1, const char *label2, int diff_context,
534 struct got_repository *repo, FILE *outfile)
536 const struct got_error *err;
537 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
539 if (id1 == NULL && id2 == NULL)
540 return got_error(GOT_ERR_NO_OBJ);
542 if (id1) {
543 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
544 if (err)
545 goto done;
547 if (id2) {
548 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
549 if (err)
550 goto done;
552 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
553 outfile);
554 done:
555 if (blob1)
556 got_object_blob_close(blob1);
557 if (blob2)
558 got_object_blob_close(blob2);
559 return err;
562 const struct got_error *
563 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
564 char *label1, char *label2, int diff_context, struct got_repository *repo,
565 FILE *outfile)
567 const struct got_error *err;
568 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
570 if (id1 == NULL && id2 == NULL)
571 return got_error(GOT_ERR_NO_OBJ);
573 if (id1) {
574 err = got_object_open_as_tree(&tree1, repo, id1);
575 if (err)
576 goto done;
578 if (id2) {
579 err = got_object_open_as_tree(&tree2, repo, id2);
580 if (err)
581 goto done;
583 err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
584 repo, outfile);
585 done:
586 if (tree1)
587 got_object_tree_close(tree1);
588 if (tree2)
589 got_object_tree_close(tree2);
590 return err;
593 const struct got_error *
594 got_diff_objects_as_commits(struct got_object_id *id1,
595 struct got_object_id *id2, int diff_context,
596 struct got_repository *repo, FILE *outfile)
598 const struct got_error *err;
599 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
601 if (id2 == NULL)
602 return got_error(GOT_ERR_NO_OBJ);
604 if (id1) {
605 err = got_object_open_as_commit(&commit1, repo, id1);
606 if (err)
607 goto done;
610 err = got_object_open_as_commit(&commit2, repo, id2);
611 if (err)
612 goto done;
614 err = got_diff_objects_as_trees(
615 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
616 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
617 outfile);
618 done:
619 if (commit1)
620 got_object_commit_close(commit1);
621 if (commit2)
622 got_object_commit_close(commit2);
623 return err;