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, 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 = 3;
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, FILE *outfile)
122 return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
125 const struct got_error *
126 got_diff_blob_lines_changed(struct got_diff_changes **changes,
127 struct got_blob_object *blob1, struct got_blob_object *blob2)
129 const struct got_error *err = NULL;
131 *changes = calloc(1, sizeof(**changes));
132 if (*changes == NULL)
133 return got_error_from_errno();
134 SIMPLEQ_INIT(&(*changes)->entries);
136 err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
138 got_diff_free_changes(*changes);
145 got_diff_free_changes(struct got_diff_changes *changes)
147 struct got_diff_change *change;
148 while (!SIMPLEQ_EMPTY(&changes->entries)) {
149 change = SIMPLEQ_FIRST(&changes->entries);
150 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
156 struct got_tree_entry *
157 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
159 struct got_tree_entry *te2;
160 const struct got_tree_entries *entries2;
162 entries2 = got_object_tree_get_entries(tree2);
163 SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
164 if (strcmp(te1->name, te2->name) == 0)
170 static const struct got_error *
171 diff_added_blob(struct got_object_id *id, const char *label,
172 struct got_repository *repo, FILE *outfile)
174 const struct got_error *err;
175 struct got_blob_object *blob = NULL;
176 struct got_object *obj = NULL;
178 err = got_object_open(&obj, repo, id);
182 err = got_object_blob_open(&blob, repo, obj, 8192);
185 err = got_diff_blob(NULL, blob, NULL, label, outfile);
187 got_object_close(obj);
189 got_object_blob_close(blob);
193 static const struct got_error *
194 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
195 const char *label1, const char *label2, struct got_repository *repo,
198 const struct got_error *err;
199 struct got_object *obj1 = NULL;
200 struct got_object *obj2 = NULL;
201 struct got_blob_object *blob1 = NULL;
202 struct got_blob_object *blob2 = NULL;
204 err = got_object_open(&obj1, repo, id1);
207 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
208 err = got_error(GOT_ERR_OBJ_TYPE);
212 err = got_object_open(&obj2, repo, id2);
215 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
216 err = got_error(GOT_ERR_BAD_OBJ_DATA);
220 err = got_object_blob_open(&blob1, repo, obj1, 8192);
224 err = got_object_blob_open(&blob2, repo, obj2, 8192);
228 err = got_diff_blob(blob1, blob2, label1, label2, outfile);
232 got_object_close(obj1);
234 got_object_close(obj2);
236 got_object_blob_close(blob1);
238 got_object_blob_close(blob2);
242 static const struct got_error *
243 diff_deleted_blob(struct got_object_id *id, const char *label,
244 struct got_repository *repo, FILE *outfile)
246 const struct got_error *err;
247 struct got_blob_object *blob = NULL;
248 struct got_object *obj = NULL;
250 err = got_object_open(&obj, repo, id);
254 err = got_object_blob_open(&blob, repo, obj, 8192);
257 err = got_diff_blob(blob, NULL, label, NULL, outfile);
259 got_object_close(obj);
261 got_object_blob_close(blob);
265 static const struct got_error *
266 diff_added_tree(struct got_object_id *id, const char *label,
267 struct got_repository *repo, FILE *outfile)
269 const struct got_error *err = NULL;
270 struct got_object *treeobj = NULL;
271 struct got_tree_object *tree = NULL;
273 err = got_object_open(&treeobj, repo, id);
277 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
278 err = got_error(GOT_ERR_OBJ_TYPE);
282 err = got_object_tree_open(&tree, repo, treeobj);
286 err = got_diff_tree(NULL, tree, NULL, label, repo, outfile);
290 got_object_tree_close(tree);
292 got_object_close(treeobj);
296 static const struct got_error *
297 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
298 const char *label1, const char *label2, struct got_repository *repo,
301 const struct got_error *err;
302 struct got_object *treeobj1 = NULL;
303 struct got_object *treeobj2 = NULL;
304 struct got_tree_object *tree1 = NULL;
305 struct got_tree_object *tree2 = NULL;
307 err = got_object_open(&treeobj1, repo, id1);
311 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
312 err = got_error(GOT_ERR_OBJ_TYPE);
316 err = got_object_open(&treeobj2, repo, id2);
320 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
321 err = got_error(GOT_ERR_OBJ_TYPE);
325 err = got_object_tree_open(&tree1, repo, treeobj1);
329 err = got_object_tree_open(&tree2, repo, treeobj2);
333 err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
337 got_object_tree_close(tree1);
339 got_object_tree_close(tree2);
341 got_object_close(treeobj1);
343 got_object_close(treeobj2);
347 static const struct got_error *
348 diff_deleted_tree(struct got_object_id *id, const char *label,
349 struct got_repository *repo, FILE *outfile)
351 const struct got_error *err;
352 struct got_object *treeobj = NULL;
353 struct got_tree_object *tree = NULL;
355 err = got_object_open(&treeobj, repo, id);
359 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
360 err = got_error(GOT_ERR_OBJ_TYPE);
364 err = got_object_tree_open(&tree, repo, treeobj);
368 err = got_diff_tree(tree, NULL, label, NULL, repo, outfile);
371 got_object_tree_close(tree);
373 got_object_close(treeobj);
377 static const struct got_error *
378 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
379 const char *label1, const char *label2, FILE *outfile)
385 static const struct got_error *
386 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
387 const char *label1, const char *label2, struct got_repository *repo,
390 const struct got_error *err = NULL;
393 if (S_ISDIR(te1->mode))
394 err = diff_deleted_tree(te1->id, label1, repo, outfile);
396 err = diff_deleted_blob(te1->id, label1, repo, outfile);
400 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
401 if (got_object_id_cmp(te1->id, te2->id) != 0)
402 return diff_modified_tree(te1->id, te2->id,
403 label1, label2, repo, outfile);
404 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
405 if (got_object_id_cmp(te1->id, te2->id) != 0)
406 return diff_modified_blob(te1->id, te2->id,
407 label1, label2, repo, outfile);
410 if (got_object_id_cmp(te1->id, te2->id) == 0)
413 return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
416 static const struct got_error *
417 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
418 const char *label2, struct got_repository *repo, FILE *outfile)
420 if (te1 != NULL) /* handled by diff_entry_old_new() */
423 if (S_ISDIR(te2->mode))
424 return diff_added_tree(te2->id, label2, repo, outfile);
426 return diff_added_blob(te2->id, label2, repo, outfile);
429 const struct got_error *
430 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
431 const char *label1, const char *label2, struct got_repository *repo,
434 const struct got_error *err = NULL;
435 struct got_tree_entry *te1 = NULL;
436 struct got_tree_entry *te2 = NULL;
437 char *l1 = NULL, *l2 = NULL;
440 const struct got_tree_entries *entries;
441 entries = got_object_tree_get_entries(tree1);
442 te1 = SIMPLEQ_FIRST(&entries->head);
443 if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
445 return got_error_from_errno();
448 const struct got_tree_entries *entries;
449 entries = got_object_tree_get_entries(tree2);
450 te2 = SIMPLEQ_FIRST(&entries->head);
451 if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
453 return got_error_from_errno();
458 struct got_tree_entry *te = NULL;
460 te = match_entry_by_name(te1, tree2);
464 if (te && asprintf(&l2, "%s%s%s", label2,
465 label2[0] ? "/" : "", te->name) == -1)
466 return got_error_from_errno();
468 err = diff_entry_old_new(te1, te, l1, l2, repo,
475 struct got_tree_entry *te = NULL;
477 te = match_entry_by_name(te2, tree1);
478 err = diff_entry_new_old(te2, te, l2, repo, outfile);
486 te1 = SIMPLEQ_NEXT(te1, entry);
488 asprintf(&l1, "%s%s%s", label1,
489 label1[0] ? "/" : "", te1->name) == -1)
490 return got_error_from_errno();
495 te2 = SIMPLEQ_NEXT(te2, entry);
497 asprintf(&l2, "%s%s%s", label2,
498 label2[0] ? "/" : "", te2->name) == -1)
499 return got_error_from_errno();
501 } while (te1 || te2);
506 const struct got_error *
507 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
508 const char *label1, const char *label2, struct got_repository *repo,
511 const struct got_error *err;
512 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
514 if (obj1 == NULL && obj2 == NULL)
515 return got_error(GOT_ERR_NO_OBJ);
518 err = got_object_blob_open(&blob1, repo, obj1, 8192);
523 err = got_object_blob_open(&blob2, repo, obj2, 8192);
527 err = got_diff_blob(blob1, blob2, label1, label2, outfile);
530 got_object_blob_close(blob1);
532 got_object_blob_close(blob2);
536 const struct got_error *
537 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
538 char *label1, char *label2, struct got_repository *repo, FILE *outfile)
540 const struct got_error *err;
541 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
543 if (obj1 == NULL && obj2 == NULL)
544 return got_error(GOT_ERR_NO_OBJ);
547 err = got_object_tree_open(&tree1, repo, obj1);
552 err = got_object_tree_open(&tree2, repo, obj2);
556 err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
559 got_object_tree_close(tree1);
561 got_object_tree_close(tree2);
566 get_datestr(time_t *time, char *datebuf)
568 char *p, *s = ctime_r(time, datebuf);
575 const struct got_error *
576 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
577 struct got_repository *repo, FILE *outfile)
579 const struct got_error *err;
580 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
581 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
587 return got_error(GOT_ERR_NO_OBJ);
590 err = got_object_commit_open(&commit1, repo, obj1);
593 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
598 err = got_object_commit_open(&commit2, repo, obj2);
601 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
604 err = got_object_get_id_str(&id_str, obj2);
607 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
608 err = got_error_from_errno();
613 if (fprintf(outfile, "from: %s\n", commit2->author) < 0) {
614 err = got_error_from_errno();
617 time = mktime(&commit2->tm_committer);
618 if (fprintf(outfile, "date: %s UTC\n",
619 get_datestr(&time, datebuf)) < 0) {
620 err = got_error_from_errno();
623 if (strcmp(commit2->author, commit2->committer) != 0 &&
624 fprintf(outfile, "via: %s\n", commit2->committer) < 0) {
625 err = got_error_from_errno();
628 if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
629 err = got_error_from_errno();
633 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo,
637 got_object_close(tree_obj1);
639 got_object_close(tree_obj2);
641 got_object_commit_close(commit1);
643 got_object_commit_close(commit2);