1#!/bin/sh
2# Copyright (c) 2005 Linus Torvalds
3
4. git-sh-setup-script || die "Not a git archive"
5
6usage () {
7 echo >&2 "Usage: git-tag-script [-a | -s] [-f] [-m "tag message"] tagname"
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
49
50object=$(git-rev-parse --verify --revs-only --default HEAD "$@") || exit 1
51type=$(git-cat-file -t $object) || exit 1
52tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
53
54trap 'rm -f .tmp-tag* .tagmsg .editmsg' 0
55
56if [ "$annotate" ]; then
57 if [ -z "$message" ]; then
58 ( echo "#"
59 echo "# Write a tag message"
60 echo "#" ) > .editmsg
61 ${VISUAL:-${EDITOR:-vi}} .editmsg || exit
62 else
63 echo "$message" > .editmsg
64 fi
65
66 grep -v '^#' < .editmsg | git-stripspace > .tagmsg
67
68 [ -s .tagmsg ] || exit
69
70 ( echo -e "object $object\ntype $type\ntag $name\ntagger $tagger\n"; cat .tagmsg ) > .tmp-tag
71 rm -f .tmp-tag.asc .tagmsg
72 if [ "$signed" ]; then
73 me=$(expr "$tagger" : '\(.*>\)') &&
74 gpg -bsa -u "$me" .tmp-tag &&
75 cat .tmp-tag.asc >>.tmp-tag ||
76 die "failed to sign the tag with GPG."
77 fi
78 object=$(git-mktag < .tmp-tag)
79fi
80
81mkdir -p "$GIT_DIR/refs/tags"
82echo $object > "$GIT_DIR/refs/tags/$name"