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