clone: do not use port number as dir name

If the URI contains a port number and the URI's path component is empty we fail to guess a sensible directory name. E.g. cloning a repository 'ssh://example.com:2222/' we guess a directory name '2222' where we would want the hostname only, e.g. 'example.com'. We need to take care to not drop trailing port-like numbers in certain cases. E.g. when cloning a repository 'foo/bar:2222.git' we want to guess the directory name '2222' instead of 'bar'. Thus, we have to first check the stripped URI for path separators and only strip port numbers if there are path separators present. This heuristic breaks when cloning a repository 'bar:2222.git', though. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 10, 2015 at 17:48 UTC 92722efec01f67a54b68c83fcbc3cd65f9fbb7b8
2 files changed +24 -7
builtin/clone.c
+17
@@ -181,6 +181,23 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
181 end--;
182 }
183
184 + /*
185 + * Strip trailing port number if we've got only a
186 + * hostname (that is, there is no dir separator but a
187 + * colon). This check is required such that we do not
188 + * strip URI's like '/foo/bar:2222.git', which should
189 + * result in a dir '2222' being guessed due to backwards
190 + * compatibility.
191 + */
192 + if (memchr(start, '/', end - start) == NULL
193 + && memchr(start, ':', end - start) != NULL) {
194 + ptr = end;
195 + while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
196 + ptr--;
197 + if (start < ptr && ptr[-1] == ':')
198 + end = ptr - 1;
199 + }
200 +
201 /*
202 * Find last component. To remain backwards compatible we
203 * also regard colons as path separators, such that
t/t5603-clone-dirname.sh
+7 -7
@@ -76,17 +76,17 @@ test_clone_dir host:foo/.git/// foo
76
77 # omitting the path should default to the hostname
78 test_clone_dir ssh://host/ host
79 -test_clone_dir ssh://host:1234/ host fail
79 +test_clone_dir ssh://host:1234/ host
80 test_clone_dir ssh://user@host/ host
81 -test_clone_dir host:/ host fail
81 +test_clone_dir host:/ host
82
83 # auth materials should be redacted
84 test_clone_dir ssh://user:password@host/ host
85 -test_clone_dir ssh://user:password@host:1234/ host fail
86 -test_clone_dir ssh://user:passw@rd@host:1234/ host fail
87 -test_clone_dir user@host:/ host fail
88 -test_clone_dir user:password@host:/ host fail
89 -test_clone_dir user:passw@rd@host:/ host fail
85 +test_clone_dir ssh://user:password@host:1234/ host
86 +test_clone_dir ssh://user:passw@rd@host:1234/ host
87 +test_clone_dir user@host:/ host
88 +test_clone_dir user:password@host:/ host
89 +test_clone_dir user:passw@rd@host:/ host
90
91 # auth-like material should not be dropped
92 test_clone_dir ssh://host/foo@bar foo@bar