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, f1, blob1);
78 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
79 err = got_object_blob_dump_to_file(&size2, 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 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
113 const struct got_error *
114 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
115 const char *label1, const char *label2, FILE *outfile)
117 return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
120 const struct got_error *
121 got_diff_blob_lines_changed(struct got_diff_changes **changes,
122 struct got_blob_object *blob1, struct got_blob_object *blob2)
124 const struct got_error *err = NULL;
126 *changes = calloc(1, sizeof(**changes));
127 if (*changes == NULL)
128 return got_error_from_errno();
129 SIMPLEQ_INIT(&(*changes)->entries);
131 err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
133 got_diff_free_changes(*changes);
140 got_diff_free_changes(struct got_diff_changes *changes)
142 struct got_diff_change *change;
143 while (!SIMPLEQ_EMPTY(&changes->entries)) {
144 change = SIMPLEQ_FIRST(&changes->entries);
145 SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
151 struct got_tree_entry *
152 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
154 struct got_tree_entry *te2;
156 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
157 if (strcmp(te1->name, te2->name) == 0)
163 static const struct got_error *
164 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
167 const struct got_error *err;
168 struct got_blob_object *blob = NULL;
169 struct got_object *obj = NULL;
171 err = got_object_open(&obj, repo, id);
175 err = got_object_blob_open(&blob, repo, obj, 8192);
178 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
180 got_object_close(obj);
182 got_object_blob_close(blob);
186 static const struct got_error *
187 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
188 struct got_repository *repo, FILE *outfile)
190 const struct got_error *err;
191 struct got_object *obj1 = NULL;
192 struct got_object *obj2 = NULL;
193 struct got_blob_object *blob1 = NULL;
194 struct got_blob_object *blob2 = NULL;
196 err = got_object_open(&obj1, repo, id1);
199 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
200 err = got_error(GOT_ERR_OBJ_TYPE);
204 err = got_object_open(&obj2, repo, id2);
207 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
208 err = got_error(GOT_ERR_BAD_OBJ_DATA);
212 err = got_object_blob_open(&blob1, repo, obj1, 8192);
216 err = got_object_blob_open(&blob2, repo, obj2, 8192);
220 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
224 got_object_close(obj1);
226 got_object_close(obj2);
228 got_object_blob_close(blob1);
230 got_object_blob_close(blob2);
234 static const struct got_error *
235 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
238 const struct got_error *err;
239 struct got_blob_object *blob = NULL;
240 struct got_object *obj = NULL;
242 err = got_object_open(&obj, repo, id);
246 err = got_object_blob_open(&blob, repo, obj, 8192);
249 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
251 got_object_close(obj);
253 got_object_blob_close(blob);
257 static const struct got_error *
258 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
261 const struct got_error *err = NULL;
262 struct got_object *treeobj = NULL;
263 struct got_tree_object *tree = NULL;
265 err = got_object_open(&treeobj, repo, id);
269 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
270 err = got_error(GOT_ERR_OBJ_TYPE);
274 err = got_object_tree_open(&tree, repo, treeobj);
278 err = got_diff_tree(NULL, tree, repo, outfile);
282 got_object_tree_close(tree);
284 got_object_close(treeobj);
288 static const struct got_error *
289 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
290 struct got_repository *repo, FILE *outfile)
292 const struct got_error *err = NULL;
293 struct got_object *treeobj1 = NULL;
294 struct got_object *treeobj2 = NULL;
295 struct got_tree_object *tree1 = NULL;
296 struct got_tree_object *tree2 = NULL;
298 err = got_object_open(&treeobj1, repo, id1);
302 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
307 err = got_object_open(&treeobj2, repo, id2);
311 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
312 err = got_error(GOT_ERR_OBJ_TYPE);
316 err = got_object_tree_open(&tree1, repo, treeobj1);
320 err = got_object_tree_open(&tree2, repo, treeobj2);
324 err = got_diff_tree(tree1, tree2, repo, outfile);
328 got_object_tree_close(tree1);
330 got_object_tree_close(tree2);
332 got_object_close(treeobj1);
334 got_object_close(treeobj2);
338 static const struct got_error *
339 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
341 const struct got_error *err = NULL;
342 struct got_object *treeobj = NULL;
343 struct got_tree_object *tree = NULL;
345 err = got_object_open(&treeobj, repo, id);
349 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
350 err = got_error(GOT_ERR_OBJ_TYPE);
354 err = got_object_tree_open(&tree, repo, treeobj);
358 err = got_diff_tree(tree, NULL, repo, outfile);
362 got_object_tree_close(tree);
364 got_object_close(treeobj);
368 static const struct got_error *
369 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
376 static const struct got_error *
377 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
378 struct got_repository *repo, FILE *outfile)
380 struct got_tree_entry *te2 = NULL;
383 te2 = match_entry_by_name(te1, tree2);
385 if (S_ISDIR(te1->mode))
386 return diff_deleted_tree(te1->id, repo, outfile);
387 return diff_deleted_blob(te1->id, repo, outfile);
390 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
391 if (got_object_id_cmp(te1->id, te2->id) != 0)
392 return diff_modified_tree(te1->id, te2->id, repo,
394 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
395 if (got_object_id_cmp(te1->id, te2->id) != 0)
396 return diff_modified_blob(te1->id, te2->id, repo,
400 return diff_kind_mismatch(te1->id, te2->id, outfile);
403 static const struct got_error *
404 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
405 struct got_repository *repo, FILE *outfile)
408 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
409 if (te1 != NULL) /* handled by diff_entry_old_new() */
413 if (S_ISDIR(te2->mode))
414 return diff_added_tree(te2->id, repo, outfile);
415 return diff_added_blob(te2->id, repo, outfile);
418 const struct got_error *
419 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
420 struct got_repository *repo, FILE *outfile)
422 const struct got_error *err = NULL;
423 struct got_tree_entry *te1 = NULL;
424 struct got_tree_entry *te2 = NULL;
427 te1 = SIMPLEQ_FIRST(&tree1->entries);
429 te2 = SIMPLEQ_FIRST(&tree2->entries);
433 err = diff_entry_old_new(te1, tree2, repo, outfile);
439 err = diff_entry_new_old(te2, tree1, repo, outfile);
445 te1 = SIMPLEQ_NEXT(te1, entry);
447 te2 = SIMPLEQ_NEXT(te2, entry);
448 } while (te1 || te2);
453 const struct got_error *
454 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
455 struct got_repository *repo, FILE *outfile)
457 const struct got_error *err;
458 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
460 if (obj1 == NULL && obj2 == NULL)
461 return got_error(GOT_ERR_NO_OBJ);
464 err = got_object_blob_open(&blob1, repo, obj1, 8192);
469 err = got_object_blob_open(&blob2, repo, obj2, 8192);
473 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
476 got_object_blob_close(blob1);
478 got_object_blob_close(blob2);
482 const struct got_error *
483 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
484 struct got_repository *repo, FILE *outfile)
486 const struct got_error *err;
487 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
489 if (obj1 == NULL && obj2 == NULL)
490 return got_error(GOT_ERR_NO_OBJ);
493 err = got_object_tree_open(&tree1, repo, obj1);
498 err = got_object_tree_open(&tree2, repo, obj2);
502 err = got_diff_tree(tree1, tree2, repo, outfile);
505 got_object_tree_close(tree1);
507 got_object_tree_close(tree2);
512 get_datestr(time_t *time, char *datebuf)
514 char *p, *s = ctime_r(time, datebuf);
521 const struct got_error *
522 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
523 struct got_repository *repo, FILE *outfile)
525 const struct got_error *err;
526 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
527 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
533 return got_error(GOT_ERR_NO_OBJ);
536 err = got_object_commit_open(&commit1, repo, obj1);
539 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
544 err = got_object_commit_open(&commit2, repo, obj2);
547 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
550 err = got_object_get_id_str(&id_str, obj2);
553 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
554 err = got_error_from_errno();
559 time = mktime(&commit2->tm_author);
560 if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
561 get_datestr(&time, datebuf)) < 0) {
562 err = got_error_from_errno();
565 time = mktime(&commit2->tm_committer);
566 if (strcmp(commit2->author, commit2->committer) != 0 &&
567 fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
568 get_datestr(&time, datebuf)) < 0) {
569 err = got_error_from_errno();
572 if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
573 err = got_error_from_errno();
577 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
580 got_object_close(tree_obj1);
582 got_object_close(tree_obj2);
584 got_object_commit_close(commit1);
586 got_object_commit_close(commit2);