Correct usages of sed in git-tag for Mac OS X
[git/dscho.git] / git-tag.sh
blob3917cd829842462ca4e3d196246c637d8c3a58dc
1 #!/bin/sh
2 # Copyright (c) 2005 Linus Torvalds
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
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
19 case "$1" in
20 -a)
21 annotate=1
23 -s)
24 annotate=1
25 signed=1
27 -f)
28 force=1
30 -n)
31 case $2 in
32 -*) LINES=1 # no argument
34 *) shift
35 LINES=$(expr "$1" : '\([0-9]*\)')
36 [ -z "$LINES" ] && LINES=1 # 1 line is default when -n is used
38 esac
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
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)
55 ANNOTATION=$(git cat-file tag "$TAG" |
56 sed -e '1,/^$/d' |
57 sed -n -e "
58 /^-----BEGIN PGP SIGNATURE-----\$/q
59 2,\$s/^/ /
61 ${LINES}q
63 printf "%-15s %s\n" "$TAG" "$ANNOTATION"
65 *) echo "$TAG"
67 esac
68 done
70 -m)
71 annotate=1
72 shift
73 message="$1"
74 if test "$#" = "0"; then
75 die "error: option -m needs an argument"
76 else
77 message_given=1
80 -F)
81 annotate=1
82 shift
83 if test "$#" = "0"; then
84 die "error: option -F needs an argument"
85 else
86 message="$(cat "$1")"
87 message_given=1
90 -u)
91 annotate=1
92 signed=1
93 shift
94 username="$1"
96 -d)
97 shift
98 had_error=0
99 for tag
101 cur=$(git-show-ref --verify --hash -- "refs/tags/$tag") || {
102 echo >&2 "Seriously, what tag are you talking about?"
103 had_error=1
104 continue
106 git-update-ref -m 'tag: delete' -d "refs/tags/$tag" "$cur" || {
107 had_error=1
108 continue
110 echo "Deleted tag $tag."
111 done
112 exit $had_error
115 shift
116 tag_name="$1"
117 tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
118 die "Seriously, what tag are you talking about?"
119 git-verify-tag -v "$tag"
120 exit $?
123 usage
126 break
128 esac
129 shift
130 done
132 [ -n "$list" ] && exit 0
134 name="$1"
135 [ "$name" ] || usage
136 prev=0000000000000000000000000000000000000000
137 if git-show-ref --verify --quiet -- "refs/tags/$name"
138 then
139 test -n "$force" || die "tag '$name' already exists"
140 prev=`git rev-parse "refs/tags/$name"`
142 shift
143 git-check-ref-format "tags/$name" ||
144 die "we do not like '$name' as a tag name."
146 object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
147 type=$(git-cat-file -t $object) || exit 1
148 tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
150 test -n "$username" ||
151 username=$(git-repo-config user.signingkey) ||
152 username=$(expr "z$tagger" : 'z\(.*>\)')
154 trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
156 if [ "$annotate" ]; then
157 if [ -z "$message_given" ]; then
158 ( echo "#"
159 echo "# Write a tag message"
160 echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
161 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
162 else
163 printf '%s\n' "$message" >"$GIT_DIR"/TAG_EDITMSG
166 grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
167 git-stripspace >"$GIT_DIR"/TAG_FINALMSG
169 [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
170 echo >&2 "No tag message?"
171 exit 1
174 ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
175 "$object" "$type" "$name" "$tagger";
176 cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
177 rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
178 if [ "$signed" ]; then
179 gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
180 cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
181 die "failed to sign the tag with GPG."
183 object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
186 git update-ref "refs/tags/$name" "$object" "$prev"