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>
Alex Riesen committed
May 13, 2009 at 18:32 UTC
8a94bc7bdc54db0d77058e63baf173ff932cba7c
1 file changed
+29
-5
builtin-clone.c
+29
-5
@@ -104,11 +104,12 @@ static char *get_repo_path(const char *repo, int *is_bundle)
104
static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
105
{
106
const char *end = repo + strlen(repo), *start;
107
+ char *dir;
108
109
/*
109
- * Strip trailing slashes and /.git
110
+ * Strip trailing spaces, slashes and /.git
111
*/
111
- while (repo < end && is_dir_sep(end[-1]))
112
+ while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
113
end--;
114
if (end - repo > 5 && is_dir_sep(end[-5]) &&
115
!strncmp(end - 4, ".git", 4)) {
@@ -140,10 +141,33 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
141
if (is_bare) {
142
struct strbuf result = STRBUF_INIT;
143
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';
169
}
145
-
146
- return xstrndup(start, end - start);
170
+ return dir;
171
}
172
173
static void strip_trailing_slashes(char *dir)