Skip to content

Commit 35ba83c

Browse files
moygitster
authored andcommitted
git-completion.bash: zsh does not implement function redirection correctly
A recent change added functions whose entire standard error stream is redirected to /dev/null using a construct that is valid POSIX.1 but is not widely used: funcname () { cd "$1" && run some command "$2" } 2>/dev/null Even though this file is "git-completion.bash", zsh completion support dot-sources it (instead of asking bash to grok it like tcsh completion does), and zsh does not implement this redirection correctly. With zsh, trying to complete an inexistant directory gave this: git add no-such-dir/__git_ls_files_helper:cd:2: no such file or directory: no-such-dir/ Also these functions use "cd" to first go somewhere else before running a command, but the location the caller wants them to go that is given as an argument to them should not be affected by CDPATH variable the users may have set for their interactive session. To fix both of these, wrap the body of the function in a subshell, unset CDPATH at the beginning of the subshell, and redirect the standard error stream of the subshell to /dev/null. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 926eb7b commit 35ba83c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

contrib/completion/git-completion.bash

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,24 @@ __git_index_file_list_filter ()
299299
# the second argument.
300300
__git_ls_files_helper ()
301301
{
302-
# NOTE: $2 is not quoted in order to support multiple options
303-
cd "$1" && git ls-files --exclude-standard $2
304-
} 2>/dev/null
302+
(
303+
test -n "${CDPATH+set}" && unset CDPATH
304+
# NOTE: $2 is not quoted in order to support multiple options
305+
cd "$1" && git ls-files --exclude-standard $2
306+
) 2>/dev/null
307+
}
305308

306309

307310
# Execute git diff-index, returning paths relative to the directory
308311
# specified in the first argument, and using the tree object id
309312
# specified in the second argument.
310313
__git_diff_index_helper ()
311314
{
312-
cd "$1" && git diff-index --name-only --relative "$2"
313-
} 2>/dev/null
315+
(
316+
test -n "${CDPATH+set}" && unset CDPATH
317+
cd "$1" && git diff-index --name-only --relative "$2"
318+
) 2>/dev/null
319+
}
314320

315321
# __git_index_files accepts 1 or 2 arguments:
316322
# 1: Options to pass to ls-files (required).

0 commit comments

Comments
 (0)