Merge branch 'jg/dash-is-last-branch-in-worktree-add'
authorJunio C Hamano <gitster@pobox.com>
Mon, 20 Jun 2016 18:01:02 +0000 (11:01 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 20 Jun 2016 18:01:02 +0000 (11:01 -0700)
"git worktree add" learned that '-' can be used as a short-hand for
"@{-1}", the previous branch.

* jg/dash-is-last-branch-in-worktree-add:
worktree: allow "-" short-hand for @{-1} in add command

Documentation/git-worktree.txt
builtin/worktree.c
t/t2025-worktree-add.sh
index c62234538ba6be8c82b9a5b86c362606b80cde2a..23d8d2ace04e7867a5b147d3d59c2d1b0d7a36ae 100644 (file)
@@ -48,7 +48,8 @@ add <path> [<branch>]::
 
 Create `<path>` and checkout `<branch>` into it. The new working directory
 is linked to the current repository, sharing everything except working
-directory specific files such as HEAD, index, etc.
+directory specific files such as HEAD, index, etc. `-` may also be
+specified as `<branch>`; it is synonymous with `@{-1}`.
 +
 If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used,
 then, as a convenience, a new branch based at HEAD is created automatically,
index 96a2834a18be8ee03427cfdd408914ea71d59243..e3199a22e5a1111366b03ad0a2564e8d4f0790c7 100644 (file)
@@ -340,6 +340,9 @@ static int add(int ac, const char **av, const char *prefix)
        path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
        branch = ac < 2 ? "HEAD" : av[1];
 
+       if (!strcmp(branch, "-"))
+               branch = "@{-1}";
+
        opts.force_new_branch = !!new_branch_force;
        if (opts.force_new_branch) {
                struct strbuf symref = STRBUF_INIT;
index 3a22fc55fc324fbfbfdcdf39fb71e829c89729b5..4bcc335a19f9d7864560945ed0f430728d0f1986 100755 (executable)
@@ -20,6 +20,22 @@ test_expect_success '"add" an existing empty worktree' '
        git worktree add --detach existing_empty master
 '
 
+test_expect_success '"add" using shorthand - fails when no previous branch' '
+       test_must_fail git worktree add existing_short -
+'
+
+test_expect_success '"add" using - shorthand' '
+       git checkout -b newbranch &&
+       echo hello >myworld &&
+       git add myworld &&
+       git commit -m myworld &&
+       git checkout master &&
+       git worktree add short-hand - &&
+       echo refs/heads/newbranch >expect &&
+       git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success '"add" refuses to checkout locked branch' '
        test_must_fail git worktree add zere master &&
        ! test -d zere &&