Skip to content

Commit 92722ef

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e895986 commit 92722ef

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

builtin/clone.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
181181
end--;
182182
}
183183

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+
184201
/*
185202
* Find last component. To remain backwards compatible we
186203
* also regard colons as path separators, such that

t/t5603-clone-dirname.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ test_clone_dir host:foo/.git/// foo
7676

7777
# omitting the path should default to the hostname
7878
test_clone_dir ssh://host/ host
79-
test_clone_dir ssh://host:1234/ host fail
79+
test_clone_dir ssh://host:1234/ host
8080
test_clone_dir ssh://user@host/ host
81-
test_clone_dir host:/ host fail
81+
test_clone_dir host:/ host
8282

8383
# auth materials should be redacted
8484
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
9090

9191
# auth-like material should not be dropped
9292
test_clone_dir ssh://host/foo@bar foo@bar

0 commit comments

Comments
 (0)