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 | |
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')
-rw-r--r-- | tests-clar/clar_libgit2.c | 59 | ||||
-rw-r--r-- | tests-clar/clar_libgit2.h | 8 | ||||
-rw-r--r-- | tests-clar/diff/submodules.c | 25 | ||||
-rw-r--r-- | tests-clar/index/addall.c | 35 | ||||
-rw-r--r-- | tests-clar/repo/init.c | 5 | ||||
-rw-r--r-- | tests-clar/stash/drop.c | 16 | ||||
-rw-r--r-- | tests-clar/stash/save.c | 5 | ||||
-rw-r--r-- | tests-clar/stash/stash_helpers.c | 33 | ||||
-rw-r--r-- | tests-clar/stash/stash_helpers.h | 5 | ||||
-rw-r--r-- | tests-clar/status/worktree.c | 25 | ||||
-rw-r--r-- | tests-clar/stress/diff.c | 24 |
11 files changed, 93 insertions, 147 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; diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h index 76299e4e3..f2d9c4d0b 100644 --- a/tests-clar/clar_libgit2.h +++ b/tests-clar/clar_libgit2.h @@ -98,6 +98,14 @@ const char* cl_git_path_url(const char *path); /* Test repository cleaner */ int cl_git_remove_placeholders(const char *directory_path, const char *filename); +/* commit creation helpers */ +void cl_repo_commit_from_index( + git_oid *out, + git_repository *repo, + git_signature *sig, + git_time_t time, + const char *msg); + /* config setting helpers */ void cl_repo_set_bool(git_repository *repo, const char *cfg, int value); int cl_repo_get_bool(git_repository *repo, const char *cfg); diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c index 167dedfc6..036ff09aa 100644 --- a/tests-clar/diff/submodules.c +++ b/tests-clar/diff/submodules.c @@ -228,11 +228,11 @@ void test_diff_submodules__invalid_cache(void) "<END>" }; static const char *expected_moved[] = { - "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..0910a13 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 0910a13dfa2210496f6c590d75bc360dd11b2a1b\n", + "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..7002348 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 700234833f6ccc20d744b238612646be071acaae\n", "<END>" }; static const char *expected_moved_dirty[] = { - "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..0910a13 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 0910a13dfa2210496f6c590d75bc360dd11b2a1b-dirty\n", + "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..7002348 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 700234833f6ccc20d744b238612646be071acaae-dirty\n", "<END>" }; @@ -309,26 +309,7 @@ void test_diff_submodules__invalid_cache(void) git_diff_list_free(diff); /* commit changed index of submodule */ - { - git_object *parent; - git_oid tree_id, commit_id; - git_tree *tree; - git_signature *sig; - git_reference *ref; - - cl_git_pass(git_revparse_ext(&parent, &ref, smrepo, "HEAD")); - cl_git_pass(git_index_write_tree(&tree_id, smindex)); - cl_git_pass(git_index_write(smindex)); - cl_git_pass(git_tree_lookup(&tree, smrepo, &tree_id)); - cl_git_pass(git_signature_new(&sig, "Sm Test", "sm@tester.test", 1372350000, 480)); - cl_git_pass(git_commit_create_v( - &commit_id, smrepo, git_reference_name(ref), sig, sig, - NULL, "Move it", tree, 1, parent)); - git_object_free(parent); - git_tree_free(tree); - git_reference_free(ref); - git_signature_free(sig); - } + cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Move it"); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_DIRTY); diff --git a/tests-clar/index/addall.c b/tests-clar/index/addall.c index 00388ee00..f46a1e16c 100644 --- a/tests-clar/index/addall.c +++ b/tests-clar/index/addall.c @@ -120,37 +120,6 @@ static void check_stat_data(git_index *index, const char *path, bool match) } } -static void commit_index_to_head( - git_repository *repo, - const char *commit_message) -{ - git_index *index; - git_oid tree_id, commit_id; - git_tree *tree; - git_signature *sig; - git_commit *parent = NULL; - - git_revparse_single((git_object **)&parent, repo, "HEAD"); - /* it is okay if looking up the HEAD fails */ - - 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)); /* not needed, but might as well */ - git_index_free(index); - - cl_git_pass(git_tree_lookup(&tree, repo, &tree_id)); - - cl_git_pass(git_signature_now(&sig, "Testy McTester", "tt@tester.test")); - - cl_git_pass(git_commit_create_v( - &commit_id, repo, "HEAD", sig, sig, - NULL, commit_message, tree, parent ? 1 : 0, parent)); - - git_commit_free(parent); - git_tree_free(tree); - git_signature_free(sig); -} - void test_index_addall__repo_lifecycle(void) { int error; @@ -197,7 +166,7 @@ void test_index_addall__repo_lifecycle(void) check_stat_data(index, "addall/file.zzz", true); check_status(g_repo, 2, 0, 0, 3, 0, 0, 1); - commit_index_to_head(g_repo, "first commit"); + cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit"); check_status(g_repo, 0, 0, 0, 3, 0, 0, 1); /* attempt to add an ignored file - does nothing */ @@ -244,7 +213,7 @@ void test_index_addall__repo_lifecycle(void) cl_git_pass(git_index_add_bypath(index, "file.zzz")); check_status(g_repo, 1, 0, 1, 3, 0, 0, 0); - commit_index_to_head(g_repo, "second commit"); + cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit"); check_status(g_repo, 0, 0, 0, 3, 0, 0, 0); cl_must_pass(p_unlink("addall/file.zzz")); diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c index e3fc112b3..caa211e75 100644 --- a/tests-clar/repo/init.c +++ b/tests-clar/repo/init.c @@ -565,6 +565,11 @@ void test_repo_init__init_with_initial_commit(void) cl_git_pass(git_index_add_bypath(index, "file.txt")); cl_git_pass(git_index_write(index)); + /* Intentionally not using cl_repo_commit_from_index here so this code + * can be used as an example of how an initial commit is typically + * made to a repository... + */ + /* Make sure we're ready to use git_signature_default :-) */ { git_config *cfg, *local; diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c index 60b3c72e0..59413f01e 100644 --- a/tests-clar/stash/drop.c +++ b/tests-clar/stash/drop.c @@ -36,25 +36,27 @@ static void push_three_states(void) cl_git_mkfile("stash/zero.txt", "content\n"); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_add_bypath(index, "zero.txt")); - commit_staged_files(&oid, index, signature); + cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); cl_assert(git_path_exists("stash/zero.txt")); + git_index_free(index); cl_git_mkfile("stash/one.txt", "content\n"); - cl_git_pass(git_stash_save(&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED)); + cl_git_pass(git_stash_save( + &oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED)); cl_assert(!git_path_exists("stash/one.txt")); cl_assert(git_path_exists("stash/zero.txt")); cl_git_mkfile("stash/two.txt", "content\n"); - cl_git_pass(git_stash_save(&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED)); + cl_git_pass(git_stash_save( + &oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED)); cl_assert(!git_path_exists("stash/two.txt")); cl_assert(git_path_exists("stash/zero.txt")); cl_git_mkfile("stash/three.txt", "content\n"); - cl_git_pass(git_stash_save(&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED)); + cl_git_pass(git_stash_save( + &oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED)); cl_assert(!git_path_exists("stash/three.txt")); cl_assert(git_path_exists("stash/zero.txt")); - - git_index_free(index); } void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void) @@ -160,7 +162,7 @@ void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void) retrieve_top_stash_id(&oid); cl_git_pass(git_revparse_single(&next_top_stash, repo, "stash@{1}")); - cl_assert_equal_i(false, git_oid_cmp(&oid, git_object_id(next_top_stash)) == 0); + cl_assert(git_oid_cmp(&oid, git_object_id(next_top_stash)) != 0); cl_git_pass(git_stash_drop(repo, 0)); diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c index bb35a3d71..035b62279 100644 --- a/tests-clar/stash/save.c +++ b/tests-clar/stash/save.c @@ -241,7 +241,7 @@ void test_stash_save__stashing_updates_the_reflog(void) void test_stash_save__cannot_stash_when_there_are_no_local_change(void) { git_index *index; - git_oid commit_oid, stash_tip_oid; + git_oid stash_tip_oid; cl_git_pass(git_repository_index(&index, repo)); @@ -251,8 +251,7 @@ void test_stash_save__cannot_stash_when_there_are_no_local_change(void) */ cl_git_pass(git_index_add_bypath(index, "what")); cl_git_pass(git_index_add_bypath(index, "who")); - cl_git_pass(git_index_write(index)); - commit_staged_files(&commit_oid, index, signature); + cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); git_index_free(index); cl_assert_equal_i(GIT_ENOTFOUND, diff --git a/tests-clar/stash/stash_helpers.c b/tests-clar/stash/stash_helpers.c index f462a1351..06b63f177 100644 --- a/tests-clar/stash/stash_helpers.c +++ b/tests-clar/stash/stash_helpers.c @@ -2,38 +2,8 @@ #include "fileops.h" #include "stash_helpers.h" -void commit_staged_files( - git_oid *commit_oid, - git_index *index, - git_signature *signature) -{ - git_tree *tree; - git_oid tree_oid; - git_repository *repo; - - repo = git_index_owner(index); - - cl_git_pass(git_index_write_tree(&tree_oid, index)); - - cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); - - cl_git_pass(git_commit_create_v( - commit_oid, - repo, - "HEAD", - signature, - signature, - NULL, - "Initial commit", - tree, - 0)); - - git_tree_free(tree); -} - void setup_stash(git_repository *repo, git_signature *signature) { - git_oid commit_oid; git_index *index; cl_git_pass(git_repository_index(&index, repo)); @@ -50,9 +20,8 @@ void setup_stash(git_repository *repo, git_signature *signature) cl_git_pass(git_index_add_bypath(index, "how")); cl_git_pass(git_index_add_bypath(index, "who")); cl_git_pass(git_index_add_bypath(index, ".gitignore")); - cl_git_pass(git_index_write(index)); - commit_staged_files(&commit_oid, index, signature); + cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); cl_git_rewritefile("stash/what", "goodbye\n"); /* dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 */ cl_git_rewritefile("stash/how", "not so small and\n"); /* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */ diff --git a/tests-clar/stash/stash_helpers.h b/tests-clar/stash/stash_helpers.h index bb7fec4f5..7c3e13de3 100644 --- a/tests-clar/stash/stash_helpers.h +++ b/tests-clar/stash/stash_helpers.h @@ -1,8 +1,3 @@ void setup_stash( git_repository *repo, git_signature *signature); - -void commit_staged_files( - git_oid *commit_oid, - git_index *index, - git_signature *signature);
\ No newline at end of file diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c index be7398cb6..135a95871 100644 --- a/tests-clar/status/worktree.c +++ b/tests-clar/status/worktree.c @@ -632,35 +632,12 @@ void test_status_worktree__conflicted_item(void) static void stage_and_commit(git_repository *repo, const char *path) { - git_oid tree_oid, commit_oid; - git_tree *tree; - git_signature *signature; git_index *index; cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_add_bypath(index, path)); - cl_git_pass(git_index_write(index)); - - cl_git_pass(git_index_write_tree(&tree_oid, index)); + cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n"); git_index_free(index); - - cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); - - cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); - - cl_git_pass(git_commit_create_v( - &commit_oid, - repo, - "HEAD", - signature, - signature, - NULL, - "Initial commit\n\0", - tree, - 0)); - - git_tree_free(tree); - git_signature_free(signature); } static void assert_ignore_case( diff --git a/tests-clar/stress/diff.c b/tests-clar/stress/diff.c index 0524aa108..1d319738e 100644 --- a/tests-clar/stress/diff.c +++ b/tests-clar/stress/diff.c @@ -54,27 +54,9 @@ static void test_with_many(int expected_new) git_diff_list_free(diff); - { - git_object *parent; - git_signature *sig; - git_oid tree_id, commit_id; - git_reference *ref; - - cl_git_pass(git_index_write_tree(&tree_id, index)); - cl_git_pass(git_tree_lookup(&new_tree, g_repo, &tree_id)); - - cl_git_pass(git_revparse_ext(&parent, &ref, g_repo, "HEAD")); - cl_git_pass(git_signature_new( - &sig, "Sm Test", "sm@tester.test", 1372350000, 480)); - - cl_git_pass(git_commit_create_v( - &commit_id, g_repo, git_reference_name(ref), sig, sig, - NULL, "yoyoyo", new_tree, 1, parent)); - - git_object_free(parent); - git_reference_free(ref); - git_signature_free(sig); - } + cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "yoyoyo"); + cl_git_pass(git_revparse_single( + (git_object **)&new_tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree, new_tree, &diffopts)); |