@matheusbsbs / matheus-bagliano / commits / 88e21dc746
Fork 0

Teach bash about completing arguments for git-tag

Lately I have been doing a lot of calls to `git tag -d` and also to `git tag -v`. In both such cases being able to complete the names of existing tags saves the fingers some typing effort. We now look for the -d or -v option to git-tag in the bash completion support and offer up existing tag names as possible choices for these. When creating a new tag we now also offer bash completion support for the second argument to git-tag (the object to be tagged) as this can often be a specific existing branch name and is not necessarily the current HEAD. If the -f option is being used to recreate an existing tag we now also offer completion support on the existing tag names for the first argument of git-tag, helping to the user to reselect the prior tag name that they are trying to replace. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 31, 2007 at 23:47 UTC 88e21dc7461dca1ebc70d8579bcc9246364511ee
1 file changed +58
contrib/completion/git-completion.bash
+58
@@ -114,6 +114,27 @@ __git_heads ()
114 done
115 }
116
117 +__git_tags ()
118 +{
119 + local cmd i is_hash=y dir="$(__gitdir "$1")"
120 + if [ -d "$dir" ]; then
121 + for i in $(git --git-dir="$dir" \
122 + for-each-ref --format='%(refname)' \
123 + refs/tags ); do
124 + echo "${i#refs/tags/}"
125 + done
126 + return
127 + fi
128 + for i in $(git-ls-remote "$1" 2>/dev/null); do
129 + case "$is_hash,$i" in
130 + y,*) is_hash=n ;;
131 + n,*^{}) is_hash=y ;;
132 + n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
133 + n,*) is_hash=y; echo "$i" ;;
134 + esac
135 + done
136 +}
137 +
138 __git_refs ()
139 {
140 local cmd i is_hash=y dir="$(__gitdir "$1")"
@@ -1050,6 +1071,40 @@ _git_submodule ()
1071 fi
1072 }
1073
1074 +_git_tag ()
1075 +{
1076 + local i c=1 f=0
1077 + while [ $c -lt $COMP_CWORD ]; do
1078 + i="${COMP_WORDS[c]}"
1079 + case "$i" in
1080 + -d|-v)
1081 + __gitcomp "$(__git_tags)"
1082 + return
1083 + ;;
1084 + -f)
1085 + f=1
1086 + ;;
1087 + esac
1088 + c=$((++c))
1089 + done
1090 +
1091 + case "${COMP_WORDS[COMP_CWORD-1]}" in
1092 + -m|-F)
1093 + COMPREPLY=()
1094 + ;;
1095 + -*|tag|git-tag)
1096 + if [ $f = 1 ]; then
1097 + __gitcomp "$(__git_tags)"
1098 + else
1099 + COMPREPLY=()
1100 + fi
1101 + ;;
1102 + *)
1103 + __gitcomp "$(__git_refs)"
1104 + ;;
1105 + esac
1106 +}
1107 +
1108 _git ()
1109 {
1110 local i c=1 command __git_dir
@@ -1117,6 +1172,7 @@ _git ()
1172 show-branch) _git_log ;;
1173 stash) _git_stash ;;
1174 submodule) _git_submodule ;;
1175 + tag) _git_tag ;;
1176 whatchanged) _git_log ;;
1177 *) COMPREPLY=() ;;
1178 esac
@@ -1167,6 +1223,7 @@ complete -o default -o nospace -F _git_show git-show
1223 complete -o default -o nospace -F _git_stash git-stash
1224 complete -o default -o nospace -F _git_submodule git-submodule
1225 complete -o default -o nospace -F _git_log git-show-branch
1226 +complete -o default -o nospace -F _git_tag git-tag
1227 complete -o default -o nospace -F _git_log git-whatchanged
1228
1229 # The following are necessary only for Cygwin, and only are needed
@@ -1192,5 +1249,6 @@ complete -o default -o nospace -F _git_config git-config
1249 complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1250 complete -o default -o nospace -F _git_show git-show.exe
1251 complete -o default -o nospace -F _git_log git-show-branch.exe
1252 +complete -o default -o nospace -F _git_tag git-tag.exe
1253 complete -o default -o nospace -F _git_log git-whatchanged.exe
1254 fi