worktree: factor out dwim_branch function
Factor out a dwim_branch function, which takes care of the dwim'ery in 'git worktree add <path>'. It's not too much code currently, but we're adding a new kind of dwim in a subsequent patch, at which point it makes more sense to have it as a separate function. Factor it out now to reduce the patch noise in the next patch. Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
2c27002a0a
commit
6427f87186
@ -387,6 +387,21 @@ static void print_preparing_worktree_line(int detach,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *dwim_branch(const char *path, const char **new_branch)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
const char *s = worktree_basename(path, &n);
|
||||||
|
*new_branch = xstrndup(s, n);
|
||||||
|
UNLEAK(*new_branch);
|
||||||
|
if (guess_remote) {
|
||||||
|
struct object_id oid;
|
||||||
|
const char *remote =
|
||||||
|
unique_tracking_name(*new_branch, &oid);
|
||||||
|
return remote;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static int add(int ac, const char **av, const char *prefix)
|
static int add(int ac, const char **av, const char *prefix)
|
||||||
{
|
{
|
||||||
struct add_opts opts;
|
struct add_opts opts;
|
||||||
@ -439,17 +454,9 @@ static int add(int ac, const char **av, const char *prefix)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ac < 2 && !new_branch && !opts.detach) {
|
if (ac < 2 && !new_branch && !opts.detach) {
|
||||||
int n;
|
const char *s = dwim_branch(path, &new_branch);
|
||||||
const char *s = worktree_basename(path, &n);
|
if (s)
|
||||||
new_branch = xstrndup(s, n);
|
branch = s;
|
||||||
UNLEAK(new_branch);
|
|
||||||
if (guess_remote) {
|
|
||||||
struct object_id oid;
|
|
||||||
const char *remote =
|
|
||||||
unique_tracking_name(new_branch, &oid);
|
|
||||||
if (remote)
|
|
||||||
branch = remote;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ac == 2 && !new_branch && !opts.detach) {
|
if (ac == 2 && !new_branch && !opts.detach) {
|
||||||
|
Loading…
Reference in New Issue
Block a user