1#!/bin/sh
2# Copyright (c) 2005 Linus Torvalds
3
4. git-sh-setup || die "Not a git archive"
5
6usage () {
7 echo >&2 "Usage: git-tag [-a | -s] [-f] [-m "tag message"] tagname [head]"
8 exit 1
9}
10
11annotate=
12signed=
13force=
14message=
15while case "$#" in 0) break ;; esac
16do
17 case "$1" in
18 -a)
19 annotate=1
20 ;;
21 -s)
22 annotate=1
23 signed=1
24 ;;
25 -f)
26 force=1
27 ;;
28 -m)
29 annotate=1
30 shift
31 message="$1"
32 ;;
33 -*)
34 usage
35 ;;
36 *)
37 break
38 ;;
39 esac
40 shift
41done
42
43name="$1"
44[ "$name" ] || usage
45if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
46 die "tag '$name' already exists"
47fi
48shift
49git-check-ref-format "tags/$name" ||
50 die "we do not like '$name' as a tag name."
51
52object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
53type=$(git-cat-file -t $object) || exit 1
54tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
55
56trap 'rm -f .tmp-tag* .tagmsg .editmsg' 0
57
58if [ "$annotate" ]; then
59 if [ -z "$message" ]; then
60 ( echo "#"
61 echo "# Write a tag message"
62 echo "#" ) > .editmsg
63 ${VISUAL:-${EDITOR:-vi}} .editmsg || exit
64 else
65 echo "$message" > .editmsg
66 fi
67
68 grep -v '^#' < .editmsg | git-stripspace > .tagmsg
69
70 [ -s .tagmsg ] || exit
71
72 ( echo -e "object $object\ntype $type\ntag $name\ntagger $tagger\n"; cat .tagmsg ) > .tmp-tag
73 rm -f .tmp-tag.asc .tagmsg
74 if [ "$signed" ]; then
75 me=$(expr "$tagger" : '\(.*>\)') &&
76 gpg -bsa -u "$me" .tmp-tag &&
77 cat .tmp-tag.asc >>.tmp-tag ||
78 die "failed to sign the tag with GPG."
79 fi
80 object=$(git-mktag < .tmp-tag)
81fi
82
83mkdir -p "$GIT_DIR/refs/tags"
84echo $object > "$GIT_DIR/refs/tags/$name"