Skip to content

Commit f55f3be

Browse files
committed
Merge branch 'jc/complete-checkout' into jch
* jc/complete-checkout: completion: 'git checkout' completes untracked paths as a last resort completion: complete tracked paths for "git checkout" completion: no-op refactoring of checkout completion
2 parents d85604d + 90aec65 commit f55f3be

2 files changed

Lines changed: 105 additions & 39 deletions

File tree

‎contrib/completion/git-completion.bash‎

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,49 +1742,59 @@ __git_checkout_default_dwim_mode ()
17421742

17431743
_git_checkout ()
17441744
{
1745-
__git_has_doubledash && return
1745+
if ! __git_has_doubledash; then
1746+
local dwim_opt="$(__git_checkout_default_dwim_mode)"
17461747

1747-
local dwim_opt="$(__git_checkout_default_dwim_mode)"
1748+
case "$prev" in
1749+
-b|-B|--orphan)
1750+
# Complete local branches (and DWIM branch
1751+
# remote branch names) for an option argument
1752+
# specifying a new branch name. This is for
1753+
# convenience, assuming new branches are
1754+
# possibly based on pre-existing branch names.
1755+
__git_complete_refs $dwim_opt --mode="heads"
1756+
return
1757+
;;
1758+
*)
1759+
;;
1760+
esac
17481761

1749-
case "$prev" in
1750-
-b|-B|--orphan)
1751-
# Complete local branches (and DWIM branch
1752-
# remote branch names) for an option argument
1753-
# specifying a new branch name. This is for
1754-
# convenience, assuming new branches are
1755-
# possibly based on pre-existing branch names.
1756-
__git_complete_refs $dwim_opt --mode="heads"
1757-
return
1758-
;;
1759-
*)
1760-
;;
1761-
esac
1762+
case "$cur" in
1763+
--conflict=*)
1764+
__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1765+
return
1766+
;;
1767+
--*)
1768+
__gitcomp_builtin checkout
1769+
return
1770+
;;
1771+
*)
1772+
# At this point, we've already handled special completion for
1773+
# the arguments to -b/-B, and --orphan. There are 3 main
1774+
# things left we can possibly complete:
1775+
# 1) a start-point for -b/-B, -d/--detach, or --orphan
1776+
# 2) a remote head, for --track
1777+
# 3) an arbitrary reference, possibly including DWIM names
1778+
#
1779+
1780+
if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1781+
__git_complete_refs --mode="refs"
1782+
elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1783+
__git_complete_refs --mode="remote-heads"
1784+
else
1785+
__git_complete_refs $dwim_opt --mode="refs"
1786+
fi
1787+
;;
1788+
esac
1789+
fi
17621790

1763-
case "$cur" in
1764-
--conflict=*)
1765-
__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1766-
;;
1767-
--*)
1768-
__gitcomp_builtin checkout
1769-
;;
1770-
*)
1771-
# At this point, we've already handled special completion for
1772-
# the arguments to -b/-B, and --orphan. There are 3 main
1773-
# things left we can possibly complete:
1774-
# 1) a start-point for -b/-B, -d/--detach, or --orphan
1775-
# 2) a remote head, for --track
1776-
# 3) an arbitrary reference, possibly including DWIM names
1777-
#
1791+
if [ ${#COMPREPLY[@]} -eq 0 ]; then
1792+
__git_complete_index_file ""
1793+
fi
17781794

1779-
if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1780-
__git_complete_refs --mode="refs"
1781-
elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1782-
__git_complete_refs --mode="remote-heads"
1783-
else
1784-
__git_complete_refs $dwim_opt --mode="refs"
1785-
fi
1786-
;;
1787-
esac
1795+
if [ ${#COMPREPLY[@]} -eq 0 ]; then
1796+
__git_complete_index_file "--others"
1797+
fi
17881798
}
17891799

17901800
__git_sequencer_inprogress_options="--continue --quit --abort --skip"

‎t/t9902-completion.sh‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,62 @@ test_expect_success 'git -C <path> checkout uses the right repo' '
27202720
EOF
27212721
'
27222722

2723+
test_expect_success 'git checkout completes tracked paths when no refs match' '
2724+
# file1 and file2 are tracked but file3 is not
2725+
# there is no ref that begins with f
2726+
test_completion "git checkout f" <<-\EOF &&
2727+
file1
2728+
file2
2729+
EOF
2730+
test_completion "git checkout -- f" <<-\EOF
2731+
file1
2732+
file2
2733+
EOF
2734+
'
2735+
2736+
test_expect_success 'git checkout completes untracked paths, too' '
2737+
# ufile is not tracked and there is no ref that begins with u
2738+
test_completion "git checkout u" <<-\EOF &&
2739+
ufile
2740+
EOF
2741+
test_completion "git checkout -- u" <<-\EOF
2742+
ufile
2743+
EOF
2744+
'
2745+
2746+
test_expect_success 'git -C <path> checkout completes paths in specified repo' '
2747+
# otherfile is tracked, oops is not
2748+
# lostfile is tracked but lost, ufile is untracked.
2749+
test_when_finished "rm -rf repo-for-checkout" &&
2750+
git init repo-for-checkout &&
2751+
echo content >repo-for-checkout/otherfile &&
2752+
echo content >repo-for-checkout/lostfile &&
2753+
git -C repo-for-checkout add otherfile &&
2754+
git -C repo-for-checkout add lostfile &&
2755+
git -C repo-for-checkout commit -m otherfile &&
2756+
echo untracked >repo-for-checkout/oops &&
2757+
echo untracked >repo-for-checkout/ufile &&
2758+
rm -f repo-for-checkout/lostfile &&
2759+
test_completion "git -C repo-for-checkout checkout o" <<-\EOF &&
2760+
otherfile
2761+
EOF
2762+
test_completion "git -C repo-for-checkout checkout -- o" <<-\EOF &&
2763+
otherfile
2764+
EOF
2765+
test_completion "git -C repo-for-checkout checkout l" <<-\EOF &&
2766+
lostfile
2767+
EOF
2768+
test_completion "git -C repo-for-checkout checkout -- l" <<-\EOF &&
2769+
lostfile
2770+
EOF
2771+
test_completion "git -C repo-for-checkout checkout u" <<-\EOF &&
2772+
ufile
2773+
EOF
2774+
test_completion "git -C repo-for-checkout checkout -- u" <<-\EOF
2775+
ufile
2776+
EOF
2777+
'
2778+
27232779
test_expect_success 'git diff completes tracked paths when no refs match' '
27242780
# file1 and file2 are tracked but file3 is not
27252781
# there is no ref that begins with f

0 commit comments

Comments
 (0)