#!/bin/sh setglobal USAGE = '"(edit [-F | -m ] | show) [commit]'" source git-sh-setup test -z $1 && usage setglobal ACTION = $1; shift test -z $GIT_NOTES_REF && setglobal GIT_NOTES_REF = $[git config core.notesref] test -z $GIT_NOTES_REF && setglobal GIT_NOTES_REF = '"refs/notes/commits'" setglobal MESSAGE = '' while test $# != 0 { match $1 { with -m test $ACTION = "edit" || usage shift if test "$Argc" = "0" { die "error: option -m needs an argument" } else { if test -z $MESSAGE { setglobal MESSAGE = $1 } else { setglobal MESSAGE = ""$MESSAGE $1"" } shift } with -F test $ACTION = "edit" || usage shift if test "$Argc" = "0" { die "error: option -F needs an argument" } else { if test -z $MESSAGE { setglobal MESSAGE = $[cat $1] } else { setglobal MESSAGE = ""$MESSAGE $[cat $1]"" } shift } with -* usage with * break } } setglobal COMMIT = $[git rev-parse --verify --default HEAD @Argv] || die "Invalid commit: $ifsjoin(Argv)" match $ACTION { with edit if test $(GIT_NOTES_REF#refs/notes/) = $GIT_NOTES_REF { die "Refusing to edit notes in $GIT_NOTES_REF (outside of refs/notes/)" } setglobal MSG_FILE = ""$GIT_DIR/new-notes-$COMMIT"" setglobal GIT_INDEX_FILE = ""$MSG_FILE.idx"" export GIT_INDEX_FILE trap ' test -f "$MSG_FILE" && rm "$MSG_FILE" test -f "$GIT_INDEX_FILE" && rm "$GIT_INDEX_FILE" ' 0 setglobal CURRENT_HEAD = $[git show-ref $GIT_NOTES_REF | cut -f 1 -d ' ] if test -z $CURRENT_HEAD { setglobal PARENT = '' } else { setglobal PARENT = ""-p $CURRENT_HEAD"" git read-tree $GIT_NOTES_REF || die "Could not read index" } if test -z $MESSAGE { env GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > $MSG_FILE if test ! -z $CURRENT_HEAD { git cat-file blob :$COMMIT >> $MSG_FILE !2 > /dev/null } setglobal core_editor = $[git config core.editor] $(GIT_EDITOR:-${core_editor:-${VISUAL:-${EDITOR:-vi}}}) $MSG_FILE } else { echo $MESSAGE > $MSG_FILE } grep -v ^# < $MSG_FILE | git stripspace > "$MSG_FILE".processed mv "$MSG_FILE".processed $MSG_FILE if test -s $MSG_FILE { setglobal BLOB = $[git hash-object -w $MSG_FILE] || die "Could not write into object database" git update-index --add --cacheinfo 0644 $BLOB $COMMIT || die "Could not write index" } else { test -z $CURRENT_HEAD && die "Will not initialise with empty tree" git update-index --force-remove $COMMIT || die "Could not update index" } setglobal TREE = $[git write-tree] || die "Could not write tree" setglobal NEW_HEAD = $[echo Annotate $COMMIT | git commit-tree $TREE $PARENT] || die "Could not annotate" git update-ref -m "Annotate $COMMIT" \ $GIT_NOTES_REF $NEW_HEAD $CURRENT_HEAD with show git rev-parse -q --verify "$GIT_NOTES_REF":$COMMIT > /dev/null || die "No note for commit $COMMIT." git show "$GIT_NOTES_REF":$COMMIT with * usage }