completion: fix __git_complete_worktree_paths

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Feb 27, 2024 at 22:06 UTC 3574816d98a332187fc331826cdf170b0adf3a47
2 files changed +24 -1
contrib/completion/git-completion.bash
+1 -1
@@ -3571,7 +3571,7 @@ __git_complete_worktree_paths ()
3571 # Generate completion reply from worktree list skipping the first
3572 # entry: it's the path of the main worktree, which can't be moved,
3573 # removed, locked, etc.
3574 - __gitcomp_nl "$(git worktree list --porcelain |
3574 + __gitcomp_nl "$(__git worktree list --porcelain |
3575 sed -n -e '2,$ s/^worktree //p')"
3576 }
3577
t/t9902-completion.sh
+23
@@ -1263,6 +1263,29 @@ test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1263 test_cmp expected out
1264 '
1265
1266 +test_expect_success '__git_complete_worktree_paths' '
1267 + test_when_finished "git worktree remove other_wt" &&
1268 + git worktree add --orphan other_wt &&
1269 + run_completion "git worktree remove " &&
1270 + grep other_wt out
1271 +'
1272 +
1273 +test_expect_success '__git_complete_worktree_paths - not a git repository' '
1274 + (
1275 + cd non-repo &&
1276 + GIT_CEILING_DIRECTORIES="$ROOT" &&
1277 + export GIT_CEILING_DIRECTORIES &&
1278 + test_completion "git worktree remove " ""
1279 + )
1280 +'
1281 +
1282 +test_expect_success '__git_complete_worktree_paths with -C' '
1283 + test_when_finished "git -C otherrepo worktree remove otherrepo_wt" &&
1284 + git -C otherrepo worktree add --orphan otherrepo_wt &&
1285 + run_completion "git -C otherrepo worktree remove " &&
1286 + grep otherrepo_wt out
1287 +'
1288 +
1289 test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1290 test_completion "git switch " <<-\EOF
1291 branch-in-other Z