worktree: extract basename computation to new function

A subsequent patch will also need to compute the basename of the new worktree, so factor out this logic into a new function. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 6, 2015 at 13:30 UTC f5682b2a861cbde6e9aca324caa5e2d36c49569a
1 file changed +20 -9
builtin/worktree.c
+20 -9
@@ -152,6 +152,25 @@ static void remove_junk_on_signal(int signo)
152 raise(signo);
153 }
154
155 +static const char *worktree_basename(const char *path, int *olen)
156 +{
157 + const char *name;
158 + int len;
159 +
160 + len = strlen(path);
161 + while (len && is_dir_sep(path[len - 1]))
162 + len--;
163 +
164 + for (name = path + len - 1; name > path; name--)
165 + if (is_dir_sep(*name)) {
166 + name++;
167 + break;
168 + }
169 +
170 + *olen = len;
171 + return name;
172 +}
173 +
174 static int add_worktree(const char *path, const char **child_argv)
175 {
176 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
@@ -165,15 +184,7 @@ static int add_worktree(const char *path, const char **child_argv)
184 if (file_exists(path) && !is_empty_dir(path))
185 die(_("'%s' already exists"), path);
186
168 - len = strlen(path);
169 - while (len && is_dir_sep(path[len - 1]))
170 - len--;
171 -
172 - for (name = path + len - 1; name > path; name--)
173 - if (is_dir_sep(*name)) {
174 - name++;
175 - break;
176 - }
187 + name = worktree_basename(path, &len);
188 strbuf_addstr(&sb_repo,
189 git_path("worktrees/%.*s", (int)(path + len - name), name));
190 len = sb_repo.len;