contrib / examples / git-notes.shon commit Merge branch 'rj/xdiff-prepare-plug-leak-on-error-codepath' (aa3a2c2)
   1#!/bin/sh
   2
   3USAGE="(edit [-F <file> | -m <msg>] | show) [commit]"
   4. git-sh-setup
   5
   6test -z "$1" && usage
   7ACTION="$1"; shift
   8
   9test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="$(git config core.notesref)"
  10test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="refs/notes/commits"
  11
  12MESSAGE=
  13while test $# != 0
  14do
  15        case "$1" in
  16        -m)
  17                test "$ACTION" = "edit" || usage
  18                shift
  19                if test "$#" = "0"; then
  20                        die "error: option -m needs an argument"
  21                else
  22                        if [ -z "$MESSAGE" ]; then
  23                                MESSAGE="$1"
  24                        else
  25                                MESSAGE="$MESSAGE
  26
  27$1"
  28                        fi
  29                        shift
  30                fi
  31                ;;
  32        -F)
  33                test "$ACTION" = "edit" || usage
  34                shift
  35                if test "$#" = "0"; then
  36                        die "error: option -F needs an argument"
  37                else
  38                        if [ -z "$MESSAGE" ]; then
  39                                MESSAGE="$(cat "$1")"
  40                        else
  41                                MESSAGE="$MESSAGE
  42
  43$(cat "$1")"
  44                        fi
  45                        shift
  46                fi
  47                ;;
  48        -*)
  49                usage
  50                ;;
  51        *)
  52                break
  53                ;;
  54        esac
  55done
  56
  57COMMIT=$(git rev-parse --verify --default HEAD "$@") ||
  58die "Invalid commit: $@"
  59
  60case "$ACTION" in
  61edit)
  62        if [ "${GIT_NOTES_REF#refs/notes/}" = "$GIT_NOTES_REF" ]; then
  63                die "Refusing to edit notes in $GIT_NOTES_REF (outside of refs/notes/)"
  64        fi
  65
  66        MSG_FILE="$GIT_DIR/new-notes-$COMMIT"
  67        GIT_INDEX_FILE="$MSG_FILE.idx"
  68        export GIT_INDEX_FILE
  69
  70        trap '
  71                test -f "$MSG_FILE" && rm "$MSG_FILE"
  72                test -f "$GIT_INDEX_FILE" && rm "$GIT_INDEX_FILE"
  73        ' 0
  74
  75        CURRENT_HEAD=$(git show-ref "$GIT_NOTES_REF" | cut -f 1 -d ' ')
  76        if [ -z "$CURRENT_HEAD" ]; then
  77                PARENT=
  78        else
  79                PARENT="-p $CURRENT_HEAD"
  80                git read-tree "$GIT_NOTES_REF" || die "Could not read index"
  81        fi
  82
  83        if [ -z "$MESSAGE" ]; then
  84                GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MSG_FILE"
  85                if [ ! -z "$CURRENT_HEAD" ]; then
  86                        git cat-file blob :$COMMIT >> "$MSG_FILE" 2> /dev/null
  87                fi
  88                core_editor="$(git config core.editor)"
  89                ${GIT_EDITOR:-${core_editor:-${VISUAL:-${EDITOR:-vi}}}} "$MSG_FILE"
  90        else
  91                echo "$MESSAGE" > "$MSG_FILE"
  92        fi
  93
  94        grep -v ^# < "$MSG_FILE" | git stripspace > "$MSG_FILE".processed
  95        mv "$MSG_FILE".processed "$MSG_FILE"
  96        if [ -s "$MSG_FILE" ]; then
  97                BLOB=$(git hash-object -w "$MSG_FILE") ||
  98                        die "Could not write into object database"
  99                git update-index --add --cacheinfo 0644 $BLOB $COMMIT ||
 100                        die "Could not write index"
 101        else
 102                test -z "$CURRENT_HEAD" &&
 103                        die "Will not initialise with empty tree"
 104                git update-index --force-remove $COMMIT ||
 105                        die "Could not update index"
 106        fi
 107
 108        TREE=$(git write-tree) || die "Could not write tree"
 109        NEW_HEAD=$(echo Annotate $COMMIT | git commit-tree $TREE $PARENT) ||
 110                die "Could not annotate"
 111        git update-ref -m "Annotate $COMMIT" \
 112                "$GIT_NOTES_REF" $NEW_HEAD $CURRENT_HEAD
 113;;
 114show)
 115        git rev-parse -q --verify "$GIT_NOTES_REF":$COMMIT > /dev/null ||
 116                die "No note for commit $COMMIT."
 117        git show "$GIT_NOTES_REF":$COMMIT
 118;;
 119*)
 120        usage
 121esac