Commit 89e5ed98 by Carlos Martín Nieto

Add git_remote_save()

parent bcb8c007
......@@ -54,6 +54,14 @@ GIT_EXTERN(int) git_remote_new(git_remote **out, git_repository *repo, const cha
GIT_EXTERN(int) git_remote_load(git_remote **out, git_repository *repo, const char *name);
/**
* Save a remote to its repository's configuration
*
* @param remote the remote to save to config
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_remote_save(const git_remote *remote);
/**
* Get the remote's name
*
* @param remote the remote
......
......@@ -187,6 +187,56 @@ cleanup:
return error;
}
int git_remote_save(const git_remote *remote)
{
int error;
git_config *config;
git_buf buf = GIT_BUF_INIT, value = GIT_BUF_INIT;
error = git_repository_config__weakptr(&config, remote->repo);
if (error < GIT_SUCCESS)
return error;
git_buf_printf(&buf, "remote.%s.%s", remote->name, "url");
if (git_buf_oom(&buf))
return GIT_ENOMEM;
error = git_config_set_string(config, git_buf_cstr(&buf), remote->url);
if (error < GIT_SUCCESS)
goto cleanup;
if (remote->fetch.src != NULL && remote->fetch.src != NULL) {
git_buf_clear(&buf);
git_buf_clear(&value);
git_buf_printf(&buf, "remote.%s.%s", remote->name, "fetch");
git_buf_printf(&value, "%s:%s", remote->fetch.src, remote->fetch.dst);
if (git_buf_oom(&buf) || git_buf_oom(&value))
return GIT_ENOMEM;
error = git_config_set_string(config, git_buf_cstr(&buf), git_buf_cstr(&value));
if (error < GIT_SUCCESS)
goto cleanup;
}
if (remote->push.src != NULL && remote->push.src != NULL) {
git_buf_clear(&buf);
git_buf_clear(&value);
git_buf_printf(&buf, "remote.%s.%s", remote->name, "push");
git_buf_printf(&value, "%s:%s", remote->push.src, remote->push.dst);
if (git_buf_oom(&buf) || git_buf_oom(&value))
return GIT_ENOMEM;
error = git_config_set_string(config, git_buf_cstr(&buf), git_buf_cstr(&value));
if (error < GIT_SUCCESS)
goto cleanup;
}
cleanup:
git_buf_free(&buf);
git_buf_free(&value);
return error;
}
const char *git_remote_name(git_remote *remote)
{
assert(remote);
......
......@@ -52,6 +52,32 @@ void test_network_remotes__set_pushspec(void)
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/*"));
}
void test_network_remotes__save(void)
{
git_remote_free(_remote);
/* Set up the remote and save it to config */
cl_git_pass(git_remote_new(&_remote, _repo, "git://github.com/libgit2/libgit2", "upstream"));
cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*"));
cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*"));
cl_git_pass(git_remote_save(_remote));
git_remote_free(_remote);
_remote = NULL;
/* Load it from config and make sure everything matches */
cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
_refspec = git_remote_fetchspec(_remote);
cl_assert(_refspec != NULL);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/upstream/*"));
_refspec = git_remote_pushspec(_remote);
cl_assert(_refspec != NULL);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/heads/*"));
}
void test_network_remotes__fnmatch(void)
{
cl_git_pass(git_refspec_src_match(_refspec, "refs/heads/master"));
......
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