Merge branch 'jc/blame' (early part)
[git] / git-tag.sh
1 #!/bin/sh
2 # Copyright (c) 2005 Linus Torvalds
3
4 USAGE='[-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg>] <tagname> [<head>]'
5 SUBDIRECTORY_OK='Yes'
6 . git-sh-setup
7
8 message_given=
9 annotate=
10 signed=
11 force=
12 message=
13 username=
14 list=
15 verify=
16 LINES=0
17 while case "$#" in 0) break ;; esac
18 do
19     case "$1" in
20     -a)
21         annotate=1
22         ;;
23     -s)
24         annotate=1
25         signed=1
26         ;;
27     -f)
28         force=1
29         ;;
30     -n)
31         case $2 in
32         -*)     LINES=1         # no argument
33                 ;;
34         *)      shift
35                 LINES=$(expr "$1" : '\([0-9]*\)')
36                 [ -z "$LINES" ] && LINES=1 # 1 line is default when -n is used
37                 ;;
38         esac
39         ;;
40     -l)
41         list=1
42         shift
43         PATTERN="$1"    # select tags by shell pattern, not re
44         git rev-parse --symbolic --tags | sort |
45             while read TAG
46             do
47                 case "$TAG" in
48                 *$PATTERN*) ;;
49                 *)          continue ;;
50                 esac
51                 [ "$LINES" -le 0 ] && { echo "$TAG"; continue ;}
52                 OBJTYPE=$(git cat-file -t "$TAG")
53                 case $OBJTYPE in
54                 tag)    ANNOTATION=$(git cat-file tag "$TAG" |
55                                        sed -e '1,/^$/d' \
56                                            -e '/^-----BEGIN PGP SIGNATURE-----$/Q' )
57                         printf "%-15s %s\n" "$TAG" "$ANNOTATION" |
58                           sed -e '2,$s/^/    /' \
59                               -e "${LINES}q"
60                         ;;
61                 *)      echo "$TAG"
62                         ;;
63                 esac
64             done
65         ;;
66     -m)
67         annotate=1
68         shift
69         message="$1"
70         if test "$#" = "0"; then
71             die "error: option -m needs an argument"
72         else
73             message_given=1
74         fi
75         ;;
76     -F)
77         annotate=1
78         shift
79         if test "$#" = "0"; then
80             die "error: option -F needs an argument"
81         else
82             message="$(cat "$1")"
83             message_given=1
84         fi
85         ;;
86     -u)
87         annotate=1
88         signed=1
89         shift
90         username="$1"
91         ;;
92     -d)
93         shift
94         had_error=0
95         for tag
96         do
97                 cur=$(git-show-ref --verify --hash -- "refs/tags/$tag") || {
98                         echo >&2 "Seriously, what tag are you talking about?"
99                         had_error=1
100                         continue
101                 }
102                 git-update-ref -m 'tag: delete' -d "refs/tags/$tag" "$cur" || {
103                         had_error=1
104                         continue
105                 }
106                 echo "Deleted tag $tag."
107         done
108         exit $had_error
109         ;;
110     -v)
111         shift
112         tag_name="$1"
113         tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
114                 die "Seriously, what tag are you talking about?"
115         git-verify-tag -v "$tag"
116         exit $?
117         ;;
118     -*)
119         usage
120         ;;
121     *)
122         break
123         ;;
124     esac
125     shift
126 done
127
128 [ -n "$list" ] && exit 0
129
130 name="$1"
131 [ "$name" ] || usage
132 prev=0000000000000000000000000000000000000000
133 if git-show-ref --verify --quiet -- "refs/tags/$name"
134 then
135     test -n "$force" || die "tag '$name' already exists"
136     prev=`git rev-parse "refs/tags/$name"`
137 fi
138 shift
139 git-check-ref-format "tags/$name" ||
140         die "we do not like '$name' as a tag name."
141
142 object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
143 type=$(git-cat-file -t $object) || exit 1
144 tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
145
146 test -n "$username" ||
147         username=$(git-repo-config user.signingkey) ||
148         username=$(expr "z$tagger" : 'z\(.*>\)')
149
150 trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
151
152 if [ "$annotate" ]; then
153     if [ -z "$message_given" ]; then
154         ( echo "#"
155           echo "# Write a tag message"
156           echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
157         ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
158     else
159         printf '%s\n' "$message" >"$GIT_DIR"/TAG_EDITMSG
160     fi
161
162     grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
163     git-stripspace >"$GIT_DIR"/TAG_FINALMSG
164
165     [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
166         echo >&2 "No tag message?"
167         exit 1
168     }
169
170     ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
171         "$object" "$type" "$name" "$tagger";
172       cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
173     rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
174     if [ "$signed" ]; then
175         gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
176         cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
177         die "failed to sign the tag with GPG."
178     fi
179     object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
180 fi
181
182 git update-ref "refs/tags/$name" "$object" "$prev"