Merge branch 'ps/guess-repo-name-at-root'
"git clone $URL", when cloning from a site whose sole purpose is to host a single repository (hence, no path after <scheme>://<site>/), tried to use the site name as the new repository name, but did not remove username or password when <site> part was of the form <user>@<pass>:<host>. The code is taught to redact these. * ps/guess-repo-name-at-root: clone: abort if no dir name could be guessed clone: do not use port number as dir name clone: do not include authentication data in guessed dir
This commit is contained in:
@ -146,30 +146,68 @@ static char *get_repo_path(const char *repo, int *is_bundle)
|
|||||||
|
|
||||||
static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
|
static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
|
||||||
{
|
{
|
||||||
const char *end = repo + strlen(repo), *start;
|
const char *end = repo + strlen(repo), *start, *ptr;
|
||||||
size_t len;
|
size_t len;
|
||||||
char *dir;
|
char *dir;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Skip scheme.
|
||||||
|
*/
|
||||||
|
start = strstr(repo, "://");
|
||||||
|
if (start == NULL)
|
||||||
|
start = repo;
|
||||||
|
else
|
||||||
|
start += 3;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Skip authentication data. The stripping does happen
|
||||||
|
* greedily, such that we strip up to the last '@' inside
|
||||||
|
* the host part.
|
||||||
|
*/
|
||||||
|
for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
|
||||||
|
if (*ptr == '@')
|
||||||
|
start = ptr + 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Strip trailing spaces, slashes and /.git
|
* Strip trailing spaces, slashes and /.git
|
||||||
*/
|
*/
|
||||||
while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
|
while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
|
||||||
end--;
|
end--;
|
||||||
if (end - repo > 5 && is_dir_sep(end[-5]) &&
|
if (end - start > 5 && is_dir_sep(end[-5]) &&
|
||||||
!strncmp(end - 4, ".git", 4)) {
|
!strncmp(end - 4, ".git", 4)) {
|
||||||
end -= 5;
|
end -= 5;
|
||||||
while (repo < end && is_dir_sep(end[-1]))
|
while (start < end && is_dir_sep(end[-1]))
|
||||||
end--;
|
end--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find last component, but be prepared that repo could have
|
* Strip trailing port number if we've got only a
|
||||||
* the form "remote.example.com:foo.git", i.e. no slash
|
* hostname (that is, there is no dir separator but a
|
||||||
* in the directory part.
|
* colon). This check is required such that we do not
|
||||||
|
* strip URI's like '/foo/bar:2222.git', which should
|
||||||
|
* result in a dir '2222' being guessed due to backwards
|
||||||
|
* compatibility.
|
||||||
*/
|
*/
|
||||||
start = end;
|
if (memchr(start, '/', end - start) == NULL
|
||||||
while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
|
&& memchr(start, ':', end - start) != NULL) {
|
||||||
start--;
|
ptr = end;
|
||||||
|
while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
|
||||||
|
ptr--;
|
||||||
|
if (start < ptr && ptr[-1] == ':')
|
||||||
|
end = ptr - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Find last component. To remain backwards compatible we
|
||||||
|
* also regard colons as path separators, such that
|
||||||
|
* cloning a repository 'foo:bar.git' would result in a
|
||||||
|
* directory 'bar' being guessed.
|
||||||
|
*/
|
||||||
|
ptr = end;
|
||||||
|
while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
|
||||||
|
ptr--;
|
||||||
|
start = ptr;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Strip .{bundle,git}.
|
* Strip .{bundle,git}.
|
||||||
@ -177,6 +215,10 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
|
|||||||
len = end - start;
|
len = end - start;
|
||||||
strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
|
strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
|
||||||
|
|
||||||
|
if (!len || (len == 1 && *start == '/'))
|
||||||
|
die("No directory name could be guessed.\n"
|
||||||
|
"Please specify a directory on the command line");
|
||||||
|
|
||||||
if (is_bare)
|
if (is_bare)
|
||||||
dir = xstrfmt("%.*s.git", (int)len, start);
|
dir = xstrfmt("%.*s.git", (int)len, start);
|
||||||
else
|
else
|
||||||
|
@ -76,17 +76,17 @@ test_clone_dir host:foo/.git/// foo
|
|||||||
|
|
||||||
# omitting the path should default to the hostname
|
# omitting the path should default to the hostname
|
||||||
test_clone_dir ssh://host/ host
|
test_clone_dir ssh://host/ host
|
||||||
test_clone_dir ssh://host:1234/ host fail
|
test_clone_dir ssh://host:1234/ host
|
||||||
test_clone_dir ssh://user@host/ host fail
|
test_clone_dir ssh://user@host/ host
|
||||||
test_clone_dir host:/ host fail
|
test_clone_dir host:/ host
|
||||||
|
|
||||||
# auth materials should be redacted
|
# auth materials should be redacted
|
||||||
test_clone_dir ssh://user:password@host/ host fail
|
test_clone_dir ssh://user:password@host/ host
|
||||||
test_clone_dir ssh://user:password@host:1234/ host fail
|
test_clone_dir ssh://user:password@host:1234/ host
|
||||||
test_clone_dir ssh://user:passw@rd@host:1234/ host fail
|
test_clone_dir ssh://user:passw@rd@host:1234/ host
|
||||||
test_clone_dir user@host:/ host fail
|
test_clone_dir user@host:/ host
|
||||||
test_clone_dir user:password@host:/ host fail
|
test_clone_dir user:password@host:/ host
|
||||||
test_clone_dir user:passw@rd@host:/ host fail
|
test_clone_dir user:passw@rd@host:/ host
|
||||||
|
|
||||||
# auth-like material should not be dropped
|
# auth-like material should not be dropped
|
||||||
test_clone_dir ssh://host/foo@bar foo@bar
|
test_clone_dir ssh://host/foo@bar foo@bar
|
||||||
|
Reference in New Issue
Block a user