diff options
-rw-r--r-- | builtin/worktree.c | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c index 39bf1ea865..6bd32b6090 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -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) { 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) { - 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); - if (remote) - branch = remote; - } + const char *s = dwim_branch(path, &new_branch); + if (s) + branch = s; } if (ac == 2 && !new_branch && !opts.detach) { |