]> git.ipfire.org Git - thirdparty/git.git/blame - git-tag.sh
Rewrite "git-frotz" to "git frotz"
[thirdparty/git.git] / git-tag.sh
CommitLineData
65f0d0ee 1#!/bin/sh
8ac069ac
JH
2# Copyright (c) 2005 Linus Torvalds
3
980ea5c5 4USAGE='[-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg>] <tagname> [<head>]'
806f36d4
FK
5SUBDIRECTORY_OK='Yes'
6. git-sh-setup
d165fa14 7
aabd7693 8message_given=
0fad0fdd 9annotate=
d727782e
LT
10signed=
11force=
c882bc93 12message=
bc162e40 13username=
b867c7c2 14list=
0bc72abd 15verify=
980ea5c5 16LINES=0
d727782e
LT
17while case "$#" in 0) break ;; esac
18do
19 case "$1" in
0fad0fdd
JH
20 -a)
21 annotate=1
72bb989d 22 shift
0fad0fdd 23 ;;
d727782e 24 -s)
0fad0fdd 25 annotate=1
d727782e 26 signed=1
72bb989d 27 shift
d727782e
LT
28 ;;
29 -f)
30 force=1
72bb989d 31 shift
d727782e 32 ;;
980ea5c5 33 -n)
72bb989d
AV
34 case "$#,$2" in
35 1,* | *,-*)
36 LINES=1 # no argument
980ea5c5
MM
37 ;;
38 *) shift
39 LINES=$(expr "$1" : '\([0-9]*\)')
40 [ -z "$LINES" ] && LINES=1 # 1 line is default when -n is used
41 ;;
b867c7c2 42 esac
72bb989d 43 shift
980ea5c5
MM
44 ;;
45 -l)
46 list=1
e6ebb8a3 47 shift
72bb989d
AV
48 case $# in
49 0) PATTERN=
50 ;;
51 *)
52 PATTERN="$1" # select tags by shell pattern, not re
53 shift
54 ;;
55 esac
980ea5c5
MM
56 git rev-parse --symbolic --tags | sort |
57 while read TAG
58 do
59 case "$TAG" in
60 *$PATTERN*) ;;
61 *) continue ;;
62 esac
63 [ "$LINES" -le 0 ] && { echo "$TAG"; continue ;}
64 OBJTYPE=$(git cat-file -t "$TAG")
65 case $OBJTYPE in
bfc04bb9
SP
66 tag)
67 ANNOTATION=$(git cat-file tag "$TAG" |
68 sed -e '1,/^$/d' |
69 sed -n -e "
70 /^-----BEGIN PGP SIGNATURE-----\$/q
71 2,\$s/^/ /
72 p
73 ${LINES}q
74 ")
75 printf "%-15s %s\n" "$TAG" "$ANNOTATION"
980ea5c5
MM
76 ;;
77 *) echo "$TAG"
78 ;;
79 esac
80 done
b867c7c2 81 ;;
c882bc93 82 -m)
a6080a0a 83 annotate=1
c882bc93
CW
84 shift
85 message="$1"
aabd7693
HWN
86 if test "$#" = "0"; then
87 die "error: option -m needs an argument"
aabd7693 88 else
72bb989d 89 message="$1"
aabd7693 90 message_given=1
72bb989d 91 shift
aabd7693 92 fi
c882bc93 93 ;;
f79c73ce
JS
94 -F)
95 annotate=1
96 shift
97 if test "$#" = "0"; then
98 die "error: option -F needs an argument"
f79c73ce
JS
99 else
100 message="$(cat "$1")"
101 message_given=1
72bb989d 102 shift
f79c73ce
JS
103 fi
104 ;;
bc162e40
LT
105 -u)
106 annotate=1
107 signed=1
108 shift
72bb989d
AV
109 if test "$#" = "0"; then
110 die "error: option -u needs an argument"
111 else
112 username="$1"
113 shift
114 fi
bc162e40 115 ;;
61f81518 116 -d)
a6080a0a 117 shift
453c1e85
JH
118 had_error=0
119 for tag
120 do
5be60078 121 cur=$(git show-ref --verify --hash -- "refs/tags/$tag") || {
453c1e85
JH
122 echo >&2 "Seriously, what tag are you talking about?"
123 had_error=1
124 continue
125 }
5be60078 126 git update-ref -m 'tag: delete' -d "refs/tags/$tag" "$cur" || {
453c1e85
JH
127 had_error=1
128 continue
129 }
130 echo "Deleted tag $tag."
131 done
132 exit $had_error
61f81518 133 ;;
0bc72abd
SB
134 -v)
135 shift
136 tag_name="$1"
5be60078 137 tag=$(git show-ref --verify --hash -- "refs/tags/$tag_name") ||
0bc72abd
SB
138 die "Seriously, what tag are you talking about?"
139 git-verify-tag -v "$tag"
140 exit $?
141 ;;
0fad0fdd
JH
142 -*)
143 usage
144 ;;
d727782e
LT
145 *)
146 break
147 ;;
148 esac
d727782e
LT
149done
150
980ea5c5
MM
151[ -n "$list" ] && exit 0
152
918c05f1 153name="$1"
0fad0fdd 154[ "$name" ] || usage
cede7526 155prev=0000000000000000000000000000000000000000
5be60078 156if git show-ref --verify --quiet -- "refs/tags/$name"
cede7526
JH
157then
158 test -n "$force" || die "tag '$name' already exists"
159 prev=`git rev-parse "refs/tags/$name"`
0fad0fdd 160fi
d727782e 161shift
5be60078 162git check-ref-format "tags/$name" ||
03feddd6 163 die "we do not like '$name' as a tag name."
8ac069ac 164
5be60078
JH
165object=$(git rev-parse --verify --default HEAD "$@") || exit 1
166type=$(git cat-file -t $object) || exit 1
c818566d 167tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
d67778ec 168
63460f28 169test -n "$username" ||
5be60078 170 username=$(git repo-config user.signingkey) ||
63460f28 171 username=$(expr "z$tagger" : 'z\(.*>\)')
918c05f1 172
a10aad6a 173trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
0fad0fdd
JH
174
175if [ "$annotate" ]; then
aabd7693 176 if [ -z "$message_given" ]; then
c882bc93
CW
177 ( echo "#"
178 echo "# Write a tag message"
a10aad6a
JH
179 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
180 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
c882bc93 181 else
a23bfaed 182 printf '%s\n' "$message" >"$GIT_DIR"/TAG_EDITMSG
c882bc93 183 fi
d727782e 184
a10aad6a 185 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
5be60078 186 git stripspace >"$GIT_DIR"/TAG_FINALMSG
918c05f1 187
aabd7693 188 [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
bc162e40
LT
189 echo >&2 "No tag message?"
190 exit 1
191 }
918c05f1 192
0dbc4e89
AR
193 ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
194 "$object" "$type" "$name" "$tagger";
a10aad6a
JH
195 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
196 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
0fad0fdd 197 if [ "$signed" ]; then
63460f28 198 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
a10aad6a 199 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
0fad0fdd
JH
200 die "failed to sign the tag with GPG."
201 fi
a10aad6a 202 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
d727782e 203fi
918c05f1 204
36733704 205git update-ref "refs/tags/$name" "$object" "$prev"