commit 11528a829855823153f2aaa9438bdf400a2995d7 from: Stefan Sperling date: Sat May 19 14:28:32 2018 UTC move common diff helpers from got and tog into lib/diff.c commit - 26ed57b23d183a3ea7aa46e7509f935e35e7554e commit + 11528a829855823153f2aaa9438bdf400a2995d7 blob - c8a1143bff915c1c1c59d727dfcef953b0a84956 blob + a4134598a23ddc10569bfd34fb1e4250e3385913 --- got/got.c +++ got/got.c @@ -519,90 +519,8 @@ cmd_log(int argc, char *argv[]) free(id); got_repo_close(repo); return error; -} - -static const struct got_error * -diff_blobs(struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo) -{ - const struct got_error *err; - struct got_blob_object *blob1 = NULL, *blob2 = NULL; - - err = got_object_blob_open(&blob1, repo, obj1, 8192); - if (err) - goto done; - err = got_object_blob_open(&blob2, repo, obj2, 8192); - if (err) - goto done; - - err = got_diff_blob(blob1, blob2, NULL, NULL, stdout); -done: - if (blob1) - got_object_blob_close(blob1); - if (blob2) - got_object_blob_close(blob2); - return err; -} - -static const struct got_error * -diff_trees(struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo) -{ - const struct got_error *err; - struct got_tree_object *tree1 = NULL, *tree2 = NULL; - - err = got_object_tree_open(&tree1, repo, obj1); - if (err) - goto done; - err = got_object_tree_open(&tree2, repo, obj2); - if (err) - goto done; - - err = got_diff_tree(tree1, tree2, repo, stdout); -done: - if (tree1) - got_object_tree_close(tree1); - if (tree2) - got_object_tree_close(tree2); - return err; } -static const struct got_error * -diff_commits(struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo) -{ - const struct got_error *err; - struct got_commit_object *commit1 = NULL, *commit2 = NULL; - struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL; - - err = got_object_commit_open(&commit1, repo, obj1); - if (err) - goto done; - err = got_object_commit_open(&commit2, repo, obj2); - if (err) - goto done; - - err = got_object_open(&tree_obj1, repo, commit1->tree_id); - if (err) - goto done; - err = got_object_open(&tree_obj2, repo, commit2->tree_id); - if (err) - goto done; - - err = diff_trees(tree_obj1, tree_obj2, repo); -done: - if (tree_obj1) - got_object_close(tree_obj1); - if (tree_obj2) - got_object_close(tree_obj2); - if (commit1) - got_object_commit_close(commit1); - if (commit2) - got_object_commit_close(commit2); - return err; - -} - __dead void usage_diff(void) { @@ -685,13 +603,13 @@ cmd_diff(int argc, char *argv[]) switch (got_object_get_type(obj1)) { case GOT_OBJ_TYPE_BLOB: - error = diff_blobs(obj1, obj2, repo); + error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout); break; case GOT_OBJ_TYPE_TREE: - error = diff_trees(obj1, obj2, repo); + error = got_diff_objects_as_trees(obj1, obj2, repo, stdout); break; case GOT_OBJ_TYPE_COMMIT: - error = diff_commits(obj1, obj2, repo); + error = got_diff_objects_as_commits(obj1, obj2, repo, stdout); break; default: error = got_error(GOT_ERR_OBJ_TYPE); blob - 4673b9c5bf89bc6b2ed60ed83a5a1ee337865617 blob + 0f04bd8dc24610ab8b6aa4a71934829503df5230 --- include/got_diff.h +++ include/got_diff.h @@ -29,3 +29,24 @@ const struct got_error *got_diff_blob(struct got_blob_ */ const struct got_error *got_diff_tree(struct got_tree_object *, struct got_tree_object *, struct got_repository *, FILE *); + +/* + * Diff two objects, assuming both objects are blobs. + * Write unified diff text to the provided output FILE. + */ +const struct got_error *got_diff_objects_as_blobs(struct got_object *, + struct got_object *, struct got_repository *, FILE *); + +/* + * Diff two objects, assuming both objects are trees. + * Write unified diff text to the provided output FILE. + */ +const struct got_error *got_diff_objects_as_trees(struct got_object *, + struct got_object *, struct got_repository *, FILE *); + +/* + * Diff two objects, assuming both objects are commits. + * Write unified diff text to the provided output FILE. + */ +const struct got_error *got_diff_objects_as_commits(struct got_object *, + struct got_object *, struct got_repository *, FILE *); blob - 2edc9fdd16423ddcf986299d5139b69fdf772c6d blob + 85c538615c4936d03916032ae8518959958e942f --- lib/diff.c +++ lib/diff.c @@ -436,6 +436,87 @@ got_diff_tree(struct got_tree_object *tree1, struct go if (te2) te2 = SIMPLEQ_NEXT(te2, entry); } while (te1 || te2); + + return err; +} + +const struct got_error * +got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2, + struct got_repository *repo, FILE *outfile) +{ + const struct got_error *err; + struct got_blob_object *blob1 = NULL, *blob2 = NULL; + + err = got_object_blob_open(&blob1, repo, obj1, 8192); + if (err) + goto done; + err = got_object_blob_open(&blob2, repo, obj2, 8192); + if (err) + goto done; + err = got_diff_blob(blob1, blob2, NULL, NULL, outfile); +done: + if (blob1) + got_object_blob_close(blob1); + if (blob2) + got_object_blob_close(blob2); return err; } + +const struct got_error * +got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2, + struct got_repository *repo, FILE *outfile) +{ + const struct got_error *err; + struct got_tree_object *tree1 = NULL, *tree2 = NULL; + + err = got_object_tree_open(&tree1, repo, obj1); + if (err) + goto done; + err = got_object_tree_open(&tree2, repo, obj2); + if (err) + goto done; + + err = got_diff_tree(tree1, tree2, repo, outfile); +done: + if (tree1) + got_object_tree_close(tree1); + if (tree2) + got_object_tree_close(tree2); + return err; +} + +const struct got_error * +got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2, + struct got_repository *repo, FILE *outfile) +{ + const struct got_error *err; + struct got_commit_object *commit1 = NULL, *commit2 = NULL; + struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL; + + err = got_object_commit_open(&commit1, repo, obj1); + if (err) + goto done; + err = got_object_commit_open(&commit2, repo, obj2); + if (err) + goto done; + + err = got_object_open(&tree_obj1, repo, commit1->tree_id); + if (err) + goto done; + err = got_object_open(&tree_obj2, repo, commit2->tree_id); + if (err) + goto done; + + err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile); +done: + if (tree_obj1) + got_object_close(tree_obj1); + if (tree_obj2) + got_object_close(tree_obj2); + if (commit1) + got_object_commit_close(commit1); + if (commit2) + got_object_commit_close(commit2); + return err; +} blob - beae1ba444da519f2b75a4992c9b4d1316a2775c blob + 1eeab3c01b4b597a967bbbbd27de4658259cedac --- tog/tog.c +++ tog/tog.c @@ -776,90 +776,8 @@ usage_diff(void) fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n", getprogname()); exit(1); -} - -static const struct got_error * -diff_blobs(FILE *outfile, struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo) -{ - const struct got_error *err; - struct got_blob_object *blob1 = NULL, *blob2 = NULL; - - err = got_object_blob_open(&blob1, repo, obj1, 8192); - if (err) - goto done; - err = got_object_blob_open(&blob2, repo, obj2, 8192); - if (err) - goto done; - - err = got_diff_blob(blob1, blob2, NULL, NULL, outfile); -done: - if (blob1) - got_object_blob_close(blob1); - if (blob2) - got_object_blob_close(blob2); - return err; -} - -static const struct got_error * -diff_trees(FILE *outfile, struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo) -{ - const struct got_error *err; - struct got_tree_object *tree1 = NULL, *tree2 = NULL; - - err = got_object_tree_open(&tree1, repo, obj1); - if (err) - goto done; - err = got_object_tree_open(&tree2, repo, obj2); - if (err) - goto done; - - err = got_diff_tree(tree1, tree2, repo, outfile); -done: - if (tree1) - got_object_tree_close(tree1); - if (tree2) - got_object_tree_close(tree2); - return err; } -static const struct got_error * -diff_commits(FILE *outfile, struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo) -{ - const struct got_error *err; - struct got_commit_object *commit1 = NULL, *commit2 = NULL; - struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL; - - err = got_object_commit_open(&commit1, repo, obj1); - if (err) - goto done; - err = got_object_commit_open(&commit2, repo, obj2); - if (err) - goto done; - - err = got_object_open(&tree_obj1, repo, commit1->tree_id); - if (err) - goto done; - err = got_object_open(&tree_obj2, repo, commit2->tree_id); - if (err) - goto done; - - err = diff_trees(outfile, tree_obj1, tree_obj2, repo); -done: - if (tree_obj1) - got_object_close(tree_obj1); - if (tree_obj2) - got_object_close(tree_obj2); - if (commit1) - got_object_commit_close(commit1); - if (commit2) - got_object_commit_close(commit2); - return err; - -} - const struct got_error * draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line, int *eof, int max_lines) @@ -920,13 +838,13 @@ show_diff_view(struct got_object *obj1, struct got_obj switch (got_object_get_type(obj1)) { case GOT_OBJ_TYPE_BLOB: - err = diff_blobs(f, obj1, obj2, repo); + err = got_diff_objects_as_blobs(obj1, obj2, repo, f); break; case GOT_OBJ_TYPE_TREE: - err = diff_trees(f, obj1, obj2, repo); + err = got_diff_objects_as_trees(obj1, obj2, repo, f); break; case GOT_OBJ_TYPE_COMMIT: - err = diff_commits(f, obj1, obj2, repo); + err = got_diff_objects_as_commits(obj1, obj2, repo, f); break; default: return got_error(GOT_ERR_OBJ_TYPE);