Skip to content

Commit f5682b2

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ca560c commit f5682b2

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

builtin/worktree.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ static void remove_junk_on_signal(int signo)
152152
raise(signo);
153153
}
154154

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+
155174
static int add_worktree(const char *path, const char **child_argv)
156175
{
157176
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)
165184
if (file_exists(path) && !is_empty_dir(path))
166185
die(_("'%s' already exists"), path);
167186

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);
177188
strbuf_addstr(&sb_repo,
178189
git_path("worktrees/%.*s", (int)(path + len - name), name));
179190
len = sb_repo.len;

0 commit comments

Comments
 (0)