worktree: add: make -b/-B default to HEAD when <branch> is omitted
As a convenience, like "git branch" and "git checkout -b", make "git worktree add -b <newbranch> <path> <branch>" default to HEAD when <branch> is omitted. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eric Sunshine committed
Jul 6, 2015 at 13:30 UTC
0f4af3b9ea1fc62e445271bb2e7fbb8e1ac230b7
3 files changed
+10
-2
Documentation/git-worktree.txt
+1
@@ -68,6 +68,7 @@ OPTIONS
68
-B <new-branch>::
69
With `add`, create a new branch named `<new-branch>` starting at
70
`<branch>`, and check out `<new-branch>` into the new worktree.
71
+ If `<branch>` is omitted, it defaults to HEAD.
72
By default, `-b` refuses to create a new branch if it already
73
exists. `-B` overrides this safeguard, resetting `<new-branch>` to
74
`<branch>`.
builtin/worktree.c
+4
-2
@@ -289,11 +289,13 @@ static int add(int ac, const char **av, const char *prefix)
289
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
290
if (new_branch && new_branch_force)
291
die(_("-b and -B are mutually exclusive"));
292
- if (ac != 2)
292
+ if (ac < 1 || ac > 2)
293
+ usage_with_options(worktree_usage, options);
294
+ if (ac < 2 && !new_branch && !new_branch_force)
295
usage_with_options(worktree_usage, options);
296
297
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
296
- branch = av[1];
298
+ branch = ac < 2 ? "HEAD" : av[1];
299
300
argv_array_push(&cmd, "checkout");
301
if (force)
t/t2025-worktree-add.sh
+5
@@ -140,4 +140,9 @@ test_expect_success '"add" from relative HEAD' '
140
test_cmp expected actual
141
'
142
143
+test_expect_success '"add -b" with <branch> omitted' '
144
+ git worktree add -b burble flornk &&
145
+ test_cmp_rev HEAD burble
146
+'
147
+
148
test_done