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 const struct got_error *
37 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
38 const char *label1, const char *label2, FILE *outfile)
40 struct got_diff_state ds;
41 struct got_diff_args args;
42 const struct got_error *err = NULL;
43 FILE *f1 = NULL, *f2 = NULL;
44 char hex1[SHA1_DIGEST_STRING_LENGTH];
45 char hex2[SHA1_DIGEST_STRING_LENGTH];
46 char *idstr1 = NULL, *idstr2 = NULL;
53 return got_error(GOT_ERR_FILE_OPEN);
61 return got_error(GOT_ERR_FILE_OPEN);
68 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
69 err = got_object_blob_dump_to_file(&size1, f1, blob1);
77 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
78 err = got_object_blob_dump_to_file(&size2, f2, blob2);
84 memset(&ds, 0, sizeof(ds));
85 /* XXX should stat buffers be passed in args instead of ds? */
86 ds.stb1.st_mode = S_IFREG;
88 ds.stb1.st_size = size1;
89 ds.stb1.st_mtime = 0; /* XXX */
91 ds.stb2.st_mode = S_IFREG;
93 ds.stb2.st_size = size2;
94 ds.stb2.st_mtime = 0; /* XXX */
96 memset(&args, 0, sizeof(args));
97 args.diff_format = D_UNIFIED;
98 args.label[0] = label1 ? label1 : idstr1;
99 args.label[1] = label2 ? label2 : idstr2;
100 args.diff_context = 3;
101 flags |= D_PROTOTYPE;
103 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
112 struct got_tree_entry *
113 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
115 struct got_tree_entry *te2;
117 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
118 if (strcmp(te1->name, te2->name) == 0)
124 static const struct got_error *
125 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
128 const struct got_error *err;
129 struct got_blob_object *blob = NULL;
130 struct got_object *obj = NULL;
132 err = got_object_open(&obj, repo, id);
136 err = got_object_blob_open(&blob, repo, obj, 8192);
139 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
141 got_object_close(obj);
143 got_object_blob_close(blob);
147 static const struct got_error *
148 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
149 struct got_repository *repo, FILE *outfile)
151 const struct got_error *err;
152 struct got_object *obj1 = NULL;
153 struct got_object *obj2 = NULL;
154 struct got_blob_object *blob1 = NULL;
155 struct got_blob_object *blob2 = NULL;
157 err = got_object_open(&obj1, repo, id1);
160 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
161 err = got_error(GOT_ERR_OBJ_TYPE);
165 err = got_object_open(&obj2, repo, id2);
168 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
169 err = got_error(GOT_ERR_BAD_OBJ_DATA);
173 err = got_object_blob_open(&blob1, repo, obj1, 8192);
177 err = got_object_blob_open(&blob2, repo, obj2, 8192);
181 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
185 got_object_close(obj1);
187 got_object_close(obj2);
189 got_object_blob_close(blob1);
191 got_object_blob_close(blob2);
195 static const struct got_error *
196 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
199 const struct got_error *err;
200 struct got_blob_object *blob = NULL;
201 struct got_object *obj = NULL;
203 err = got_object_open(&obj, repo, id);
207 err = got_object_blob_open(&blob, repo, obj, 8192);
210 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
212 got_object_close(obj);
214 got_object_blob_close(blob);
218 static const struct got_error *
219 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
222 const struct got_error *err = NULL;
223 struct got_object *treeobj = NULL;
224 struct got_tree_object *tree = NULL;
226 err = got_object_open(&treeobj, repo, id);
230 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
231 err = got_error(GOT_ERR_OBJ_TYPE);
235 err = got_object_tree_open(&tree, repo, treeobj);
239 err = got_diff_tree(NULL, tree, repo, outfile);
243 got_object_tree_close(tree);
245 got_object_close(treeobj);
249 static const struct got_error *
250 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
251 struct got_repository *repo, FILE *outfile)
253 const struct got_error *err = NULL;
254 struct got_object *treeobj1 = NULL;
255 struct got_object *treeobj2 = NULL;
256 struct got_tree_object *tree1 = NULL;
257 struct got_tree_object *tree2 = NULL;
259 err = got_object_open(&treeobj1, repo, id1);
263 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
264 err = got_error(GOT_ERR_OBJ_TYPE);
268 err = got_object_open(&treeobj2, repo, id2);
272 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
273 err = got_error(GOT_ERR_OBJ_TYPE);
277 err = got_object_tree_open(&tree1, repo, treeobj1);
281 err = got_object_tree_open(&tree2, repo, treeobj2);
285 err = got_diff_tree(tree1, tree2, repo, outfile);
289 got_object_tree_close(tree1);
291 got_object_tree_close(tree2);
293 got_object_close(treeobj1);
295 got_object_close(treeobj2);
299 static const struct got_error *
300 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
302 const struct got_error *err = NULL;
303 struct got_object *treeobj = NULL;
304 struct got_tree_object *tree = NULL;
306 err = got_object_open(&treeobj, repo, id);
310 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
311 err = got_error(GOT_ERR_OBJ_TYPE);
315 err = got_object_tree_open(&tree, repo, treeobj);
319 err = got_diff_tree(tree, NULL, repo, outfile);
323 got_object_tree_close(tree);
325 got_object_close(treeobj);
329 static const struct got_error *
330 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
337 static const struct got_error *
338 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
339 struct got_repository *repo, FILE *outfile)
341 struct got_tree_entry *te2 = NULL;
344 te2 = match_entry_by_name(te1, tree2);
346 if (S_ISDIR(te1->mode))
347 return diff_deleted_tree(te1->id, repo, outfile);
348 return diff_deleted_blob(te1->id, repo, outfile);
351 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
352 if (got_object_id_cmp(te1->id, te2->id) != 0)
353 return diff_modified_tree(te1->id, te2->id, repo,
355 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
356 if (got_object_id_cmp(te1->id, te2->id) != 0)
357 return diff_modified_blob(te1->id, te2->id, repo,
361 return diff_kind_mismatch(te1->id, te2->id, outfile);
364 static const struct got_error *
365 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
366 struct got_repository *repo, FILE *outfile)
369 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
370 if (te1 != NULL) /* handled by diff_entry_old_new() */
374 if (S_ISDIR(te2->mode))
375 return diff_added_tree(te2->id, repo, outfile);
376 return diff_added_blob(te2->id, repo, outfile);
379 const struct got_error *
380 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
381 struct got_repository *repo, FILE *outfile)
383 const struct got_error *err = NULL;
384 struct got_tree_entry *te1 = NULL;
385 struct got_tree_entry *te2 = NULL;
388 te1 = SIMPLEQ_FIRST(&tree1->entries);
390 te2 = SIMPLEQ_FIRST(&tree2->entries);
394 err = diff_entry_old_new(te1, tree2, repo, outfile);
400 err = diff_entry_new_old(te2, tree1, repo, outfile);
406 te1 = SIMPLEQ_NEXT(te1, entry);
408 te2 = SIMPLEQ_NEXT(te2, entry);
409 } while (te1 || te2);
414 const struct got_error *
415 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
416 struct got_repository *repo, FILE *outfile)
418 const struct got_error *err;
419 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
421 if (obj1 == NULL && obj2 == NULL)
422 return got_error(GOT_ERR_NO_OBJ);
425 err = got_object_blob_open(&blob1, repo, obj1, 8192);
430 err = got_object_blob_open(&blob2, repo, obj2, 8192);
434 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
437 got_object_blob_close(blob1);
439 got_object_blob_close(blob2);
443 const struct got_error *
444 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
445 struct got_repository *repo, FILE *outfile)
447 const struct got_error *err;
448 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
450 if (obj1 == NULL && obj2 == NULL)
451 return got_error(GOT_ERR_NO_OBJ);
454 err = got_object_tree_open(&tree1, repo, obj1);
459 err = got_object_tree_open(&tree2, repo, obj2);
463 err = got_diff_tree(tree1, tree2, repo, outfile);
466 got_object_tree_close(tree1);
468 got_object_tree_close(tree2);
473 get_datestr(time_t *time, char *datebuf)
475 char *p, *s = ctime_r(time, datebuf);
482 const struct got_error *
483 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
484 struct got_repository *repo, FILE *outfile)
486 const struct got_error *err;
487 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
488 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
494 return got_error(GOT_ERR_NO_OBJ);
497 err = got_object_commit_open(&commit1, repo, obj1);
500 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
505 err = got_object_commit_open(&commit2, repo, obj2);
508 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
511 err = got_object_get_id_str(&id_str, obj2);
514 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
515 err = got_error_from_errno();
520 time = mktime(&commit2->tm_author);
521 if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
522 get_datestr(&time, datebuf)) < 0) {
523 err = got_error_from_errno();
526 time = mktime(&commit2->tm_committer);
527 if (strcmp(commit2->author, commit2->committer) != 0 &&
528 fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
529 get_datestr(&time, datebuf)) < 0) {
530 err = got_error_from_errno();
533 if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
534 err = got_error_from_errno();
538 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
541 got_object_close(tree_obj1);
543 got_object_close(tree_obj2);
545 got_object_commit_close(commit1);
547 got_object_commit_close(commit2);