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,
331 const struct got_error *err = NULL;
332 struct got_object *treeobj = NULL;
333 struct got_tree_object *tree = NULL;
335 err = got_object_open(&treeobj, repo, id);
339 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
340 err = got_error(GOT_ERR_OBJ_TYPE);
344 err = got_object_tree_open(&tree, repo, treeobj);
348 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
352 got_object_tree_close(tree);
354 got_object_close(treeobj);
358 static const struct got_error *
359 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
360 const char *label1, const char *label2, struct got_repository *repo,
361 got_diff_blob_cb cb, void *cb_arg, int diff_content)
363 const struct got_error *err;
364 struct got_object *treeobj1 = NULL;
365 struct got_object *treeobj2 = NULL;
366 struct got_tree_object *tree1 = NULL;
367 struct got_tree_object *tree2 = NULL;
369 err = got_object_open(&treeobj1, repo, id1);
373 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
374 err = got_error(GOT_ERR_OBJ_TYPE);
378 err = got_object_open(&treeobj2, repo, id2);
382 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
383 err = got_error(GOT_ERR_OBJ_TYPE);
387 err = got_object_tree_open(&tree1, repo, treeobj1);
391 err = got_object_tree_open(&tree2, repo, treeobj2);
395 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
400 got_object_tree_close(tree1);
402 got_object_tree_close(tree2);
404 got_object_close(treeobj1);
406 got_object_close(treeobj2);
410 static const struct got_error *
411 diff_deleted_tree(struct got_object_id *id, const char *label,
412 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
415 const struct got_error *err;
416 struct got_object *treeobj = NULL;
417 struct got_tree_object *tree = NULL;
419 err = got_object_open(&treeobj, repo, id);
423 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
424 err = got_error(GOT_ERR_OBJ_TYPE);
428 err = got_object_tree_open(&tree, repo, treeobj);
432 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
436 got_object_tree_close(tree);
438 got_object_close(treeobj);
442 static const struct got_error *
443 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
444 const char *label1, const char *label2, struct got_repository *repo,
445 got_diff_blob_cb cb, void *cb_arg)
451 static const struct got_error *
452 diff_entry_old_new(const struct got_tree_entry *te1,
453 const struct got_tree_entry *te2, const char *label1, const char *label2,
454 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
457 const struct got_error *err = NULL;
461 if (S_ISDIR(te1->mode))
462 err = diff_deleted_tree(te1->id, label1, repo,
463 cb, cb_arg, diff_content);
466 err = diff_deleted_blob(te1->id, label1, repo,
469 err = cb(cb_arg, NULL, NULL, te1->id, NULL,
475 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
476 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
478 return diff_modified_tree(te1->id, te2->id,
479 label1, label2, repo, cb, cb_arg, diff_content);
480 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
483 return diff_modified_blob(te1->id, te2->id,
484 label1, label2, repo, cb, cb_arg);
486 return cb(cb_arg, NULL, NULL, te1->id,
487 te2->id, label1, label2, repo);
494 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
498 static const struct got_error *
499 diff_entry_new_old(const struct got_tree_entry *te2,
500 const struct got_tree_entry *te1, const char *label2,
501 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
504 if (te1 != NULL) /* handled by diff_entry_old_new() */
507 if (S_ISDIR(te2->mode))
508 return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
512 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
514 return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
517 const struct got_error *
518 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
519 const char *label1, const char *label2, struct got_repository *repo,
520 got_diff_blob_cb cb, void *cb_arg, int diff_content)
522 const struct got_error *err = NULL;
523 struct got_tree_entry *te1 = NULL;
524 struct got_tree_entry *te2 = NULL;
525 char *l1 = NULL, *l2 = NULL;
528 const struct got_tree_entries *entries;
529 entries = got_object_tree_get_entries(tree1);
530 te1 = SIMPLEQ_FIRST(&entries->head);
531 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
533 return got_error_from_errno("asprintf");
536 const struct got_tree_entries *entries;
537 entries = got_object_tree_get_entries(tree2);
538 te2 = SIMPLEQ_FIRST(&entries->head);
539 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
541 return got_error_from_errno("asprintf");
546 const struct got_tree_entry *te = NULL;
548 te = got_object_tree_find_entry(tree2,
553 if (te && asprintf(&l2, "%s%s%s", label2,
554 label2[0] ? "/" : "", te->name) == -1)
556 got_error_from_errno("asprintf");
558 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
559 cb_arg, diff_content);
565 const struct got_tree_entry *te = NULL;
567 te = got_object_tree_find_entry(tree1,
571 if (asprintf(&l2, "%s%s%s", label2,
572 label2[0] ? "/" : "", te->name) == -1)
574 got_error_from_errno("asprintf");
576 if (asprintf(&l2, "%s%s%s", label2,
577 label2[0] ? "/" : "", te2->name) == -1)
579 got_error_from_errno("asprintf");
581 err = diff_entry_new_old(te2, te, l2, repo,
582 cb, cb_arg, diff_content);
590 te1 = SIMPLEQ_NEXT(te1, entry);
592 asprintf(&l1, "%s%s%s", label1,
593 label1[0] ? "/" : "", te1->name) == -1)
594 return got_error_from_errno("asprintf");
599 te2 = SIMPLEQ_NEXT(te2, entry);
601 asprintf(&l2, "%s%s%s", label2,
602 label2[0] ? "/" : "", te2->name) == -1)
603 return got_error_from_errno("asprintf");
605 } while (te1 || te2);
610 const struct got_error *
611 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
612 const char *label1, const char *label2, int diff_context,
613 struct got_repository *repo, FILE *outfile)
615 const struct got_error *err;
616 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
618 if (id1 == NULL && id2 == NULL)
619 return got_error(GOT_ERR_NO_OBJ);
622 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
627 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
631 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
635 got_object_blob_close(blob1);
637 got_object_blob_close(blob2);
641 const struct got_error *
642 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
643 char *label1, char *label2, int diff_context, struct got_repository *repo,
646 const struct got_error *err;
647 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
648 struct got_diff_blob_output_unidiff_arg arg;
650 if (id1 == NULL && id2 == NULL)
651 return got_error(GOT_ERR_NO_OBJ);
654 err = got_object_open_as_tree(&tree1, repo, id1);
659 err = got_object_open_as_tree(&tree2, repo, id2);
663 arg.diff_context = diff_context;
664 arg.outfile = outfile;
665 err = got_diff_tree(tree1, tree2, label1, label2, repo,
666 got_diff_blob_output_unidiff, &arg, 1);
669 got_object_tree_close(tree1);
671 got_object_tree_close(tree2);
675 const struct got_error *
676 got_diff_objects_as_commits(struct got_object_id *id1,
677 struct got_object_id *id2, int diff_context,
678 struct got_repository *repo, FILE *outfile)
680 const struct got_error *err;
681 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
684 return got_error(GOT_ERR_NO_OBJ);
687 err = got_object_open_as_commit(&commit1, repo, id1);
692 err = got_object_open_as_commit(&commit2, repo, id2);
696 err = got_diff_objects_as_trees(
697 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
698 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
702 got_object_commit_close(commit1);
704 got_object_commit_close(commit2);