Subject: Re: git: Wrong parsing of ssh urls with IPv6 literals ignores port

From: Jonathan Nieder <[email-masked]>
Date: Sun, 10 Jun 2012 04:05:16 -0500

Hi Eduardo,

Eduardo Trápani wrote[1]:

> git clone ssh://[2001:0:53aa:64c:1845:430c:4179:d71f]:3333/deponejo/unua
>
> Will try to connect to port 22 and not 3333.  The port number seems to be
> ignored.

True.  How about something like this (untested)?

Signed-off-by: Jonathan Nieder <[email-masked]>
---
[1] http://bugs.debian.org/646178

 connect.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git i/connect.c w/connect.c
index 912cddee..7ec1b258 100644
--- i/connect.c
+++ w/connect.c
@@ -494,11 +494,19 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
 	if (host[0] == '[') {
 		end = strchr(host + 1, ']');
 		if (end) {
-			if (protocol != PROTO_GIT) {
+			if (protocol == PROTO_GIT)
+				end++;
+			else {
 				*end = 0;
 				host++;
+				end++;
+				if (!*end || *end == c)
+					; /* good */
+				else if (protocol == PROTO_SSH && c != ':' && *end == ':')
+					port = end + 1;
+				else
+					die("garbage after [] string in URL");
 			}
-			end++;
 		} else
 			end = host;
 	} else
@@ -535,7 +543,7 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
 	/*
 	 * Add support for ssh port: ssh://host.xy:<port>/...
 	 */
-	if (protocol == PROTO_SSH && host != url)
+	if (protocol == PROTO_SSH && host != url && !port)
 		port = get_port(host);
 
 	if (protocol == PROTO_GIT) {