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"
36 static const struct got_error *
37 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
38 const char *label1, const char *label2, FILE *outfile,
39 struct got_diff_changes *changes)
40 {
41 struct got_diff_state ds;
42 struct got_diff_args args;
43 const struct got_error *err = NULL;
44 FILE *f1 = NULL, *f2 = NULL;
45 char hex1[SHA1_DIGEST_STRING_LENGTH];
46 char hex2[SHA1_DIGEST_STRING_LENGTH];
47 char *idstr1 = NULL, *idstr2 = NULL;
48 size_t size1, size2;
49 int res, flags = 0;
51 if (blob1) {
52 f1 = got_opentemp();
53 if (f1 == NULL)
54 return got_error(GOT_ERR_FILE_OPEN);
55 } else
56 flags |= D_EMPTY1;
58 if (blob2) {
59 f2 = got_opentemp();
60 if (f2 == NULL) {
61 fclose(f1);
62 return got_error(GOT_ERR_FILE_OPEN);
63 }
64 } else
65 flags |= D_EMPTY2;
67 size1 = 0;
68 if (blob1) {
69 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
70 err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
71 if (err)
72 goto done;
73 } else
74 idstr1 = "/dev/null";
76 size2 = 0;
77 if (blob2) {
78 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
79 err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
80 if (err)
81 goto done;
82 } else
83 idstr2 = "/dev/null";
85 memset(&ds, 0, sizeof(ds));
86 /* XXX should stat buffers be passed in args instead of ds? */
87 ds.stb1.st_mode = S_IFREG;
88 if (blob1)
89 ds.stb1.st_size = size1;
90 ds.stb1.st_mtime = 0; /* XXX */
92 ds.stb2.st_mode = S_IFREG;
93 if (blob2)
94 ds.stb2.st_size = size2;
95 ds.stb2.st_mtime = 0; /* XXX */
97 memset(&args, 0, sizeof(args));
98 args.diff_format = D_UNIFIED;
99 args.label[0] = label1 ? label1 : idstr1;
100 args.label[1] = label2 ? label2 : idstr2;
101 args.diff_context = 3;
102 flags |= D_PROTOTYPE;
104 fprintf(outfile, "blob %s ---\n", idstr1);
105 fprintf(outfile, "blob %s +++\n", idstr2);
107 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
108 done:
109 if (f1)
110 fclose(f1);
111 if (f2)
112 fclose(f2);
113 return err;
116 const struct got_error *
117 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
118 const char *label1, const char *label2, FILE *outfile)
120 return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
123 const struct got_error *
124 got_diff_blob_lines_changed(struct got_diff_changes **changes,
125 struct got_blob_object *blob1, struct got_blob_object *blob2)
127 const struct got_error *err = NULL;
129 *changes = calloc(1, sizeof(**changes));
130 if (*changes == NULL)
131 return got_error_from_errno();
132 SIMPLEQ_INIT(&(*changes)->entries);
134 err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
135 if (err) {
136 got_diff_free_changes(*changes);
137 *changes = NULL;
139 return err;
142 void
143 got_diff_free_changes(struct got_diff_changes *changes)
145 struct got_diff_change *change;
146 while (!SIMPLEQ_EMPTY(&changes->entries)) {
147 change = SIMPLEQ_FIRST(&changes->entries);
148 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
149 free(change);
151 free(changes);
154 struct got_tree_entry *
155 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
157 struct got_tree_entry *te2;
158 const struct got_tree_entries *entries2;
160 entries2 = got_object_tree_get_entries(tree2);
161 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
162 if (strcmp(te1->name, te2->name) == 0)
163 return te2;
165 return NULL;
168 static const struct got_error *
169 diff_added_blob(struct got_object_id *id, const char *label,
170 struct got_repository *repo, FILE *outfile)
172 const struct got_error *err;
173 struct got_blob_object *blob = NULL;
174 struct got_object *obj = NULL;
176 err = got_object_open(&obj, repo, id);
177 if (err)
178 return err;
180 err = got_object_blob_open(&blob, repo, obj, 8192);
181 if (err)
182 goto done;
183 err = got_diff_blob(NULL, blob, NULL, label, outfile);
184 done:
185 got_object_close(obj);
186 if (blob)
187 got_object_blob_close(blob);
188 return err;
191 static const struct got_error *
192 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
193 const char *label1, const char *label2, struct got_repository *repo,
194 FILE *outfile)
196 const struct got_error *err;
197 struct got_object *obj1 = NULL;
198 struct got_object *obj2 = NULL;
199 struct got_blob_object *blob1 = NULL;
200 struct got_blob_object *blob2 = NULL;
202 err = got_object_open(&obj1, repo, id1);
203 if (err)
204 return err;
205 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
206 err = got_error(GOT_ERR_OBJ_TYPE);
207 goto done;
210 err = got_object_open(&obj2, repo, id2);
211 if (err)
212 goto done;
213 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
214 err = got_error(GOT_ERR_BAD_OBJ_DATA);
215 goto done;
218 err = got_object_blob_open(&blob1, repo, obj1, 8192);
219 if (err)
220 goto done;
222 err = got_object_blob_open(&blob2, repo, obj2, 8192);
223 if (err)
224 goto done;
226 err = got_diff_blob(blob1, blob2, label1, label2, outfile);
228 done:
229 if (obj1)
230 got_object_close(obj1);
231 if (obj2)
232 got_object_close(obj2);
233 if (blob1)
234 got_object_blob_close(blob1);
235 if (blob2)
236 got_object_blob_close(blob2);
237 return err;
240 static const struct got_error *
241 diff_deleted_blob(struct got_object_id *id, const char *label,
242 struct got_repository *repo, FILE *outfile)
244 const struct got_error *err;
245 struct got_blob_object *blob = NULL;
246 struct got_object *obj = NULL;
248 err = got_object_open(&obj, repo, id);
249 if (err)
250 return err;
252 err = got_object_blob_open(&blob, repo, obj, 8192);
253 if (err)
254 goto done;
255 err = got_diff_blob(blob, NULL, label, NULL, outfile);
256 done:
257 got_object_close(obj);
258 if (blob)
259 got_object_blob_close(blob);
260 return err;
263 static const struct got_error *
264 diff_added_tree(struct got_object_id *id, const char *label,
265 struct got_repository *repo, FILE *outfile)
267 const struct got_error *err = NULL;
268 struct got_object *treeobj = NULL;
269 struct got_tree_object *tree = NULL;
271 err = got_object_open(&treeobj, repo, id);
272 if (err)
273 goto done;
275 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
276 err = got_error(GOT_ERR_OBJ_TYPE);
277 goto done;
280 err = got_object_tree_open(&tree, repo, treeobj);
281 if (err)
282 goto done;
284 err = got_diff_tree(NULL, tree, NULL, label, repo, outfile);
286 done:
287 if (tree)
288 got_object_tree_close(tree);
289 if (treeobj)
290 got_object_close(treeobj);
291 return err;
294 static const struct got_error *
295 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
296 const char *label1, const char *label2, struct got_repository *repo,
297 FILE *outfile)
299 const struct got_error *err;
300 struct got_object *treeobj1 = NULL;
301 struct got_object *treeobj2 = NULL;
302 struct got_tree_object *tree1 = NULL;
303 struct got_tree_object *tree2 = NULL;
305 err = got_object_open(&treeobj1, repo, id1);
306 if (err)
307 goto done;
309 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
310 err = got_error(GOT_ERR_OBJ_TYPE);
311 goto done;
314 err = got_object_open(&treeobj2, repo, id2);
315 if (err)
316 goto done;
318 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
319 err = got_error(GOT_ERR_OBJ_TYPE);
320 goto done;
323 err = got_object_tree_open(&tree1, repo, treeobj1);
324 if (err)
325 goto done;
327 err = got_object_tree_open(&tree2, repo, treeobj2);
328 if (err)
329 goto done;
331 err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
333 done:
334 if (tree1)
335 got_object_tree_close(tree1);
336 if (tree2)
337 got_object_tree_close(tree2);
338 if (treeobj1)
339 got_object_close(treeobj1);
340 if (treeobj2)
341 got_object_close(treeobj2);
342 return err;
345 static const struct got_error *
346 diff_deleted_tree(struct got_object_id *id, const char *label,
347 struct got_repository *repo, FILE *outfile)
349 const struct got_error *err;
350 struct got_object *treeobj = NULL;
351 struct got_tree_object *tree = NULL;
353 err = got_object_open(&treeobj, repo, id);
354 if (err)
355 goto done;
357 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
358 err = got_error(GOT_ERR_OBJ_TYPE);
359 goto done;
362 err = got_object_tree_open(&tree, repo, treeobj);
363 if (err)
364 goto done;
366 err = got_diff_tree(tree, NULL, label, NULL, repo, outfile);
367 done:
368 if (tree)
369 got_object_tree_close(tree);
370 if (treeobj)
371 got_object_close(treeobj);
372 return err;
375 static const struct got_error *
376 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
377 const char *label1, const char *label2, FILE *outfile)
379 /* XXX TODO */
380 return NULL;
383 static const struct got_error *
384 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
385 const char *label1, const char *label2, struct got_repository *repo,
386 FILE *outfile)
388 const struct got_error *err = NULL;
390 if (te2 == NULL) {
391 if (S_ISDIR(te1->mode))
392 err = diff_deleted_tree(te1->id, label1, repo, outfile);
393 else
394 err = diff_deleted_blob(te1->id, label1, repo, outfile);
395 return err;
398 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
399 if (got_object_id_cmp(te1->id, te2->id) != 0)
400 return diff_modified_tree(te1->id, te2->id,
401 label1, label2, repo, outfile);
402 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
403 if (got_object_id_cmp(te1->id, te2->id) != 0)
404 return diff_modified_blob(te1->id, te2->id,
405 label1, label2, repo, outfile);
408 if (got_object_id_cmp(te1->id, te2->id) == 0)
409 return NULL;
411 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
414 static const struct got_error *
415 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
416 const char *label2, struct got_repository *repo, FILE *outfile)
418 if (te1 != NULL) /* handled by diff_entry_old_new() */
419 return NULL;
421 if (S_ISDIR(te2->mode))
422 return diff_added_tree(te2->id, label2, repo, outfile);
424 return diff_added_blob(te2->id, label2, repo, outfile);
427 const struct got_error *
428 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
429 const char *label1, const char *label2, struct got_repository *repo,
430 FILE *outfile)
432 const struct got_error *err = NULL;
433 struct got_tree_entry *te1 = NULL;
434 struct got_tree_entry *te2 = NULL;
435 char *l1 = NULL, *l2 = NULL;
437 if (tree1) {
438 const struct got_tree_entries *entries;
439 entries = got_object_tree_get_entries(tree1);
440 te1 = SIMPLEQ_FIRST(&entries->head);
441 if (asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
442 te1->name) == -1)
443 return got_error_from_errno();
445 if (tree2) {
446 const struct got_tree_entries *entries;
447 entries = got_object_tree_get_entries(tree2);
448 te2 = SIMPLEQ_FIRST(&entries->head);
449 if (asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
450 te2->name) == -1)
451 return got_error_from_errno();
454 do {
455 if (te1) {
456 struct got_tree_entry *te = NULL;
457 if (tree2)
458 te = match_entry_by_name(te1, tree2);
459 if (te) {
460 free(l2);
461 l2 = NULL;
462 if (te && asprintf(&l2, "%s%s%s", label2,
463 label2[0] ? "/" : "", te->name) == -1)
464 return got_error_from_errno();
466 err = diff_entry_old_new(te1, te, l1, l2, repo,
467 outfile);
468 if (err)
469 break;
472 if (te2) {
473 struct got_tree_entry *te = NULL;
474 if (tree1)
475 te = match_entry_by_name(te2, tree1);
476 err = diff_entry_new_old(te2, te, l2, repo, outfile);
477 if (err)
478 break;
481 free(l1);
482 l1 = NULL;
483 if (te1) {
484 te1 = SIMPLEQ_NEXT(te1, entry);
485 if (te1 &&
486 asprintf(&l1, "%s%s%s", label1,
487 label1[0] ? "/" : "", te1->name) == -1)
488 return got_error_from_errno();
490 free(l2);
491 l2 = NULL;
492 if (te2) {
493 te2 = SIMPLEQ_NEXT(te2, entry);
494 if (te2 &&
495 asprintf(&l2, "%s%s%s", label2,
496 label2[0] ? "/" : "", te2->name) == -1)
497 return got_error_from_errno();
499 } while (te1 || te2);
501 return err;
504 const struct got_error *
505 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
506 const char *label1, const char *label2, struct got_repository *repo,
507 FILE *outfile)
509 const struct got_error *err;
510 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
512 if (obj1 == NULL && obj2 == NULL)
513 return got_error(GOT_ERR_NO_OBJ);
515 if (obj1) {
516 err = got_object_blob_open(&blob1, repo, obj1, 8192);
517 if (err)
518 goto done;
520 if (obj2) {
521 err = got_object_blob_open(&blob2, repo, obj2, 8192);
522 if (err)
523 goto done;
525 err = got_diff_blob(blob1, blob2, label1, label2, outfile);
526 done:
527 if (blob1)
528 got_object_blob_close(blob1);
529 if (blob2)
530 got_object_blob_close(blob2);
531 return err;
534 const struct got_error *
535 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
536 char *label1, char *label2, struct got_repository *repo, FILE *outfile)
538 const struct got_error *err;
539 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
541 if (obj1 == NULL && obj2 == NULL)
542 return got_error(GOT_ERR_NO_OBJ);
544 if (obj1) {
545 err = got_object_tree_open(&tree1, repo, obj1);
546 if (err)
547 goto done;
549 if (obj2) {
550 err = got_object_tree_open(&tree2, repo, obj2);
551 if (err)
552 goto done;
554 err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
555 done:
556 if (tree1)
557 got_object_tree_close(tree1);
558 if (tree2)
559 got_object_tree_close(tree2);
560 return err;
563 static char *
564 get_datestr(time_t *time, char *datebuf)
566 char *p, *s = ctime_r(time, datebuf);
567 p = strchr(s, '\n');
568 if (p)
569 *p = '\0';
570 return s;
573 const struct got_error *
574 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
575 struct got_repository *repo, FILE *outfile)
577 const struct got_error *err;
578 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
579 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
580 char *id_str;
581 char datebuf[26];
582 time_t time;
584 if (obj2 == NULL)
585 return got_error(GOT_ERR_NO_OBJ);
587 if (obj1) {
588 err = got_object_commit_open(&commit1, repo, obj1);
589 if (err)
590 goto done;
591 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
592 if (err)
593 goto done;
596 err = got_object_commit_open(&commit2, repo, obj2);
597 if (err)
598 goto done;
599 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
600 if (err)
601 goto done;
602 err = got_object_get_id_str(&id_str, obj2);
603 if (err)
604 goto done;
605 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
606 err = got_error_from_errno();
607 free(id_str);
608 goto done;
610 free(id_str);
611 time = mktime(&commit2->tm_author);
612 if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
613 get_datestr(&time, datebuf)) < 0) {
614 err = got_error_from_errno();
615 goto done;
617 time = mktime(&commit2->tm_committer);
618 if (strcmp(commit2->author, commit2->committer) != 0 &&
619 fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
620 get_datestr(&time, datebuf)) < 0) {
621 err = got_error_from_errno();
622 goto done;
624 if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
625 err = got_error_from_errno();
626 goto done;
629 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo,
630 outfile);
631 done:
632 if (tree_obj1)
633 got_object_close(tree_obj1);
634 if (tree_obj2)
635 got_object_close(tree_obj2);
636 if (commit1)
637 got_object_commit_close(commit1);
638 if (commit2)
639 got_object_commit_close(commit2);
640 return err;