aboutsummaryrefslogtreecommitdiff
path: root/builtin-clone.c
diff options
context:
space:
mode:
authorJohannes Sixt <johannes.sixt@telecom.at>2008-07-19 11:32:45 +0200
committerJunio C Hamano <gitster@pobox.com>2008-07-19 11:17:43 -0700
commitb8c5db35fc23913d72c52d990d399b8ef66b646f (patch)
treec8607c0851a2822d654c59fc715a7f0a277a68ad /builtin-clone.c
parentfe77b6959c47e1eaec0c96599b2c5ce7907707ec (diff)
downloadgit-b8c5db35fc23913d72c52d990d399b8ef66b646f.tar.gz
git-b8c5db35fc23913d72c52d990d399b8ef66b646f.tar.xz
builtin-clone: rewrite guess_dir_name()
The function has to do three small and independent tasks, but all of them were crammed into a single loop. This rewrites the function entirely by unrolling these tasks. We also now use is_dir_sep(c) instead of c == '/' to increase portability. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin-clone.c')
-rw-r--r--builtin-clone.c61
1 files changed, 32 insertions, 29 deletions
diff --git a/builtin-clone.c b/builtin-clone.c
index 8112716c1..352224591 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c
@@ -95,35 +95,38 @@ static char *get_repo_path(const char *repo, int *is_bundle)
static char *guess_dir_name(const char *repo, int is_bundle)
{
- const char *p, *start, *end, *limit;
- int after_slash_or_colon;
-
- /* Guess dir name from repository: strip trailing '/',
- * strip trailing '[:/]*.{git,bundle}', strip leading '.*[/:]'. */
-
- after_slash_or_colon = 1;
- limit = repo + strlen(repo);
- start = repo;
- end = limit;
- for (p = repo; p < limit; p++) {
- const char *prefix = is_bundle ? ".bundle" : ".git";
- if (!prefixcmp(p, prefix)) {
- if (!after_slash_or_colon)
- end = p;
- p += strlen(prefix) - 1;
- } else if (!prefixcmp(p, ".bundle")) {
- if (!after_slash_or_colon)
- end = p;
- p += 7;
- } else if (*p == '/' || *p == ':') {
- if (end == limit)
- end = p;
- after_slash_or_colon = 1;
- } else if (after_slash_or_colon) {
- start = p;
- end = limit;
- after_slash_or_colon = 0;
- }
+ const char *end = repo + strlen(repo), *start;
+
+ /*
+ * Strip trailing slashes and /.git
+ */
+ while (repo < end && is_dir_sep(end[-1]))
+ end--;
+ if (end - repo > 5 && is_dir_sep(end[-5]) &&
+ !strncmp(end - 4, ".git", 4)) {
+ end -= 5;
+ while (repo < end && is_dir_sep(end[-1]))
+ end--;
+ }
+
+ /*
+ * Find last component, but be prepared that repo could have
+ * the form "remote.example.com:foo.git", i.e. no slash
+ * in the directory part.
+ */
+ start = end;
+ while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
+ start--;
+
+ /*
+ * Strip .{bundle,git}.
+ */
+ if (is_bundle) {
+ if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
+ end -= 7;
+ } else {
+ if (end - start > 4 && !strncmp(end - 4, ".git", 4))
+ end -= 4;
}
return xstrndup(start, end - start);