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