Skip to content

Commit 96d1225

Browse files
alibaba0010gitster
authored andcommitted
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 ddf07bd, "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 <zakariyahali100@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 56a4f3c commit 96d1225

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

contrib/completion/git-completion.bash

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -638,25 +638,33 @@ __git_ls_files_helper ()
638638
}
639639

640640

641-
# __git_index_files accepts 1 or 2 arguments:
641+
# __git_index_files accepts 1 to 4 arguments:
642642
# 1: Options to pass to ls-files (required).
643643
# 2: A directory path (optional).
644644
# If provided, only files within the specified directory are listed.
645645
# Sub directories are never recursed. Path must have a trailing
646646
# slash.
647647
# 3: List only paths matching this path component (optional).
648+
# 4: Hide paths whose first component starts with a dot if this is
649+
# "hide-dotfiles" and the third argument is empty (optional).
648650
__git_index_files ()
649651
{
650-
local root="$2" match="$3"
652+
local root="$2" match="$3" hide_dotfiles="${4-}"
653+
local hide_dotfiles_awk=0
654+
if [ "$hide_dotfiles" = "hide-dotfiles" ] && [ -z "$match" ]; then
655+
hide_dotfiles_awk=1
656+
fi
651657

652658
__git_ls_files_helper "$root" "$1" "${match:-?}" |
653-
awk -F / -v pfx="${2//\\/\\\\}" '{
659+
awk -F / -v pfx="${2//\\/\\\\}" -v hide_dotfiles="$hide_dotfiles_awk" '{
654660
paths[$1] = 1
655661
}
656662
END {
657663
for (p in paths) {
658664
if (substr(p, 1, 1) != "\"") {
659665
# No special characters, easy!
666+
if (hide_dotfiles == 1 && substr(p, 1, 1) == ".")
667+
continue
660668
print pfx p
661669
continue
662670
}
@@ -675,8 +683,10 @@ __git_index_files ()
675683
# We have seen the same directory unquoted,
676684
# skip it.
677685
continue
678-
else
679-
print pfx p
686+
687+
if (hide_dotfiles == 1 && substr(p, 1, 1) == ".")
688+
continue
689+
print pfx p
680690
}
681691
}
682692
function dequote(p, bs_idx, out, esc, esc_idx, dec) {
@@ -721,13 +731,15 @@ __git_index_files ()
721731
}'
722732
}
723733

724-
# __git_complete_index_file requires 1 argument:
734+
# __git_complete_index_file accepts 1 or 2 arguments:
725735
# 1: the options to pass to ls-file
736+
# 2: Hide paths whose first component starts with a dot if this is
737+
# "hide-dotfiles" and the current word is empty (optional).
726738
#
727739
# The exception is --committable, which finds the files appropriate commit.
728740
__git_complete_index_file ()
729741
{
730-
local dequoted_word pfx="" cur_
742+
local dequoted_word pfx="" cur_ hide_dotfiles="${2-}"
731743

732744
__git_dequote "$cur"
733745

@@ -740,7 +752,7 @@ __git_complete_index_file ()
740752
cur_="$dequoted_word"
741753
esac
742754

743-
__gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
755+
__gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_" "$hide_dotfiles")"
744756
}
745757

746758
# Lists branches from the local repository.
@@ -2164,7 +2176,7 @@ _git_ls_files ()
21642176

21652177
# XXX ignore options like --modified and always suggest all cached
21662178
# files.
2167-
__git_complete_index_file "--cached"
2179+
__git_complete_index_file "--cached" hide-dotfiles
21682180
}
21692181

21702182
_git_ls_remote ()
@@ -2397,9 +2409,9 @@ _git_mv ()
23972409
if [ $(__git_count_arguments "mv") -gt 0 ]; then
23982410
# We need to show both cached and untracked files (including
23992411
# empty directories) since this may not be the last argument.
2400-
__git_complete_index_file "--cached --others --directory"
2412+
__git_complete_index_file "--cached --others --directory" hide-dotfiles
24012413
else
2402-
__git_complete_index_file "--cached"
2414+
__git_complete_index_file "--cached" hide-dotfiles
24032415
fi
24042416
}
24052417

@@ -3219,7 +3231,7 @@ _git_rm ()
32193231
;;
32203232
esac
32213233

3222-
__git_complete_index_file "--cached"
3234+
__git_complete_index_file "--cached" hide-dotfiles
32233235
}
32243236

32253237
_git_shortlog ()

t/t9902-completion.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,17 +2811,15 @@ test_expect_success 'complete files' '
28112811
28122812
touch untracked &&
28132813
2814-
: TODO .gitignore should not be here &&
28152814
test_completion "git rm " <<-\EOF &&
2816-
.gitignore
28172815
modified
28182816
EOF
28192817
2818+
test_completion "git rm ." ".gitignore" &&
2819+
28202820
test_completion "git clean " "untracked" &&
28212821
2822-
: TODO .gitignore should not be here &&
28232822
test_completion "git mv " <<-\EOF &&
2824-
.gitignore
28252823
modified
28262824
EOF
28272825
@@ -2832,9 +2830,7 @@ test_expect_success 'complete files' '
28322830
28332831
mkdir untracked-dir &&
28342832
2835-
: TODO .gitignore should not be here &&
28362833
test_completion "git mv modified " <<-\EOF &&
2837-
.gitignore
28382834
dir
28392835
modified
28402836
untracked
@@ -2843,9 +2839,7 @@ test_expect_success 'complete files' '
28432839
28442840
test_completion "git commit " "modified" &&
28452841
2846-
: TODO .gitignore should not be here &&
28472842
test_completion "git ls-files " <<-\EOF &&
2848-
.gitignore
28492843
dir
28502844
modified
28512845
EOF

0 commit comments

Comments
 (0)