Code

git_config(): not having a per-repo config file is not an error
[git.git] / git-notes.sh
1 #!/bin/sh
3 USAGE="(edit | show) [commit]"
4 . git-sh-setup
6 test -n "$3" && usage
8 test -z "$1" && usage
9 ACTION="$1"; shift
11 test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="$(git config core.notesref)"
12 test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="refs/notes/commits"
14 COMMIT=$(git rev-parse --verify --default HEAD "$@") ||
15 die "Invalid commit: $@"
17 MESSAGE="$GIT_DIR"/new-notes-$COMMIT
18 trap '
19         test -f "$MESSAGE" && rm "$MESSAGE"
20 ' 0
22 case "$ACTION" in
23 edit)
24         GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MESSAGE"
26         GIT_INDEX_FILE="$MESSAGE".idx
27         export GIT_INDEX_FILE
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
38         ${VISUAL:-${EDITOR:-vi}} "$MESSAGE"
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
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 ;;
60 show)
61         git show "$GIT_NOTES_REF":$COMMIT
62 ;;
63 *)
64         usage
65 esac