2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
27 #include "got_object.h"
28 #include "got_repository.h"
29 #include "got_error.h"
31 #include "got_opentemp.h"
34 #include "got_lib_diff.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 static const struct got_error *
40 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
41 const char *label1, const char *label2, int diff_context, FILE *outfile,
42 struct got_diff_changes *changes)
44 struct got_diff_state ds;
45 struct got_diff_args args;
46 const struct got_error *err = NULL;
47 FILE *f1 = NULL, *f2 = NULL;
48 char hex1[SHA1_DIGEST_STRING_LENGTH];
49 char hex2[SHA1_DIGEST_STRING_LENGTH];
50 char *idstr1 = NULL, *idstr2 = NULL;
57 return got_error_from_errno("got_opentemp");
64 err = got_error_from_errno("got_opentemp");
73 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
74 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
83 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
84 err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
91 memset(&ds, 0, sizeof(ds));
92 /* XXX should stat buffers be passed in args instead of ds? */
93 ds.stb1.st_mode = S_IFREG;
95 ds.stb1.st_size = size1;
96 ds.stb1.st_mtime = 0; /* XXX */
98 ds.stb2.st_mode = S_IFREG;
100 ds.stb2.st_size = size2;
101 ds.stb2.st_mtime = 0; /* XXX */
103 memset(&args, 0, sizeof(args));
104 args.diff_format = D_UNIFIED;
105 args.label[0] = label1 ? label1 : idstr1;
106 args.label[1] = label2 ? label2 : idstr2;
107 args.diff_context = diff_context;
108 flags |= D_PROTOTYPE;
111 fprintf(outfile, "blob - %s\n", idstr1);
112 fprintf(outfile, "blob + %s\n", idstr2);
114 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
116 if (f1 && fclose(f1) != 0 && err == NULL)
117 err = got_error_from_errno("fclose");
118 if (f2 && fclose(f2) != 0 && err == NULL)
119 err = got_error_from_errno("fclose");
123 const struct got_error *
124 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
125 struct got_blob_object *blob2, struct got_object_id *id1,
126 struct got_object_id *id2, const char *label1, const char *label2,
127 struct got_repository *repo)
129 struct got_diff_blob_output_unidiff_arg *a = arg;
131 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
135 const struct got_error *
136 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
137 const char *label1, const char *label2, int diff_context, FILE *outfile)
139 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
143 const struct got_error *
144 got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
145 const char *label2, int diff_context, FILE *outfile)
147 struct got_diff_state ds;
148 struct got_diff_args args;
149 const struct got_error *err = NULL;
151 char hex1[SHA1_DIGEST_STRING_LENGTH];
160 return got_error_from_errno("got_opentemp");
161 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
162 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
168 idstr1 = "/dev/null";
174 memset(&ds, 0, sizeof(ds));
175 /* XXX should stat buffers be passed in args instead of ds? */
176 ds.stb1.st_mode = S_IFREG;
178 ds.stb1.st_size = size1;
179 ds.stb1.st_mtime = 0; /* XXX */
181 ds.stb2.st_mode = S_IFREG;
182 ds.stb2.st_size = size2;
183 ds.stb2.st_mtime = 0; /* XXX */
185 memset(&args, 0, sizeof(args));
186 args.diff_format = D_UNIFIED;
187 args.label[0] = label2;
188 args.label[1] = label2;
189 args.diff_context = diff_context;
190 flags |= D_PROTOTYPE;
192 fprintf(outfile, "blob - %s\n", idstr1);
193 fprintf(outfile, "file + %s\n", f2 == NULL ? "/dev/null" : label2);
194 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
196 if (f1 && fclose(f1) != 0 && err == NULL)
197 err = got_error_from_errno("fclose");
201 const struct got_error *
202 got_diff_blob_lines_changed(struct got_diff_changes **changes,
203 struct got_blob_object *blob1, struct got_blob_object *blob2)
205 const struct got_error *err = NULL;
207 *changes = calloc(1, sizeof(**changes));
208 if (*changes == NULL)
209 return got_error_from_errno("calloc");
210 SIMPLEQ_INIT(&(*changes)->entries);
212 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
214 got_diff_free_changes(*changes);
221 got_diff_free_changes(struct got_diff_changes *changes)
223 struct got_diff_change *change;
224 while (!SIMPLEQ_EMPTY(&changes->entries)) {
225 change = SIMPLEQ_FIRST(&changes->entries);
226 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
232 static const struct got_error *
233 diff_added_blob(struct got_object_id *id, const char *label,
234 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
236 const struct got_error *err;
237 struct got_blob_object *blob = NULL;
238 struct got_object *obj = NULL;
240 err = got_object_open(&obj, repo, id);
244 err = got_object_blob_open(&blob, repo, obj, 8192);
247 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
249 got_object_close(obj);
251 got_object_blob_close(blob);
255 static const struct got_error *
256 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
257 const char *label1, const char *label2, struct got_repository *repo,
258 got_diff_blob_cb cb, void *cb_arg)
260 const struct got_error *err;
261 struct got_object *obj1 = NULL;
262 struct got_object *obj2 = NULL;
263 struct got_blob_object *blob1 = NULL;
264 struct got_blob_object *blob2 = NULL;
266 err = got_object_open(&obj1, repo, id1);
269 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
270 err = got_error(GOT_ERR_OBJ_TYPE);
274 err = got_object_open(&obj2, repo, id2);
277 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
278 err = got_error(GOT_ERR_BAD_OBJ_DATA);
282 err = got_object_blob_open(&blob1, repo, obj1, 8192);
286 err = got_object_blob_open(&blob2, repo, obj2, 8192);
290 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
293 got_object_close(obj1);
295 got_object_close(obj2);
297 got_object_blob_close(blob1);
299 got_object_blob_close(blob2);
303 static const struct got_error *
304 diff_deleted_blob(struct got_object_id *id, const char *label,
305 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
307 const struct got_error *err;
308 struct got_blob_object *blob = NULL;
309 struct got_object *obj = NULL;
311 err = got_object_open(&obj, repo, id);
315 err = got_object_blob_open(&blob, repo, obj, 8192);
318 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
320 got_object_close(obj);
322 got_object_blob_close(blob);
326 static const struct got_error *
327 diff_added_tree(struct got_object_id *id, const char *label,
328 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
330 const struct got_error *err = NULL;
331 struct got_object *treeobj = NULL;
332 struct got_tree_object *tree = NULL;
334 err = got_object_open(&treeobj, repo, id);
338 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
339 err = got_error(GOT_ERR_OBJ_TYPE);
343 err = got_object_tree_open(&tree, repo, treeobj);
347 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg);
350 got_object_tree_close(tree);
352 got_object_close(treeobj);
356 static const struct got_error *
357 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
358 const char *label1, const char *label2, struct got_repository *repo,
359 got_diff_blob_cb cb, void *cb_arg)
361 const struct got_error *err;
362 struct got_object *treeobj1 = NULL;
363 struct got_object *treeobj2 = NULL;
364 struct got_tree_object *tree1 = NULL;
365 struct got_tree_object *tree2 = NULL;
367 err = got_object_open(&treeobj1, repo, id1);
371 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
372 err = got_error(GOT_ERR_OBJ_TYPE);
376 err = got_object_open(&treeobj2, repo, id2);
380 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
381 err = got_error(GOT_ERR_OBJ_TYPE);
385 err = got_object_tree_open(&tree1, repo, treeobj1);
389 err = got_object_tree_open(&tree2, repo, treeobj2);
393 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg);
397 got_object_tree_close(tree1);
399 got_object_tree_close(tree2);
401 got_object_close(treeobj1);
403 got_object_close(treeobj2);
407 static const struct got_error *
408 diff_deleted_tree(struct got_object_id *id, const char *label,
409 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
411 const struct got_error *err;
412 struct got_object *treeobj = NULL;
413 struct got_tree_object *tree = NULL;
415 err = got_object_open(&treeobj, repo, id);
419 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
420 err = got_error(GOT_ERR_OBJ_TYPE);
424 err = got_object_tree_open(&tree, repo, treeobj);
428 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg);
431 got_object_tree_close(tree);
433 got_object_close(treeobj);
437 static const struct got_error *
438 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
439 const char *label1, const char *label2, struct got_repository *repo,
440 got_diff_blob_cb cb, void *cb_arg)
446 static const struct got_error *
447 diff_entry_old_new(const struct got_tree_entry *te1,
448 const struct got_tree_entry *te2, const char *label1, const char *label2,
449 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
451 const struct got_error *err = NULL;
455 if (S_ISDIR(te1->mode))
456 err = diff_deleted_tree(te1->id, label1, repo,
459 err = diff_deleted_blob(te1->id, label1, repo,
464 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
465 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
467 return diff_modified_tree(te1->id, te2->id,
468 label1, label2, repo, cb, cb_arg);
469 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
471 return diff_modified_blob(te1->id, te2->id,
472 label1, label2, repo, cb, cb_arg);
478 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
482 static const struct got_error *
483 diff_entry_new_old(const struct got_tree_entry *te2,
484 const struct got_tree_entry *te1, const char *label2,
485 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
487 if (te1 != NULL) /* handled by diff_entry_old_new() */
490 if (S_ISDIR(te2->mode))
491 return diff_added_tree(te2->id, label2, repo, cb, cb_arg);
493 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
496 const struct got_error *
497 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
498 const char *label1, const char *label2, struct got_repository *repo,
499 got_diff_blob_cb cb, void *cb_arg)
501 const struct got_error *err = NULL;
502 struct got_tree_entry *te1 = NULL;
503 struct got_tree_entry *te2 = NULL;
504 char *l1 = NULL, *l2 = NULL;
507 const struct got_tree_entries *entries;
508 entries = got_object_tree_get_entries(tree1);
509 te1 = SIMPLEQ_FIRST(&entries->head);
510 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
512 return got_error_from_errno("asprintf");
515 const struct got_tree_entries *entries;
516 entries = got_object_tree_get_entries(tree2);
517 te2 = SIMPLEQ_FIRST(&entries->head);
518 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
520 return got_error_from_errno("asprintf");
525 const struct got_tree_entry *te = NULL;
527 te = got_object_tree_find_entry(tree2,
532 if (te && asprintf(&l2, "%s%s%s", label2,
533 label2[0] ? "/" : "", te->name) == -1)
535 got_error_from_errno("asprintf");
537 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
544 const struct got_tree_entry *te = NULL;
546 te = got_object_tree_find_entry(tree1,
550 if (asprintf(&l2, "%s%s%s", label2,
551 label2[0] ? "/" : "", te->name) == -1)
553 got_error_from_errno("asprintf");
555 if (asprintf(&l2, "%s%s%s", label2,
556 label2[0] ? "/" : "", te2->name) == -1)
558 got_error_from_errno("asprintf");
560 err = diff_entry_new_old(te2, te, l2, repo, cb, cb_arg);
568 te1 = SIMPLEQ_NEXT(te1, entry);
570 asprintf(&l1, "%s%s%s", label1,
571 label1[0] ? "/" : "", te1->name) == -1)
572 return got_error_from_errno("asprintf");
577 te2 = SIMPLEQ_NEXT(te2, entry);
579 asprintf(&l2, "%s%s%s", label2,
580 label2[0] ? "/" : "", te2->name) == -1)
581 return got_error_from_errno("asprintf");
583 } while (te1 || te2);
588 const struct got_error *
589 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
590 const char *label1, const char *label2, int diff_context,
591 struct got_repository *repo, FILE *outfile)
593 const struct got_error *err;
594 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
596 if (id1 == NULL && id2 == NULL)
597 return got_error(GOT_ERR_NO_OBJ);
600 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
605 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
609 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
613 got_object_blob_close(blob1);
615 got_object_blob_close(blob2);
619 const struct got_error *
620 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
621 char *label1, char *label2, int diff_context, struct got_repository *repo,
624 const struct got_error *err;
625 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
626 struct got_diff_blob_output_unidiff_arg arg;
628 if (id1 == NULL && id2 == NULL)
629 return got_error(GOT_ERR_NO_OBJ);
632 err = got_object_open_as_tree(&tree1, repo, id1);
637 err = got_object_open_as_tree(&tree2, repo, id2);
641 arg.diff_context = diff_context;
642 arg.outfile = outfile;
643 err = got_diff_tree(tree1, tree2, label1, label2, repo,
644 got_diff_blob_output_unidiff, &arg);
647 got_object_tree_close(tree1);
649 got_object_tree_close(tree2);
653 const struct got_error *
654 got_diff_objects_as_commits(struct got_object_id *id1,
655 struct got_object_id *id2, int diff_context,
656 struct got_repository *repo, FILE *outfile)
658 const struct got_error *err;
659 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
662 return got_error(GOT_ERR_NO_OBJ);
665 err = got_object_open_as_commit(&commit1, repo, id1);
670 err = got_object_open_as_commit(&commit2, repo, id2);
674 err = got_diff_objects_as_trees(
675 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
676 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
680 got_object_commit_close(commit1);
682 got_object_commit_close(commit2);