From 7f5550445099f501dd16299c49f59a0dd949d237 Mon Sep 17 00:00:00 2001 From: Zakariyah Ali Date: Sat, 20 Jun 2026 17:55:55 +0000 Subject: [PATCH 1/2] completion: hide dotfiles for selected path completion The completion helper for index paths uses git ls-files rather than shell filename completion. As a result, leading-dot paths such as a tracked .gitignore were offered even when the user had not started the path with ".". Hide leading-dot path components for git rm, git mv, and git ls-files when completing an empty path component. Explicit dot completion is still preserved, so git rm . can still complete .gitignore. This matches standard shell filename completion behavior, where dotfiles are hidden by default unless the user starts their input with a dot. This also resolves four TODO comments in t/9902-completion.sh which have been present since 2013 (commit ddf07bddef9a, "completion: add file completion tests", 2013-04-27), expecting that .gitignore would not be shown when completing on an empty path component. Signed-off-by: Zakariyah Ali Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 36 +++++++++++++++++--------- t/t9902-completion.sh | 10 ++----- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index a8e7c6ddbf..e8f8fab125 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -638,25 +638,33 @@ __git_ls_files_helper () } -# __git_index_files accepts 1 or 2 arguments: +# __git_index_files accepts 1 to 4 arguments: # 1: Options to pass to ls-files (required). # 2: A directory path (optional). # If provided, only files within the specified directory are listed. # Sub directories are never recursed. Path must have a trailing # slash. # 3: List only paths matching this path component (optional). +# 4: Hide paths whose first component starts with a dot if this is +# "hide-dotfiles" and the third argument is empty (optional). __git_index_files () { - local root="$2" match="$3" + local root="$2" match="$3" hide_dotfiles="${4-}" + local hide_dotfiles_awk=0 + if [ "$hide_dotfiles" = "hide-dotfiles" ] && [ -z "$match" ]; then + hide_dotfiles_awk=1 + fi __git_ls_files_helper "$root" "$1" "${match:-?}" | - awk -F / -v pfx="${2//\\/\\\\}" '{ + awk -F / -v pfx="${2//\\/\\\\}" -v hide_dotfiles="$hide_dotfiles_awk" '{ paths[$1] = 1 } END { for (p in paths) { if (substr(p, 1, 1) != "\"") { # No special characters, easy! + if (hide_dotfiles == 1 && substr(p, 1, 1) == ".") + continue print pfx p continue } @@ -675,8 +683,10 @@ __git_index_files () # We have seen the same directory unquoted, # skip it. continue - else - print pfx p + + if (hide_dotfiles == 1 && substr(p, 1, 1) == ".") + continue + print pfx p } } function dequote(p, bs_idx, out, esc, esc_idx, dec) { @@ -721,13 +731,15 @@ __git_index_files () }' } -# __git_complete_index_file requires 1 argument: +# __git_complete_index_file accepts 1 or 2 arguments: # 1: the options to pass to ls-file +# 2: Hide paths whose first component starts with a dot if this is +# "hide-dotfiles" and the current word is empty (optional). # # The exception is --committable, which finds the files appropriate commit. __git_complete_index_file () { - local dequoted_word pfx="" cur_ + local dequoted_word pfx="" cur_ hide_dotfiles="${2-}" __git_dequote "$cur" @@ -740,7 +752,7 @@ __git_complete_index_file () cur_="$dequoted_word" esac - __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")" + __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_" "$hide_dotfiles")" } # Lists branches from the local repository. @@ -2164,7 +2176,7 @@ _git_ls_files () # XXX ignore options like --modified and always suggest all cached # files. - __git_complete_index_file "--cached" + __git_complete_index_file "--cached" hide-dotfiles } _git_ls_remote () @@ -2397,9 +2409,9 @@ _git_mv () if [ $(__git_count_arguments "mv") -gt 0 ]; then # We need to show both cached and untracked files (including # empty directories) since this may not be the last argument. - __git_complete_index_file "--cached --others --directory" + __git_complete_index_file "--cached --others --directory" hide-dotfiles else - __git_complete_index_file "--cached" + __git_complete_index_file "--cached" hide-dotfiles fi } @@ -3219,7 +3231,7 @@ _git_rm () ;; esac - __git_complete_index_file "--cached" + __git_complete_index_file "--cached" hide-dotfiles } _git_shortlog () diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 28f61f08fb..02aaf71876 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2811,17 +2811,15 @@ test_expect_success 'complete files' ' touch untracked && - : TODO .gitignore should not be here && test_completion "git rm " <<-\EOF && - .gitignore modified EOF + test_completion "git rm ." ".gitignore" && + test_completion "git clean " "untracked" && - : TODO .gitignore should not be here && test_completion "git mv " <<-\EOF && - .gitignore modified EOF @@ -2832,9 +2830,7 @@ test_expect_success 'complete files' ' mkdir untracked-dir && - : TODO .gitignore should not be here && test_completion "git mv modified " <<-\EOF && - .gitignore dir modified untracked @@ -2843,9 +2839,7 @@ test_expect_success 'complete files' ' test_completion "git commit " "modified" && - : TODO .gitignore should not be here && test_completion "git ls-files " <<-\EOF && - .gitignore dir modified EOF From 2cc01b9c55de08d03f30e7ec43236aa08a829dc0 Mon Sep 17 00:00:00 2001 From: Zakariyah Ali Date: Sat, 20 Jun 2026 17:55:56 +0000 Subject: [PATCH 2/2] completion: hide dotfiles by default for path completion The previous implementation required callers to explicitly pass a "hide-dotfiles" flag to __git_complete_index_file to avoid cluttering completions with hidden files. This led to inconsistent behavior across commands (e.g., `git add` and `git mv` behaved differently) and forced callers to maintain repetitive logic. As suggested by Junio C Hamano, this commit simplifies the logic: 1. __git_complete_index_file now unconditionally hides dotfiles when no match pattern is provided. 2. The awk loop in __git_index_files is refactored to check the dotfile condition in a single, obvious place after handling path dequoting, removing the previous duplication. 3. Callers no longer need to pass "hide-dotfiles". This provides a cleaner API and ensures a consistent, expected behavior where dotfiles are hidden unless explicitly requested by typing a dot. Signed-off-by: Zakariyah Ali Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 65 ++++++++++++-------------- t/t9902-completion.sh | 9 +++- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index e8f8fab125..b0b1b3c27a 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -638,20 +638,23 @@ __git_ls_files_helper () } -# __git_index_files accepts 1 to 4 arguments: +# __git_index_files accepts 1 to 3 arguments: # 1: Options to pass to ls-files (required). # 2: A directory path (optional). # If provided, only files within the specified directory are listed. # Sub directories are never recursed. Path must have a trailing # slash. # 3: List only paths matching this path component (optional). -# 4: Hide paths whose first component starts with a dot if this is -# "hide-dotfiles" and the third argument is empty (optional). +# +# If the third argument is empty, paths that begin with a dot (dotfiles) +# are hidden. This matches user expectations where dotfiles are considered +# hidden configuration files/directories and shouldn't clutter default +# completions unless explicitly requested by typing a dot. __git_index_files () { - local root="$2" match="$3" hide_dotfiles="${4-}" + local root="$2" match="$3" local hide_dotfiles_awk=0 - if [ "$hide_dotfiles" = "hide-dotfiles" ] && [ -z "$match" ]; then + if [ -z "$match" ]; then hide_dotfiles_awk=1 fi @@ -661,29 +664,23 @@ __git_index_files () } END { for (p in paths) { - if (substr(p, 1, 1) != "\"") { - # No special characters, easy! - if (hide_dotfiles == 1 && substr(p, 1, 1) == ".") + if (substr(p, 1, 1) == "\"") { + # The path is quoted. + p = dequote(p) + if (p == "") + continue + + # Even when a directory name itself does not contain + # any special characters, it will still be quoted if + # any of its (stripped) trailing path components do. + # Because of this we may have seen the same directory + # both quoted and unquoted. + if (p in paths) + # We have seen the same directory unquoted, + # skip it. continue - print pfx p - continue } - # The path is quoted. - p = dequote(p) - if (p == "") - continue - - # Even when a directory name itself does not contain - # any special characters, it will still be quoted if - # any of its (stripped) trailing path components do. - # Because of this we may have seen the same directory - # both quoted and unquoted. - if (p in paths) - # We have seen the same directory unquoted, - # skip it. - continue - if (hide_dotfiles == 1 && substr(p, 1, 1) == ".") continue print pfx p @@ -731,15 +728,13 @@ __git_index_files () }' } -# __git_complete_index_file accepts 1 or 2 arguments: -# 1: the options to pass to ls-file -# 2: Hide paths whose first component starts with a dot if this is -# "hide-dotfiles" and the current word is empty (optional). +# __git_complete_index_file accepts 1 argument: +# 1: the options to pass to ls-files # # The exception is --committable, which finds the files appropriate commit. __git_complete_index_file () { - local dequoted_word pfx="" cur_ hide_dotfiles="${2-}" + local dequoted_word pfx="" cur_ __git_dequote "$cur" @@ -752,7 +747,7 @@ __git_complete_index_file () cur_="$dequoted_word" esac - __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_" "$hide_dotfiles")" + __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")" } # Lists branches from the local repository. @@ -2176,7 +2171,7 @@ _git_ls_files () # XXX ignore options like --modified and always suggest all cached # files. - __git_complete_index_file "--cached" hide-dotfiles + __git_complete_index_file "--cached" } _git_ls_remote () @@ -2409,9 +2404,9 @@ _git_mv () if [ $(__git_count_arguments "mv") -gt 0 ]; then # We need to show both cached and untracked files (including # empty directories) since this may not be the last argument. - __git_complete_index_file "--cached --others --directory" hide-dotfiles + __git_complete_index_file "--cached --others --directory" else - __git_complete_index_file "--cached" hide-dotfiles + __git_complete_index_file "--cached" fi } @@ -3231,7 +3226,7 @@ _git_rm () ;; esac - __git_complete_index_file "--cached" hide-dotfiles + __git_complete_index_file "--cached" } _git_shortlog () diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 02aaf71876..7a7594455c 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2360,6 +2360,7 @@ test_expect_success 'setup for path completion tests' ' "spaces in dir" \ árvíztűrő && touch simple-dir/simple-file \ + simple-dir/.dotfile-in-dir \ "spaces in dir/spaces in file" \ "árvíztűrő/Сайн яваарай" && if test_have_prereq !MINGW && @@ -2380,6 +2381,11 @@ test_expect_success '__git_complete_index_file - simple' ' test_path_completion simple-dir/simple simple-dir/simple-file ' +test_expect_success '__git_complete_index_file - dotfiles' ' + test_path_completion "simple-dir/" "simple-dir/simple-file" && + test_path_completion "simple-dir/." "simple-dir/.dotfile-in-dir" +' + test_expect_success \ '__git_complete_index_file - escaped characters on cmdline' ' test_path_completion spac "spaces in dir" && # Bash will turn this @@ -2789,7 +2795,8 @@ test_expect_success 'complete files' ' echo "out_sorted" >> .gitignore && git add .gitignore && - test_completion "git commit " ".gitignore" && + test_completion "git commit " "" && + test_completion "git commit ." ".gitignore" && git commit -m ignore &&