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 const struct got_error *
37 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
38 const char *label1, const char *label2, FILE *outfile)
39 {
40 struct got_diff_state ds;
41 struct got_diff_args args;
42 const struct got_error *err = NULL;
43 FILE *f1 = NULL, *f2 = NULL;
44 char hex1[SHA1_DIGEST_STRING_LENGTH];
45 char hex2[SHA1_DIGEST_STRING_LENGTH];
46 char *idstr1 = NULL, *idstr2 = NULL;
47 size_t len, hdrlen;
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 hdrlen = got_object_blob_get_hdrlen(blob1);
71 do {
72 err = got_object_blob_read_block(&len, blob1);
73 if (err)
74 goto done;
75 if (len == 0)
76 break;
77 size1 += len;
78 /* Skip blob object header first time around. */
79 fwrite(got_object_blob_get_read_buf(blob1) + hdrlen,
80 len - hdrlen, 1, f1);
81 hdrlen = 0;
82 } while (len != 0);
83 } else
84 idstr1 = "/dev/null";
86 size2 = 0;
87 if (blob2) {
88 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
89 hdrlen = got_object_blob_get_hdrlen(blob2);
90 do {
91 err = got_object_blob_read_block(&len, blob2);
92 if (err)
93 goto done;
94 if (len == 0)
95 break;
96 size2 += len;
97 /* Skip blob object header first time around. */
98 fwrite(got_object_blob_get_read_buf(blob2) + hdrlen,
99 len - hdrlen, 1, f2);
100 hdrlen = 0;
101 } while (len != 0);
102 } else
103 idstr2 = "/dev/null";
105 if (f1) {
106 fflush(f1);
107 rewind(f1);
109 if (f2) {
110 fflush(f2);
111 rewind(f2);
114 memset(&ds, 0, sizeof(ds));
115 /* XXX should stat buffers be passed in args instead of ds? */
116 ds.stb1.st_mode = S_IFREG;
117 if (blob1)
118 ds.stb1.st_size = size1;
119 ds.stb1.st_mtime = 0; /* XXX */
121 ds.stb2.st_mode = S_IFREG;
122 if (blob2)
123 ds.stb2.st_size = size2;
124 ds.stb2.st_mtime = 0; /* XXX */
126 memset(&args, 0, sizeof(args));
127 args.diff_format = D_UNIFIED;
128 args.label[0] = label1 ? label1 : idstr1;
129 args.label[1] = label2 ? label2 : idstr2;
130 args.diff_context = 3;
131 flags |= D_PROTOTYPE;
133 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
134 done:
135 if (f1)
136 fclose(f1);
137 if (f2)
138 fclose(f2);
139 return err;
142 struct got_tree_entry *
143 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
145 struct got_tree_entry *te2;
147 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
148 if (strcmp(te1->name, te2->name) == 0)
149 return te2;
151 return NULL;
154 static const struct got_error *
155 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
156 FILE *outfile)
158 const struct got_error *err;
159 struct got_blob_object *blob = NULL;
160 struct got_object *obj = NULL;
162 err = got_object_open(&obj, repo, id);
163 if (err)
164 return err;
166 err = got_object_blob_open(&blob, repo, obj, 8192);
167 if (err)
168 goto done;
169 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
170 done:
171 got_object_close(obj);
172 if (blob)
173 got_object_blob_close(blob);
174 return err;
177 static const struct got_error *
178 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
179 struct got_repository *repo, FILE *outfile)
181 const struct got_error *err;
182 struct got_object *obj1 = NULL;
183 struct got_object *obj2 = NULL;
184 struct got_blob_object *blob1 = NULL;
185 struct got_blob_object *blob2 = NULL;
187 err = got_object_open(&obj1, repo, id1);
188 if (err)
189 return err;
190 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
191 err = got_error(GOT_ERR_OBJ_TYPE);
192 goto done;
195 err = got_object_open(&obj2, repo, id2);
196 if (err)
197 goto done;
198 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
199 err = got_error(GOT_ERR_BAD_OBJ_DATA);
200 goto done;
203 err = got_object_blob_open(&blob1, repo, obj1, 8192);
204 if (err)
205 goto done;
207 err = got_object_blob_open(&blob2, repo, obj2, 8192);
208 if (err)
209 goto done;
211 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
213 done:
214 if (obj1)
215 got_object_close(obj1);
216 if (obj2)
217 got_object_close(obj2);
218 if (blob1)
219 got_object_blob_close(blob1);
220 if (blob2)
221 got_object_blob_close(blob2);
222 return err;
225 static const struct got_error *
226 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
227 FILE *outfile)
229 const struct got_error *err;
230 struct got_blob_object *blob = NULL;
231 struct got_object *obj = NULL;
233 err = got_object_open(&obj, repo, id);
234 if (err)
235 return err;
237 err = got_object_blob_open(&blob, repo, obj, 8192);
238 if (err)
239 goto done;
240 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
241 done:
242 got_object_close(obj);
243 if (blob)
244 got_object_blob_close(blob);
245 return err;
248 static const struct got_error *
249 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
250 FILE *outfile)
252 const struct got_error *err = NULL;
253 struct got_object *treeobj = NULL;
254 struct got_tree_object *tree = NULL;
256 err = got_object_open(&treeobj, repo, id);
257 if (err)
258 goto done;
260 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
261 err = got_error(GOT_ERR_OBJ_TYPE);
262 goto done;
265 err = got_object_tree_open(&tree, repo, treeobj);
266 if (err)
267 goto done;
269 err = got_diff_tree(NULL, tree, repo, outfile);
271 done:
272 if (tree)
273 got_object_tree_close(tree);
274 if (treeobj)
275 got_object_close(treeobj);
276 return err;
279 static const struct got_error *
280 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
281 struct got_repository *repo, FILE *outfile)
283 const struct got_error *err = NULL;
284 struct got_object *treeobj1 = NULL;
285 struct got_object *treeobj2 = NULL;
286 struct got_tree_object *tree1 = NULL;
287 struct got_tree_object *tree2 = NULL;
289 err = got_object_open(&treeobj1, repo, id1);
290 if (err)
291 goto done;
293 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
294 err = got_error(GOT_ERR_OBJ_TYPE);
295 goto done;
298 err = got_object_open(&treeobj2, repo, id2);
299 if (err)
300 goto done;
302 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
304 goto done;
307 err = got_object_tree_open(&tree1, repo, treeobj1);
308 if (err)
309 goto done;
311 err = got_object_tree_open(&tree2, repo, treeobj2);
312 if (err)
313 goto done;
315 err = got_diff_tree(tree1, tree2, repo, outfile);
317 done:
318 if (tree1)
319 got_object_tree_close(tree1);
320 if (tree2)
321 got_object_tree_close(tree2);
322 if (treeobj1)
323 got_object_close(treeobj1);
324 if (treeobj2)
325 got_object_close(treeobj2);
326 return err;
329 static const struct got_error *
330 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
332 const struct got_error *err = NULL;
333 struct got_object *treeobj = NULL;
334 struct got_tree_object *tree = NULL;
336 err = got_object_open(&treeobj, repo, id);
337 if (err)
338 goto done;
340 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 err = got_object_tree_open(&tree, repo, treeobj);
346 if (err)
347 goto done;
349 err = got_diff_tree(tree, NULL, repo, outfile);
351 done:
352 if (tree)
353 got_object_tree_close(tree);
354 if (treeobj)
355 got_object_close(treeobj);
356 return err;
359 static const struct got_error *
360 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
361 FILE *outfile)
363 /* XXX TODO */
364 return NULL;
367 static const struct got_error *
368 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
369 struct got_repository *repo, FILE *outfile)
371 struct got_tree_entry *te2 = NULL;
373 if (tree2)
374 te2 = match_entry_by_name(te1, tree2);
375 if (te2 == NULL) {
376 if (S_ISDIR(te1->mode))
377 return diff_deleted_tree(te1->id, repo, outfile);
378 return diff_deleted_blob(te1->id, repo, outfile);
381 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
382 if (got_object_id_cmp(te1->id, te2->id) != 0)
383 return diff_modified_tree(te1->id, te2->id, repo,
384 outfile);
385 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
386 if (got_object_id_cmp(te1->id, te2->id) != 0)
387 return diff_modified_blob(te1->id, te2->id, repo,
388 outfile);
391 return diff_kind_mismatch(te1->id, te2->id, outfile);
394 static const struct got_error *
395 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
396 struct got_repository *repo, FILE *outfile)
398 if (tree1) {
399 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
400 if (te1 != NULL) /* handled by diff_entry_old_new() */
401 return NULL;
404 if (S_ISDIR(te2->mode))
405 return diff_added_tree(te2->id, repo, outfile);
406 return diff_added_blob(te2->id, repo, outfile);
409 const struct got_error *
410 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
411 struct got_repository *repo, FILE *outfile)
413 const struct got_error *err = NULL;
414 struct got_tree_entry *te1 = NULL;
415 struct got_tree_entry *te2 = NULL;
417 if (tree1)
418 te1 = SIMPLEQ_FIRST(&tree1->entries);
419 if (tree2)
420 te2 = SIMPLEQ_FIRST(&tree2->entries);
422 do {
423 if (te1) {
424 err = diff_entry_old_new(te1, tree2, repo, outfile);
425 if (err)
426 break;
429 if (te2) {
430 err = diff_entry_new_old(te2, tree1, repo, outfile);
431 if (err)
432 break;
435 if (te1)
436 te1 = SIMPLEQ_NEXT(te1, entry);
437 if (te2)
438 te2 = SIMPLEQ_NEXT(te2, entry);
439 } while (te1 || te2);
441 return err;
444 const struct got_error *
445 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
446 struct got_repository *repo, FILE *outfile)
448 const struct got_error *err;
449 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
451 if (obj1 == NULL && obj2 == NULL)
452 return got_error(GOT_ERR_NO_OBJ);
454 if (obj1) {
455 err = got_object_blob_open(&blob1, repo, obj1, 8192);
456 if (err)
457 goto done;
459 if (obj2) {
460 err = got_object_blob_open(&blob2, repo, obj2, 8192);
461 if (err)
462 goto done;
464 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
465 done:
466 if (blob1)
467 got_object_blob_close(blob1);
468 if (blob2)
469 got_object_blob_close(blob2);
470 return err;
473 const struct got_error *
474 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
475 struct got_repository *repo, FILE *outfile)
477 const struct got_error *err;
478 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
480 if (obj1 == NULL && obj2 == NULL)
481 return got_error(GOT_ERR_NO_OBJ);
483 if (obj1) {
484 err = got_object_tree_open(&tree1, repo, obj1);
485 if (err)
486 goto done;
488 if (obj2) {
489 err = got_object_tree_open(&tree2, repo, obj2);
490 if (err)
491 goto done;
493 err = got_diff_tree(tree1, tree2, repo, outfile);
494 done:
495 if (tree1)
496 got_object_tree_close(tree1);
497 if (tree2)
498 got_object_tree_close(tree2);
499 return err;
502 const struct got_error *
503 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
504 struct got_repository *repo, FILE *outfile)
506 const struct got_error *err;
507 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
508 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
509 char *id_str;
511 if (obj2 == NULL)
512 return got_error(GOT_ERR_NO_OBJ);
514 if (obj1) {
515 err = got_object_commit_open(&commit1, repo, obj1);
516 if (err)
517 goto done;
518 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
519 if (err)
520 goto done;
523 err = got_object_commit_open(&commit2, repo, obj2);
524 if (err)
525 goto done;
526 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
527 if (err)
528 goto done;
529 err = got_object_get_id_str(&id_str, obj2);
530 if (err)
531 goto done;
532 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
533 err = got_error_from_errno();
534 free(id_str);
535 goto done;
537 free(id_str);
538 if (fprintf(outfile, "author: %s\n", commit2->author) < 0) {
539 err = got_error_from_errno();
540 goto done;
542 if (strcmp(commit2->author, commit2->committer) != 0 &&
543 fprintf(outfile, "committer: %s\n", commit2->committer) < 0) {
544 err = got_error_from_errno();
545 goto done;
547 if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
548 err = got_error_from_errno();
549 goto done;
552 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
553 done:
554 if (tree_obj1)
555 got_object_close(tree_obj1);
556 if (tree_obj2)
557 got_object_close(tree_obj2);
558 if (commit1)
559 got_object_commit_close(commit1);
560 if (commit2)
561 got_object_commit_close(commit2);
562 return err;