git-tag.shon commit pack-objects: hash basename and direname a bit differently. (eeef713)
   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    ( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
  89        "$object" "$type" "$name" "$tagger";
  90      cat "$GIT_DIR"/TAG_FINALMSG ) >"$GIT_DIR"/TAG_TMP
  91    rm -f "$GIT_DIR"/TAG_TMP.asc "$GIT_DIR"/TAG_FINALMSG
  92    if [ "$signed" ]; then
  93        gpg -bsa -u "$username" "$GIT_DIR"/TAG_TMP &&
  94        cat "$GIT_DIR"/TAG_TMP.asc >>"$GIT_DIR"/TAG_TMP ||
  95        die "failed to sign the tag with GPG."
  96    fi
  97    object=$(git-mktag < "$GIT_DIR"/TAG_TMP)
  98fi
  99
 100leading=`expr "refs/tags/$name" : '\(.*\)/'` &&
 101mkdir -p "$GIT_DIR/$leading" &&
 102echo $object > "$GIT_DIR/refs/tags/$name"