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, 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, 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;
156 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
157 if (strcmp(te1->name, te2->name) == 0)
158 return te2;
160 return NULL;
163 static const struct got_error *
164 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
165 FILE *outfile)
167 const struct got_error *err;
168 struct got_blob_object *blob = NULL;
169 struct got_object *obj = NULL;
171 err = got_object_open(&obj, repo, id);
172 if (err)
173 return err;
175 err = got_object_blob_open(&blob, repo, obj, 8192);
176 if (err)
177 goto done;
178 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
179 done:
180 got_object_close(obj);
181 if (blob)
182 got_object_blob_close(blob);
183 return err;
186 static const struct got_error *
187 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
188 struct got_repository *repo, FILE *outfile)
190 const struct got_error *err;
191 struct got_object *obj1 = NULL;
192 struct got_object *obj2 = NULL;
193 struct got_blob_object *blob1 = NULL;
194 struct got_blob_object *blob2 = NULL;
196 err = got_object_open(&obj1, repo, id1);
197 if (err)
198 return err;
199 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
200 err = got_error(GOT_ERR_OBJ_TYPE);
201 goto done;
204 err = got_object_open(&obj2, repo, id2);
205 if (err)
206 goto done;
207 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
208 err = got_error(GOT_ERR_BAD_OBJ_DATA);
209 goto done;
212 err = got_object_blob_open(&blob1, repo, obj1, 8192);
213 if (err)
214 goto done;
216 err = got_object_blob_open(&blob2, repo, obj2, 8192);
217 if (err)
218 goto done;
220 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
222 done:
223 if (obj1)
224 got_object_close(obj1);
225 if (obj2)
226 got_object_close(obj2);
227 if (blob1)
228 got_object_blob_close(blob1);
229 if (blob2)
230 got_object_blob_close(blob2);
231 return err;
234 static const struct got_error *
235 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
236 FILE *outfile)
238 const struct got_error *err;
239 struct got_blob_object *blob = NULL;
240 struct got_object *obj = NULL;
242 err = got_object_open(&obj, repo, id);
243 if (err)
244 return err;
246 err = got_object_blob_open(&blob, repo, obj, 8192);
247 if (err)
248 goto done;
249 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
250 done:
251 got_object_close(obj);
252 if (blob)
253 got_object_blob_close(blob);
254 return err;
257 static const struct got_error *
258 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
259 FILE *outfile)
261 const struct got_error *err = NULL;
262 struct got_object *treeobj = NULL;
263 struct got_tree_object *tree = NULL;
265 err = got_object_open(&treeobj, repo, id);
266 if (err)
267 goto done;
269 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
270 err = got_error(GOT_ERR_OBJ_TYPE);
271 goto done;
274 err = got_object_tree_open(&tree, repo, treeobj);
275 if (err)
276 goto done;
278 err = got_diff_tree(NULL, tree, repo, outfile);
280 done:
281 if (tree)
282 got_object_tree_close(tree);
283 if (treeobj)
284 got_object_close(treeobj);
285 return err;
288 static const struct got_error *
289 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
290 struct got_repository *repo, FILE *outfile)
292 const struct got_error *err = NULL;
293 struct got_object *treeobj1 = NULL;
294 struct got_object *treeobj2 = NULL;
295 struct got_tree_object *tree1 = NULL;
296 struct got_tree_object *tree2 = NULL;
298 err = got_object_open(&treeobj1, repo, id1);
299 if (err)
300 goto done;
302 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
304 goto done;
307 err = got_object_open(&treeobj2, repo, id2);
308 if (err)
309 goto done;
311 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
312 err = got_error(GOT_ERR_OBJ_TYPE);
313 goto done;
316 err = got_object_tree_open(&tree1, repo, treeobj1);
317 if (err)
318 goto done;
320 err = got_object_tree_open(&tree2, repo, treeobj2);
321 if (err)
322 goto done;
324 err = got_diff_tree(tree1, tree2, repo, outfile);
326 done:
327 if (tree1)
328 got_object_tree_close(tree1);
329 if (tree2)
330 got_object_tree_close(tree2);
331 if (treeobj1)
332 got_object_close(treeobj1);
333 if (treeobj2)
334 got_object_close(treeobj2);
335 return err;
338 static const struct got_error *
339 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
341 const struct got_error *err = NULL;
342 struct got_object *treeobj = NULL;
343 struct got_tree_object *tree = NULL;
345 err = got_object_open(&treeobj, repo, id);
346 if (err)
347 goto done;
349 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
350 err = got_error(GOT_ERR_OBJ_TYPE);
351 goto done;
354 err = got_object_tree_open(&tree, repo, treeobj);
355 if (err)
356 goto done;
358 err = got_diff_tree(tree, NULL, repo, outfile);
360 done:
361 if (tree)
362 got_object_tree_close(tree);
363 if (treeobj)
364 got_object_close(treeobj);
365 return err;
368 static const struct got_error *
369 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
370 FILE *outfile)
372 /* XXX TODO */
373 return NULL;
376 static const struct got_error *
377 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
378 struct got_repository *repo, FILE *outfile)
380 struct got_tree_entry *te2 = NULL;
382 if (tree2)
383 te2 = match_entry_by_name(te1, tree2);
384 if (te2 == NULL) {
385 if (S_ISDIR(te1->mode))
386 return diff_deleted_tree(te1->id, repo, outfile);
387 return diff_deleted_blob(te1->id, repo, outfile);
390 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
391 if (got_object_id_cmp(te1->id, te2->id) != 0)
392 return diff_modified_tree(te1->id, te2->id, repo,
393 outfile);
394 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
395 if (got_object_id_cmp(te1->id, te2->id) != 0)
396 return diff_modified_blob(te1->id, te2->id, repo,
397 outfile);
400 return diff_kind_mismatch(te1->id, te2->id, outfile);
403 static const struct got_error *
404 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
405 struct got_repository *repo, FILE *outfile)
407 if (tree1) {
408 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
409 if (te1 != NULL) /* handled by diff_entry_old_new() */
410 return NULL;
413 if (S_ISDIR(te2->mode))
414 return diff_added_tree(te2->id, repo, outfile);
415 return diff_added_blob(te2->id, repo, outfile);
418 const struct got_error *
419 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
420 struct got_repository *repo, FILE *outfile)
422 const struct got_error *err = NULL;
423 struct got_tree_entry *te1 = NULL;
424 struct got_tree_entry *te2 = NULL;
426 if (tree1)
427 te1 = SIMPLEQ_FIRST(&tree1->entries);
428 if (tree2)
429 te2 = SIMPLEQ_FIRST(&tree2->entries);
431 do {
432 if (te1) {
433 err = diff_entry_old_new(te1, tree2, repo, outfile);
434 if (err)
435 break;
438 if (te2) {
439 err = diff_entry_new_old(te2, tree1, repo, outfile);
440 if (err)
441 break;
444 if (te1)
445 te1 = SIMPLEQ_NEXT(te1, entry);
446 if (te2)
447 te2 = SIMPLEQ_NEXT(te2, entry);
448 } while (te1 || te2);
450 return err;
453 const struct got_error *
454 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
455 struct got_repository *repo, FILE *outfile)
457 const struct got_error *err;
458 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
460 if (obj1 == NULL && obj2 == NULL)
461 return got_error(GOT_ERR_NO_OBJ);
463 if (obj1) {
464 err = got_object_blob_open(&blob1, repo, obj1, 8192);
465 if (err)
466 goto done;
468 if (obj2) {
469 err = got_object_blob_open(&blob2, repo, obj2, 8192);
470 if (err)
471 goto done;
473 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
474 done:
475 if (blob1)
476 got_object_blob_close(blob1);
477 if (blob2)
478 got_object_blob_close(blob2);
479 return err;
482 const struct got_error *
483 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
484 struct got_repository *repo, FILE *outfile)
486 const struct got_error *err;
487 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
489 if (obj1 == NULL && obj2 == NULL)
490 return got_error(GOT_ERR_NO_OBJ);
492 if (obj1) {
493 err = got_object_tree_open(&tree1, repo, obj1);
494 if (err)
495 goto done;
497 if (obj2) {
498 err = got_object_tree_open(&tree2, repo, obj2);
499 if (err)
500 goto done;
502 err = got_diff_tree(tree1, tree2, repo, outfile);
503 done:
504 if (tree1)
505 got_object_tree_close(tree1);
506 if (tree2)
507 got_object_tree_close(tree2);
508 return err;
511 static char *
512 get_datestr(time_t *time, char *datebuf)
514 char *p, *s = ctime_r(time, datebuf);
515 p = strchr(s, '\n');
516 if (p)
517 *p = '\0';
518 return s;
521 const struct got_error *
522 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
523 struct got_repository *repo, FILE *outfile)
525 const struct got_error *err;
526 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
527 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
528 char *id_str;
529 char datebuf[26];
530 time_t time;
532 if (obj2 == NULL)
533 return got_error(GOT_ERR_NO_OBJ);
535 if (obj1) {
536 err = got_object_commit_open(&commit1, repo, obj1);
537 if (err)
538 goto done;
539 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
540 if (err)
541 goto done;
544 err = got_object_commit_open(&commit2, repo, obj2);
545 if (err)
546 goto done;
547 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
548 if (err)
549 goto done;
550 err = got_object_get_id_str(&id_str, obj2);
551 if (err)
552 goto done;
553 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
554 err = got_error_from_errno();
555 free(id_str);
556 goto done;
558 free(id_str);
559 time = mktime(&commit2->tm_author);
560 if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
561 get_datestr(&time, datebuf)) < 0) {
562 err = got_error_from_errno();
563 goto done;
565 time = mktime(&commit2->tm_committer);
566 if (strcmp(commit2->author, commit2->committer) != 0 &&
567 fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
568 get_datestr(&time, datebuf)) < 0) {
569 err = got_error_from_errno();
570 goto done;
572 if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
573 err = got_error_from_errno();
574 goto done;
577 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
578 done:
579 if (tree_obj1)
580 got_object_close(tree_obj1);
581 if (tree_obj2)
582 got_object_close(tree_obj2);
583 if (commit1)
584 got_object_commit_close(commit1);
585 if (commit2)
586 got_object_commit_close(commit2);
587 return err;