git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Patrick Steinhardt <ps@pks.im>
Subject: [PATCH v3] completion: fix __git_complete_worktree_paths
Date: Tue, 27 Feb 2024 22:06:49 +0100	[thread overview]
Message-ID: <b8f09e20-d0d3-4e0b-afe2-31affeb61052@gmail.com> (raw)
In-Reply-To: <eaf3649e-30cf-4eba-befa-5be826c828a8@gmail.com>

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

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
        }

Here are the changes since v2:

"test_completion" already asserts if something is printed on stderr.
Therefore "test_must_be_empty err" does not make sense.  Removed.  This
makes the test_completion with an empty response, more valuable, IMO.
But I'm open to change it.

The second test_when_finished has been fixed.

Thanks in advance for your review.

Range-diff against v2:
1:  4cc071fb8e ! 1:  de5d2ca1d3 completion: fix __git_complete_worktree_paths
    @@ t/t9902-completion.sh: test_expect_success '__git_complete_fetch_refspecs - full
     +		cd non-repo &&
     +		GIT_CEILING_DIRECTORIES="$ROOT" &&
     +		export GIT_CEILING_DIRECTORIES &&
    -+		test_completion "git worktree remove " "" 2>err &&
    -+		test_must_be_empty err
    ++		test_completion "git worktree remove " ""
     +	)
     +'
     +
     +test_expect_success '__git_complete_worktree_paths with -C' '
    -+	test_when_finished "rm -rf to_delete" &&
    ++	test_when_finished "git -C otherrepo worktree remove otherrepo_wt" &&
     +	git -C otherrepo worktree add --orphan otherrepo_wt &&
     +	run_completion "git -C otherrepo worktree remove " &&
     +	grep otherrepo_wt out

 contrib/completion/git-completion.bash |  2 +-
 t/t9902-completion.sh                  | 23 +++++++++++++++++++++++
 2 files changed, 24 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..aae2cbeeea 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1263,6 +1263,29 @@ 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 " ""
+	)
+'
+
+test_expect_success '__git_complete_worktree_paths with -C' '
+	test_when_finished "git -C otherrepo worktree remove otherrepo_wt" &&
+	git -C otherrepo worktree add --orphan otherrepo_wt &&
+	run_completion "git -C otherrepo worktree remove " &&
+	grep otherrepo_wt out
+'
+
 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


      parent reply	other threads:[~2024-02-27 21:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-25  8:16 [PATCH] completion: fix __git_complete_worktree_paths Rubén Justo
2024-02-25 23:53 ` [PATCH v2] " Rubén Justo
2024-02-27  8:21   ` Patrick Steinhardt
2024-02-27 18:09     ` Rubén Justo
2024-02-27 21:06   ` Rubén Justo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b8f09e20-d0d3-4e0b-afe2-31affeb61052@gmail.com \
    --to=rjusto@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).