Skip to content

Commit 88e21dc

Browse files
committed
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>
1 parent e340d7d commit 88e21dc

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ __git_heads ()
114114
done
115115
}
116116

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+
117138
__git_refs ()
118139
{
119140
local cmd i is_hash=y dir="$(__gitdir "$1")"
@@ -1050,6 +1071,40 @@ _git_submodule ()
10501071
fi
10511072
}
10521073

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+
10531108
_git ()
10541109
{
10551110
local i c=1 command __git_dir
@@ -1117,6 +1172,7 @@ _git ()
11171172
show-branch) _git_log ;;
11181173
stash) _git_stash ;;
11191174
submodule) _git_submodule ;;
1175+
tag) _git_tag ;;
11201176
whatchanged) _git_log ;;
11211177
*) COMPREPLY=() ;;
11221178
esac
@@ -1167,6 +1223,7 @@ complete -o default -o nospace -F _git_show git-show
11671223
complete -o default -o nospace -F _git_stash git-stash
11681224
complete -o default -o nospace -F _git_submodule git-submodule
11691225
complete -o default -o nospace -F _git_log git-show-branch
1226+
complete -o default -o nospace -F _git_tag git-tag
11701227
complete -o default -o nospace -F _git_log git-whatchanged
11711228

11721229
# 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
11921249
complete -o default -o nospace -F _git_shortlog git-shortlog.exe
11931250
complete -o default -o nospace -F _git_show git-show.exe
11941251
complete -o default -o nospace -F _git_log git-show-branch.exe
1252+
complete -o default -o nospace -F _git_tag git-tag.exe
11951253
complete -o default -o nospace -F _git_log git-whatchanged.exe
11961254
fi

0 commit comments

Comments
 (0)