Merge branch 'jc/complete-diff-tracked-paths' into jch

'git -C <dir> diff fi<TAB>' did not complete 'dir/file', which has
been corrected.

* jc/complete-diff-tracked-paths:
  completion: 'git diff' completes untracked paths as a last resort
  completion: complete tracked paths for 'git diff'
  completion: no-op refactoring of diff completion
This commit is contained in:
Junio C Hamano
2026-08-07 14:48:08 -07:00
2 changed files with 100 additions and 28 deletions

View File

@@ -1947,35 +1947,48 @@ __git_diff_difftool_options="--cached --staged
_git_diff ()
{
__git_has_doubledash && return
if ! __git_has_doubledash; then
case "$cur" in
--diff-algorithm=*)
__gitcomp "$__git_diff_algorithms" \
"" "${cur##--diff-algorithm=}"
return
;;
--submodule=*)
__gitcomp "$__git_diff_submodule_formats" \
"" "${cur##--submodule=}"
return
;;
--color-moved=*)
__gitcomp "$__git_color_moved_opts" \
"" "${cur##--color-moved=}"
return
;;
--color-moved-ws=*)
__gitcomp "$__git_color_moved_ws_opts" \
"" "${cur##--color-moved-ws=}"
return
;;
--ws-error-highlight=*)
__gitcomp "$__git_ws_error_highlight_opts" \
"" "${cur##--ws-error-highlight=}"
return
;;
--*)
__gitcomp "$__git_diff_difftool_options"
return
;;
esac
__git_complete_revlist_file
fi
case "$cur" in
--diff-algorithm=*)
__gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
return
;;
--submodule=*)
__gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
return
;;
--color-moved=*)
__gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
return
;;
--color-moved-ws=*)
__gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
return
;;
--ws-error-highlight=*)
__gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
return
;;
--*)
__gitcomp "$__git_diff_difftool_options"
return
;;
esac
__git_complete_revlist_file
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

@@ -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
@@ -2712,6 +2713,64 @@ test_expect_success 'git -C <path> checkout uses the right repo' '
EOF
'
test_expect_success 'git diff completes tracked paths when no refs match' '
# file1 and file2 are tracked but file3 is not
test_completion "git diff f" <<-\EOF
file1
file2
EOF
'
test_expect_success 'git diff -- completes tracked paths' '
# file1 and file2 are tracked but file3 is not
test_completion "git diff -- f" <<-\EOF
file1
file2
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 <path> diff completes tracked paths in specified repo' '
test_when_finished "rm -rf repo-for-diff" &&
git init repo-for-diff &&
echo content >repo-for-diff/otherfile &&
git -C repo-for-diff add otherfile &&
echo untracked >repo-for-diff/oops &&
git -C repo-for-diff commit -m otherfile &&
test_completion "git -C repo-for-diff diff o" <<-\EOF
otherfile
EOF
'
test_expect_success 'git -C <path> 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 &&
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' '
test_completion "git show m" <<-\EOF
main Z