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"
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, int diff_context, FILE *outfile,
39 struct got_diff_changes *changes)
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;
54 return got_error(GOT_ERR_FILE_OPEN);
62 return got_error(GOT_ERR_FILE_OPEN);
69 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
70 err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
78 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
79 err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
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;
89 ds.stb1.st_size = size1;
90 ds.stb1.st_mtime = 0; /* XXX */
92 ds.stb2.st_mode = S_IFREG;
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 = diff_context;
102 flags |= D_PROTOTYPE;
104 if (label1 && strcmp(label1, idstr1) != 0)
105 fprintf(outfile, "blob - %s\n", idstr1);
106 if (label2 && strcmp(label2, idstr2) != 0)
107 fprintf(outfile, "blob + %s\n", idstr2);
109 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
118 const struct got_error *
119 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
120 const char *label1, const char *label2, int diff_context, FILE *outfile)
122 return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
126 const struct got_error *
127 got_diff_blob_lines_changed(struct got_diff_changes **changes,
128 struct got_blob_object *blob1, struct got_blob_object *blob2)
130 const struct got_error *err = NULL;
132 *changes = calloc(1, sizeof(**changes));
133 if (*changes == NULL)
134 return got_error_from_errno();
135 SIMPLEQ_INIT(&(*changes)->entries);
137 err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
139 got_diff_free_changes(*changes);
146 got_diff_free_changes(struct got_diff_changes *changes)
148 struct got_diff_change *change;
149 while (!SIMPLEQ_EMPTY(&changes->entries)) {
150 change = SIMPLEQ_FIRST(&changes->entries);
151 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
157 struct got_tree_entry *
158 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
160 struct got_tree_entry *te2;
161 const struct got_tree_entries *entries2;
163 entries2 = got_object_tree_get_entries(tree2);
164 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
165 if (strcmp(te1->name, te2->name) == 0)
171 static const struct got_error *
172 diff_added_blob(struct got_object_id *id, const char *label,
173 int diff_context, struct got_repository *repo, FILE *outfile)
175 const struct got_error *err;
176 struct got_blob_object *blob = NULL;
177 struct got_object *obj = NULL;
179 err = got_object_open(&obj, repo, id);
183 err = got_object_blob_open(&blob, repo, obj, 8192);
186 err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
188 got_object_close(obj);
190 got_object_blob_close(blob);
194 static const struct got_error *
195 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
196 const char *label1, const char *label2, int diff_context,
197 struct got_repository *repo, FILE *outfile)
199 const struct got_error *err;
200 struct got_object *obj1 = NULL;
201 struct got_object *obj2 = NULL;
202 struct got_blob_object *blob1 = NULL;
203 struct got_blob_object *blob2 = NULL;
205 err = got_object_open(&obj1, repo, id1);
208 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
209 err = got_error(GOT_ERR_OBJ_TYPE);
213 err = got_object_open(&obj2, repo, id2);
216 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
217 err = got_error(GOT_ERR_BAD_OBJ_DATA);
221 err = got_object_blob_open(&blob1, repo, obj1, 8192);
225 err = got_object_blob_open(&blob2, repo, obj2, 8192);
229 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
234 got_object_close(obj1);
236 got_object_close(obj2);
238 got_object_blob_close(blob1);
240 got_object_blob_close(blob2);
244 static const struct got_error *
245 diff_deleted_blob(struct got_object_id *id, const char *label,
246 int diff_context, struct got_repository *repo, FILE *outfile)
248 const struct got_error *err;
249 struct got_blob_object *blob = NULL;
250 struct got_object *obj = NULL;
252 err = got_object_open(&obj, repo, id);
256 err = got_object_blob_open(&blob, repo, obj, 8192);
259 err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
261 got_object_close(obj);
263 got_object_blob_close(blob);
267 static const struct got_error *
268 diff_added_tree(struct got_object_id *id, const char *label,
269 int diff_context, struct got_repository *repo, FILE *outfile)
271 const struct got_error *err = NULL;
272 struct got_object *treeobj = NULL;
273 struct got_tree_object *tree = NULL;
275 err = got_object_open(&treeobj, repo, id);
279 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
280 err = got_error(GOT_ERR_OBJ_TYPE);
284 err = got_object_tree_open(&tree, repo, treeobj);
288 err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo,
293 got_object_tree_close(tree);
295 got_object_close(treeobj);
299 static const struct got_error *
300 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
301 const char *label1, const char *label2, int diff_context,
302 struct got_repository *repo, FILE *outfile)
304 const struct got_error *err;
305 struct got_object *treeobj1 = NULL;
306 struct got_object *treeobj2 = NULL;
307 struct got_tree_object *tree1 = NULL;
308 struct got_tree_object *tree2 = NULL;
310 err = got_object_open(&treeobj1, repo, id1);
314 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
315 err = got_error(GOT_ERR_OBJ_TYPE);
319 err = got_object_open(&treeobj2, repo, id2);
323 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
324 err = got_error(GOT_ERR_OBJ_TYPE);
328 err = got_object_tree_open(&tree1, repo, treeobj1);
332 err = got_object_tree_open(&tree2, repo, treeobj2);
336 err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo,
341 got_object_tree_close(tree1);
343 got_object_tree_close(tree2);
345 got_object_close(treeobj1);
347 got_object_close(treeobj2);
351 static const struct got_error *
352 diff_deleted_tree(struct got_object_id *id, const char *label,
353 int diff_context, struct got_repository *repo, FILE *outfile)
355 const struct got_error *err;
356 struct got_object *treeobj = NULL;
357 struct got_tree_object *tree = NULL;
359 err = got_object_open(&treeobj, repo, id);
363 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
364 err = got_error(GOT_ERR_OBJ_TYPE);
368 err = got_object_tree_open(&tree, repo, treeobj);
372 err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
376 got_object_tree_close(tree);
378 got_object_close(treeobj);
382 static const struct got_error *
383 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
384 const char *label1, const char *label2, FILE *outfile)
390 static const struct got_error *
391 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
392 const char *label1, const char *label2, int diff_context,
393 struct got_repository *repo, FILE *outfile)
395 const struct got_error *err = NULL;
399 if (S_ISDIR(te1->mode))
400 err = diff_deleted_tree(te1->id, label1, diff_context,
403 err = diff_deleted_blob(te1->id, label1, diff_context,
408 id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
409 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
411 return diff_modified_tree(te1->id, te2->id,
412 label1, label2, diff_context, repo, outfile);
413 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
415 return diff_modified_blob(te1->id, te2->id,
416 label1, label2, diff_context, repo, outfile);
422 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
425 static const struct got_error *
426 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
427 const char *label2, int diff_context, struct got_repository *repo,
430 if (te1 != NULL) /* handled by diff_entry_old_new() */
433 if (S_ISDIR(te2->mode))
434 return diff_added_tree(te2->id, label2, diff_context, repo,
437 return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
440 const struct got_error *
441 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
442 const char *label1, const char *label2, int diff_context,
443 struct got_repository *repo, FILE *outfile)
445 const struct got_error *err = NULL;
446 struct got_tree_entry *te1 = NULL;
447 struct got_tree_entry *te2 = NULL;
448 char *l1 = NULL, *l2 = NULL;
451 const struct got_tree_entries *entries;
452 entries = got_object_tree_get_entries(tree1);
453 te1 = SIMPLEQ_FIRST(&entries->head);
454 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
456 return got_error_from_errno();
459 const struct got_tree_entries *entries;
460 entries = got_object_tree_get_entries(tree2);
461 te2 = SIMPLEQ_FIRST(&entries->head);
462 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
464 return got_error_from_errno();
469 struct got_tree_entry *te = NULL;
471 te = match_entry_by_name(te1, tree2);
475 if (te && asprintf(&l2, "%s%s%s", label2,
476 label2[0] ? "/" : "", te->name) == -1)
477 return got_error_from_errno();
479 err = diff_entry_old_new(te1, te, l1, l2, diff_context,
486 struct got_tree_entry *te = NULL;
488 te = match_entry_by_name(te2, tree1);
491 if (asprintf(&l2, "%s%s%s", label2,
492 label2[0] ? "/" : "", te->name) == -1)
493 return got_error_from_errno();
495 if (asprintf(&l2, "%s%s%s", label2,
496 label2[0] ? "/" : "", te2->name) == -1)
497 return got_error_from_errno();
499 err = diff_entry_new_old(te2, te, l2, diff_context,
508 te1 = SIMPLEQ_NEXT(te1, entry);
510 asprintf(&l1, "%s%s%s", label1,
511 label1[0] ? "/" : "", te1->name) == -1)
512 return got_error_from_errno();
517 te2 = SIMPLEQ_NEXT(te2, entry);
519 asprintf(&l2, "%s%s%s", label2,
520 label2[0] ? "/" : "", te2->name) == -1)
521 return got_error_from_errno();
523 } while (te1 || te2);
528 const struct got_error *
529 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
530 const char *label1, const char *label2, int diff_context,
531 struct got_repository *repo, FILE *outfile)
533 const struct got_error *err;
534 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
536 if (obj1 == NULL && obj2 == NULL)
537 return got_error(GOT_ERR_NO_OBJ);
540 err = got_object_blob_open(&blob1, repo, obj1, 8192);
545 err = got_object_blob_open(&blob2, repo, obj2, 8192);
549 err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
553 got_object_blob_close(blob1);
555 got_object_blob_close(blob2);
559 const struct got_error *
560 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
561 char *label1, char *label2, int diff_context, struct got_repository *repo,
564 const struct got_error *err;
565 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
567 if (obj1 == NULL && obj2 == NULL)
568 return got_error(GOT_ERR_NO_OBJ);
571 err = got_object_tree_open(&tree1, repo, obj1);
576 err = got_object_tree_open(&tree2, repo, obj2);
580 err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
584 got_object_tree_close(tree1);
586 got_object_tree_close(tree2);
591 get_datestr(time_t *time, char *datebuf)
593 char *p, *s = ctime_r(time, datebuf);
600 const struct got_error *
601 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
602 int diff_context, struct got_repository *repo, FILE *outfile)
604 const struct got_error *err;
605 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
606 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
611 return got_error(GOT_ERR_NO_OBJ);
614 err = got_object_commit_open(&commit1, repo, obj1);
617 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
622 err = got_object_commit_open(&commit2, repo, obj2);
625 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
628 err = got_object_get_id_str(&id_str, obj2);
631 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
632 err = got_error_from_errno();
637 if (fprintf(outfile, "from: %s\n", commit2->author) < 0) {
638 err = got_error_from_errno();
641 if (fprintf(outfile, "date: %s UTC\n",
642 get_datestr(&commit2->committer_time, datebuf)) < 0) {
643 err = got_error_from_errno();
646 if (strcmp(commit2->author, commit2->committer) != 0 &&
647 fprintf(outfile, "via: %s\n", commit2->committer) < 0) {
648 err = got_error_from_errno();
651 if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
652 err = got_error_from_errno();
656 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "",
657 diff_context, repo, outfile);
660 got_object_close(tree_obj1);
662 got_object_close(tree_obj2);
664 got_object_commit_close(commit1);
666 got_object_commit_close(commit2);