Merge pull request #6101 from mkhl/fix/instead-of remotes: fix insteadOf/pushInsteadOf handling
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
diff --git a/src/remote.c b/src/remote.c
index bde4ce7..fb9f6f8 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -30,7 +30,7 @@
static int dwim_refspecs(git_vector *out, git_vector *refspecs, git_vector *refs);
static int lookup_remote_prune_config(git_remote *remote, git_config *config, const char *name);
-char *apply_insteadof(git_config *config, const char *url, int direction);
+char *apply_insteadof(bool *matched, git_config *config, const char *url, int direction);
static int add_refspec_to(git_vector *vector, const char *string, bool is_fetch)
{
@@ -210,7 +210,9 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
git_str var = GIT_STR_INIT;
git_str specbuf = GIT_STR_INIT;
const git_remote_create_options dummy_opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
+ char *tmp;
int error = -1;
+ bool matched;
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(url);
@@ -245,7 +247,12 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
goto on_error;
if (opts->repository && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
- remote->url = apply_insteadof(config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
+ remote->url = apply_insteadof(&matched, config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
+ tmp = apply_insteadof(&matched, config_ro, canonical_url.ptr, GIT_DIRECTION_PUSH);
+ if (matched) {
+ remote->pushurl = tmp;
+ GIT_ERROR_CHECK_ALLOC(remote->pushurl);
+ }
} else {
remote->url = git__strdup(canonical_url.ptr);
}
@@ -457,7 +464,9 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
git_remote *remote = NULL;
git_str buf = GIT_STR_INIT;
const char *val;
+ char *tmp;
int error = 0;
+ bool matched;
git_config *config;
struct refspec_cb_data data = { NULL };
bool optional_setting_found = false, found;
@@ -498,8 +507,13 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
if (found && strlen(val) > 0) {
- remote->url = apply_insteadof(config, val, GIT_DIRECTION_FETCH);
+ remote->url = apply_insteadof(&matched, config, val, GIT_DIRECTION_FETCH);
GIT_ERROR_CHECK_ALLOC(remote->url);
+ tmp = apply_insteadof(&matched, config, val, GIT_DIRECTION_PUSH);
+ if (matched) {
+ remote->pushurl = tmp;
+ GIT_ERROR_CHECK_ALLOC(remote->pushurl);
+ }
}
val = NULL;
@@ -518,7 +532,10 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
}
if (found && strlen(val) > 0) {
- remote->pushurl = apply_insteadof(config, val, GIT_DIRECTION_PUSH);
+ if (remote->pushurl) {
+ git__free(remote->pushurl);
+ }
+ remote->pushurl = apply_insteadof(&matched, config, val, GIT_DIRECTION_FETCH);
GIT_ERROR_CHECK_ALLOC(remote->pushurl);
}
@@ -2719,7 +2736,7 @@ int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_
#define SUFFIX_FETCH "insteadof"
#define SUFFIX_PUSH "pushinsteadof"
-char *apply_insteadof(git_config *config, const char *url, int direction)
+char *apply_insteadof(bool *matched, git_config *config, const char *url, int direction)
{
size_t match_length, prefix_length, suffix_length;
char *replacement = NULL;
@@ -2732,6 +2749,8 @@ char *apply_insteadof(git_config *config, const char *url, int direction)
GIT_ASSERT_ARG_WITH_RETVAL(config, NULL);
GIT_ASSERT_ARG_WITH_RETVAL(url, NULL);
GIT_ASSERT_ARG_WITH_RETVAL(direction == GIT_DIRECTION_FETCH || direction == GIT_DIRECTION_PUSH, NULL);
+ GIT_ASSERT_ARG_WITH_RETVAL(matched, NULL);
+ *matched = 0;
/* Add 1 to prefix/suffix length due to the additional escaped dot */
prefix_length = strlen(PREFIX) + 1;
@@ -2777,6 +2796,7 @@ char *apply_insteadof(git_config *config, const char *url, int direction)
git__free(replacement);
+ *matched = 1;
return result.ptr;
}
diff --git a/tests/remote/insteadof.c b/tests/remote/insteadof.c
index 05d4757..c39df4b 100644
--- a/tests/remote/insteadof.c
+++ b/tests/remote/insteadof.c
@@ -4,7 +4,12 @@
#define REPO_PATH "testrepo2/.gitted"
#define REMOTE_ORIGIN "origin"
-#define REMOTE_INSTEADOF "insteadof-test"
+#define REMOTE_INSTEADOF_URL_FETCH "insteadof-url-fetch"
+#define REMOTE_INSTEADOF_URL_PUSH "insteadof-url-push"
+#define REMOTE_INSTEADOF_URL_BOTH "insteadof-url-both"
+#define REMOTE_INSTEADOF_PUSHURL_FETCH "insteadof-pushurl-fetch"
+#define REMOTE_INSTEADOF_PUSHURL_PUSH "insteadof-pushurl-push"
+#define REMOTE_INSTEADOF_PUSHURL_BOTH "insteadof-pushurl-both"
static git_repository *g_repo;
static git_remote *g_remote;
@@ -21,7 +26,7 @@ void test_remote_insteadof__cleanup(void)
git_remote_free(g_remote);
}
-void test_remote_insteadof__url_insteadof_not_applicable(void)
+void test_remote_insteadof__not_applicable(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_ORIGIN));
@@ -29,44 +34,121 @@ void test_remote_insteadof__url_insteadof_not_applicable(void)
cl_assert_equal_s(
git_remote_url(g_remote),
"https://github.com/libgit2/false.git");
+ cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
}
-void test_remote_insteadof__url_insteadof_applicable(void)
+void test_remote_insteadof__url_insteadof_fetch(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
- cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF));
+ cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF_URL_FETCH));
cl_assert_equal_s(
git_remote_url(g_remote),
- "http://github.com/libgit2/libgit2");
+ "http://github.com/url/fetch/libgit2");
+ cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
}
-void test_remote_insteadof__pushurl_insteadof_not_applicable(void)
+void test_remote_insteadof__url_insteadof_push(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
- cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_ORIGIN));
+ cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF_URL_PUSH));
- cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
+ cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://example.com/url/push/libgit2");
+ cl_assert_equal_s(
+ git_remote_pushurl(g_remote),
+ "git@github.com:url/push/libgit2");
}
-void test_remote_insteadof__pushurl_insteadof_applicable(void)
+void test_remote_insteadof__url_insteadof_both(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
- cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF));
+ cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF_URL_BOTH));
cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://github.com/url/both/libgit2");
+ cl_assert_equal_s(
git_remote_pushurl(g_remote),
- "git@github.com:libgit2/libgit2");
+ "git@github.com:url/both/libgit2");
}
-void test_remote_insteadof__anonymous_remote(void)
+void test_remote_insteadof__pushurl_insteadof_fetch(void)
+{
+ cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
+ cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF_PUSHURL_FETCH));
+
+ cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://github.com/url/fetch/libgit2");
+ cl_assert_equal_s(
+ git_remote_pushurl(g_remote),
+ "http://github.com/url/fetch/libgit2-push");
+}
+
+void test_remote_insteadof__pushurl_insteadof_push(void)
+{
+ cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
+ cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF_PUSHURL_PUSH));
+
+ cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://example.com/url/push/libgit2");
+ cl_assert_equal_s(
+ git_remote_pushurl(g_remote),
+ "http://example.com/url/push/libgit2-push");
+}
+
+void test_remote_insteadof__pushurl_insteadof_both(void)
+{
+ cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
+ cl_git_pass(git_remote_lookup(&g_remote, g_repo, REMOTE_INSTEADOF_PUSHURL_BOTH));
+
+ cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://github.com/url/both/libgit2");
+ cl_assert_equal_s(
+ git_remote_pushurl(g_remote),
+ "http://github.com/url/both/libgit2-push");
+}
+
+void test_remote_insteadof__anonymous_remote_fetch(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
cl_git_pass(git_remote_create_anonymous(&g_remote, g_repo,
- "http://example.com/libgit2/libgit2"));
+ "http://example.com/url/fetch/libgit2"));
cl_assert_equal_s(
git_remote_url(g_remote),
- "http://github.com/libgit2/libgit2");
+ "http://github.com/url/fetch/libgit2");
cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
}
+
+void test_remote_insteadof__anonymous_remote_push(void)
+{
+ cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
+ cl_git_pass(git_remote_create_anonymous(&g_remote, g_repo,
+ "http://example.com/url/push/libgit2"));
+
+ cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://example.com/url/push/libgit2");
+ cl_assert_equal_s(
+ git_remote_pushurl(g_remote),
+ "git@github.com:url/push/libgit2");
+}
+
+void test_remote_insteadof__anonymous_remote_both(void)
+{
+ cl_git_pass(git_repository_open(&g_repo, cl_fixture(REPO_PATH)));
+ cl_git_pass(git_remote_create_anonymous(&g_remote, g_repo,
+ "http://example.com/url/both/libgit2"));
+
+ cl_assert_equal_s(
+ git_remote_url(g_remote),
+ "http://github.com/url/both/libgit2");
+ cl_assert_equal_s(
+ git_remote_pushurl(g_remote),
+ "git@github.com:url/both/libgit2");
+}
diff --git a/tests/resources/testrepo2/.gitted/config b/tests/resources/testrepo2/.gitted/config
index 4af067f..6966b8a 100644
--- a/tests/resources/testrepo2/.gitted/config
+++ b/tests/resources/testrepo2/.gitted/config
@@ -8,19 +8,42 @@
[remote "origin"]
url = https://github.com/libgit2/false.git
fetch = +refs/heads/*:refs/remotes/origin/*
-[remote "insteadof-test"]
- url = http://example.com/libgit2/libgit2
- pushurl = http://github.com/libgit2/libgit2
+[remote "insteadof-url-fetch"]
+ url = http://example.com/url/fetch/libgit2
+ fetch = +refs/heads/*:refs/remotes/test/*
+[remote "insteadof-url-push"]
+ url = http://example.com/url/push/libgit2
+ fetch = +refs/heads/*:refs/remotes/test/*
+[remote "insteadof-url-both"]
+ url = http://example.com/url/both/libgit2
+ fetch = +refs/heads/*:refs/remotes/test/*
+[remote "insteadof-pushurl-fetch"]
+ url = http://example.com/url/fetch/libgit2
+ pushurl = http://example.com/url/fetch/libgit2-push
+ fetch = +refs/heads/*:refs/remotes/test/*
+[remote "insteadof-pushurl-push"]
+ url = http://example.com/url/push/libgit2
+ pushurl = http://example.com/url/push/libgit2-push
+ fetch = +refs/heads/*:refs/remotes/test/*
+[remote "insteadof-pushurl-both"]
+ url = http://example.com/url/both/libgit2
+ pushurl = http://example.com/url/both/libgit2-push
fetch = +refs/heads/*:refs/remotes/test/*
[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true
[url "longer-non-prefix-match"]
- insteadOf = ttp://example.com/li
+ # not applicable because it's not a prefix match
+ insteadOf = ttp://example.com/url/fetch/li
[url "shorter-prefix"]
- insteadOf = http://example.co
-[url "http://github.com"]
- insteadOf = http://example.com
-[url "git@github.com:"]
- pushInsteadOf = http://github.com/
+ # not applicable because the matched prefix is shorter
+ insteadOf = http://example.com/url/fe
+[url "http://github.com/url/fetch"]
+ insteadOf = http://example.com/url/fetch
+[url "http://github.com/url/both"]
+ insteadOf = http://example.com/url/both
+[url "git@github.com:url/push"]
+ pushInsteadOf = http://example.com/url/push
+[url "git@github.com:url/both"]
+ pushInsteadOf = http://example.com/url/both