git-tag.shon commit Strongly discourage --update-head-ok in fetch-options documentation. (a597fb0)
   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
   8message_given=
   9annotate=
  10signed=
  11force=
  12message=
  13username=
  14list=
  15while case "$#" in 0) break ;; esac
  16do
  17    case "$1" in
  18    -a)
  19        annotate=1
  20        ;;
  21    -s)
  22        annotate=1
  23        signed=1
  24        ;;
  25    -f)
  26        force=1
  27        ;;
  28    -l)
  29        case "$#" in
  30        1)
  31                set x . ;;
  32        esac
  33        shift
  34        git rev-parse --symbolic --tags | sort | grep "$@"
  35        exit $?
  36        ;;
  37    -m)
  38        annotate=1
  39        shift
  40        message="$1"
  41        if test "$#" = "0"; then
  42            die "error: option -m needs an argument"
  43        else
  44            message_given=1
  45        fi
  46        ;;
  47    -F)
  48        annotate=1
  49        shift
  50        if test "$#" = "0"; then
  51            die "error: option -F needs an argument"
  52        else
  53            message="$(cat "$1")"
  54            message_given=1
  55        fi
  56        ;;
  57    -u)
  58        annotate=1
  59        signed=1
  60        shift
  61        username="$1"
  62        ;;
  63    -d)
  64        shift
  65        tag_name="$1"
  66        tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
  67                die "Seriously, what tag are you talking about?"
  68        git-update-ref -m 'tag: delete' -d "refs/tags/$tag_name" "$tag" &&
  69                echo "Deleted tag $tag_name."
  70        exit $?
  71        ;;
  72    -*)
  73        usage
  74        ;;
  75    *)
  76        break
  77        ;;
  78    esac
  79    shift
  80done
  81
  82name="$1"
  83[ "$name" ] || usage
  84prev=0000000000000000000000000000000000000000
  85if git-show-ref --verify --quiet -- "refs/tags/$name"
  86then
  87    test -n "$force" || die "tag '$name' already exists"
  88    prev=`git rev-parse "refs/tags/$name"`
  89fi
  90shift
  91git-check-ref-format "tags/$name" ||
  92        die "we do not like '$name' as a tag name."
  93
  94object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
  95type=$(git-cat-file -t $object) || exit 1
  96tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
  97: ${username:=$(expr "z$tagger" : 'z\(.*>\)')}
  98
  99trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
 100
 101if [ "$annotate" ]; then
 102    if [ -z "$message_given" ]; then
 103        ( echo "#"
 104          echo "# Write a tag message"
 105          echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
 106        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
 107    else
 108        echo "$message" >"$GIT_DIR"/TAG_EDITMSG
 109    fi
 110
 111    grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
 112    git-stripspace >"$GIT_DIR"/TAG_FINALMSG
 113
 114    [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
 115        echo >&2 "No tag message?"
 116        exit 1
 117    }
 118
 119    ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
 120        "$object" "$type" "$name" "$tagger";
 121      cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
 122    rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
 123    if [ "$signed" ]; then
 124        gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
 125        cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
 126        die "failed to sign the tag with GPG."
 127    fi
 128    object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
 129fi
 130
 131git update-ref "refs/tags/$name" "$object" "$prev"
 132