git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] worktree: allow "-" short-hand for @{-1} in add command

From
Junio C Hamano <gitster@pobox.com>
Date
May 26, 2016, 18:54 UTC
Message-ID
<xmqqshx4vedr.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1464263662-1290-1-git-send-email-jordan.de-gea@grenoble-inp.org>
Jordan DE GEA <jordan.de-gea@grenoble-inp.org> writes:
Show 5 quoted lines
> From: Jordan DE GEA <jordan.de-gea@ensimag.grenoble-inp.fr>
>
> 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`.
OK.
Show 20 quoted lines
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>
> Signed-off-by: Jordan DE GEA <jordan.de-gea@grenoble-inp.org>
> ---
>  Documentation/git-worktree.txt |  3 ++-
>  builtin/worktree.c             |  3 +++
>  t/t2025-worktree-add.sh        | 18 ++++++++++++++++++
>  3 files changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> index c622345..48e5fdf 100644
> --- a/Documentation/git-worktree.txt
> +++ b/Documentation/git-worktree.txt
> @@ -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. The last branch you 
> +were on can be specify with `-` as `<branch>` which is synonymous with `"@{-1}"`.

You meant "can be specified", I think. Fixing it would make the line a bit too long, so fold it around the word "synonymous".

Show 11 quoted lines
> diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
> index 3acb992..b713efb 100755
> --- a/t/t2025-worktree-add.sh
> +++ b/t/t2025-worktree-add.sh
> @@ -18,6 +18,24 @@ 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 -
> +'

Just an observation, but the error message we would see here might be interesting.

Show 7 quoted lines
> +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 - &&
> +	cd short-hand &&
> +	test $(git rev-parse --symbolic-full-name HEAD) = "refs/heads/newbranch"
Broken &&-chain.
> +	branch=$(cd short-hand && git rev-parse --symbolic-full-name HEAD) &&
> +	test "$branch" = refs/heads/newbranch &&
> +	cd ..

If any of the command between "cd short-hand" and "cd .." failed, after correcting the broken &&-chain, the next test will end up running in short-hand directory, which it is not expecting. A canonical way to avoid this problem is to replace the above with:

	...
        git worktree add short-hand - &&
        (
		cd short-hand &&
                ...
                test "$branch" = refs/heads/newbranch
	)

In this particular case, alternatively, you could also do something like this:

        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
and it would be sufficient.

It is not immediately obvious to me why you have two copies of the same test in your patch to see where HEAD points at. If the reason is because you suspect that "git -C $there" form may give subtly different behaviour and wanted to test both, then you could do something like this:

        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 &&
	(cd short-hand && git rev-parse --symbolic-full-name HEAD) >actual &&
	test_cmp expect actual

but I do not think that is necessary. This test is not about "does rev-parse --symbolic-full-name work correctly with 'git -C $there'?"

Thanks.
Previous: Jordan DE GEANext: Matthieu Moy
Message 2 of 7 in “worktree: allow "-" short-hand for @{-1} in add command”
  1. worktree: allow "-" short-hand for @{-1} in add commandJordan DE GEA, May 26, 2016
  2. Junio C HamanoMay 26, 2016
  3. Matthieu MoyMay 27, 2016
  4. Junio C HamanoMay 27, 2016
  5. Jordan DE GEAMay 27, 2016
  6. worktree: allow "-" short-hand for @{-1} in add commandJordan DE GEA, May 27, 2016
  7. Junio C HamanoMay 27, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.