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
17MESSAGE="$GIT_DIR"/new-notes-$COMMIT
18trap '
19 test -f "$MESSAGE" && rm "$MESSAGE"
20' 0
21
22case "$ACTION" in
23edit)
24 GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MESSAGE"
25
26 GIT_INDEX_FILE="$MESSAGE".idx
27 export GIT_INDEX_FILE
28
29 CURRENT_HEAD=$(git show-ref "$GIT_NOTES_REF" | cut -f 1 -d ' ')
30 if [ -z "$CURRENT_HEAD" ]; then
31 PARENT=
32 else
33 PARENT="-p $CURRENT_HEAD"
34 git read-tree "$GIT_NOTES_REF" || die "Could not read index"
35 git cat-file blob :$COMMIT >> "$MESSAGE" 2> /dev/null
36 fi
37
38 ${VISUAL:-${EDITOR:-vi}} "$MESSAGE"
39
40 grep -v ^# < "$MESSAGE" | git stripspace > "$MESSAGE".processed
41 mv "$MESSAGE".processed "$MESSAGE"
42 if [ -s "$MESSAGE" ]; then
43 BLOB=$(git hash-object -w "$MESSAGE") ||
44 die "Could not write into object database"
45 git update-index --add --cacheinfo 0644 $BLOB $COMMIT ||
46 die "Could not write index"
47 else
48 test -z "$CURRENT_HEAD" &&
49 die "Will not initialise with empty tree"
50 git update-index --force-remove $COMMIT ||
51 die "Could not update index"
52 fi
53
54 TREE=$(git write-tree) || die "Could not write tree"
55 NEW_HEAD=$(echo Annotate $COMMIT | git commit-tree $TREE $PARENT) ||
56 die "Could not annotate"
57 git update-ref -m "Annotate $COMMIT" \
58 "$GIT_NOTES_REF" $NEW_HEAD $CURRENT_HEAD
59;;
60show)
61 git show "$GIT_NOTES_REF":$COMMIT
62;;
63*)
64 usage
65esac