Commit d6073b30 by Carlos Martín Nieto

submodule: make `_set_url()` affect the configuration

With this one, we can get rid of the edit_and_save test.
parent 486ba4cd
...@@ -386,20 +386,18 @@ GIT_EXTERN(const char *) git_submodule_branch(git_submodule *submodule); ...@@ -386,20 +386,18 @@ GIT_EXTERN(const char *) git_submodule_branch(git_submodule *submodule);
GIT_EXTERN(int) git_submodule_set_branch(git_repository *repo, const char *name, 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 in the configuration
* *
* This sets the URL in memory for the submodule. This will be used for
* any following submodule actions while this submodule data is in memory.
* *
* After calling this, you may wish to call `git_submodule_save()` to write * After calling this, you may wish to call `git_submodule_sync()` to
* 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 url URL that should be used for the submodule * @param url URL 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_url(git_submodule *submodule, const char *url); GIT_EXTERN(int) git_submodule_set_url(git_repository *repo, const char *name, const char *url);
/** /**
* Get the OID for the submodule in the index. * Get the OID for the submodule in the index.
......
...@@ -788,10 +788,6 @@ int git_submodule_save(git_submodule *submodule) ...@@ -788,10 +788,6 @@ int git_submodule_save(git_submodule *submodule)
(error = git_config_file_set_string(mods, key.ptr, submodule->path)) < 0) (error = git_config_file_set_string(mods, key.ptr, submodule->path)) < 0)
goto cleanup; goto cleanup;
if ((error = submodule_config_key_trunc_puts(&key, "url")) < 0 ||
(error = git_config_file_set_string(mods, key.ptr, submodule->url)) < 0)
goto cleanup;
/* update internal defaults */ /* update internal defaults */
submodule->ignore_default = submodule->ignore; submodule->ignore_default = submodule->ignore;
...@@ -890,16 +886,11 @@ int git_submodule_set_branch(git_repository *repo, const char *name, const char ...@@ -890,16 +886,11 @@ int git_submodule_set_branch(git_repository *repo, const char *name, const char
return write_var(repo, name, "branch", branch); return write_var(repo, name, "branch", branch);
} }
int git_submodule_set_url(git_submodule *submodule, const char *url) int git_submodule_set_url(git_repository *repo, const char *name, const char *url)
{ {
assert(submodule && url); assert(repo && name && url);
git__free(submodule->url);
submodule->url = git__strdup(url); return write_var(repo, name, "url", url);
GITERR_CHECK_ALLOC(submodule->url);
return 0;
} }
const git_oid *git_submodule_index_id(git_submodule *submodule) const git_oid *git_submodule_index_id(git_submodule *submodule)
......
...@@ -23,10 +23,10 @@ void test_submodule_init__absolute_url(void) ...@@ -23,10 +23,10 @@ void test_submodule_init__absolute_url(void)
cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0); cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git")); cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
/* write the absolute url to the .gitmodules file*/ /* write the absolute url to the .gitmodules file*/
cl_git_pass(git_submodule_set_url(sm, absolute_url.ptr)); cl_git_pass(git_submodule_set_url(g_repo, "testrepo", absolute_url.ptr));
cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
/* verify that the .gitmodules is set with an absolute path*/ /* verify that the .gitmodules is set with an absolute path*/
cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm)); cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm));
......
...@@ -185,47 +185,14 @@ void test_submodule_modify__set_branch(void) ...@@ -185,47 +185,14 @@ void test_submodule_modify__set_branch(void)
git_submodule_free(sm); git_submodule_free(sm);
} }
void test_submodule_modify__edit_and_save(void) void test_submodule_modify__set_url(void)
{ {
git_submodule *sm1, *sm2; git_submodule *sm;
char *old_url;
git_repository *r2;
cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));
old_url = git__strdup(git_submodule_url(sm1));
/* modify properties of submodule */
cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
/* revert without saving (and confirm setters return old value) */
cl_git_pass(git_submodule_set_url(sm1, old_url));
/* check that revert was successful */
cl_assert_equal_s(old_url, git_submodule_url(sm1));
/* modify properties of submodule (again) */
cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
/* call save */
cl_git_pass(git_submodule_save(sm1));
/* call reload and check that the new values are loaded */
cl_git_pass(git_submodule_reload(sm1, 0));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
/* open a second copy of the repo and compare submodule */
cl_git_pass(git_repository_open(&r2, "submod2"));
cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
git_submodule_free(sm1); cl_git_pass(git_submodule_set_url(g_repo, "sm_changed_head", SM_LIBGIT2_URL));
git_submodule_free(sm2); cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
git_repository_free(r2); cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm));
git__free(old_url); git_submodule_free(sm);
} }
void test_submodule_modify__save_last(void) void test_submodule_modify__save_last(void)
......
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