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