Add git_remote_save()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 9d677aa..9339434 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -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
diff --git a/src/remote.c b/src/remote.c
index 9733511..d4caf02 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -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);
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 7a43a51..beb0bcc 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -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"));