Skip to content

Commit 8a94bc7

Browse files
raalkmlgitster
authored andcommitted
Improve the naming of guessed target repository for git clone
Strip leading and trailing spaces off guessed target directory, and replace sequences of whitespace and 'control' characters with one space character. User still can have any name by specifying it explicitely after url. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 39d404d commit 8a94bc7

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

builtin-clone.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ static char *get_repo_path(const char *repo, int *is_bundle)
104104
static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
105105
{
106106
const char *end = repo + strlen(repo), *start;
107+
char *dir;
107108

108109
/*
109-
* Strip trailing slashes and /.git
110+
* Strip trailing spaces, slashes and /.git
110111
*/
111-
while (repo < end && is_dir_sep(end[-1]))
112+
while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
112113
end--;
113114
if (end - repo > 5 && is_dir_sep(end[-5]) &&
114115
!strncmp(end - 4, ".git", 4)) {
@@ -140,10 +141,33 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
140141
if (is_bare) {
141142
struct strbuf result = STRBUF_INIT;
142143
strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
143-
return strbuf_detach(&result, 0);
144+
dir = strbuf_detach(&result, 0);
145+
} else
146+
dir = xstrndup(start, end - start);
147+
/*
148+
* Replace sequences of 'control' characters and whitespace
149+
* with one ascii space, remove leading and trailing spaces.
150+
*/
151+
if (*dir) {
152+
char *out = dir;
153+
int prev_space = 1 /* strip leading whitespace */;
154+
for (end = dir; *end; ++end) {
155+
char ch = *end;
156+
if ((unsigned char)ch < '\x20')
157+
ch = '\x20';
158+
if (isspace(ch)) {
159+
if (prev_space)
160+
continue;
161+
prev_space = 1;
162+
} else
163+
prev_space = 0;
164+
*out++ = ch;
165+
}
166+
*out = '\0';
167+
if (out > dir && prev_space)
168+
out[-1] = '\0';
144169
}
145-
146-
return xstrndup(start, end - start);
170+
return dir;
147171
}
148172

149173
static void strip_trailing_slashes(char *dir)

0 commit comments

Comments
 (0)