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 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
105 done:
106 if (f1)
107 fclose(f1);
108 if (f2)
109 fclose(f2);
110 return err;
113 const struct got_error *
114 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
115 const char *label1, const char *label2, FILE *outfile)
117 return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
120 const struct got_error *
121 got_diff_blob_lines_changed(struct got_diff_changes **changes,
122 struct got_blob_object *blob1, struct got_blob_object *blob2)
124 const struct got_error *err = NULL;
126 *changes = calloc(1, sizeof(**changes));
127 if (*changes == NULL)
128 return got_error_from_errno();
129 SIMPLEQ_INIT(&(*changes)->entries);
131 err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
132 if (err) {
133 got_diff_free_changes(*changes);
134 *changes = NULL;
136 return err;
139 void
140 got_diff_free_changes(struct got_diff_changes *changes)
142 struct got_diff_change *change;
143 while (!SIMPLEQ_EMPTY(&changes->entries)) {
144 change = SIMPLEQ_FIRST(&changes->entries);
145 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
146 free(change);
148 free(changes);
151 struct got_tree_entry *
152 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
154 struct got_tree_entry *te2;
155 const struct got_tree_entries *entries2;
157 entries2 = got_object_tree_get_entries(tree2);
158 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
159 if (strcmp(te1->name, te2->name) == 0)
160 return te2;
162 return NULL;
165 static const struct got_error *
166 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
167 FILE *outfile)
169 const struct got_error *err;
170 struct got_blob_object *blob = NULL;
171 struct got_object *obj = NULL;
173 err = got_object_open(&obj, repo, id);
174 if (err)
175 return err;
177 err = got_object_blob_open(&blob, repo, obj, 8192);
178 if (err)
179 goto done;
180 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
181 done:
182 got_object_close(obj);
183 if (blob)
184 got_object_blob_close(blob);
185 return err;
188 static const struct got_error *
189 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
190 struct got_repository *repo, FILE *outfile)
192 const struct got_error *err;
193 struct got_object *obj1 = NULL;
194 struct got_object *obj2 = NULL;
195 struct got_blob_object *blob1 = NULL;
196 struct got_blob_object *blob2 = NULL;
198 err = got_object_open(&obj1, repo, id1);
199 if (err)
200 return err;
201 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
202 err = got_error(GOT_ERR_OBJ_TYPE);
203 goto done;
206 err = got_object_open(&obj2, repo, id2);
207 if (err)
208 goto done;
209 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
210 err = got_error(GOT_ERR_BAD_OBJ_DATA);
211 goto done;
214 err = got_object_blob_open(&blob1, repo, obj1, 8192);
215 if (err)
216 goto done;
218 err = got_object_blob_open(&blob2, repo, obj2, 8192);
219 if (err)
220 goto done;
222 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
224 done:
225 if (obj1)
226 got_object_close(obj1);
227 if (obj2)
228 got_object_close(obj2);
229 if (blob1)
230 got_object_blob_close(blob1);
231 if (blob2)
232 got_object_blob_close(blob2);
233 return err;
236 static const struct got_error *
237 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
238 FILE *outfile)
240 const struct got_error *err;
241 struct got_blob_object *blob = NULL;
242 struct got_object *obj = NULL;
244 err = got_object_open(&obj, repo, id);
245 if (err)
246 return err;
248 err = got_object_blob_open(&blob, repo, obj, 8192);
249 if (err)
250 goto done;
251 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
252 done:
253 got_object_close(obj);
254 if (blob)
255 got_object_blob_close(blob);
256 return err;
259 static const struct got_error *
260 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
261 FILE *outfile)
263 const struct got_error *err = NULL;
264 struct got_object *treeobj = NULL;
265 struct got_tree_object *tree = NULL;
267 err = got_object_open(&treeobj, repo, id);
268 if (err)
269 goto done;
271 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
272 err = got_error(GOT_ERR_OBJ_TYPE);
273 goto done;
276 err = got_object_tree_open(&tree, repo, treeobj);
277 if (err)
278 goto done;
280 err = got_diff_tree(NULL, tree, repo, outfile);
282 done:
283 if (tree)
284 got_object_tree_close(tree);
285 if (treeobj)
286 got_object_close(treeobj);
287 return err;
290 static const struct got_error *
291 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
292 struct got_repository *repo, FILE *outfile)
294 const struct got_error *err = NULL;
295 struct got_object *treeobj1 = NULL;
296 struct got_object *treeobj2 = NULL;
297 struct got_tree_object *tree1 = NULL;
298 struct got_tree_object *tree2 = NULL;
300 err = got_object_open(&treeobj1, repo, id1);
301 if (err)
302 goto done;
304 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
305 err = got_error(GOT_ERR_OBJ_TYPE);
306 goto done;
309 err = got_object_open(&treeobj2, repo, id2);
310 if (err)
311 goto done;
313 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
314 err = got_error(GOT_ERR_OBJ_TYPE);
315 goto done;
318 err = got_object_tree_open(&tree1, repo, treeobj1);
319 if (err)
320 goto done;
322 err = got_object_tree_open(&tree2, repo, treeobj2);
323 if (err)
324 goto done;
326 err = got_diff_tree(tree1, tree2, repo, outfile);
328 done:
329 if (tree1)
330 got_object_tree_close(tree1);
331 if (tree2)
332 got_object_tree_close(tree2);
333 if (treeobj1)
334 got_object_close(treeobj1);
335 if (treeobj2)
336 got_object_close(treeobj2);
337 return err;
340 static const struct got_error *
341 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
343 const struct got_error *err = NULL;
344 struct got_object *treeobj = NULL;
345 struct got_tree_object *tree = NULL;
347 err = got_object_open(&treeobj, repo, id);
348 if (err)
349 goto done;
351 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
353 goto done;
356 err = got_object_tree_open(&tree, repo, treeobj);
357 if (err)
358 goto done;
360 err = got_diff_tree(tree, NULL, repo, outfile);
362 done:
363 if (tree)
364 got_object_tree_close(tree);
365 if (treeobj)
366 got_object_close(treeobj);
367 return err;
370 static const struct got_error *
371 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
372 FILE *outfile)
374 /* XXX TODO */
375 return NULL;
378 static const struct got_error *
379 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
380 struct got_repository *repo, FILE *outfile)
382 struct got_tree_entry *te2 = NULL;
384 if (tree2)
385 te2 = match_entry_by_name(te1, tree2);
386 if (te2 == NULL) {
387 if (S_ISDIR(te1->mode))
388 return diff_deleted_tree(te1->id, repo, outfile);
389 return diff_deleted_blob(te1->id, repo, outfile);
392 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
393 if (got_object_id_cmp(te1->id, te2->id) != 0)
394 return diff_modified_tree(te1->id, te2->id, repo,
395 outfile);
396 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
397 if (got_object_id_cmp(te1->id, te2->id) != 0)
398 return diff_modified_blob(te1->id, te2->id, repo,
399 outfile);
402 return diff_kind_mismatch(te1->id, te2->id, outfile);
405 static const struct got_error *
406 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
407 struct got_repository *repo, FILE *outfile)
409 if (tree1) {
410 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
411 if (te1 != NULL) /* handled by diff_entry_old_new() */
412 return NULL;
415 if (S_ISDIR(te2->mode))
416 return diff_added_tree(te2->id, repo, outfile);
417 return diff_added_blob(te2->id, repo, outfile);
420 const struct got_error *
421 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
422 struct got_repository *repo, FILE *outfile)
424 const struct got_error *err = NULL;
425 struct got_tree_entry *te1 = NULL;
426 struct got_tree_entry *te2 = NULL;
428 if (tree1) {
429 const struct got_tree_entries *entries;
430 entries = got_object_tree_get_entries(tree1);
431 te1 = SIMPLEQ_FIRST(&entries->head);
433 if (tree2) {
434 const struct got_tree_entries *entries;
435 entries = got_object_tree_get_entries(tree2);
436 te2 = SIMPLEQ_FIRST(&entries->head);
439 do {
440 if (te1) {
441 err = diff_entry_old_new(te1, tree2, repo, outfile);
442 if (err)
443 break;
446 if (te2) {
447 err = diff_entry_new_old(te2, tree1, repo, outfile);
448 if (err)
449 break;
452 if (te1)
453 te1 = SIMPLEQ_NEXT(te1, entry);
454 if (te2)
455 te2 = SIMPLEQ_NEXT(te2, entry);
456 } while (te1 || te2);
458 return err;
461 const struct got_error *
462 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
463 struct got_repository *repo, FILE *outfile)
465 const struct got_error *err;
466 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
468 if (obj1 == NULL && obj2 == NULL)
469 return got_error(GOT_ERR_NO_OBJ);
471 if (obj1) {
472 err = got_object_blob_open(&blob1, repo, obj1, 8192);
473 if (err)
474 goto done;
476 if (obj2) {
477 err = got_object_blob_open(&blob2, repo, obj2, 8192);
478 if (err)
479 goto done;
481 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
482 done:
483 if (blob1)
484 got_object_blob_close(blob1);
485 if (blob2)
486 got_object_blob_close(blob2);
487 return err;
490 const struct got_error *
491 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
492 struct got_repository *repo, FILE *outfile)
494 const struct got_error *err;
495 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
497 if (obj1 == NULL && obj2 == NULL)
498 return got_error(GOT_ERR_NO_OBJ);
500 if (obj1) {
501 err = got_object_tree_open(&tree1, repo, obj1);
502 if (err)
503 goto done;
505 if (obj2) {
506 err = got_object_tree_open(&tree2, repo, obj2);
507 if (err)
508 goto done;
510 err = got_diff_tree(tree1, tree2, repo, outfile);
511 done:
512 if (tree1)
513 got_object_tree_close(tree1);
514 if (tree2)
515 got_object_tree_close(tree2);
516 return err;
519 static char *
520 get_datestr(time_t *time, char *datebuf)
522 char *p, *s = ctime_r(time, datebuf);
523 p = strchr(s, '\n');
524 if (p)
525 *p = '\0';
526 return s;
529 const struct got_error *
530 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
531 struct got_repository *repo, FILE *outfile)
533 const struct got_error *err;
534 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
535 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
536 char *id_str;
537 char datebuf[26];
538 time_t time;
540 if (obj2 == NULL)
541 return got_error(GOT_ERR_NO_OBJ);
543 if (obj1) {
544 err = got_object_commit_open(&commit1, repo, obj1);
545 if (err)
546 goto done;
547 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
548 if (err)
549 goto done;
552 err = got_object_commit_open(&commit2, repo, obj2);
553 if (err)
554 goto done;
555 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
556 if (err)
557 goto done;
558 err = got_object_get_id_str(&id_str, obj2);
559 if (err)
560 goto done;
561 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
562 err = got_error_from_errno();
563 free(id_str);
564 goto done;
566 free(id_str);
567 time = mktime(&commit2->tm_author);
568 if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
569 get_datestr(&time, datebuf)) < 0) {
570 err = got_error_from_errno();
571 goto done;
573 time = mktime(&commit2->tm_committer);
574 if (strcmp(commit2->author, commit2->committer) != 0 &&
575 fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
576 get_datestr(&time, datebuf)) < 0) {
577 err = got_error_from_errno();
578 goto done;
580 if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
581 err = got_error_from_errno();
582 goto done;
585 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
586 done:
587 if (tree_obj1)
588 got_object_close(tree_obj1);
589 if (tree_obj2)
590 got_object_close(tree_obj2);
591 if (commit1)
592 got_object_commit_close(commit1);
593 if (commit2)
594 got_object_commit_close(commit2);
595 return err;