Skip to content

Commit 6427f87

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c27002 commit 6427f87

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

builtin/worktree.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,21 @@ static void print_preparing_worktree_line(int detach,
387387
}
388388
}
389389

390+
static const char *dwim_branch(const char *path, const char **new_branch)
391+
{
392+
int n;
393+
const char *s = worktree_basename(path, &n);
394+
*new_branch = xstrndup(s, n);
395+
UNLEAK(*new_branch);
396+
if (guess_remote) {
397+
struct object_id oid;
398+
const char *remote =
399+
unique_tracking_name(*new_branch, &oid);
400+
return remote;
401+
}
402+
return NULL;
403+
}
404+
390405
static int add(int ac, const char **av, const char *prefix)
391406
{
392407
struct add_opts opts;
@@ -439,17 +454,9 @@ static int add(int ac, const char **av, const char *prefix)
439454
}
440455

441456
if (ac < 2 && !new_branch && !opts.detach) {
442-
int n;
443-
const char *s = worktree_basename(path, &n);
444-
new_branch = xstrndup(s, n);
445-
UNLEAK(new_branch);
446-
if (guess_remote) {
447-
struct object_id oid;
448-
const char *remote =
449-
unique_tracking_name(new_branch, &oid);
450-
if (remote)
451-
branch = remote;
452-
}
457+
const char *s = dwim_branch(path, &new_branch);
458+
if (s)
459+
branch = s;
453460
}
454461

455462
if (ac == 2 && !new_branch && !opts.detach) {

0 commit comments

Comments
 (0)