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;
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 hdrlen = got_object_blob_get_hdrlen(blob1);
72 err = got_object_blob_read_block(&len, blob1);
78 /* Skip blob object header first time around. */
79 fwrite(got_object_blob_get_read_buf(blob1) + hdrlen,
88 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
89 hdrlen = got_object_blob_get_hdrlen(blob2);
91 err = got_object_blob_read_block(&len, blob2);
97 /* Skip blob object header first time around. */
98 fwrite(got_object_blob_get_read_buf(blob2) + hdrlen,
103 idstr2 = "/dev/null";
114 memset(&ds, 0, sizeof(ds));
115 /* XXX should stat buffers be passed in args instead of ds? */
116 ds.stb1.st_mode = S_IFREG;
118 ds.stb1.st_size = size1;
119 ds.stb1.st_mtime = 0; /* XXX */
121 ds.stb2.st_mode = S_IFREG;
123 ds.stb2.st_size = size2;
124 ds.stb2.st_mtime = 0; /* XXX */
126 memset(&args, 0, sizeof(args));
127 args.diff_format = D_UNIFIED;
128 args.label[0] = label1 ? label1 : idstr1;
129 args.label[1] = label2 ? label2 : idstr2;
130 args.diff_context = 3;
131 flags |= D_PROTOTYPE;
133 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
142 struct got_tree_entry *
143 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
145 struct got_tree_entry *te2;
147 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
148 if (strcmp(te1->name, te2->name) == 0)
154 static const struct got_error *
155 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
158 const struct got_error *err;
159 struct got_blob_object *blob = NULL;
160 struct got_object *obj = NULL;
162 err = got_object_open(&obj, repo, id);
166 err = got_object_blob_open(&blob, repo, obj, 8192);
169 err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
171 got_object_close(obj);
173 got_object_blob_close(blob);
177 static const struct got_error *
178 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
179 struct got_repository *repo, FILE *outfile)
181 const struct got_error *err;
182 struct got_object *obj1 = NULL;
183 struct got_object *obj2 = NULL;
184 struct got_blob_object *blob1 = NULL;
185 struct got_blob_object *blob2 = NULL;
187 err = got_object_open(&obj1, repo, id1);
190 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
191 err = got_error(GOT_ERR_OBJ_TYPE);
195 err = got_object_open(&obj2, repo, id2);
198 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
199 err = got_error(GOT_ERR_BAD_OBJ_DATA);
203 err = got_object_blob_open(&blob1, repo, obj1, 8192);
207 err = got_object_blob_open(&blob2, repo, obj2, 8192);
211 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
215 got_object_close(obj1);
217 got_object_close(obj2);
219 got_object_blob_close(blob1);
221 got_object_blob_close(blob2);
225 static const struct got_error *
226 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
229 const struct got_error *err;
230 struct got_blob_object *blob = NULL;
231 struct got_object *obj = NULL;
233 err = got_object_open(&obj, repo, id);
237 err = got_object_blob_open(&blob, repo, obj, 8192);
240 err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
242 got_object_close(obj);
244 got_object_blob_close(blob);
248 static const struct got_error *
249 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
252 const struct got_error *err = NULL;
253 struct got_object *treeobj = NULL;
254 struct got_tree_object *tree = NULL;
256 err = got_object_open(&treeobj, repo, id);
260 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
261 err = got_error(GOT_ERR_OBJ_TYPE);
265 err = got_object_tree_open(&tree, repo, treeobj);
269 err = got_diff_tree(NULL, tree, repo, outfile);
273 got_object_tree_close(tree);
275 got_object_close(treeobj);
279 static const struct got_error *
280 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
281 struct got_repository *repo, FILE *outfile)
283 const struct got_error *err = NULL;
284 struct got_object *treeobj1 = NULL;
285 struct got_object *treeobj2 = NULL;
286 struct got_tree_object *tree1 = NULL;
287 struct got_tree_object *tree2 = NULL;
289 err = got_object_open(&treeobj1, repo, id1);
293 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
294 err = got_error(GOT_ERR_OBJ_TYPE);
298 err = got_object_open(&treeobj2, repo, id2);
302 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
307 err = got_object_tree_open(&tree1, repo, treeobj1);
311 err = got_object_tree_open(&tree2, repo, treeobj2);
315 err = got_diff_tree(tree1, tree2, repo, outfile);
319 got_object_tree_close(tree1);
321 got_object_tree_close(tree2);
323 got_object_close(treeobj1);
325 got_object_close(treeobj2);
329 static const struct got_error *
330 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
332 const struct got_error *err = NULL;
333 struct got_object *treeobj = NULL;
334 struct got_tree_object *tree = NULL;
336 err = got_object_open(&treeobj, repo, id);
340 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 err = got_object_tree_open(&tree, repo, treeobj);
349 err = got_diff_tree(tree, NULL, repo, outfile);
353 got_object_tree_close(tree);
355 got_object_close(treeobj);
359 static const struct got_error *
360 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
367 static const struct got_error *
368 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
369 struct got_repository *repo, FILE *outfile)
371 struct got_tree_entry *te2 = NULL;
374 te2 = match_entry_by_name(te1, tree2);
376 if (S_ISDIR(te1->mode))
377 return diff_deleted_tree(te1->id, repo, outfile);
378 return diff_deleted_blob(te1->id, repo, outfile);
381 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
382 if (got_object_id_cmp(te1->id, te2->id) != 0)
383 return diff_modified_tree(te1->id, te2->id, repo,
385 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
386 if (got_object_id_cmp(te1->id, te2->id) != 0)
387 return diff_modified_blob(te1->id, te2->id, repo,
391 return diff_kind_mismatch(te1->id, te2->id, outfile);
394 static const struct got_error *
395 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
396 struct got_repository *repo, FILE *outfile)
399 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
400 if (te1 != NULL) /* handled by diff_entry_old_new() */
404 if (S_ISDIR(te2->mode))
405 return diff_added_tree(te2->id, repo, outfile);
406 return diff_added_blob(te2->id, repo, outfile);
409 const struct got_error *
410 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
411 struct got_repository *repo, FILE *outfile)
413 const struct got_error *err = NULL;
414 struct got_tree_entry *te1 = NULL;
415 struct got_tree_entry *te2 = NULL;
418 te1 = SIMPLEQ_FIRST(&tree1->entries);
420 te2 = SIMPLEQ_FIRST(&tree2->entries);
424 err = diff_entry_old_new(te1, tree2, repo, outfile);
430 err = diff_entry_new_old(te2, tree1, repo, outfile);
436 te1 = SIMPLEQ_NEXT(te1, entry);
438 te2 = SIMPLEQ_NEXT(te2, entry);
439 } while (te1 || te2);
444 const struct got_error *
445 got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
446 struct got_repository *repo, FILE *outfile)
448 const struct got_error *err;
449 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
451 if (obj1 == NULL && obj2 == NULL)
452 return got_error(GOT_ERR_NO_OBJ);
455 err = got_object_blob_open(&blob1, repo, obj1, 8192);
460 err = got_object_blob_open(&blob2, repo, obj2, 8192);
464 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
467 got_object_blob_close(blob1);
469 got_object_blob_close(blob2);
473 const struct got_error *
474 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
475 struct got_repository *repo, FILE *outfile)
477 const struct got_error *err;
478 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
480 if (obj1 == NULL && obj2 == NULL)
481 return got_error(GOT_ERR_NO_OBJ);
484 err = got_object_tree_open(&tree1, repo, obj1);
489 err = got_object_tree_open(&tree2, repo, obj2);
493 err = got_diff_tree(tree1, tree2, repo, outfile);
496 got_object_tree_close(tree1);
498 got_object_tree_close(tree2);
502 const struct got_error *
503 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
504 struct got_repository *repo, FILE *outfile)
506 const struct got_error *err;
507 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
508 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
512 return got_error(GOT_ERR_NO_OBJ);
515 err = got_object_commit_open(&commit1, repo, obj1);
518 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
523 err = got_object_commit_open(&commit2, repo, obj2);
526 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
529 err = got_object_get_id_str(&id_str, obj2);
532 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
533 err = got_error_from_errno();
538 if (fprintf(outfile, "author: %s\n", commit2->author) < 0) {
539 err = got_error_from_errno();
542 if (strcmp(commit2->author, commit2->committer) != 0 &&
543 fprintf(outfile, "committer: %s\n", commit2->committer) < 0) {
544 err = got_error_from_errno();
547 if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
548 err = got_error_from_errno();
552 err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
555 got_object_close(tree_obj1);
557 got_object_close(tree_obj2);
559 got_object_commit_close(commit1);
561 got_object_commit_close(commit2);