1#!/bin/sh
2# Copyright (c) 2005 Linus Torvalds
3
4USAGE='-l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg>] <tagname> [<head>]'
5SUBDIRECTORY_OK='Yes'
6. git-sh-setup
7
8message_given=
9annotate=
10signed=
11force=
12message=
13username=
14list=
15verify=
16while case "$#" in 0) break ;; esac
17do
18 case "$1" in
19 -a)
20 annotate=1
21 ;;
22 -s)
23 annotate=1
24 signed=1
25 ;;
26 -f)
27 force=1
28 ;;
29 -l)
30 case "$#" in
31 1)
32 set x . ;;
33 esac
34 shift
35 git rev-parse --symbolic --tags | sort | grep "$@"
36 exit $?
37 ;;
38 -m)
39 annotate=1
40 shift
41 message="$1"
42 if test "$#" = "0"; then
43 die "error: option -m needs an argument"
44 else
45 message_given=1
46 fi
47 ;;
48 -F)
49 annotate=1
50 shift
51 if test "$#" = "0"; then
52 die "error: option -F needs an argument"
53 else
54 message="$(cat "$1")"
55 message_given=1
56 fi
57 ;;
58 -u)
59 annotate=1
60 signed=1
61 shift
62 username="$1"
63 ;;
64 -d)
65 shift
66 had_error=0
67 for tag
68 do
69 cur=$(git-show-ref --verify --hash -- "refs/tags/$tag") || {
70 echo >&2 "Seriously, what tag are you talking about?"
71 had_error=1
72 continue
73 }
74 git-update-ref -m 'tag: delete' -d "refs/tags/$tag" "$cur" || {
75 had_error=1
76 continue
77 }
78 echo "Deleted tag $tag."
79 done
80 exit $had_error
81 ;;
82 -v)
83 shift
84 tag_name="$1"
85 tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
86 die "Seriously, what tag are you talking about?"
87 git-verify-tag -v "$tag"
88 exit $?
89 ;;
90 -*)
91 usage
92 ;;
93 *)
94 break
95 ;;
96 esac
97 shift
98done
99
100name="$1"
101[ "$name" ] || usage
102prev=0000000000000000000000000000000000000000
103if git-show-ref --verify --quiet -- "refs/tags/$name"
104then
105 test -n "$force" || die "tag '$name' already exists"
106 prev=`git rev-parse "refs/tags/$name"`
107fi
108shift
109git-check-ref-format "tags/$name" ||
110 die "we do not like '$name' as a tag name."
111
112object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
113type=$(git-cat-file -t $object) || exit 1
114tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
115: ${username:=$(expr "z$tagger" : 'z\(.*>\)')}
116
117trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
118
119if [ "$annotate" ]; then
120 if [ -z "$message_given" ]; then
121 ( echo "#"
122 echo "# Write a tag message"
123 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
124 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
125 else
126 echo "$message" >"$GIT_DIR"/TAG_EDITMSG
127 fi
128
129 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
130 git-stripspace >"$GIT_DIR"/TAG_FINALMSG
131
132 [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
133 echo >&2 "No tag message?"
134 exit 1
135 }
136
137 ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
138 "$object" "$type" "$name" "$tagger";
139 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
140 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
141 if [ "$signed" ]; then
142 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
143 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
144 die "failed to sign the tag with GPG."
145 fi
146 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
147fi
148
149git update-ref "refs/tags/$name" "$object" "$prev"
150