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