Skip to content

Commit 5de40f5

Browse files
spearceJunio C Hamano
authored andcommitted
Teach bash about git-repo-config.
This is a really ugly completion script for git-repo-config, but it has some nice properties. I've added all of the documented configuration parameters from Documentation/config.txt to the script, allowing the user to complete any standard configuration parameter name. We also have some intelligence for the remote.*.* and branch.*.* keys by completing not only the key name (e.g. remote.origin) but also the values (e.g. remote.*.fetch completes to the branches available on the corresponding remote). Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 35e65ec commit 5de40f5

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ __git_ps1 ()
4343
fi
4444
}
4545

46+
__git_heads ()
47+
{
48+
local cmd i is_hash=y dir="${1:-$(__gitdir)}"
49+
if [ -d "$dir" ]; then
50+
for i in $(git --git-dir="$dir" \
51+
for-each-ref --format='%(refname)' \
52+
refs/heads ); do
53+
echo "${i#refs/heads/}"
54+
done
55+
return
56+
fi
57+
for i in $(git-ls-remote "$dir" 2>/dev/null); do
58+
case "$is_hash,$i" in
59+
y,*) is_hash=n ;;
60+
n,*^{}) is_hash=y ;;
61+
n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
62+
n,*) is_hash=y; echo "$i" ;;
63+
esac
64+
done
65+
}
66+
4667
__git_refs ()
4768
{
4869
local cmd i is_hash=y dir="${1:-$(__gitdir)}"
@@ -91,6 +112,23 @@ __git_refs2 ()
91112
done
92113
}
93114

115+
__git_refs_remotes ()
116+
{
117+
local cmd i is_hash=y
118+
for i in $(git-ls-remote "$1" 2>/dev/null); do
119+
case "$is_hash,$i" in
120+
n,refs/heads/*)
121+
is_hash=y
122+
echo "$i:refs/remotes/$1/${i#refs/heads/}"
123+
;;
124+
y,*) is_hash=n ;;
125+
n,*^{}) is_hash=y ;;
126+
n,refs/tags/*) is_hash=y;;
127+
n,*) is_hash=y; ;;
128+
esac
129+
done
130+
}
131+
94132
__git_remotes ()
95133
{
96134
local i ngoff IFS=$'\n' d="$(__gitdir)"
@@ -500,6 +538,119 @@ _git_rebase ()
500538
COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
501539
}
502540

541+
_git_repo_config ()
542+
{
543+
local cur="${COMP_WORDS[COMP_CWORD]}"
544+
local prv="${COMP_WORDS[COMP_CWORD-1]}"
545+
case "$prv" in
546+
branch.*.remote)
547+
COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
548+
return
549+
;;
550+
branch.*.merge)
551+
COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
552+
return
553+
;;
554+
remote.*.fetch)
555+
local remote="${prv#remote.}"
556+
remote="${remote%.fetch}"
557+
COMPREPLY=($(compgen -W "$(__git_refs_remotes "$remote")" \
558+
-- "$cur"))
559+
return
560+
;;
561+
remote.*.push)
562+
local remote="${prv#remote.}"
563+
remote="${remote%.push}"
564+
COMPREPLY=($(compgen -W "$(git --git-dir="$(__gitdir)" \
565+
for-each-ref --format='%(refname):%(refname)' \
566+
refs/heads)" -- "$cur"))
567+
return
568+
;;
569+
*.*)
570+
COMPREPLY=()
571+
return
572+
;;
573+
esac
574+
case "$cur" in
575+
--*)
576+
COMPREPLY=($(compgen -W "
577+
--global --list --replace-all
578+
--get --get-all --get-regexp
579+
--unset --unset-all
580+
" -- "$cur"))
581+
return
582+
;;
583+
branch.*.*)
584+
local pfx="${cur%.*}."
585+
cur="${cur##*.}"
586+
COMPREPLY=($(compgen -P "$pfx" -W "remote merge" -- "$cur"))
587+
return
588+
;;
589+
branch.*)
590+
local pfx="${cur%.*}."
591+
cur="${cur#*.}"
592+
COMPREPLY=($(compgen -P "$pfx" -S . \
593+
-W "$(__git_heads)" -- "$cur"))
594+
return
595+
;;
596+
remote.*.*)
597+
local pfx="${cur%.*}."
598+
cur="${cur##*.}"
599+
COMPREPLY=($(compgen -P "$pfx" -W "url fetch push" -- "$cur"))
600+
return
601+
;;
602+
remote.*)
603+
local pfx="${cur%.*}."
604+
cur="${cur#*.}"
605+
COMPREPLY=($(compgen -P "$pfx" -S . \
606+
-W "$(__git_remotes)" -- "$cur"))
607+
return
608+
;;
609+
esac
610+
COMPREPLY=($(compgen -W "
611+
apply.whitespace
612+
core.fileMode
613+
core.gitProxy
614+
core.ignoreStat
615+
core.preferSymlinkRefs
616+
core.logAllRefUpdates
617+
core.repositoryFormatVersion
618+
core.sharedRepository
619+
core.warnAmbiguousRefs
620+
core.compression
621+
core.legacyHeaders
622+
i18n.commitEncoding
623+
diff.color
624+
diff.renameLimit
625+
diff.renames
626+
pager.color
627+
status.color
628+
log.showroot
629+
show.difftree
630+
showbranch.default
631+
whatchanged.difftree
632+
http.sslVerify
633+
http.sslCert
634+
http.sslKey
635+
http.sslCAInfo
636+
http.sslCAPath
637+
http.maxRequests
638+
http.lowSpeedLimit http.lowSpeedTime
639+
http.noEPSV
640+
pack.window
641+
repack.useDeltaBaseOffset
642+
pull.octopus pull.twohead
643+
merge.summary
644+
receive.unpackLimit
645+
receive.denyNonFastForwards
646+
user.name user.email
647+
tar.umask
648+
gitcvs.enabled
649+
gitcvs.logfile
650+
branch. remote.
651+
" -- "$cur"))
652+
}
653+
503654
_git_reset ()
504655
{
505656
local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -552,6 +703,7 @@ _git ()
552703
pull) _git_pull ;;
553704
push) _git_push ;;
554705
rebase) _git_rebase ;;
706+
repo-config) _git_repo_config ;;
555707
reset) _git_reset ;;
556708
show) _git_log ;;
557709
show-branch) _git_log ;;
@@ -585,6 +737,7 @@ complete -o default -F _git_name_rev git-name-rev
585737
complete -o default -o nospace -F _git_pull git-pull
586738
complete -o default -o nospace -F _git_push git-push
587739
complete -o default -F _git_rebase git-rebase
740+
complete -o default -F _git_repo_config git-repo-config
588741
complete -o default -F _git_reset git-reset
589742
complete -o default -F _git_log git-show
590743
complete -o default -o nospace -F _git_log git-show-branch
@@ -606,6 +759,7 @@ complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
606759
complete -o default -F _git_merge_base git-merge-base.exe
607760
complete -o default -F _git_name_rev git-name-rev.exe
608761
complete -o default -o nospace -F _git_push git-push.exe
762+
complete -o default -F _git_repo_config git-repo-config
609763
complete -o default -o nospace -F _git_log git-show.exe
610764
complete -o default -o nospace -F _git_log git-show-branch.exe
611765
complete -o default -o nospace -F _git_log git-whatchanged.exe

0 commit comments

Comments
 (0)