remote.c: refactor match_explicit_refs()
[gitweb.git] / git-tag.sh
index e1bfa82f1ea1213fad8ff8c4b6ad2b17e6ca1f7e..c84043902f9fcaf7c09846b9fd68986bd7fc5108 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 # Copyright (c) 2005 Linus Torvalds
 
-USAGE='-l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d] [-m <msg>] <tagname> [<head>]'
+USAGE='[-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg>] <tagname> [<head>]'
 SUBDIRECTORY_OK='Yes'
 . git-sh-setup
 
@@ -12,6 +12,8 @@ force=
 message=
 username=
 list=
+verify=
+LINES=0
 while case "$#" in 0) break ;; esac
 do
     case "$1" in
@@ -25,17 +27,44 @@ do
     -f)
        force=1
        ;;
-    -l)
-       case "$#" in
-       1)
-               set x . ;;
+    -n)
+        case $2 in
+       -*)     LINES=1         # no argument
+               ;;
+       *)      shift
+               LINES=$(expr "$1" : '\([0-9]*\)')
+               [ -z "$LINES" ] && LINES=1 # 1 line is default when -n is used
+               ;;
        esac
+       ;;
+    -l)
+       list=1
        shift
-       git rev-parse --symbolic --tags | sort | grep "$@"
-       exit $?
+       PATTERN="$1"    # select tags by shell pattern, not re
+       git rev-parse --symbolic --tags | sort |
+           while read TAG
+           do
+               case "$TAG" in
+               *$PATTERN*) ;;
+               *)          continue ;;
+               esac
+               [ "$LINES" -le 0 ] && { echo "$TAG"; continue ;}
+               OBJTYPE=$(git cat-file -t "$TAG")
+               case $OBJTYPE in
+               tag)    ANNOTATION=$(git cat-file tag "$TAG" |
+                                      sed -e '1,/^$/d' \
+                                          -e '/^-----BEGIN PGP SIGNATURE-----$/Q' )
+                       printf "%-15s %s\n" "$TAG" "$ANNOTATION" |
+                         sed -e '2,$s/^/    /' \
+                             -e "${LINES}q"
+                       ;;
+               *)      echo "$TAG"
+                       ;;
+               esac
+           done
        ;;
     -m)
-       annotate=1
+       annotate=1
        shift
        message="$1"
        if test "$#" = "0"; then
@@ -61,12 +90,29 @@ do
        username="$1"
        ;;
     -d)
-       shift
+       shift
+       had_error=0
+       for tag
+       do
+               cur=$(git-show-ref --verify --hash -- "refs/tags/$tag") || {
+                       echo >&2 "Seriously, what tag are you talking about?"
+                       had_error=1
+                       continue
+               }
+               git-update-ref -m 'tag: delete' -d "refs/tags/$tag" "$cur" || {
+                       had_error=1
+                       continue
+               }
+               echo "Deleted tag $tag."
+       done
+       exit $had_error
+       ;;
+    -v)
+       shift
        tag_name="$1"
        tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
                die "Seriously, what tag are you talking about?"
-       git-update-ref -m 'tag: delete' -d "refs/tags/$tag_name" "$tag" &&
-               echo "Deleted tag $tag_name."
+       git-verify-tag -v "$tag"
        exit $?
        ;;
     -*)
@@ -79,6 +125,8 @@ do
     shift
 done
 
+[ -n "$list" ] && exit 0
+
 name="$1"
 [ "$name" ] || usage
 prev=0000000000000000000000000000000000000000
@@ -94,7 +142,10 @@ git-check-ref-format "tags/$name" ||
 object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
 type=$(git-cat-file -t $object) || exit 1
 tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
-: ${username:=$(expr "z$tagger" : 'z\(.*>\)')}
+
+test -n "$username" ||
+       username=$(git-repo-config user.signingkey) ||
+       username=$(expr "z$tagger" : 'z\(.*>\)')
 
 trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
 
@@ -105,7 +156,7 @@ if [ "$annotate" ]; then
           echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
         ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
     else
-        echo "$message" >"$GIT_DIR"/TAG_EDITMSG
+        printf '%s\n' "$message" >"$GIT_DIR"/TAG_EDITMSG
     fi
 
     grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
@@ -129,4 +180,3 @@ if [ "$annotate" ]; then
 fi
 
 git update-ref "refs/tags/$name" "$object" "$prev"
-