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 size1, size2;
48 int res, flags = 0;
50 if (blob1) {
51 f1 = got_opentemp();
52 if (f1 == NULL)
53 return got_error(GOT_ERR_FILE_OPEN);
54 } else
55 flags |= D_EMPTY1;
57 if (blob2) {
58 f2 = got_opentemp();
59 if (f2 == NULL) {
60 fclose(f1);
61 return got_error(GOT_ERR_FILE_OPEN);
62 }
63 } else
64 flags |= D_EMPTY2;
66 size1 = 0;
67 if (blob1) {
68 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
69 err = got_object_blob_dump_to_file(&size1, f1, blob1);
70 if (err)
71 goto done;
72 } else
73 idstr1 = "/dev/null";
75 size2 = 0;
76 if (blob2) {
77 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
78 err = got_object_blob_dump_to_file(&size2, f2, blob2);
79 if (err)
80 goto done;
81 } else
82 idstr2 = "/dev/null";
84 memset(&ds, 0, sizeof(ds));
85 /* XXX should stat buffers be passed in args instead of ds? */
86 ds.stb1.st_mode = S_IFREG;
87 if (blob1)
88 ds.stb1.st_size = size1;
89 ds.stb1.st_mtime = 0; /* XXX */
91 ds.stb2.st_mode = S_IFREG;
92 if (blob2)
93 ds.stb2.st_size = size2;
94 ds.stb2.st_mtime = 0; /* XXX */
96 memset(&args, 0, sizeof(args));
97 args.diff_format = D_UNIFIED;
98 args.label[0] = label1 ? label1 : idstr1;
99 args.label[1] = label2 ? label2 : idstr2;
100 args.diff_context = 3;
101 flags |= D_PROTOTYPE;
103 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
104 done:
105 if (f1)
106 fclose(f1);
107 if (f2)
108 fclose(f2);
109 return err;
112 struct got_tree_entry *
113 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
115 struct got_tree_entry *te2;
117 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
118 if (strcmp(te1->name, te2->name) == 0)
119 return te2;
121 return NULL;
124 static const struct got_error *
125 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
126 FILE *outfile)
128 const struct got_error *err;
129 struct got_blob_object *blob = NULL;
130 struct got_object *obj = NULL;
132 err = got_object_open(&obj, repo, id);
133 if (err)
134 return err;
136 err = got_object_blob_open(&blob, repo, obj, 8192);
137 if (err)
138 goto done;
139 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
140 done:
141 got_object_close(obj);
142 if (blob)
143 got_object_blob_close(blob);
144 return err;
147 static const struct got_error *
148 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
149 struct got_repository *repo, FILE *outfile)
151 const struct got_error *err;
152 struct got_object *obj1 = NULL;
153 struct got_object *obj2 = NULL;
154 struct got_blob_object *blob1 = NULL;
155 struct got_blob_object *blob2 = NULL;
157 err = got_object_open(&obj1, repo, id1);
158 if (err)
159 return err;
160 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
161 err = got_error(GOT_ERR_OBJ_TYPE);
162 goto done;
165 err = got_object_open(&obj2, repo, id2);
166 if (err)
167 goto done;
168 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
169 err = got_error(GOT_ERR_BAD_OBJ_DATA);
170 goto done;
173 err = got_object_blob_open(&blob1, repo, obj1, 8192);
174 if (err)
175 goto done;
177 err = got_object_blob_open(&blob2, repo, obj2, 8192);
178 if (err)
179 goto done;
181 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
183 done:
184 if (obj1)
185 got_object_close(obj1);
186 if (obj2)
187 got_object_close(obj2);
188 if (blob1)
189 got_object_blob_close(blob1);
190 if (blob2)
191 got_object_blob_close(blob2);
192 return err;
195 static const struct got_error *
196 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
197 FILE *outfile)
199 const struct got_error *err;
200 struct got_blob_object *blob = NULL;
201 struct got_object *obj = NULL;
203 err = got_object_open(&obj, repo, id);
204 if (err)
205 return err;
207 err = got_object_blob_open(&blob, repo, obj, 8192);
208 if (err)
209 goto done;
210 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
211 done:
212 got_object_close(obj);
213 if (blob)
214 got_object_blob_close(blob);
215 return err;
218 static const struct got_error *
219 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
220 FILE *outfile)
222 const struct got_error *err = NULL;
223 struct got_object *treeobj = NULL;
224 struct got_tree_object *tree = NULL;
226 err = got_object_open(&treeobj, repo, id);
227 if (err)
228 goto done;
230 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
231 err = got_error(GOT_ERR_OBJ_TYPE);
232 goto done;
235 err = got_object_tree_open(&tree, repo, treeobj);
236 if (err)
237 goto done;
239 err = got_diff_tree(NULL, tree, repo, outfile);
241 done:
242 if (tree)
243 got_object_tree_close(tree);
244 if (treeobj)
245 got_object_close(treeobj);
246 return err;
249 static const struct got_error *
250 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
251 struct got_repository *repo, FILE *outfile)
253 const struct got_error *err = NULL;
254 struct got_object *treeobj1 = NULL;
255 struct got_object *treeobj2 = NULL;
256 struct got_tree_object *tree1 = NULL;
257 struct got_tree_object *tree2 = NULL;
259 err = got_object_open(&treeobj1, repo, id1);
260 if (err)
261 goto done;
263 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
264 err = got_error(GOT_ERR_OBJ_TYPE);
265 goto done;
268 err = got_object_open(&treeobj2, repo, id2);
269 if (err)
270 goto done;
272 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
273 err = got_error(GOT_ERR_OBJ_TYPE);
274 goto done;
277 err = got_object_tree_open(&tree1, repo, treeobj1);
278 if (err)
279 goto done;
281 err = got_object_tree_open(&tree2, repo, treeobj2);
282 if (err)
283 goto done;
285 err = got_diff_tree(tree1, tree2, repo, outfile);
287 done:
288 if (tree1)
289 got_object_tree_close(tree1);
290 if (tree2)
291 got_object_tree_close(tree2);
292 if (treeobj1)
293 got_object_close(treeobj1);
294 if (treeobj2)
295 got_object_close(treeobj2);
296 return err;
299 static const struct got_error *
300 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
302 const struct got_error *err = NULL;
303 struct got_object *treeobj = NULL;
304 struct got_tree_object *tree = NULL;
306 err = got_object_open(&treeobj, repo, id);
307 if (err)
308 goto done;
310 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
311 err = got_error(GOT_ERR_OBJ_TYPE);
312 goto done;
315 err = got_object_tree_open(&tree, repo, treeobj);
316 if (err)
317 goto done;
319 err = got_diff_tree(tree, NULL, repo, outfile);
321 done:
322 if (tree)
323 got_object_tree_close(tree);
324 if (treeobj)
325 got_object_close(treeobj);
326 return err;
329 static const struct got_error *
330 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
331 FILE *outfile)
333 /* XXX TODO */
334 return NULL;
337 static const struct got_error *
338 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
339 struct got_repository *repo, FILE *outfile)
341 struct got_tree_entry *te2 = NULL;
343 if (tree2)
344 te2 = match_entry_by_name(te1, tree2);
345 if (te2 == NULL) {
346 if (S_ISDIR(te1->mode))
347 return diff_deleted_tree(te1->id, repo, outfile);
348 return diff_deleted_blob(te1->id, repo, outfile);
351 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
352 if (got_object_id_cmp(te1->id, te2->id) != 0)
353 return diff_modified_tree(te1->id, te2->id, repo,
354 outfile);
355 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
356 if (got_object_id_cmp(te1->id, te2->id) != 0)
357 return diff_modified_blob(te1->id, te2->id, repo,
358 outfile);
361 return diff_kind_mismatch(te1->id, te2->id, outfile);
364 static const struct got_error *
365 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
366 struct got_repository *repo, FILE *outfile)
368 if (tree1) {
369 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
370 if (te1 != NULL) /* handled by diff_entry_old_new() */
371 return NULL;
374 if (S_ISDIR(te2->mode))
375 return diff_added_tree(te2->id, repo, outfile);
376 return diff_added_blob(te2->id, repo, outfile);
379 const struct got_error *
380 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
381 struct got_repository *repo, FILE *outfile)
383 const struct got_error *err = NULL;
384 struct got_tree_entry *te1 = NULL;
385 struct got_tree_entry *te2 = NULL;
387 if (tree1)
388 te1 = SIMPLEQ_FIRST(&tree1->entries);
389 if (tree2)
390 te2 = SIMPLEQ_FIRST(&tree2->entries);
392 do {
393 if (te1) {
394 err = diff_entry_old_new(te1, tree2, repo, outfile);
395 if (err)
396 break;
399 if (te2) {
400 err = diff_entry_new_old(te2, tree1, repo, outfile);
401 if (err)
402 break;
405 if (te1)
406 te1 = SIMPLEQ_NEXT(te1, entry);
407 if (te2)
408 te2 = SIMPLEQ_NEXT(te2, entry);
409 } while (te1 || te2);
411 return err;
414 const struct got_error *
415 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
416 struct got_repository *repo, FILE *outfile)
418 const struct got_error *err;
419 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
421 if (obj1 == NULL && obj2 == NULL)
422 return got_error(GOT_ERR_NO_OBJ);
424 if (obj1) {
425 err = got_object_blob_open(&blob1, repo, obj1, 8192);
426 if (err)
427 goto done;
429 if (obj2) {
430 err = got_object_blob_open(&blob2, repo, obj2, 8192);
431 if (err)
432 goto done;
434 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
435 done:
436 if (blob1)
437 got_object_blob_close(blob1);
438 if (blob2)
439 got_object_blob_close(blob2);
440 return err;
443 const struct got_error *
444 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
445 struct got_repository *repo, FILE *outfile)
447 const struct got_error *err;
448 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
450 if (obj1 == NULL && obj2 == NULL)
451 return got_error(GOT_ERR_NO_OBJ);
453 if (obj1) {
454 err = got_object_tree_open(&tree1, repo, obj1);
455 if (err)
456 goto done;
458 if (obj2) {
459 err = got_object_tree_open(&tree2, repo, obj2);
460 if (err)
461 goto done;
463 err = got_diff_tree(tree1, tree2, repo, outfile);
464 done:
465 if (tree1)
466 got_object_tree_close(tree1);
467 if (tree2)
468 got_object_tree_close(tree2);
469 return err;
472 static char *
473 get_datestr(time_t *time, char *datebuf)
475 char *p, *s = ctime_r(time, datebuf);
476 p = strchr(s, '\n');
477 if (p)
478 *p = '\0';
479 return s;
482 const struct got_error *
483 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
484 struct got_repository *repo, FILE *outfile)
486 const struct got_error *err;
487 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
488 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
489 char *id_str;
490 char datebuf[26];
491 time_t time;
493 if (obj2 == NULL)
494 return got_error(GOT_ERR_NO_OBJ);
496 if (obj1) {
497 err = got_object_commit_open(&commit1, repo, obj1);
498 if (err)
499 goto done;
500 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
501 if (err)
502 goto done;
505 err = got_object_commit_open(&commit2, repo, obj2);
506 if (err)
507 goto done;
508 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
509 if (err)
510 goto done;
511 err = got_object_get_id_str(&id_str, obj2);
512 if (err)
513 goto done;
514 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
515 err = got_error_from_errno();
516 free(id_str);
517 goto done;
519 free(id_str);
520 time = mktime(&commit2->tm_author);
521 if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
522 get_datestr(&time, datebuf)) < 0) {
523 err = got_error_from_errno();
524 goto done;
526 time = mktime(&commit2->tm_committer);
527 if (strcmp(commit2->author, commit2->committer) != 0 &&
528 fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
529 get_datestr(&time, datebuf)) < 0) {
530 err = got_error_from_errno();
531 goto done;
533 if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
534 err = got_error_from_errno();
535 goto done;
538 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
539 done:
540 if (tree_obj1)
541 got_object_close(tree_obj1);
542 if (tree_obj2)
543 got_object_close(tree_obj2);
544 if (commit1)
545 got_object_commit_close(commit1);
546 if (commit2)
547 got_object_commit_close(commit2);
548 return err;