git-notes.shon commit Speed up git notes lookup (fd53c9e)
   1#!/bin/sh
   2
   3USAGE="(edit | show) [commit]"
   4. git-sh-setup
   5
   6test -n "$3" && usage
   7
   8test -z "$1" && usage
   9ACTION="$1"; shift
  10
  11test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="$(git config core.notesref)"
  12test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="refs/notes/commits"
  13
  14COMMIT=$(git rev-parse --verify --default HEAD "$@") ||
  15die "Invalid commit: $@"
  16
  17case "$ACTION" in
  18edit)
  19        if [ "${GIT_NOTES_REF#refs/notes/}" = "$GIT_NOTES_REF" ]; then
  20                die "Refusing to edit notes in $GIT_NOTES_REF (outside of refs/notes/)"
  21        fi
  22
  23        MSG_FILE="$GIT_DIR/new-notes-$COMMIT"
  24        GIT_INDEX_FILE="$MSG_FILE.idx"
  25        export GIT_INDEX_FILE
  26
  27        trap '
  28                test -f "$MSG_FILE" && rm "$MSG_FILE"
  29                test -f "$GIT_INDEX_FILE" && rm "$GIT_INDEX_FILE"
  30        ' 0
  31
  32        GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MSG_FILE"
  33
  34        CURRENT_HEAD=$(git show-ref "$GIT_NOTES_REF" | cut -f 1 -d ' ')
  35        if [ -z "$CURRENT_HEAD" ]; then
  36                PARENT=
  37        else
  38                PARENT="-p $CURRENT_HEAD"
  39                git read-tree "$GIT_NOTES_REF" || die "Could not read index"
  40                git cat-file blob :$COMMIT >> "$MSG_FILE" 2> /dev/null
  41        fi
  42
  43        core_editor="$(git config core.editor)"
  44        ${GIT_EDITOR:-${core_editor:-${VISUAL:-${EDITOR:-vi}}}} "$MSG_FILE"
  45
  46        grep -v ^# < "$MSG_FILE" | git stripspace > "$MSG_FILE".processed
  47        mv "$MSG_FILE".processed "$MSG_FILE"
  48        if [ -s "$MSG_FILE" ]; then
  49                BLOB=$(git hash-object -w "$MSG_FILE") ||
  50                        die "Could not write into object database"
  51                git update-index --add --cacheinfo 0644 $BLOB $COMMIT ||
  52                        die "Could not write index"
  53        else
  54                test -z "$CURRENT_HEAD" &&
  55                        die "Will not initialise with empty tree"
  56                git update-index --force-remove $COMMIT ||
  57                        die "Could not update index"
  58        fi
  59
  60        TREE=$(git write-tree) || die "Could not write tree"
  61        NEW_HEAD=$(echo Annotate $COMMIT | git commit-tree $TREE $PARENT) ||
  62                die "Could not annotate"
  63        git update-ref -m "Annotate $COMMIT" \
  64                "$GIT_NOTES_REF" $NEW_HEAD $CURRENT_HEAD
  65;;
  66show)
  67        git rev-parse -q --verify "$GIT_NOTES_REF":$COMMIT > /dev/null ||
  68                die "No note for commit $COMMIT."
  69        git show "$GIT_NOTES_REF":$COMMIT
  70;;
  71*)
  72        usage
  73esac