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.gumme...@gmail.com>
---
 builtin/worktree.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index f686ee1440..47189d50dd 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -363,6 +363,20 @@ static int add_worktree(const char *path, const char 
*refname,
        return ret;
 }
 
+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);
+       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;
@@ -415,16 +429,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);
-               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) {
-- 
2.16.1.78.ga2082135d8

Reply via email to