remote: introduce set_instance_url Users may want to override the URL on a particular instance of a remote, instead of updating the configuration. Previously, users could use a callback to do this, but this is not particularly idiomatic.
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 5d7a536..5b67717 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -212,7 +212,8 @@ GIT_EXTERN(const char *) git_remote_name(const git_remote *remote);
* Get the remote's url
*
* If url.*.insteadOf has been configured for this URL, it will
- * return the modified URL.
+ * return the modified URL. If `git_remote_set_instance_pushurl`
+ * has been called for this remote, then that URL will be returned.
*
* @param remote the remote
* @return a pointer to the url
@@ -220,10 +221,11 @@ GIT_EXTERN(const char *) git_remote_name(const git_remote *remote);
GIT_EXTERN(const char *) git_remote_url(const git_remote *remote);
/**
- * Get the remote's url for pushing
+ * Get the remote's url for pushing.
*
* If url.*.pushInsteadOf has been configured for this URL, it
- * will return the modified URL.
+ * will return the modified URL. If `git_remote_set_instance_pushurl`
+ * has been called for this remote, then that URL will be returned.
*
* @param remote the remote
* @return a pointer to the url or NULL if no special url for pushing is set
@@ -258,6 +260,26 @@ GIT_EXTERN(int) git_remote_set_url(git_repository *repo, const char *remote, con
GIT_EXTERN(int) git_remote_set_pushurl(git_repository *repo, const char *remote, const char* url);
/**
+ * Set the url for this particular url instance. The URL in the
+ * configuration will be ignored, and will not be changed.
+ *
+ * @param remote the remote's name
+ * @param url the url to set
+ * @return 0 or an error value
+ */
+GIT_EXTERN(int) git_remote_set_instance_url(git_remote *remote, const char *url);
+
+/**
+ * Set the push url for this particular url instance. The URL in the
+ * configuration will be ignored, and will not be changed.
+ *
+ * @param remote the remote's name
+ * @param url the url to set
+ * @return 0 or an error value
+ */
+GIT_EXTERN(int) git_remote_set_instance_pushurl(git_remote *remote, const char *url);
+
+/**
* Add a fetch refspec to the remote's configuration
*
* Add the given refspec to the fetch list in the configuration. No
diff --git a/src/remote.c b/src/remote.c
index ec68cc0..236f39a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -600,6 +600,22 @@ const char *git_remote_url(const git_remote *remote)
return remote->url;
}
+int git_remote_set_instance_url(git_remote *remote, const char *url)
+{
+ char *tmp;
+
+ GIT_ASSERT_ARG(remote);
+ GIT_ASSERT_ARG(url);
+
+ if ((tmp = git__strdup(url)) == NULL)
+ return -1;
+
+ git__free(remote->url);
+ remote->url = tmp;
+
+ return 0;
+}
+
static int set_url(git_repository *repo, const char *remote, const char *pattern, const char *url)
{
git_config *cfg;
@@ -645,6 +661,22 @@ const char *git_remote_pushurl(const git_remote *remote)
return remote->pushurl;
}
+int git_remote_set_instance_pushurl(git_remote *remote, const char *url)
+{
+ char *tmp;
+
+ GIT_ASSERT_ARG(remote);
+ GIT_ASSERT_ARG(url);
+
+ if ((tmp = git__strdup(url)) == NULL)
+ return -1;
+
+ git__free(remote->pushurl);
+ remote->pushurl = tmp;
+
+ return 0;
+}
+
int git_remote_set_pushurl(git_repository *repo, const char *remote, const char* url)
{
return set_url(repo, remote, CONFIG_PUSHURL_FMT, url);
diff --git a/tests/network/remote/remotes.c b/tests/network/remote/remotes.c
index 0694a6f..a962d92 100644
--- a/tests/network/remote/remotes.c
+++ b/tests/network/remote/remotes.c
@@ -121,6 +121,51 @@ void test_network_remote_remotes__urlresolve_passthrough(void)
git_buf_dispose(&url);
}
+void test_network_remote_remotes__instance_url(void)
+{
+ git_buf url = GIT_BUF_INIT;
+ const char *orig_url = "git://github.com/libgit2/libgit2";
+
+ cl_assert_equal_s(git_remote_name(_remote), "test");
+ cl_assert_equal_s(git_remote_url(_remote), orig_url);
+
+ cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL));
+ cl_assert_equal_s(url.ptr, orig_url);
+ git_buf_clear(&url);
+
+ cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL));
+ cl_assert_equal_s(url.ptr, orig_url);
+ git_buf_clear(&url);
+
+ /* Setting the instance url updates the fetch and push URLs */
+ git_remote_set_instance_url(_remote, "https://github.com/new/remote/url");
+ cl_assert_equal_s(git_remote_url(_remote), "https://github.com/new/remote/url");
+ cl_assert_equal_p(git_remote_pushurl(_remote), NULL);
+
+ cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL));
+ cl_assert_equal_s(url.ptr, "https://github.com/new/remote/url");
+ git_buf_clear(&url);
+
+ cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL));
+ cl_assert_equal_s(url.ptr, "https://github.com/new/remote/url");
+ git_buf_clear(&url);
+
+ /* Setting the instance push url updates only the push URL */
+ git_remote_set_instance_pushurl(_remote, "https://github.com/new/push/url");
+ cl_assert_equal_s(git_remote_url(_remote), "https://github.com/new/remote/url");
+ cl_assert_equal_s(git_remote_pushurl(_remote), "https://github.com/new/push/url");
+
+ cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL));
+ cl_assert_equal_s(url.ptr, "https://github.com/new/remote/url");
+ git_buf_clear(&url);
+
+ cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL));
+ cl_assert_equal_s(url.ptr, "https://github.com/new/push/url");
+ git_buf_clear(&url);
+
+ git_buf_dispose(&url);
+}
+
void test_network_remote_remotes__pushurl(void)
{
const char *name = git_remote_name(_remote);