clone: simplify string handling in guess_dir_name()

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sebastian Schuberth committed Jul 9, 2015 at 18:24 UTC 7e837c6477b59cc51756e241ccfee76068e5e379
1 file changed +6 -13
builtin/clone.c
+6 -13
@@ -147,6 +147,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
147 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
148 {
149 const char *end = repo + strlen(repo), *start;
150 + size_t len;
151 char *dir;
152
153 /*
@@ -173,20 +174,12 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
174 /*
175 * Strip .{bundle,git}.
176 */
176 - if (is_bundle) {
177 - if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
178 - end -= 7;
179 - } else {
180 - if (end - start > 4 && !strncmp(end - 4, ".git", 4))
181 - end -= 4;
182 - }
177 + strip_suffix(start, is_bundle ? ".bundle" : ".git" , &len);
178
184 - if (is_bare) {
185 - struct strbuf result = STRBUF_INIT;
186 - strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
187 - dir = strbuf_detach(&result, NULL);
188 - } else
189 - dir = xstrndup(start, end - start);
179 + if (is_bare)
180 + dir = xstrfmt("%.*s.git", (int)len, start);
181 + else
182 + dir = xstrndup(start, len);
183 /*
184 * Replace sequences of 'control' characters and whitespace
185 * with one ascii space, remove leading and trailing spaces.