Remotes: Use correct url in git_remote_connect
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
diff --git a/src/remote.c b/src/remote.c
index bee1ab6..b4a21a6 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -356,13 +356,32 @@ const git_refspec *git_remote_pushspec(git_remote *remote)
return &remote->push;
}
+const char* git_remote__urlfordirection(git_remote *remote, int direction)
+{
+ assert(remote);
+
+ if (direction == GIT_DIR_FETCH) {
+ return remote->url;
+ }
+
+ if (direction == GIT_DIR_PUSH) {
+ return remote->pushurl ? remote->pushurl : remote->url;
+ }
+
+ return NULL;
+}
+
int git_remote_connect(git_remote *remote, int direction)
{
git_transport *t;
assert(remote);
- if (git_transport_new(&t, remote->url) < 0)
+ const char* url = git_remote__urlfordirection(remote, direction);
+ if (url == NULL )
+ return -1;
+
+ if (git_transport_new(&t, url) < 0)
return -1;
t->check_cert = remote->check_cert;
diff --git a/src/remote.h b/src/remote.h
index abdaa57..623d40c 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -24,4 +24,6 @@ struct git_remote {
check_cert;
};
+const char* git_remote__urlfordirection(struct git_remote *remote, int direction);
+
#endif
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 3d989c1..f1d6f47 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -2,6 +2,7 @@
#include "buffer.h"
#include "refspec.h"
#include "transport.h"
+#include "remote.h"
static git_remote *_remote;
static git_repository *_repo;
@@ -33,10 +34,21 @@ void test_network_remotes__parsing(void)
cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
cl_assert(git_remote_pushurl(_remote) == NULL);
+ cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIR_FETCH),
+ "git://github.com/libgit2/libgit2");
+ cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIR_PUSH),
+ "git://github.com/libgit2/libgit2");
+
cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl"));
cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl");
cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2");
cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2");
+
+ cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIR_FETCH),
+ "git://github.com/libgit2/fetchlibgit2");
+ cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIR_PUSH),
+ "git://github.com/libgit2/pushlibgit2");
+
git_remote_free(_remote2);
}