Commit 155fa234 by Russell Belfer

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.
parent 13f36ffb
...@@ -337,6 +337,65 @@ int cl_git_remove_placeholders(const char *directory_path, const char *filename) ...@@ -337,6 +337,65 @@ int cl_git_remove_placeholders(const char *directory_path, const char *filename)
return error; 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) void cl_repo_set_bool(git_repository *repo, const char *cfg, int value)
{ {
git_config *config; git_config *config;
......
...@@ -98,6 +98,14 @@ const char* cl_git_path_url(const char *path); ...@@ -98,6 +98,14 @@ const char* cl_git_path_url(const char *path);
/* Test repository cleaner */ /* Test repository cleaner */
int cl_git_remove_placeholders(const char *directory_path, const char *filename); 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 */ /* config setting helpers */
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value); void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
int cl_repo_get_bool(git_repository *repo, const char *cfg); int cl_repo_get_bool(git_repository *repo, const char *cfg);
......
...@@ -228,11 +228,11 @@ void test_diff_submodules__invalid_cache(void) ...@@ -228,11 +228,11 @@ void test_diff_submodules__invalid_cache(void)
"<END>" "<END>"
}; };
static const char *expected_moved[] = { 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>" "<END>"
}; };
static const char *expected_moved_dirty[] = { 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>" "<END>"
}; };
...@@ -309,26 +309,7 @@ void test_diff_submodules__invalid_cache(void) ...@@ -309,26 +309,7 @@ void test_diff_submodules__invalid_cache(void)
git_diff_list_free(diff); git_diff_list_free(diff);
/* commit changed index of submodule */ /* commit changed index of submodule */
{ cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Move it");
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);
}
git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_DIRTY); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_DIRTY);
......
...@@ -120,37 +120,6 @@ static void check_stat_data(git_index *index, const char *path, bool match) ...@@ -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) void test_index_addall__repo_lifecycle(void)
{ {
int error; int error;
...@@ -197,7 +166,7 @@ void test_index_addall__repo_lifecycle(void) ...@@ -197,7 +166,7 @@ void test_index_addall__repo_lifecycle(void)
check_stat_data(index, "addall/file.zzz", true); check_stat_data(index, "addall/file.zzz", true);
check_status(g_repo, 2, 0, 0, 3, 0, 0, 1); 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); check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
/* attempt to add an ignored file - does nothing */ /* attempt to add an ignored file - does nothing */
...@@ -244,7 +213,7 @@ void test_index_addall__repo_lifecycle(void) ...@@ -244,7 +213,7 @@ void test_index_addall__repo_lifecycle(void)
cl_git_pass(git_index_add_bypath(index, "file.zzz")); cl_git_pass(git_index_add_bypath(index, "file.zzz"));
check_status(g_repo, 1, 0, 1, 3, 0, 0, 0); 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); check_status(g_repo, 0, 0, 0, 3, 0, 0, 0);
cl_must_pass(p_unlink("addall/file.zzz")); cl_must_pass(p_unlink("addall/file.zzz"));
......
...@@ -565,6 +565,11 @@ void test_repo_init__init_with_initial_commit(void) ...@@ -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_add_bypath(index, "file.txt"));
cl_git_pass(git_index_write(index)); 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 :-) */ /* Make sure we're ready to use git_signature_default :-) */
{ {
git_config *cfg, *local; git_config *cfg, *local;
......
...@@ -36,25 +36,27 @@ static void push_three_states(void) ...@@ -36,25 +36,27 @@ static void push_three_states(void)
cl_git_mkfile("stash/zero.txt", "content\n"); cl_git_mkfile("stash/zero.txt", "content\n");
cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "zero.txt")); 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")); cl_assert(git_path_exists("stash/zero.txt"));
git_index_free(index);
cl_git_mkfile("stash/one.txt", "content\n"); 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/one.txt"));
cl_assert(git_path_exists("stash/zero.txt")); cl_assert(git_path_exists("stash/zero.txt"));
cl_git_mkfile("stash/two.txt", "content\n"); 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/two.txt"));
cl_assert(git_path_exists("stash/zero.txt")); cl_assert(git_path_exists("stash/zero.txt"));
cl_git_mkfile("stash/three.txt", "content\n"); 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/three.txt"));
cl_assert(git_path_exists("stash/zero.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) 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) ...@@ -160,7 +162,7 @@ void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void)
retrieve_top_stash_id(&oid); retrieve_top_stash_id(&oid);
cl_git_pass(git_revparse_single(&next_top_stash, repo, "stash@{1}")); 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)); cl_git_pass(git_stash_drop(repo, 0));
......
...@@ -241,7 +241,7 @@ void test_stash_save__stashing_updates_the_reflog(void) ...@@ -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) void test_stash_save__cannot_stash_when_there_are_no_local_change(void)
{ {
git_index *index; git_index *index;
git_oid commit_oid, stash_tip_oid; git_oid stash_tip_oid;
cl_git_pass(git_repository_index(&index, repo)); 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) ...@@ -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, "what"));
cl_git_pass(git_index_add_bypath(index, "who")); cl_git_pass(git_index_add_bypath(index, "who"));
cl_git_pass(git_index_write(index)); cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
commit_staged_files(&commit_oid, index, signature);
git_index_free(index); git_index_free(index);
cl_assert_equal_i(GIT_ENOTFOUND, cl_assert_equal_i(GIT_ENOTFOUND,
......
...@@ -2,38 +2,8 @@ ...@@ -2,38 +2,8 @@
#include "fileops.h" #include "fileops.h"
#include "stash_helpers.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) void setup_stash(git_repository *repo, git_signature *signature)
{ {
git_oid commit_oid;
git_index *index; git_index *index;
cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_repository_index(&index, repo));
...@@ -50,9 +20,8 @@ void setup_stash(git_repository *repo, git_signature *signature) ...@@ -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, "how"));
cl_git_pass(git_index_add_bypath(index, "who")); cl_git_pass(git_index_add_bypath(index, "who"));
cl_git_pass(git_index_add_bypath(index, ".gitignore")); 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/what", "goodbye\n"); /* dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 */
cl_git_rewritefile("stash/how", "not so small and\n"); /* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */ cl_git_rewritefile("stash/how", "not so small and\n"); /* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */
......
void setup_stash( void setup_stash(
git_repository *repo, git_repository *repo,
git_signature *signature); git_signature *signature);
void commit_staged_files(
git_oid *commit_oid,
git_index *index,
git_signature *signature);
\ No newline at end of file
...@@ -632,35 +632,12 @@ void test_status_worktree__conflicted_item(void) ...@@ -632,35 +632,12 @@ void test_status_worktree__conflicted_item(void)
static void stage_and_commit(git_repository *repo, const char *path) 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; git_index *index;
cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, path)); cl_git_pass(git_index_add_bypath(index, path));
cl_git_pass(git_index_write(index)); cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n");
cl_git_pass(git_index_write_tree(&tree_oid, index));
git_index_free(index); 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( static void assert_ignore_case(
......
...@@ -54,27 +54,9 @@ static void test_with_many(int expected_new) ...@@ -54,27 +54,9 @@ static void test_with_many(int expected_new)
git_diff_list_free(diff); git_diff_list_free(diff);
{ cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "yoyoyo");
git_object *parent; cl_git_pass(git_revparse_single(
git_signature *sig; (git_object **)&new_tree, g_repo, "HEAD^{tree}"));
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_git_pass(git_diff_tree_to_tree( cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, tree, new_tree, &diffopts)); &diff, g_repo, tree, new_tree, &diffopts));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment