[PATCH v4 3/3] completion: 'git diff' completes untracked paths as a last resort

Junio C Hamano <[email protected]>
Newsgroups org.kernel.vger.git
Message-ID <[email protected]>
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 <[email protected]>
---
 contrib/completion/git-completion.bash |  4 ++++
 t/t9902-completion.sh                  | 22 ++++++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

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 adfaf414fd..eea4bdbb7e 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -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,21 +2727,38 @@ test_expect_success 'git diff completes tracked paths when no refs match' '
 	EOF
 '
 
+test_expect_success 'git diff [--] completes untracked paths, too' '
+	# there is no ref or tracked path that begin 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 in the specified repo' '
 	test_when_finished "rm -rf repo-for-diff" &&
 	git init repo-for-diff &&
 
-	# otherfile is tracked, oops is untracked
+	# otherfile is tracked, oops and ufile are untracked
 	echo content >repo-for-diff/otherfile &&
 	git -C repo-for-diff add otherfile &&
 	git -C repo-for-diff commit -m otherfile &&
 	echo untracked >repo-for-diff/oops &&
+	echo untracked >repo-for-diff/ufile &&
 	test_completion "git -C repo-for-diff diff o" <<-\EOF &&
 	otherfile
 	EOF
-	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 u" <<-\EOF &&
+	ufile
+	EOF
+	test_completion "git -C repo-for-diff diff -- u" <<-\EOF
+	ufile
+	EOF
 '
 
 test_expect_success 'show completes all refs' '
-- 
2.55.0-655-gb2c071042d
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.