git-tag.shon commit git-svn: rename 'commit' command to 'set-tree' (3289e86)
   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            exit 2
  44        else
  45            message_given=1
  46        fi
  47        ;;
  48    -u)
  49        annotate=1
  50        signed=1
  51        shift
  52        username="$1"
  53        ;;
  54    -d)
  55        shift
  56        tag_name="$1"
  57        tag=$(git-show-ref --verify --hash -- "refs/tags/$tag_name") ||
  58                die "Seriously, what tag are you talking about?"
  59        git-update-ref -m 'tag: delete' -d "refs/tags/$tag_name" "$tag" &&
  60                echo "Deleted tag $tag_name."
  61        exit $?
  62        ;;
  63    -*)
  64        usage
  65        ;;
  66    *)
  67        break
  68        ;;
  69    esac
  70    shift
  71done
  72
  73name="$1"
  74[ "$name" ] || usage
  75prev=0000000000000000000000000000000000000000
  76if git-show-ref --verify --quiet -- "refs/tags/$name"
  77then
  78    test -n "$force" || die "tag '$name' already exists"
  79    prev=`git rev-parse "refs/tags/$name"`
  80fi
  81shift
  82git-check-ref-format "tags/$name" ||
  83        die "we do not like '$name' as a tag name."
  84
  85object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
  86type=$(git-cat-file -t $object) || exit 1
  87tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
  88: ${username:=$(expr "z$tagger" : 'z\(.*>\)')}
  89
  90trap 'rm -f "$GIT_DIR"/TAG_TMP* "$GIT_DIR"/TAG_FINALMSG "$GIT_DIR"/TAG_EDITMSG' 0
  91
  92if [ "$annotate" ]; then
  93    if [ -z "$message_given" ]; then
  94        ( echo "#"
  95          echo "# Write a tag message"
  96          echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
  97        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
  98    else
  99        echo "$message" >"$GIT_DIR"/TAG_EDITMSG
 100    fi
 101
 102    grep -v '^#' <"$GIT_DIR"/TAG_EDITMSG |
 103    git-stripspace >"$GIT_DIR"/TAG_FINALMSG
 104
 105    [ -s "$GIT_DIR"/TAG_FINALMSG -o -n "$message_given" ] || {
 106        echo >&2 "No tag message?"
 107        exit 1
 108    }
 109
 110    ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
 111        "$object" "$type" "$name" "$tagger";
 112      cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
 113    rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
 114    if [ "$signed" ]; then
 115        gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
 116        cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
 117        die "failed to sign the tag with GPG."
 118    fi
 119    object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
 120fi
 121
 122git update-ref "refs/tags/$name" "$object" "$prev"
 123