Skip to content

Commit 1eb07d8

Browse files
sunshinecogitster
authored andcommitted
worktree: add: auto-vivify new branch when <branch> is omitted
As a convenience, when <branch> is omitted from "git worktree <path> <branch>" and neither -b nor -B is used, automatically create a new branch named after <path>, as if "-b $(basename <path>)" was specified. Thus, "git worktree add ../hotfix" creates a new branch named "hotfix" and associates it with new worktree "../hotfix". Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0f4af3b commit 1eb07d8

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Documentation/git-worktree.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-worktree - Manage multiple worktrees
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git worktree add' [-f] [--detach] [-b <new-branch>] <path> <branch>
12+
'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>]
1313
'git worktree prune' [-n] [-v] [--expire <expire>]
1414

1515
DESCRIPTION
@@ -45,11 +45,15 @@ pruning should be suppressed. See section "DETAILS" for more information.
4545

4646
COMMANDS
4747
--------
48-
add <path> <branch>::
48+
add <path> [<branch>]::
4949

5050
Create `<path>` and checkout `<branch>` into it. The new working directory
5151
is linked to the current repository, sharing everything except working
5252
directory specific files such as HEAD, index, etc.
53+
+
54+
If `<branch>` is omitted and neither `-b` nor `-B` is used, then, as a
55+
convenience, a new branch based at HEAD is created automatically, as if
56+
`-b $(basename <path>)` was specified.
5357

5458
prune::
5559

builtin/worktree.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,16 @@ static int add(int ac, const char **av, const char *prefix)
291291
die(_("-b and -B are mutually exclusive"));
292292
if (ac < 1 || ac > 2)
293293
usage_with_options(worktree_usage, options);
294-
if (ac < 2 && !new_branch && !new_branch_force)
295-
usage_with_options(worktree_usage, options);
296294

297295
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
298296
branch = ac < 2 ? "HEAD" : av[1];
299297

298+
if (ac < 2 && !new_branch && !new_branch_force) {
299+
int n;
300+
const char *s = worktree_basename(path, &n);
301+
new_branch = xstrndup(s, n);
302+
}
303+
300304
argv_array_push(&cmd, "checkout");
301305
if (force)
302306
argv_array_push(&cmd, "--ignore-other-worktrees");

t/t2025-worktree-add.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,18 @@ test_expect_success '"add -b" with <branch> omitted' '
145145
test_cmp_rev HEAD burble
146146
'
147147

148+
test_expect_success '"add" with <branch> omitted' '
149+
git worktree add wiffle/bat &&
150+
test_cmp_rev HEAD bat
151+
'
152+
153+
test_expect_success '"add" auto-vivify does not clobber existing branch' '
154+
test_commit c1 &&
155+
test_commit c2 &&
156+
git branch precious HEAD~1 &&
157+
test_must_fail git worktree add precious &&
158+
test_cmp_rev HEAD~1 precious &&
159+
test_path_is_missing precious
160+
'
161+
148162
test_done

0 commit comments

Comments
 (0)