From ba5dc6f164d53b872fff8067930ae9fa1280f3b3 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 5 Aug 2026 12:42:50 -0700 Subject: [PATCH] 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 finds the 'untracked' file in another-directory and offers it as a completion candidate. Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 4 ++++ t/t9902-completion.sh | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 845fd19f70..7741789e41 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -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 diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 10ac690e21..53a2bfb2ac 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2663,6 +2663,7 @@ test_expect_success 'setup for integration tests' ' echo content >file1 && echo more >file2 && git add file1 file2 && + echo untracked >ufile && git commit -m one && git branch mybranch && git tag mytag @@ -2728,6 +2729,15 @@ test_expect_success 'git diff -- completes tracked paths' ' EOF ' +test_expect_success 'git diff [--] completes untracked paths, too' ' + test_completion "git diff u" <<-\EOF && + ufile + EOF + test_completion "git diff -- u" <<-\EOF + ufile + EOF +' + test_expect_success 'git -C diff completes tracked paths in specified repo' ' test_when_finished "rm -rf repo-for-diff" && git init repo-for-diff && @@ -2744,11 +2754,21 @@ test_expect_success 'git -C diff -- completes pathspecs in specified repo test_when_finished "rm -rf repo-for-diff" && git init repo-for-diff && echo content >repo-for-diff/otherfile && + echo untracked >repo-for-diff/untracked && git -C repo-for-diff add otherfile && git -C repo-for-diff commit -m otherfile && - test_completion "git -C repo-for-diff diff -- o" <<-\EOF + test_completion "git -C repo-for-diff diff o" <<-\EOF && otherfile EOF + test_completion "git -C repo-for-diff diff -- o" <<-\EOF && + otherfile + EOF + test_completion "git -C repo-for-diff diff u" <<-\EOF && + untracked + EOF + test_completion "git -C repo-for-diff diff -- u" <<-\EOF + untracked + EOF ' test_expect_success 'show completes all refs' '