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

Re: [PATCH v2] completion: fix __git_complete_worktree_paths

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 27, 2024, 08:21 UTC
Message-ID
<Zd2bnJZtgW2tFMFn@tanuki>
In-Reply-To
<eaf3649e-30cf-4eba-befa-5be826c828a8@gmail.com>
On Mon, Feb 26, 2024 at 12:53:31AM +0100, Rubén Justo wrote:
Show 75 quoted lines
> Use __git to invoke "worktree list" in __git_complete_worktree_paths, to
> respect any "-C" and "--git-dir" options present on the command line.
> 
> Signed-off-by: Rubén Justo <rjusto@gmail.com>
> ---
> 
> I stumbled upon this in a situation like:
> 
>    $ git init /tmp/foo && cd /tmp/foo
>    $ git worktree add --orphan foo_wt
> 
>    $ git init /tmp/bar && cd /tmp/bar
>    $ git worktree add --orphan bar_wt
> 
>    $ cd /tmp/foo
>    $ git -C /tmp/bar worktree remove <TAB>
>    ... expecting /tmp/bar/wt, but ...
>    $ git -C /tmp/bar worktree remove /tmp/foo_wt
> 
> In this iteration, v2, some tests are included.
> 
> The function __git was introduced in 1cd23e9e05 (completion: don't use
> __gitdir() for git commands, 2017-02-03).  It is a small function, so
> I'll include here to ease the review of this patch:
> 
> 	# Runs git with all the options given as argument, respecting any
> 	# '--git-dir=<path>' and '-C <path>' options present on the command line
> 	__git ()
> 	{
> 		git ${__git_C_args:+"${__git_C_args[@]}"} \
> 			${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
> 	}
> 
> 
>  contrib/completion/git-completion.bash |  2 +-
>  t/t9902-completion.sh                  | 24 ++++++++++++++++++++++++
>  2 files changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 444b3efa63..86e55dc67f 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -3571,7 +3571,7 @@ __git_complete_worktree_paths ()
>  	# Generate completion reply from worktree list skipping the first
>  	# entry: it's the path of the main worktree, which can't be moved,
>  	# removed, locked, etc.
> -	__gitcomp_nl "$(git worktree list --porcelain |
> +	__gitcomp_nl "$(__git worktree list --porcelain |
>  		sed -n -e '2,$ s/^worktree //p')"
>  }
>  
> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> index b16c284181..7c0f82f31a 100755
> --- a/t/t9902-completion.sh
> +++ b/t/t9902-completion.sh
> @@ -1263,6 +1263,30 @@ test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
>  	test_cmp expected out
>  '
>  
> +test_expect_success '__git_complete_worktree_paths' '
> +	test_when_finished "git worktree remove other_wt" &&
> +	git worktree add --orphan other_wt &&
> +	run_completion "git worktree remove " &&
> +	grep other_wt out
> +'
> +
> +test_expect_success '__git_complete_worktree_paths - not a git repository' '
> +	(
> +		cd non-repo &&
> +		GIT_CEILING_DIRECTORIES="$ROOT" &&
> +		export GIT_CEILING_DIRECTORIES &&
> +		test_completion "git worktree remove " "" 2>err &&
> +		test_must_be_empty err
> +	)
> +'

If I understand correctly, we assume that the repo isn't detected here, and thus we will fail to complete the command. We don't want an error message though, which we assert. But do we also want to assert that there is no output on stdout?

> +
> +test_expect_success '__git_complete_worktree_paths with -C' '
> +	test_when_finished "rm -rf to_delete" &&

What does this delete? I don't see "to_delete" being created as part of this test.

> +	git -C otherrepo worktree add --orphan otherrepo_wt &&
> +	run_completion "git -C otherrepo worktree remove " &&
> +	grep otherrepo_wt out

And as far as I can see, we don't write to "out" in this test, either. So I think we're accidentally relying on state by the first test here.

Patrick
Show 8 quoted lines
> +'
> +
>  test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
>  	test_completion "git switch " <<-\EOF
>  	branch-in-other Z
> -- 
> 2.44.0.1.g0da3aa8f7f
> 
Previous: Rubén JustoNext: Rubén Justo
Message 3 of 5 in “completion: fix __git_complete_worktree_paths”
  1. completion: fix __git_complete_worktree_pathsRubén Justo, Feb 25, 2024
  2. completion: fix __git_complete_worktree_pathsRubén Justo, Feb 25, 2024
  3. Patrick SteinhardtFeb 27, 2024
  4. Rubén JustoFeb 27, 2024
  5. completion: fix __git_complete_worktree_pathsRubén Justo, Feb 27, 2024

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.