git-tag.shon commit git-svn: bugfix and optimize the 'log' command (c0d4822)
   1#!/bin/sh
   2# Copyright (c) 2005 Linus Torvalds
   3
   4USAGE='-l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d] [-m <msg>] <tagname> [<head>]'
   5SUBDIRECTORY_OK='Yes'
   6. git-sh-setup
   7
   8annotate=
   9signed=
  10force=
  11message=
  12username=
  13list=
  14while case "$#" in 0) break ;; esac
  15do
  16    case "$1" in
  17    -a)
  18        annotate=1
  19        ;;
  20    -s)
  21        annotate=1
  22        signed=1
  23        ;;
  24    -f)
  25        force=1
  26        ;;
  27    -l)
  28        case "$#" in
  29        1)
  30                set x . ;;
  31        esac
  32        shift
  33        git rev-parse --symbolic --tags | sort | grep "$@"
  34        exit $?
  35        ;;
  36    -m)
  37        annotate=1
  38        shift
  39        message="$1"
  40        ;;
  41    -u)
  42        annotate=1
  43        signed=1
  44        shift
  45        username="$1"
  46        ;;
  47    -d)
  48        shift
  49        tag_name="$1"
  50        rm "$GIT_DIR/refs/tags/$tag_name" && \
  51                echo "Deleted tag $tag_name."
  52        exit $?
  53        ;;
  54    -*)
  55        usage
  56        ;;
  57    *)
  58        break
  59        ;;
  60    esac
  61    shift
  62done
  63
  64name="$1"
  65[ "$name" ] || usage
  66if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
  67    die "tag '$name' already exists"
  68fi
  69shift
  70git-check-ref-format "tags/$name" ||
  71        die "we do not like '$name' as a tag name."
  72
  73object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
  74type=$(git-cat-file -t $object) || exit 1
  75tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
  76: ${username:=$(expr "z$tagger" : 'z\(.*>\)')}
  77
  78trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
  79
  80if [ "$annotate" ]; then
  81    if [ -z "$message" ]; then
  82        ( echo "#"
  83          echo "# Write a tag message"
  84          echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
  85        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
  86    else
  87        echo "$message" >"$GIT_DIR"/TAG_EDITMSG
  88    fi
  89
  90    grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
  91    git-stripspace >"$GIT_DIR"/TAG_FINALMSG
  92
  93    [ -s "$GIT_DIR"/TAG_FINALMSG ] || {
  94        echo >&2 "No tag message?"
  95        exit 1
  96    }
  97
  98    ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
  99        "$object" "$type" "$name" "$tagger";
 100      cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
 101    rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
 102    if [ "$signed" ]; then
 103        gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
 104        cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
 105        die "failed to sign the tag with GPG."
 106    fi
 107    object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
 108fi
 109
 110leading=`expr "refs/tags/$name" : '\(.*\)/'` &&
 111mkdir -p "$GIT_DIR/$leading" &&
 112echo $object > "$GIT_DIR/refs/tags/$name"