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