completion: 'git diff' completes untracked paths as a last resort

We taught 'git diff' to first try to complete revisions (unless '--'
is present on the command line) and, failing that, to complete
tracked paths.  If this yields nothing, it lets the Bash default,
which offers paths in $PWD, kick in.

Teach it to complete untracked paths before giving up and letting
the Bash default kick in.  With this change,

    $ git -C another-directory diff un<TAB>

finds the 'untracked' file in another-directory and offers it as a
completion candidate.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Junio C Hamano 2026-08-12 09:25:51 -07:00
parent 4b1b7a4e95
commit 354d1bf3a0
2 changed files with 23 additions and 1 deletions

View File

@ -1985,6 +1985,10 @@ _git_diff ()
if [ ${#COMPREPLY[@]} -eq 0 ]; then
__git_complete_index_file ""
fi

if [ ${#COMPREPLY[@]} -eq 0 ]; then
__git_complete_index_file "--others --directory"
fi
}

__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff

View File

@ -2664,6 +2664,7 @@ test_expect_success 'setup for integration tests' '
echo more >file2 &&
git add file1 file2 &&
echo untracked >file3 &&
echo untracked >ufile &&
git commit -m one &&
git branch mybranch &&
git tag mytag
@ -2726,6 +2727,16 @@ test_expect_success 'git diff completes tracked paths when no refs match' '
EOF
'

test_expect_success 'git diff [--] completes untracked paths, too' '
# ufile is not tracked and there is no ref that begins with u
test_completion "git diff u" <<-\EOF &&
ufile
EOF
test_completion "git diff -- u" <<-\EOF
ufile
EOF
'

test_expect_success 'git -C <path> diff completes paths in specified repo' '
test_when_finished "rm -rf repo-for-diff" &&
git init repo-for-diff &&
@ -2735,6 +2746,7 @@ test_expect_success 'git -C <path> diff completes paths in specified repo' '
git -C repo-for-diff add lostfile &&
git -C repo-for-diff commit -m otherfile &&
echo untracked >repo-for-diff/oops &&
echo untracked >repo-for-diff/ufile &&
rm -f repo-for-diff/lostfile &&

test_completion "git -C repo-for-diff diff o" <<-\EOF &&
@ -2743,13 +2755,19 @@ test_expect_success 'git -C <path> diff completes paths in specified repo' '
test_completion "git -C repo-for-diff diff l" <<-\EOF &&
lostfile
EOF
test_completion "git -C repo-for-diff diff u" <<-\EOF &&
ufile
EOF

test_completion "git -C repo-for-diff diff -- o" <<-\EOF &&
otherfile
EOF
test_completion "git -C repo-for-diff diff -- l" <<-\EOF
test_completion "git -C repo-for-diff diff -- l" <<-\EOF &&
lostfile
EOF
test_completion "git -C repo-for-diff diff -- u" <<-\EOF
ufile
EOF
'

test_expect_success 'show completes all refs' '