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_repository.h"
28 #include "got_object.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, f1, blob1);
82 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
83 err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
89 memset(&ds, 0, sizeof(ds));
90 /* XXX should stat buffers be passed in args instead of ds? */
91 ds.stb1.st_mode = S_IFREG;
93 ds.stb1.st_size = size1;
94 ds.stb1.st_mtime = 0; /* XXX */
96 ds.stb2.st_mode = S_IFREG;
98 ds.stb2.st_size = size2;
99 ds.stb2.st_mtime = 0; /* XXX */
101 memset(&args, 0, sizeof(args));
102 args.diff_format = D_UNIFIED;
103 args.label[0] = label1 ? label1 : idstr1;
104 args.label[1] = label2 ? label2 : idstr2;
105 args.diff_context = diff_context;
106 flags |= D_PROTOTYPE;
109 fprintf(outfile, "blob - %s\n", idstr1);
110 fprintf(outfile, "blob + %s\n", idstr2);
112 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
114 if (f1 && fclose(f1) != 0 && err == NULL)
115 err = got_error_from_errno("fclose");
116 if (f2 && fclose(f2) != 0 && err == NULL)
117 err = got_error_from_errno("fclose");
121 const struct got_error *
122 got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
123 struct got_blob_object *blob2, struct got_object_id *id1,
124 struct got_object_id *id2, const char *label1, const char *label2,
125 struct got_repository *repo)
127 struct got_diff_blob_output_unidiff_arg *a = arg;
129 return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
133 const struct got_error *
134 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
135 const char *label1, const char *label2, int diff_context, FILE *outfile)
137 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
141 const struct got_error *
142 got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
143 const char *label2, int diff_context, FILE *outfile)
145 struct got_diff_state ds;
146 struct got_diff_args args;
147 const struct got_error *err = NULL;
149 char hex1[SHA1_DIGEST_STRING_LENGTH];
158 return got_error_from_errno("got_opentemp");
159 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
160 err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
165 idstr1 = "/dev/null";
171 memset(&ds, 0, sizeof(ds));
172 /* XXX should stat buffers be passed in args instead of ds? */
173 ds.stb1.st_mode = S_IFREG;
175 ds.stb1.st_size = size1;
176 ds.stb1.st_mtime = 0; /* XXX */
178 ds.stb2.st_mode = S_IFREG;
179 ds.stb2.st_size = size2;
180 ds.stb2.st_mtime = 0; /* XXX */
182 memset(&args, 0, sizeof(args));
183 args.diff_format = D_UNIFIED;
184 args.label[0] = label2;
185 args.label[1] = label2;
186 args.diff_context = diff_context;
187 flags |= D_PROTOTYPE;
189 fprintf(outfile, "blob - %s\n", idstr1);
190 fprintf(outfile, "file + %s\n", f2 == NULL ? "/dev/null" : label2);
191 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
193 if (f1 && fclose(f1) != 0 && err == NULL)
194 err = got_error_from_errno("fclose");
198 const struct got_error *
199 got_diff_blob_lines_changed(struct got_diff_changes **changes,
200 struct got_blob_object *blob1, struct got_blob_object *blob2)
202 const struct got_error *err = NULL;
204 *changes = calloc(1, sizeof(**changes));
205 if (*changes == NULL)
206 return got_error_from_errno("calloc");
207 SIMPLEQ_INIT(&(*changes)->entries);
209 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
211 got_diff_free_changes(*changes);
218 got_diff_free_changes(struct got_diff_changes *changes)
220 struct got_diff_change *change;
221 while (!SIMPLEQ_EMPTY(&changes->entries)) {
222 change = SIMPLEQ_FIRST(&changes->entries);
223 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
229 static const struct got_error *
230 diff_added_blob(struct got_object_id *id, const char *label,
231 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
233 const struct got_error *err;
234 struct got_blob_object *blob = NULL;
235 struct got_object *obj = NULL;
237 err = got_object_open(&obj, repo, id);
241 err = got_object_blob_open(&blob, repo, obj, 8192);
244 err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
246 got_object_close(obj);
248 got_object_blob_close(blob);
252 static const struct got_error *
253 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
254 const char *label1, const char *label2, struct got_repository *repo,
255 got_diff_blob_cb cb, void *cb_arg)
257 const struct got_error *err;
258 struct got_object *obj1 = NULL;
259 struct got_object *obj2 = NULL;
260 struct got_blob_object *blob1 = NULL;
261 struct got_blob_object *blob2 = NULL;
263 err = got_object_open(&obj1, repo, id1);
266 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
267 err = got_error(GOT_ERR_OBJ_TYPE);
271 err = got_object_open(&obj2, repo, id2);
274 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
275 err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 err = got_object_blob_open(&blob1, repo, obj1, 8192);
283 err = got_object_blob_open(&blob2, repo, obj2, 8192);
287 err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
290 got_object_close(obj1);
292 got_object_close(obj2);
294 got_object_blob_close(blob1);
296 got_object_blob_close(blob2);
300 static const struct got_error *
301 diff_deleted_blob(struct got_object_id *id, const char *label,
302 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
304 const struct got_error *err;
305 struct got_blob_object *blob = NULL;
306 struct got_object *obj = NULL;
308 err = got_object_open(&obj, repo, id);
312 err = got_object_blob_open(&blob, repo, obj, 8192);
315 err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
317 got_object_close(obj);
319 got_object_blob_close(blob);
323 static const struct got_error *
324 diff_added_tree(struct got_object_id *id, const char *label,
325 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
327 const struct got_error *err = NULL;
328 struct got_object *treeobj = NULL;
329 struct got_tree_object *tree = NULL;
331 err = got_object_open(&treeobj, repo, id);
335 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
336 err = got_error(GOT_ERR_OBJ_TYPE);
340 err = got_object_tree_open(&tree, repo, treeobj);
344 err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg);
347 got_object_tree_close(tree);
349 got_object_close(treeobj);
353 static const struct got_error *
354 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
355 const char *label1, const char *label2, struct got_repository *repo,
356 got_diff_blob_cb cb, void *cb_arg)
358 const struct got_error *err;
359 struct got_object *treeobj1 = NULL;
360 struct got_object *treeobj2 = NULL;
361 struct got_tree_object *tree1 = NULL;
362 struct got_tree_object *tree2 = NULL;
364 err = got_object_open(&treeobj1, repo, id1);
368 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
369 err = got_error(GOT_ERR_OBJ_TYPE);
373 err = got_object_open(&treeobj2, repo, id2);
377 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
378 err = got_error(GOT_ERR_OBJ_TYPE);
382 err = got_object_tree_open(&tree1, repo, treeobj1);
386 err = got_object_tree_open(&tree2, repo, treeobj2);
390 err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg);
394 got_object_tree_close(tree1);
396 got_object_tree_close(tree2);
398 got_object_close(treeobj1);
400 got_object_close(treeobj2);
404 static const struct got_error *
405 diff_deleted_tree(struct got_object_id *id, const char *label,
406 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
408 const struct got_error *err;
409 struct got_object *treeobj = NULL;
410 struct got_tree_object *tree = NULL;
412 err = got_object_open(&treeobj, repo, id);
416 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
417 err = got_error(GOT_ERR_OBJ_TYPE);
421 err = got_object_tree_open(&tree, repo, treeobj);
425 err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg);
428 got_object_tree_close(tree);
430 got_object_close(treeobj);
434 static const struct got_error *
435 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
436 const char *label1, const char *label2, struct got_repository *repo,
437 got_diff_blob_cb cb, void *cb_arg)
443 static const struct got_error *
444 diff_entry_old_new(const struct got_tree_entry *te1,
445 const struct got_tree_entry *te2, const char *label1, const char *label2,
446 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
448 const struct got_error *err = NULL;
452 if (S_ISDIR(te1->mode))
453 err = diff_deleted_tree(te1->id, label1, repo,
456 err = diff_deleted_blob(te1->id, label1, repo,
461 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
462 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
464 return diff_modified_tree(te1->id, te2->id,
465 label1, label2, repo, cb, cb_arg);
466 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
468 return diff_modified_blob(te1->id, te2->id,
469 label1, label2, repo, cb, cb_arg);
475 return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
479 static const struct got_error *
480 diff_entry_new_old(const struct got_tree_entry *te2,
481 const struct got_tree_entry *te1, const char *label2,
482 struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
484 if (te1 != NULL) /* handled by diff_entry_old_new() */
487 if (S_ISDIR(te2->mode))
488 return diff_added_tree(te2->id, label2, repo, cb, cb_arg);
490 return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
493 const struct got_error *
494 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
495 const char *label1, const char *label2, struct got_repository *repo,
496 got_diff_blob_cb cb, void *cb_arg)
498 const struct got_error *err = NULL;
499 struct got_tree_entry *te1 = NULL;
500 struct got_tree_entry *te2 = NULL;
501 char *l1 = NULL, *l2 = NULL;
504 const struct got_tree_entries *entries;
505 entries = got_object_tree_get_entries(tree1);
506 te1 = SIMPLEQ_FIRST(&entries->head);
507 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
509 return got_error_from_errno("asprintf");
512 const struct got_tree_entries *entries;
513 entries = got_object_tree_get_entries(tree2);
514 te2 = SIMPLEQ_FIRST(&entries->head);
515 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
517 return got_error_from_errno("asprintf");
522 const struct got_tree_entry *te = NULL;
524 te = got_object_tree_find_entry(tree2,
529 if (te && asprintf(&l2, "%s%s%s", label2,
530 label2[0] ? "/" : "", te->name) == -1)
532 got_error_from_errno("asprintf");
534 err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
541 const struct got_tree_entry *te = NULL;
543 te = got_object_tree_find_entry(tree1,
547 if (asprintf(&l2, "%s%s%s", label2,
548 label2[0] ? "/" : "", te->name) == -1)
550 got_error_from_errno("asprintf");
552 if (asprintf(&l2, "%s%s%s", label2,
553 label2[0] ? "/" : "", te2->name) == -1)
555 got_error_from_errno("asprintf");
557 err = diff_entry_new_old(te2, te, l2, repo, cb, cb_arg);
565 te1 = SIMPLEQ_NEXT(te1, entry);
567 asprintf(&l1, "%s%s%s", label1,
568 label1[0] ? "/" : "", te1->name) == -1)
569 return got_error_from_errno("asprintf");
574 te2 = SIMPLEQ_NEXT(te2, entry);
576 asprintf(&l2, "%s%s%s", label2,
577 label2[0] ? "/" : "", te2->name) == -1)
578 return got_error_from_errno("asprintf");
580 } while (te1 || te2);
585 const struct got_error *
586 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
587 const char *label1, const char *label2, int diff_context,
588 struct got_repository *repo, FILE *outfile)
590 const struct got_error *err;
591 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
593 if (id1 == NULL && id2 == NULL)
594 return got_error(GOT_ERR_NO_OBJ);
597 err = got_object_open_as_blob(&blob1, repo, id1, 8192);
602 err = got_object_open_as_blob(&blob2, repo, id2, 8192);
606 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
610 got_object_blob_close(blob1);
612 got_object_blob_close(blob2);
616 const struct got_error *
617 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
618 char *label1, char *label2, int diff_context, struct got_repository *repo,
621 const struct got_error *err;
622 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
623 struct got_diff_blob_output_unidiff_arg arg;
625 if (id1 == NULL && id2 == NULL)
626 return got_error(GOT_ERR_NO_OBJ);
629 err = got_object_open_as_tree(&tree1, repo, id1);
634 err = got_object_open_as_tree(&tree2, repo, id2);
638 arg.diff_context = diff_context;
639 arg.outfile = outfile;
640 err = got_diff_tree(tree1, tree2, label1, label2, repo,
641 got_diff_blob_output_unidiff, &arg);
644 got_object_tree_close(tree1);
646 got_object_tree_close(tree2);
650 const struct got_error *
651 got_diff_objects_as_commits(struct got_object_id *id1,
652 struct got_object_id *id2, int diff_context,
653 struct got_repository *repo, FILE *outfile)
655 const struct got_error *err;
656 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
659 return got_error(GOT_ERR_NO_OBJ);
662 err = got_object_open_as_commit(&commit1, repo, id1);
667 err = got_object_open_as_commit(&commit2, repo, id2);
671 err = got_diff_objects_as_trees(
672 commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
673 got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
677 got_object_commit_close(commit1);
679 got_object_commit_close(commit2);