Merge branch 'lt/follow'
[git/spearce.git] / git-tag.sh
blobc84043902f9fcaf7c09846b9fd68986bd7fc5108
1 #!/bin/sh
2 # Copyright (c) 2005 Linus Torvalds
4 USAGE='[-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg>] <tagname> [<head>]'
5 SUBDIRECTORY_OK='Yes'
6 . git-sh-setup
8 message_given=
9 annotate=
10 signed=
11 force=
12 message=
13 username=
14 list=
15 verify=
16 LINES=0
17 while case "$#" in 0) break ;; esac
19 case "$1" in
20 -a)
21 annotate=1
23 -s)
24 annotate=1
25 signed=1
27 -f)
28 force=1
30 -n)
31 case $2 in
32 -*) LINES=1 # no argument
34 *) shift
35 LINES=$(expr "$1" : '\([0-9]*\)')
36 [ -z "$LINES" ] && LINES=1 # 1 line is default when -n is used
38 esac
40 -l)
41 list=1
42 shift
43 PATTERN="$1" # select tags by shell pattern, not re
44 git rev-parse --symbolic --tags | sort |
45 while read TAG
47 case "$TAG" in
48 *$PATTERN*) ;;
49 *) continue ;;
50 esac
51 [ "$LINES" -le 0 ] && { echo "$TAG"; continue ;}
52 OBJTYPE=$(git cat-file -t "$TAG")
53 case $OBJTYPE in
54 tag) ANNOTATION=$(git cat-file tag "$TAG" |
55 sed -e '1,/^$/d' \
56 -e '/^-----BEGIN PGP SIGNATURE-----$/Q' )
57 printf "%-15s %s\n" "$TAG" "$ANNOTATION" |
58 sed -e '2,$s/^/ /' \
59 -e "${LINES}q"
61 *) echo "$TAG"
63 esac
64 done
66 -m)
67 annotate=1
68 shift
69 message="$1"
70 if test "$#" = "0"; then
71 die "error: option -m needs an argument"
72 else
73 message_given=1
76 -F)
77 annotate=1
78 shift
79 if test "$#" = "0"; then
80 die "error: option -F needs an argument"
81 else
82 message="$(cat "$1")"
83 message_given=1
86 -u)
87 annotate=1
88 signed=1
89 shift
90 username="$1"
92 -d)
93 shift
94 had_error=0
95 for tag
97 cur=$(git-show-ref --verify --hash -- "refs/tags/$tag") || {
98 echo >&2 "Seriously, what tag are you talking about?"
99 had_error=1
100 continue
102 git-update-ref -m 'tag: delete' -d "refs/tags/$tag" "$cur" || {
103 had_error=1
104 continue
106 echo "Deleted tag $tag."
107 done
108 exit $had_error
111 shift
112 tag_name="$1"
113 tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
114 die "Seriously, what tag are you talking about?"
115 git-verify-tag -v "$tag"
116 exit $?
119 usage
122 break
124 esac
125 shift
126 done
128 [ -n "$list" ] && exit 0
130 name="$1"
131 [ "$name" ] || usage
132 prev=0000000000000000000000000000000000000000
133 if git-show-ref --verify --quiet -- "refs/tags/$name"
134 then
135 test -n "$force" || die "tag '$name' already exists"
136 prev=`git rev-parse "refs/tags/$name"`
138 shift
139 git-check-ref-format "tags/$name" ||
140 die "we do not like '$name' as a tag name."
142 object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
143 type=$(git-cat-file -t $object) || exit 1
144 tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
146 test -n "$username" ||
147 username=$(git-repo-config user.signingkey) ||
148 username=$(expr "z$tagger" : 'z\(.*>\)')
150 trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
152 if [ "$annotate" ]; then
153 if [ -z "$message_given" ]; then
154 ( echo "#"
155 echo "# Write a tag message"
156 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
157 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
158 else
159 printf '%s\n' "$message" >"$GIT_DIR"/TAG_EDITMSG
162 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
163 git-stripspace >"$GIT_DIR"/TAG_FINALMSG
165 [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
166 echo >&2 "No tag message?"
167 exit 1
170 ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
171 "$object" "$type" "$name" "$tagger";
172 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
173 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
174 if [ "$signed" ]; then
175 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
176 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
177 die "failed to sign the tag with GPG."
179 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
182 git update-ref "refs/tags/$name" "$object" "$prev"