Skip to content

Commit e895986

Browse files
pks-tgitster
authored andcommitted
clone: do not include authentication data in guessed dir
If the URI contains authentication data and the URI's path component is empty, we fail to guess a sensible directory name. E.g. cloning a repository 'ssh://user:password@example.com/' we guess a directory name 'password@example.com' where we would want the hostname only, e.g. 'example.com'. The naive way of just adding '@' as a path separator would break cloning repositories like 'foo/bar@baz.git' (which would currently become 'bar@baz' but would then become 'baz' only). Instead fix this by first dropping the scheme and then greedily scanning for an '@' sign until we find the first path separator. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent db2e220 commit e895986

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

builtin/clone.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,51 @@ static char *get_repo_path(const char *repo, int *is_bundle)
146146

147147
static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
148148
{
149-
const char *end = repo + strlen(repo), *start;
149+
const char *end = repo + strlen(repo), *start, *ptr;
150150
size_t len;
151151
char *dir;
152152

153+
/*
154+
* Skip scheme.
155+
*/
156+
start = strstr(repo, "://");
157+
if (start == NULL)
158+
start = repo;
159+
else
160+
start += 3;
161+
162+
/*
163+
* Skip authentication data. The stripping does happen
164+
* greedily, such that we strip up to the last '@' inside
165+
* the host part.
166+
*/
167+
for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
168+
if (*ptr == '@')
169+
start = ptr + 1;
170+
}
171+
153172
/*
154173
* Strip trailing spaces, slashes and /.git
155174
*/
156-
while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
175+
while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
157176
end--;
158-
if (end - repo > 5 && is_dir_sep(end[-5]) &&
177+
if (end - start > 5 && is_dir_sep(end[-5]) &&
159178
!strncmp(end - 4, ".git", 4)) {
160179
end -= 5;
161-
while (repo < end && is_dir_sep(end[-1]))
180+
while (start < end && is_dir_sep(end[-1]))
162181
end--;
163182
}
164183

165184
/*
166-
* Find last component, but be prepared that repo could have
167-
* the form "remote.example.com:foo.git", i.e. no slash
168-
* in the directory part.
185+
* Find last component. To remain backwards compatible we
186+
* also regard colons as path separators, such that
187+
* cloning a repository 'foo:bar.git' would result in a
188+
* directory 'bar' being guessed.
169189
*/
170-
start = end;
171-
while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
172-
start--;
190+
ptr = end;
191+
while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
192+
ptr--;
193+
start = ptr;
173194

174195
/*
175196
* Strip .{bundle,git}.

t/t5603-clone-dirname.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ test_clone_dir host:foo/.git/// foo
7777
# omitting the path should default to the hostname
7878
test_clone_dir ssh://host/ host
7979
test_clone_dir ssh://host:1234/ host fail
80-
test_clone_dir ssh://user@host/ host fail
80+
test_clone_dir ssh://user@host/ host
8181
test_clone_dir host:/ host fail
8282

8383
# auth materials should be redacted
84-
test_clone_dir ssh://user:password@host/ host fail
84+
test_clone_dir ssh://user:password@host/ host
8585
test_clone_dir ssh://user:password@host:1234/ host fail
8686
test_clone_dir ssh://user:passw@rd@host:1234/ host fail
8787
test_clone_dir user@host:/ host fail

0 commit comments

Comments
 (0)