Skip to content

Commit

Permalink
worktree: extract basename computation to new function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Eric Sunshine authored and Junio C Hamano committed Jul 6, 2015
1 parent 0ca560c commit f5682b2
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions builtin/worktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ static void remove_junk_on_signal(int signo)
raise(signo);
}

static const char *worktree_basename(const char *path, int *olen)
{
const char *name;
int len;

len = strlen(path);
while (len && is_dir_sep(path[len - 1]))
len--;

for (name = path + len - 1; name > path; name--)
if (is_dir_sep(*name)) {
name++;
break;
}

*olen = len;
return name;
}

static int add_worktree(const char *path, const char **child_argv)
{
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
Expand All @@ -165,15 +184,7 @@ static int add_worktree(const char *path, const char **child_argv)
if (file_exists(path) && !is_empty_dir(path))
die(_("'%s' already exists"), path);

len = strlen(path);
while (len && is_dir_sep(path[len - 1]))
len--;

for (name = path + len - 1; name > path; name--)
if (is_dir_sep(*name)) {
name++;
break;
}
name = worktree_basename(path, &len);
strbuf_addstr(&sb_repo,
git_path("worktrees/%.*s", (int)(path + len - name), name));
len = sb_repo.len;
Expand Down

0 comments on commit f5682b2

Please sign in to comment.