diff options
author | Russell Belfer <rb@github.com> | 2013-09-06 02:06:42 +0400 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-09-17 20:31:46 +0400 |
commit | 155fa2342d838bdb2aa873c95a42e091351bb69a (patch) | |
tree | 4441514a2ad0b4cb2c0e088e03864b9d9f7acbe6 /tests-clar/clar_libgit2.c | |
parent | 13f36ffb9e1c4fb70b44a477d716873fecfc0407 (diff) |
Add clar helper to create new commit from index
There were a lot of places in the test code base that were creating
a commit from the index on the current branch. This just adds a
helper to handle that case pretty easily. There was only one test
where this change ended up tweaking the test data, so pretty easy
and mostly just a cleanup.
Diffstat (limited to 'tests-clar/clar_libgit2.c')
-rw-r--r-- | tests-clar/clar_libgit2.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c index 522f73634..4cf682449 100644 --- a/tests-clar/clar_libgit2.c +++ b/tests-clar/clar_libgit2.c @@ -337,6 +337,65 @@ int cl_git_remove_placeholders(const char *directory_path, const char *filename) return error; } +#define CL_COMMIT_NAME "Libgit2 Tester" +#define CL_COMMIT_EMAIL "libgit2-test@github.com" +#define CL_COMMIT_MSG "Test commit of tree " + +void cl_repo_commit_from_index( + git_oid *out, + git_repository *repo, + git_signature *sig, + git_time_t time, + const char *msg) +{ + git_index *index; + git_oid commit_id, tree_id; + git_object *parent = NULL; + git_reference *ref = NULL; + git_tree *tree = NULL; + char buf[128]; + int free_sig = (sig == NULL); + + /* it is fine if looking up HEAD fails - we make this the first commit */ + git_revparse_ext(&parent, &ref, repo, "HEAD"); + + /* write the index content as a tree */ + cl_git_pass(git_repository_index(&index, repo)); + cl_git_pass(git_index_write_tree(&tree_id, index)); + cl_git_pass(git_index_write(index)); + git_index_free(index); + + cl_git_pass(git_tree_lookup(&tree, repo, &tree_id)); + + if (sig) + cl_assert(sig->name && sig->email); + else if (!time) + cl_git_pass(git_signature_now(&sig, CL_COMMIT_NAME, CL_COMMIT_EMAIL)); + else + cl_git_pass(git_signature_new( + &sig, CL_COMMIT_NAME, CL_COMMIT_EMAIL, time, 0)); + + if (!msg) { + strcpy(buf, CL_COMMIT_MSG); + git_oid_tostr(buf + strlen(CL_COMMIT_MSG), + sizeof(buf) - strlen(CL_COMMIT_MSG), &tree_id); + msg = buf; + } + + cl_git_pass(git_commit_create_v( + &commit_id, repo, ref ? git_reference_name(ref) : "HEAD", + sig, sig, NULL, msg, tree, parent ? 1 : 0, parent)); + + if (out) + git_oid_cpy(out, &commit_id); + + git_object_free(parent); + git_reference_free(ref); + if (free_sig) + git_signature_free(sig); + git_tree_free(tree); +} + void cl_repo_set_bool(git_repository *repo, const char *cfg, int value) { git_config *config; |