Shell utilities: Guard against expr' magic tokens.
[git/git.git] / git-tag.sh
CommitLineData
65f0d0ee 1#!/bin/sh
8ac069ac
JH
2# Copyright (c) 2005 Linus Torvalds
3
b867c7c2 4USAGE='-l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d] [-m <msg>] <tagname> [<head>]'
806f36d4
FK
5SUBDIRECTORY_OK='Yes'
6. git-sh-setup
d165fa14 7
0fad0fdd 8annotate=
d727782e
LT
9signed=
10force=
c882bc93 11message=
bc162e40 12username=
b867c7c2 13list=
d727782e
LT
14while case "$#" in 0) break ;; esac
15do
16 case "$1" in
0fad0fdd
JH
17 -a)
18 annotate=1
19 ;;
d727782e 20 -s)
0fad0fdd 21 annotate=1
d727782e
LT
22 signed=1
23 ;;
24 -f)
25 force=1
26 ;;
b867c7c2
JH
27 -l)
28 cd "$GIT_DIR/refs" &&
29 case "$#" in
30 1)
31 find tags -type f -print ;;
32 *)
33 shift
34 find tags -type f -print | grep "$@" ;;
35 esac
36 exit $?
37 ;;
c882bc93
CW
38 -m)
39 annotate=1
40 shift
41 message="$1"
42 ;;
bc162e40
LT
43 -u)
44 annotate=1
45 signed=1
46 shift
47 username="$1"
48 ;;
61f81518
KR
49 -d)
50 shift
51 tag_name="$1"
52 rm "$GIT_DIR/refs/tags/$tag_name" && \
53 echo "Deleted tag $tag_name."
54 exit $?
55 ;;
0fad0fdd
JH
56 -*)
57 usage
58 ;;
d727782e
LT
59 *)
60 break
61 ;;
62 esac
63 shift
64done
65
918c05f1 66name="$1"
0fad0fdd
JH
67[ "$name" ] || usage
68if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
69 die "tag '$name' already exists"
70fi
d727782e 71shift
03feddd6
JH
72git-check-ref-format "tags/$name" ||
73 die "we do not like '$name' as a tag name."
8ac069ac 74
2ad77e67 75object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
0fc65a45 76type=$(git-cat-file -t $object) || exit 1
c818566d 77tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
f327dbce 78: ${username:=$(expr "z$tagger" : 'z\(.*>\)')}
918c05f1 79
a10aad6a 80trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
0fad0fdd
JH
81
82if [ "$annotate" ]; then
c882bc93
CW
83 if [ -z "$message" ]; then
84 ( echo "#"
85 echo "# Write a tag message"
a10aad6a
JH
86 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
87 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
c882bc93 88 else
a10aad6a 89 echo "$message" >"$GIT_DIR"/TAG_EDITMSG
c882bc93 90 fi
d727782e 91
a10aad6a
JH
92 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
93 git-stripspace >"$GIT_DIR"/TAG_FINALMSG
918c05f1 94
a10aad6a 95 [ -s "$GIT_DIR"/TAG_FINALMSG ] || {
bc162e40
LT
96 echo >&2 "No tag message?"
97 exit 1
98 }
918c05f1 99
0dbc4e89
AR
100 ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
101 "$object" "$type" "$name" "$tagger";
a10aad6a
JH
102 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
103 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
0fad0fdd 104 if [ "$signed" ]; then
a10aad6a
JH
105 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
106 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
0fad0fdd
JH
107 die "failed to sign the tag with GPG."
108 fi
a10aad6a 109 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
d727782e 110fi
918c05f1 111
52963a7a
JH
112leading=`expr "refs/tags/$name" : '\(.*\)/'` &&
113mkdir -p "$GIT_DIR/$leading" &&
d727782e 114echo $object > "$GIT_DIR/refs/tags/$name"