Commit 486ba4cd by Carlos Martín Nieto

submodule: make `_set_branch()` affect the configuration

parent 4e636423
...@@ -373,20 +373,17 @@ GIT_EXTERN(int) git_submodule_resolve_url(git_buf *out, git_repository *repo, co ...@@ -373,20 +373,17 @@ GIT_EXTERN(int) git_submodule_resolve_url(git_buf *out, git_repository *repo, co
GIT_EXTERN(const char *) git_submodule_branch(git_submodule *submodule); GIT_EXTERN(const char *) git_submodule_branch(git_submodule *submodule);
/** /**
* Set the branch for the submodule. * Set the branch for the submodule in the configuration
* *
* This sets the branch in memory for the submodule. This will be used for * After calling this, you may wish to call `git_submodule_sync()` to
* any following submodule actions while this submodule data is in memory.
*
* After calling this, you may wish to call `git_submodule_save()` to write
* the changes back to the ".gitmodules" file and `git_submodule_sync()` to
* write the changes to the checked out submodule repository. * write the changes to the checked out submodule repository.
* *
* @param submodule Pointer to the submodule object * @param repo the repository to affect
* @param name the name of the submodule to configure
* @param branch Branch that should be used for the submodule * @param branch Branch that should be used for the submodule
* @return 0 on success, <0 on failure * @return 0 on success, <0 on failure
*/ */
GIT_EXTERN(int) git_submodule_set_branch(git_submodule *submodule, const char *branch); GIT_EXTERN(int) git_submodule_set_branch(git_repository *repo, const char *name, const char *branch);
/** /**
* Set the URL for the submodule. * Set the URL for the submodule.
......
...@@ -792,19 +792,6 @@ int git_submodule_save(git_submodule *submodule) ...@@ -792,19 +792,6 @@ int git_submodule_save(git_submodule *submodule)
(error = git_config_file_set_string(mods, key.ptr, submodule->url)) < 0) (error = git_config_file_set_string(mods, key.ptr, submodule->url)) < 0)
goto cleanup; goto cleanup;
if ((error = submodule_config_key_trunc_puts(&key, "branch")) < 0)
goto cleanup;
if (submodule->branch == NULL)
error = git_config_file_delete(mods, key.ptr);
else
error = git_config_file_set_string(mods, key.ptr, submodule->branch);
if (error == GIT_ENOTFOUND) {
error = 0;
giterr_clear();
}
if (error < 0)
goto cleanup;
/* update internal defaults */ /* update internal defaults */
submodule->ignore_default = submodule->ignore; submodule->ignore_default = submodule->ignore;
...@@ -864,25 +851,43 @@ int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *ur ...@@ -864,25 +851,43 @@ int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *ur
return error; return error;
} }
static int write_var(git_repository *repo, const char *name, const char *var, const char *val)
{
git_buf key = GIT_BUF_INIT;
git_config_backend *mods;
int error;
mods = open_gitmodules(repo, GITMODULES_CREATE);
if (!mods)
return -1;
if ((error = git_buf_printf(&key, "submodule.%s.%s", name, var)) < 0)
goto cleanup;
if (val)
error = git_config_file_set_string(mods, key.ptr, val);
else
error = git_config_file_delete(mods, key.ptr);
git_buf_free(&key);
cleanup:
git_config_file_free(mods);
return error;
}
const char *git_submodule_branch(git_submodule *submodule) const char *git_submodule_branch(git_submodule *submodule)
{ {
assert(submodule); assert(submodule);
return submodule->branch; return submodule->branch;
} }
int git_submodule_set_branch(git_submodule *submodule, const char *branch) int git_submodule_set_branch(git_repository *repo, const char *name, const char *branch)
{ {
assert(submodule);
git__free(submodule->branch);
submodule->branch = NULL;
if (branch != NULL) { assert(repo && name);
submodule->branch = git__strdup(branch);
GITERR_CHECK_ALLOC(submodule->branch);
}
return 0; return write_var(repo, name, "branch", branch);
} }
int git_submodule_set_url(git_submodule *submodule, const char *url) int git_submodule_set_url(git_submodule *submodule, const char *url)
...@@ -945,31 +950,6 @@ git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule) ...@@ -945,31 +950,6 @@ git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule)
GIT_SUBMODULE_IGNORE_NONE : submodule->ignore; GIT_SUBMODULE_IGNORE_NONE : submodule->ignore;
} }
static int write_var(git_repository *repo, const char *name, const char *var, const char *val)
{
git_buf key = GIT_BUF_INIT;
git_config_backend *mods;
int error;
mods = open_gitmodules(repo, GITMODULES_CREATE);
if (!mods)
return -1;
if ((error = git_buf_printf(&key, "submodule.%s.%s", name, var)) < 0)
goto cleanup;
if (val)
error = git_config_file_set_string(mods, key.ptr, val);
else
error = git_config_file_delete(mods, key.ptr);
git_buf_free(&key);
cleanup:
git_config_file_free(mods);
return error;
}
int git_submodule_set_ignore(git_repository *repo, const char *name, git_submodule_ignore_t ignore) int git_submodule_set_ignore(git_repository *repo, const char *name, git_submodule_ignore_t ignore)
{ {
const char *val; const char *val;
......
...@@ -166,34 +166,47 @@ void test_submodule_modify__set_fetch_recurse_submodules(void) ...@@ -166,34 +166,47 @@ void test_submodule_modify__set_fetch_recurse_submodules(void)
git_submodule_free(sm); git_submodule_free(sm);
} }
void test_submodule_modify__set_branch(void)
{
git_submodule *sm;
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
cl_assert(git_submodule_branch(sm) == NULL);
git_submodule_free(sm);
cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", SM_LIBGIT2_BRANCH));
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm));
git_submodule_free(sm);
cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", NULL));
cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
cl_assert(git_submodule_branch(sm) == NULL);
git_submodule_free(sm);
}
void test_submodule_modify__edit_and_save(void) void test_submodule_modify__edit_and_save(void)
{ {
git_submodule *sm1, *sm2; git_submodule *sm1, *sm2;
char *old_url, *old_branch; char *old_url;
git_repository *r2; git_repository *r2;
cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head")); cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));
old_url = git__strdup(git_submodule_url(sm1)); old_url = git__strdup(git_submodule_url(sm1));
old_branch = NULL;
/* modify properties of submodule */ /* modify properties of submodule */
cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL)); cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1)); cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
/* revert without saving (and confirm setters return old value) */ /* revert without saving (and confirm setters return old value) */
cl_git_pass(git_submodule_set_url(sm1, old_url)); cl_git_pass(git_submodule_set_url(sm1, old_url));
cl_git_pass(git_submodule_set_branch(sm1, old_branch));
/* check that revert was successful */ /* check that revert was successful */
cl_assert_equal_s(old_url, git_submodule_url(sm1)); cl_assert_equal_s(old_url, git_submodule_url(sm1));
cl_assert_equal_s(old_branch, git_submodule_branch(sm1));
/* modify properties of submodule (again) */ /* modify properties of submodule (again) */
cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL)); cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
/* call save */ /* call save */
cl_git_pass(git_submodule_save(sm1)); cl_git_pass(git_submodule_save(sm1));
...@@ -202,13 +215,6 @@ void test_submodule_modify__edit_and_save(void) ...@@ -202,13 +215,6 @@ void test_submodule_modify__edit_and_save(void)
cl_git_pass(git_submodule_reload(sm1, 0)); cl_git_pass(git_submodule_reload(sm1, 0));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1)); cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
/* unset branch again and verify that the property is deleted in config */
cl_git_pass(git_submodule_set_branch(sm1, NULL));
cl_git_pass(git_submodule_save(sm1));
cl_git_pass(git_submodule_reload(sm1, 0));
cl_assert_equal_s(NULL, git_submodule_branch(sm1));
/* open a second copy of the repo and compare submodule */ /* open a second copy of the repo and compare submodule */
cl_git_pass(git_repository_open(&r2, "submod2")); cl_git_pass(git_repository_open(&r2, "submod2"));
......
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