worktree: allow "-" short-hand for @{-1} in add command
Since `git worktree add` uses `git checkout` when `[<branch>]` is used, and `git checkout -` is already supported, it makes sense to allow the same shortcut in `git worktree add`. Signed-off-by: Jordan DE GEA <jordan.de-gea@grenoble-inp.org> Signed-off-by: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jordan DE GEA committed
May 27, 2016 at 15:17 UTC
1a450e2fd1f82311b214851d5b097b74c8fb0ade
3 files changed
+21
-1
Documentation/git-worktree.txt
+2
-1
@@ -48,7 +48,8 @@ add <path> [<branch>]::
48
49
Create `<path>` and checkout `<branch>` into it. The new working directory
50
is linked to the current repository, sharing everything except working
51
-directory specific files such as HEAD, index, etc.
51
+directory specific files such as HEAD, index, etc. `-` may also be
52
+specified as `<branch>`; it is synonymous with `@{-1}`.
53
+
54
If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used,
55
then, as a convenience, a new branch based at HEAD is created automatically,
builtin/worktree.c
+3
@@ -340,6 +340,9 @@ static int add(int ac, const char **av, const char *prefix)
340
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
341
branch = ac < 2 ? "HEAD" : av[1];
342
343
+ if (!strcmp(branch, "-"))
344
+ branch = "@{-1}";
345
+
346
opts.force_new_branch = !!new_branch_force;
347
if (opts.force_new_branch) {
348
struct strbuf symref = STRBUF_INIT;
t/t2025-worktree-add.sh
+16
@@ -20,6 +20,22 @@ test_expect_success '"add" an existing empty worktree' '
20
git worktree add --detach existing_empty master
21
'
22
23
+test_expect_success '"add" using shorthand - fails when no previous branch' '
24
+ test_must_fail git worktree add existing_short -
25
+'
26
+
27
+test_expect_success '"add" using - shorthand' '
28
+ git checkout -b newbranch &&
29
+ echo hello >myworld &&
30
+ git add myworld &&
31
+ git commit -m myworld &&
32
+ git checkout master &&
33
+ git worktree add short-hand - &&
34
+ echo refs/heads/newbranch >expect &&
35
+ git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
36
+ test_cmp expect actual
37
+'
38
+
39
test_expect_success '"add" refuses to checkout locked branch' '
40
test_must_fail git worktree add zere master &&
41
! test -d zere &&