git-am: do not lose already edited final-commit when resuming.
[git/jnareb-git/bp-gitweb.git] / git-tag.sh
blob6130904a94ddeffb15b1b10ded8336c67112c12e
1 #!/bin/sh
2 # Copyright (c) 2005 Linus Torvalds
4 . git-sh-setup || die "Not a git archive"
6 usage () {
7 echo >&2 "Usage: git-tag [-a | -s | -u <key-id>] [-f] [-m <msg>] <tagname> [<head>]"
8 exit 1
11 annotate=
12 signed=
13 force=
14 message=
15 username=
16 while case "$#" in 0) break ;; esac
18 case "$1" in
19 -a)
20 annotate=1
22 -s)
23 annotate=1
24 signed=1
26 -f)
27 force=1
29 -m)
30 annotate=1
31 shift
32 message="$1"
34 -u)
35 annotate=1
36 signed=1
37 shift
38 username="$1"
40 -*)
41 usage
44 break
46 esac
47 shift
48 done
50 name="$1"
51 [ "$name" ] || usage
52 if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
53 die "tag '$name' already exists"
55 shift
56 git-check-ref-format "tags/$name" ||
57 die "we do not like '$name' as a tag name."
59 object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
60 type=$(git-cat-file -t $object) || exit 1
61 tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
62 : ${username:=$(expr "$tagger" : '\(.*>\)')}
64 trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
66 if [ "$annotate" ]; then
67 if [ -z "$message" ]; then
68 ( echo "#"
69 echo "# Write a tag message"
70 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
71 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
72 else
73 echo "$message" >"$GIT_DIR"/TAG_EDITMSG
76 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
77 git-stripspace >"$GIT_DIR"/TAG_FINALMSG
79 [ -s "$GIT_DIR"/TAG_FINALMSG ] || {
80 echo >&2 "No tag message?"
81 exit 1
84 ( echo -e "object $object\ntype $type\ntag $name\ntagger $tagger\n";
85 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
86 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
87 if [ "$signed" ]; then
88 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
89 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
90 die "failed to sign the tag with GPG."
92 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
95 leading=`expr "refs/tags/$name" : '\(.*\)/'` &&
96 mkdir -p "$GIT_DIR/$leading" &&
97 echo $object > "$GIT_DIR/refs/tags/$name"