git: grok 'help' to mean '--help'.
[git/vmiklos.git] / git-tag.sh
blob6d0c973239a1b72bbef43aa430f1ee534b7ab458
1 #!/bin/sh
2 # Copyright (c) 2005 Linus Torvalds
4 USAGE='[-a | -s | -u <key-id>] [-f | -d] [-m <msg>] <tagname> [<head>]'
5 SUBDIRECTORY_OK='Yes'
6 . git-sh-setup
8 annotate=
9 signed=
10 force=
11 message=
12 username=
13 while case "$#" in 0) break ;; esac
15 case "$1" in
16 -a)
17 annotate=1
19 -s)
20 annotate=1
21 signed=1
23 -f)
24 force=1
26 -m)
27 annotate=1
28 shift
29 message="$1"
31 -u)
32 annotate=1
33 signed=1
34 shift
35 username="$1"
37 -d)
38 shift
39 tag_name="$1"
40 rm "$GIT_DIR/refs/tags/$tag_name" && \
41 echo "Deleted tag $tag_name."
42 exit $?
44 -*)
45 usage
48 break
50 esac
51 shift
52 done
54 name="$1"
55 [ "$name" ] || usage
56 if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
57 die "tag '$name' already exists"
59 shift
60 git-check-ref-format "tags/$name" ||
61 die "we do not like '$name' as a tag name."
63 object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
64 type=$(git-cat-file -t $object) || exit 1
65 tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
66 : ${username:=$(expr "$tagger" : '\(.*>\)')}
68 trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
70 if [ "$annotate" ]; then
71 if [ -z "$message" ]; then
72 ( echo "#"
73 echo "# Write a tag message"
74 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
75 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
76 else
77 echo "$message" >"$GIT_DIR"/TAG_EDITMSG
80 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
81 git-stripspace >"$GIT_DIR"/TAG_FINALMSG
83 [ -s "$GIT_DIR"/TAG_FINALMSG ] || {
84 echo >&2 "No tag message?"
85 exit 1
88 ( echo -e "object $object\ntype $type\ntag $name\ntagger $tagger\n";
89 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
90 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
91 if [ "$signed" ]; then
92 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
93 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
94 die "failed to sign the tag with GPG."
96 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
99 leading=`expr "refs/tags/$name" : '\(.*\)/'` &&
100 mkdir -p "$GIT_DIR/$leading" &&
101 echo $object > "$GIT_DIR/refs/tags/$name"