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>
This commit is contained in:
Jordan DE GEA 2016-05-27 15:17:08 +02:00 коммит произвёл Junio C Hamano
Родитель f3913c2d03
Коммит 1a450e2fd1
3 изменённых файлов: 21 добавлений и 1 удалений

Просмотреть файл

@ -48,7 +48,8 @@ add <path> [<branch>]::
Create `<path>` and checkout `<branch>` into it. The new working directory Create `<path>` and checkout `<branch>` into it. The new working directory
is linked to the current repository, sharing everything except working 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, 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, then, as a convenience, a new branch based at HEAD is created automatically,

Просмотреть файл

@ -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]; path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
branch = ac < 2 ? "HEAD" : av[1]; branch = ac < 2 ? "HEAD" : av[1];
if (!strcmp(branch, "-"))
branch = "@{-1}";
opts.force_new_branch = !!new_branch_force; opts.force_new_branch = !!new_branch_force;
if (opts.force_new_branch) { if (opts.force_new_branch) {
struct strbuf symref = STRBUF_INIT; struct strbuf symref = STRBUF_INIT;

Просмотреть файл

@ -20,6 +20,22 @@ test_expect_success '"add" an existing empty worktree' '
git worktree add --detach existing_empty master 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_expect_success '"add" refuses to checkout locked branch' '
test_must_fail git worktree add zere master && test_must_fail git worktree add zere master &&
! test -d zere && ! test -d zere &&