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>
Johannes Sixt committed
Jul 19, 2008 at 11:32 UTC
b8c5db35fc23913d72c52d990d399b8ef66b646f
1 file changed
+32
-29
builtin-clone.c
+32
-29
@@ -95,35 +95,38 @@ static char *get_repo_path(const char *repo, int *is_bundle)
95
96
static char *guess_dir_name(const char *repo, int is_bundle)
97
{
98
- const char *p, *start, *end, *limit;
99
- int after_slash_or_colon;
100
-
101
- /* Guess dir name from repository: strip trailing '/',
102
- * strip trailing '[:/]*.{git,bundle}', strip leading '.*[/:]'. */
103
-
104
- after_slash_or_colon = 1;
105
- limit = repo + strlen(repo);
106
- start = repo;
107
- end = limit;
108
- for (p = repo; p < limit; p++) {
109
- const char *prefix = is_bundle ? ".bundle" : ".git";
110
- if (!prefixcmp(p, prefix)) {
111
- if (!after_slash_or_colon)
112
- end = p;
113
- p += strlen(prefix) - 1;
114
- } else if (!prefixcmp(p, ".bundle")) {
115
- if (!after_slash_or_colon)
116
- end = p;
117
- p += 7;
118
- } else if (*p == '/' || *p == ':') {
119
- if (end == limit)
120
- end = p;
121
- after_slash_or_colon = 1;
122
- } else if (after_slash_or_colon) {
123
- start = p;
124
- end = limit;
125
- after_slash_or_colon = 0;
126
- }
98
+ const char *end = repo + strlen(repo), *start;
99
+
100
+ /*
101
+ * Strip trailing slashes and /.git
102
+ */
103
+ while (repo < end && is_dir_sep(end[-1]))
104
+ end--;
105
+ if (end - repo > 5 && is_dir_sep(end[-5]) &&
106
+ !strncmp(end - 4, ".git", 4)) {
107
+ end -= 5;
108
+ while (repo < end && is_dir_sep(end[-1]))
109
+ end--;
110
+ }
111
+
112
+ /*
113
+ * Find last component, but be prepared that repo could have
114
+ * the form "remote.example.com:foo.git", i.e. no slash
115
+ * in the directory part.
116
+ */
117
+ start = end;
118
+ while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
119
+ start--;
120
+
121
+ /*
122
+ * Strip .{bundle,git}.
123
+ */
124
+ if (is_bundle) {
125
+ if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
126
+ end -= 7;
127
+ } else {
128
+ if (end - start > 4 && !strncmp(end - 4, ".git", 4))
129
+ end -= 4;
130
}
131
132
return xstrndup(start, end - start);