{"thread":{"id":"42378","subject":"[PATCH] Allow the short-hand - replacing @{-1} in git worktree add","startedAt":"2016-05-18T22:49:18Z","lastAt":"2016-05-18T22:49:18Z","messageCount":1,"participants":["Jordan DE GEA"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"286947","messageId":"1463611758-1210-1-git-send-email-jordan.de-gea@ensimag.grenoble-inp.fr","threadId":"42378","inReplyTo":null,"subject":"[PATCH] Allow the short-hand - replacing @{-1} in git worktree add","fromName":"Jordan DE GEA","fromEmail":"jordan.de-gea@ensimag.grenoble-inp.fr","sentAt":"2016-05-18T22:49:18Z","receivedAt":"2016-05-18T22:49:18Z","isPatch":true,"sender":{"key":"jordan.de-gea@ensimag.grenoble-inp.fr","avatar":null},"body":"Since `git worktree add` uses `git checkout` when `[<branch>]` is used,\nand `git checkout -` is already supported, it makes sense to allow the\nsame shortcut in `git worktree add`.\n\nSigned-off-by: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>, Jordan DE GEA <jordan.de-gea@ensimag.grenoble-inp.fr>\n---\n Documentation/git-worktree.txt |  3 ++-\n builtin/worktree.c             |  3 +++\n t/t2025-worktree-add.sh        | 17 +++++++++++++++++\n 3 files changed, 22 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex c622345..28dc559 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -48,7 +48,8 @@ add <path> [<branch>]::\n \n Create `<path>` and checkout `<branch>` into it. The new working directory\n is linked to the current repository, sharing everything except working\n-directory specific files such as HEAD, index, etc.\n+directory specific files such as HEAD, index, etc. You may also specify\n+`-` as `<branch>` which is synonymous with `\"@{-1}\"`.\n +\n If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used,\n then, as a convenience, a new branch based at HEAD is created automatically,\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex d8e3795..d800d47 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -340,6 +340,9 @@ static int add(int ac, const char **av, const char *prefix)\n \tpath = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];\n \tbranch = ac < 2 ? \"HEAD\" : av[1];\n \n+\tif (!strcmp(branch, \"-\"))\n+\t\tbranch = \"@{-1}\";\n+\n \topts.force_new_branch = !!new_branch_force;\n \tif (opts.force_new_branch) {\n \t\tstruct strbuf symref = STRBUF_INIT;\ndiff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\nindex 3acb992..3f8437b 100755\n--- a/t/t2025-worktree-add.sh\n+++ b/t/t2025-worktree-add.sh\n@@ -18,6 +18,23 @@ test_expect_success '\"add\" an existing empty worktree' '\n \tgit worktree add --detach existing_empty master\n '\n \n+test_expect_success '\"add\" using shorthand - fails when no previous branch' '\n+\ttest_must_fail git worktree add existing -\n+'\n+\n+test_expect_success '\"add\" using shorthand' '\n+\tgit checkout -b newbranch &&\n+\techo hello >myworld &&\n+\tgit add myworld &&\n+\tgit commit -m myworld &&\n+\tgit checkout master &&\n+\tgit worktree add short-hand - && \n+\t(\n+\t\tcd short-hand &&\n+\t\tgit status | head -1 | grep newbranch\n+\t)\n+'\n+\n test_expect_success '\"add\" refuses to checkout locked branch' '\n \ttest_must_fail git worktree add zere master &&\n \t! test -d zere &&\n-- \n2.7.4 (Apple Git-66)\n"}]}