Add git_remote_set_{fetch,push}spec() Allow setting the fetch and push refspecs, which is useful for creating new refspecs.
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 118
diff --git a/include/git2/remote.h b/include/git2/remote.h
index c7eb08c..9d677aa 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -70,6 +70,15 @@ GIT_EXTERN(const char *) git_remote_name(git_remote *remote);
GIT_EXTERN(const char *) git_remote_url(git_remote *remote);
/**
+ * Set the remote's fetch refspec
+ *
+ * @param remote the remote
+ * @apram spec the new fetch refspec
+ * @return GIT_SUCCESS or an error value
+ */
+GIT_EXTERN(int) git_remote_set_fetchspec(git_remote *remote, const char *spec);
+
+/**
* Get the fetch refspec
*
* @param remote the remote
@@ -78,6 +87,15 @@ GIT_EXTERN(const char *) git_remote_url(git_remote *remote);
GIT_EXTERN(const git_refspec *) git_remote_fetchspec(git_remote *remote);
/**
+ * Set the remote's push refspec
+ *
+ * @param remote the remote
+ * @apram spec the new push refspec
+ * @return GIT_SUCCESS or an error value
+ */
+GIT_EXTERN(int) git_remote_set_pushspec(git_remote *remote, const char *spec);
+
+/**
* Get the push refspec
*
* @param remote the remote
diff --git a/src/remote.c b/src/remote.c
index c10c337..9733511 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -199,12 +199,50 @@ const char *git_remote_url(git_remote *remote)
return remote->url;
}
+int git_remote_set_fetchspec(git_remote *remote, const char *spec)
+{
+ int error;
+ git_refspec refspec;
+
+ assert(remote && spec);
+
+ error = refspec_parse(&refspec, spec);
+ if (error != GIT_SUCCESS)
+ return error;
+
+ git__free(remote->fetch.src);
+ git__free(remote->fetch.dst);
+ remote->fetch.src = refspec.src;
+ remote->fetch.dst = refspec.dst;
+
+ return GIT_SUCCESS;
+}
+
const git_refspec *git_remote_fetchspec(git_remote *remote)
{
assert(remote);
return &remote->fetch;
}
+int git_remote_set_pushspec(git_remote *remote, const char *spec)
+{
+ int error;
+ git_refspec refspec;
+
+ assert(remote && spec);
+
+ error = refspec_parse(&refspec, spec);
+ if (error != GIT_SUCCESS)
+ return error;
+
+ git__free(remote->push.src);
+ git__free(remote->push.dst);
+ remote->push.src = refspec.src;
+ remote->push.dst = refspec.dst;
+
+ return GIT_SUCCESS;
+}
+
const git_refspec *git_remote_pushspec(git_remote *remote)
{
assert(remote);
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index f3a45d6..7a43a51 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -36,6 +36,22 @@ void test_network_remotes__refspec_parsing(void)
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/test/*"));
}
+void test_network_remotes__set_fetchspec(void)
+{
+ cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*"));
+ _refspec = git_remote_fetchspec(_remote);
+ cl_assert(!strcmp(git_refspec_src(_refspec), "refs/*"));
+ cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/*"));
+}
+
+void test_network_remotes__set_pushspec(void)
+{
+ cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*"));
+ _refspec = git_remote_pushspec(_remote);
+ cl_assert(!strcmp(git_refspec_src(_refspec), "refs/*"));
+ cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/*"));
+}
+
void test_network_remotes__fnmatch(void)
{
cl_git_pass(git_refspec_src_match(_refspec, "refs/heads/master"));