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 if (label1 && strcmp(label1, idstr1) != 0)
105 fprintf(outfile, "blob - %s\n", idstr1);
106 if (label2 && strcmp(label2, idstr2) != 0)
107 fprintf(outfile, "blob + %s\n", idstr2);
109 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
110 done:
111 if (f1)
112 fclose(f1);
113 if (f2)
114 fclose(f2);
115 return err;
118 const struct got_error *
119 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
120 const char *label1, const char *label2, FILE *outfile)
122 return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
125 const struct got_error *
126 got_diff_blob_lines_changed(struct got_diff_changes **changes,
127 struct got_blob_object *blob1, struct got_blob_object *blob2)
129 const struct got_error *err = NULL;
131 *changes = calloc(1, sizeof(**changes));
132 if (*changes == NULL)
133 return got_error_from_errno();
134 SIMPLEQ_INIT(&(*changes)->entries);
136 err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
137 if (err) {
138 got_diff_free_changes(*changes);
139 *changes = NULL;
141 return err;
144 void
145 got_diff_free_changes(struct got_diff_changes *changes)
147 struct got_diff_change *change;
148 while (!SIMPLEQ_EMPTY(&changes->entries)) {
149 change = SIMPLEQ_FIRST(&changes->entries);
150 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
151 free(change);
153 free(changes);
156 struct got_tree_entry *
157 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
159 struct got_tree_entry *te2;
160 const struct got_tree_entries *entries2;
162 entries2 = got_object_tree_get_entries(tree2);
163 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
164 if (strcmp(te1->name, te2->name) == 0)
165 return te2;
167 return NULL;
170 static const struct got_error *
171 diff_added_blob(struct got_object_id *id, const char *label,
172 struct got_repository *repo, FILE *outfile)
174 const struct got_error *err;
175 struct got_blob_object *blob = NULL;
176 struct got_object *obj = NULL;
178 err = got_object_open(&obj, repo, id);
179 if (err)
180 return err;
182 err = got_object_blob_open(&blob, repo, obj, 8192);
183 if (err)
184 goto done;
185 err = got_diff_blob(NULL, blob, NULL, label, outfile);
186 done:
187 got_object_close(obj);
188 if (blob)
189 got_object_blob_close(blob);
190 return err;
193 static const struct got_error *
194 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
195 const char *label1, const char *label2, struct got_repository *repo,
196 FILE *outfile)
198 const struct got_error *err;
199 struct got_object *obj1 = NULL;
200 struct got_object *obj2 = NULL;
201 struct got_blob_object *blob1 = NULL;
202 struct got_blob_object *blob2 = NULL;
204 err = got_object_open(&obj1, repo, id1);
205 if (err)
206 return err;
207 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
208 err = got_error(GOT_ERR_OBJ_TYPE);
209 goto done;
212 err = got_object_open(&obj2, repo, id2);
213 if (err)
214 goto done;
215 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
216 err = got_error(GOT_ERR_BAD_OBJ_DATA);
217 goto done;
220 err = got_object_blob_open(&blob1, repo, obj1, 8192);
221 if (err)
222 goto done;
224 err = got_object_blob_open(&blob2, repo, obj2, 8192);
225 if (err)
226 goto done;
228 err = got_diff_blob(blob1, blob2, label1, label2, outfile);
230 done:
231 if (obj1)
232 got_object_close(obj1);
233 if (obj2)
234 got_object_close(obj2);
235 if (blob1)
236 got_object_blob_close(blob1);
237 if (blob2)
238 got_object_blob_close(blob2);
239 return err;
242 static const struct got_error *
243 diff_deleted_blob(struct got_object_id *id, const char *label,
244 struct got_repository *repo, FILE *outfile)
246 const struct got_error *err;
247 struct got_blob_object *blob = NULL;
248 struct got_object *obj = NULL;
250 err = got_object_open(&obj, repo, id);
251 if (err)
252 return err;
254 err = got_object_blob_open(&blob, repo, obj, 8192);
255 if (err)
256 goto done;
257 err = got_diff_blob(blob, NULL, label, NULL, outfile);
258 done:
259 got_object_close(obj);
260 if (blob)
261 got_object_blob_close(blob);
262 return err;
265 static const struct got_error *
266 diff_added_tree(struct got_object_id *id, const char *label,
267 struct got_repository *repo, FILE *outfile)
269 const struct got_error *err = NULL;
270 struct got_object *treeobj = NULL;
271 struct got_tree_object *tree = NULL;
273 err = got_object_open(&treeobj, repo, id);
274 if (err)
275 goto done;
277 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
278 err = got_error(GOT_ERR_OBJ_TYPE);
279 goto done;
282 err = got_object_tree_open(&tree, repo, treeobj);
283 if (err)
284 goto done;
286 err = got_diff_tree(NULL, tree, NULL, label, repo, outfile);
288 done:
289 if (tree)
290 got_object_tree_close(tree);
291 if (treeobj)
292 got_object_close(treeobj);
293 return err;
296 static const struct got_error *
297 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
298 const char *label1, const char *label2, struct got_repository *repo,
299 FILE *outfile)
301 const struct got_error *err;
302 struct got_object *treeobj1 = NULL;
303 struct got_object *treeobj2 = NULL;
304 struct got_tree_object *tree1 = NULL;
305 struct got_tree_object *tree2 = NULL;
307 err = got_object_open(&treeobj1, repo, id1);
308 if (err)
309 goto done;
311 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
312 err = got_error(GOT_ERR_OBJ_TYPE);
313 goto done;
316 err = got_object_open(&treeobj2, repo, id2);
317 if (err)
318 goto done;
320 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
321 err = got_error(GOT_ERR_OBJ_TYPE);
322 goto done;
325 err = got_object_tree_open(&tree1, repo, treeobj1);
326 if (err)
327 goto done;
329 err = got_object_tree_open(&tree2, repo, treeobj2);
330 if (err)
331 goto done;
333 err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
335 done:
336 if (tree1)
337 got_object_tree_close(tree1);
338 if (tree2)
339 got_object_tree_close(tree2);
340 if (treeobj1)
341 got_object_close(treeobj1);
342 if (treeobj2)
343 got_object_close(treeobj2);
344 return err;
347 static const struct got_error *
348 diff_deleted_tree(struct got_object_id *id, const char *label,
349 struct got_repository *repo, FILE *outfile)
351 const struct got_error *err;
352 struct got_object *treeobj = NULL;
353 struct got_tree_object *tree = NULL;
355 err = got_object_open(&treeobj, repo, id);
356 if (err)
357 goto done;
359 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
360 err = got_error(GOT_ERR_OBJ_TYPE);
361 goto done;
364 err = got_object_tree_open(&tree, repo, treeobj);
365 if (err)
366 goto done;
368 err = got_diff_tree(tree, NULL, label, NULL, repo, outfile);
369 done:
370 if (tree)
371 got_object_tree_close(tree);
372 if (treeobj)
373 got_object_close(treeobj);
374 return err;
377 static const struct got_error *
378 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
379 const char *label1, const char *label2, FILE *outfile)
381 /* XXX TODO */
382 return NULL;
385 static const struct got_error *
386 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
387 const char *label1, const char *label2, struct got_repository *repo,
388 FILE *outfile)
390 const struct got_error *err = NULL;
392 if (te2 == NULL) {
393 if (S_ISDIR(te1->mode))
394 err = diff_deleted_tree(te1->id, label1, repo, outfile);
395 else
396 err = diff_deleted_blob(te1->id, label1, repo, outfile);
397 return err;
400 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
401 if (got_object_id_cmp(te1->id, te2->id) != 0)
402 return diff_modified_tree(te1->id, te2->id,
403 label1, label2, repo, outfile);
404 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
405 if (got_object_id_cmp(te1->id, te2->id) != 0)
406 return diff_modified_blob(te1->id, te2->id,
407 label1, label2, repo, outfile);
410 if (got_object_id_cmp(te1->id, te2->id) == 0)
411 return NULL;
413 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
416 static const struct got_error *
417 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
418 const char *label2, struct got_repository *repo, FILE *outfile)
420 if (te1 != NULL) /* handled by diff_entry_old_new() */
421 return NULL;
423 if (S_ISDIR(te2->mode))
424 return diff_added_tree(te2->id, label2, repo, outfile);
426 return diff_added_blob(te2->id, label2, repo, outfile);
429 const struct got_error *
430 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
431 const char *label1, const char *label2, struct got_repository *repo,
432 FILE *outfile)
434 const struct got_error *err = NULL;
435 struct got_tree_entry *te1 = NULL;
436 struct got_tree_entry *te2 = NULL;
437 char *l1 = NULL, *l2 = NULL;
439 if (tree1) {
440 const struct got_tree_entries *entries;
441 entries = got_object_tree_get_entries(tree1);
442 te1 = SIMPLEQ_FIRST(&entries->head);
443 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
444 te1->name) == -1)
445 return got_error_from_errno();
447 if (tree2) {
448 const struct got_tree_entries *entries;
449 entries = got_object_tree_get_entries(tree2);
450 te2 = SIMPLEQ_FIRST(&entries->head);
451 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
452 te2->name) == -1)
453 return got_error_from_errno();
456 do {
457 if (te1) {
458 struct got_tree_entry *te = NULL;
459 if (tree2)
460 te = match_entry_by_name(te1, tree2);
461 if (te) {
462 free(l2);
463 l2 = NULL;
464 if (te && asprintf(&l2, "%s%s%s", label2,
465 label2[0] ? "/" : "", te->name) == -1)
466 return got_error_from_errno();
468 err = diff_entry_old_new(te1, te, l1, l2, repo,
469 outfile);
470 if (err)
471 break;
474 if (te2) {
475 struct got_tree_entry *te = NULL;
476 if (tree1)
477 te = match_entry_by_name(te2, tree1);
478 err = diff_entry_new_old(te2, te, l2, repo, outfile);
479 if (err)
480 break;
483 free(l1);
484 l1 = NULL;
485 if (te1) {
486 te1 = SIMPLEQ_NEXT(te1, entry);
487 if (te1 &&
488 asprintf(&l1, "%s%s%s", label1,
489 label1[0] ? "/" : "", te1->name) == -1)
490 return got_error_from_errno();
492 free(l2);
493 l2 = NULL;
494 if (te2) {
495 te2 = SIMPLEQ_NEXT(te2, entry);
496 if (te2 &&
497 asprintf(&l2, "%s%s%s", label2,
498 label2[0] ? "/" : "", te2->name) == -1)
499 return got_error_from_errno();
501 } while (te1 || te2);
503 return err;
506 const struct got_error *
507 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
508 const char *label1, const char *label2, struct got_repository *repo,
509 FILE *outfile)
511 const struct got_error *err;
512 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
514 if (obj1 == NULL && obj2 == NULL)
515 return got_error(GOT_ERR_NO_OBJ);
517 if (obj1) {
518 err = got_object_blob_open(&blob1, repo, obj1, 8192);
519 if (err)
520 goto done;
522 if (obj2) {
523 err = got_object_blob_open(&blob2, repo, obj2, 8192);
524 if (err)
525 goto done;
527 err = got_diff_blob(blob1, blob2, label1, label2, outfile);
528 done:
529 if (blob1)
530 got_object_blob_close(blob1);
531 if (blob2)
532 got_object_blob_close(blob2);
533 return err;
536 const struct got_error *
537 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
538 char *label1, char *label2, struct got_repository *repo, FILE *outfile)
540 const struct got_error *err;
541 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
543 if (obj1 == NULL && obj2 == NULL)
544 return got_error(GOT_ERR_NO_OBJ);
546 if (obj1) {
547 err = got_object_tree_open(&tree1, repo, obj1);
548 if (err)
549 goto done;
551 if (obj2) {
552 err = got_object_tree_open(&tree2, repo, obj2);
553 if (err)
554 goto done;
556 err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
557 done:
558 if (tree1)
559 got_object_tree_close(tree1);
560 if (tree2)
561 got_object_tree_close(tree2);
562 return err;
565 static char *
566 get_datestr(time_t *time, char *datebuf)
568 char *p, *s = ctime_r(time, datebuf);
569 p = strchr(s, '\n');
570 if (p)
571 *p = '\0';
572 return s;
575 const struct got_error *
576 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
577 struct got_repository *repo, FILE *outfile)
579 const struct got_error *err;
580 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
581 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
582 char *id_str;
583 char datebuf[26];
584 time_t time;
586 if (obj2 == NULL)
587 return got_error(GOT_ERR_NO_OBJ);
589 if (obj1) {
590 err = got_object_commit_open(&commit1, repo, obj1);
591 if (err)
592 goto done;
593 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
594 if (err)
595 goto done;
598 err = got_object_commit_open(&commit2, repo, obj2);
599 if (err)
600 goto done;
601 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
602 if (err)
603 goto done;
604 err = got_object_get_id_str(&id_str, obj2);
605 if (err)
606 goto done;
607 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
608 err = got_error_from_errno();
609 free(id_str);
610 goto done;
612 free(id_str);
613 if (fprintf(outfile, "from: %s\n", commit2->author) < 0) {
614 err = got_error_from_errno();
615 goto done;
617 time = mktime(&commit2->tm_committer);
618 if (fprintf(outfile, "date: %s UTC\n",
619 get_datestr(&time, datebuf)) < 0) {
620 err = got_error_from_errno();
621 goto done;
623 if (strcmp(commit2->author, commit2->committer) != 0 &&
624 fprintf(outfile, "via: %s\n", commit2->committer) < 0) {
625 err = got_error_from_errno();
626 goto done;
628 if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
629 err = got_error_from_errno();
630 goto done;
633 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo,
634 outfile);
635 done:
636 if (tree_obj1)
637 got_object_close(tree_obj1);
638 if (tree_obj2)
639 got_object_close(tree_obj2);
640 if (commit1)
641 got_object_commit_close(commit1);
642 if (commit2)
643 got_object_commit_close(commit2);
644 return err;