@matheusbsbs / matheus-bagliano / commits / 65d9fb487f
Fork 0

Add a script to edit/inspect notes

The script 'git notes' allows you to edit and show commit notes, by calling either git notes show <commit> or git notes edit <commit> This patch has been improved by the following contributions: - Tor Arne Vestbø: fix printing of multi-line notes - Michael J Gruber: test and handle empty notes gracefully - Thomas Rast: - only clean up message file when editing - use GIT_EDITOR and core.editor over VISUAL/EDITOR - t3301: fix confusing quoting in test for valid notes ref - t3301: use test_must_fail instead of ! - refuse to edit notes outside refs/notes/ - Junio C Hamano: tests: fix "export var=val" - Christian Couder: documentation: fix 'linkgit' macro in "git-notes.txt" - Johan Herland: minor cleanup and bugfixing in git-notes.sh (v2) Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Tor Arne Vestbø <tavestbo@trolltech.com> Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 9, 2009 at 12:21 UTC 65d9fb487f36d4a12a169dc18cbbb5225337c085
6 files changed +236
.gitignore
+1
@@ -86,6 +86,7 @@ git-mktag
86 git-mktree
87 git-name-rev
88 git-mv
89 +git-notes
90 git-pack-redundant
91 git-pack-objects
92 git-pack-refs
Documentation/git-notes.txt new
+46
@@ -0,0 +1,46 @@
1 +git-notes(1)
2 +============
3 +
4 +NAME
5 +----
6 +git-notes - Add/inspect commit notes
7 +
8 +SYNOPSIS
9 +--------
10 +[verse]
11 +'git-notes' (edit | show) [commit]
12 +
13 +DESCRIPTION
14 +-----------
15 +This command allows you to add notes to commit messages, without
16 +changing the commit. To discern these notes from the message stored
17 +in the commit object, the notes are indented like the message, after
18 +an unindented line saying "Notes:".
19 +
20 +To disable commit notes, you have to set the config variable
21 +core.notesRef to the empty string. Alternatively, you can set it
22 +to a different ref, something like "refs/notes/bugzilla". This setting
23 +can be overridden by the environment variable "GIT_NOTES_REF".
24 +
25 +
26 +SUBCOMMANDS
27 +-----------
28 +
29 +edit::
30 + Edit the notes for a given commit (defaults to HEAD).
31 +
32 +show::
33 + Show the notes for a given commit (defaults to HEAD).
34 +
35 +
36 +Author
37 +------
38 +Written by Johannes Schindelin <johannes.schindelin@gmx.de>
39 +
40 +Documentation
41 +-------------
42 +Documentation by Johannes Schindelin
43 +
44 +GIT
45 +---
46 +Part of the linkgit:git[7] suite
Makefile
+1
@@ -321,6 +321,7 @@ SCRIPT_SH += git-merge-one-file.sh
321 SCRIPT_SH += git-merge-resolve.sh
322 SCRIPT_SH += git-mergetool.sh
323 SCRIPT_SH += git-mergetool--lib.sh
324 +SCRIPT_SH += git-notes.sh
325 SCRIPT_SH += git-parse-remote.sh
326 SCRIPT_SH += git-pull.sh
327 SCRIPT_SH += git-quiltimport.sh
command-list.txt
+1
@@ -74,6 +74,7 @@ git-mktag plumbingmanipulators
74 git-mktree plumbingmanipulators
75 git-mv mainporcelain common
76 git-name-rev plumbinginterrogators
77 +git-notes mainporcelain
78 git-pack-objects plumbingmanipulators
79 git-pack-redundant plumbinginterrogators
80 git-pack-refs ancillarymanipulators
git-notes.sh new
+73
@@ -0,0 +1,73 @@
1 +#!/bin/sh
2 +
3 +USAGE="(edit | show) [commit]"
4 +. git-sh-setup
5 +
6 +test -n "$3" && usage
7 +
8 +test -z "$1" && usage
9 +ACTION="$1"; shift
10 +
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"
13 +
14 +COMMIT=$(git rev-parse --verify --default HEAD "$@") ||
15 +die "Invalid commit: $@"
16 +
17 +case "$ACTION" in
18 +edit)
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 +;;
66 +show)
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
73 +esac
t/t3301-notes.sh new
+114
@@ -0,0 +1,114 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Johannes E. Schindelin
4 +#
5 +
6 +test_description='Test commit notes'
7 +
8 +. ./test-lib.sh
9 +
10 +cat > fake_editor.sh << \EOF
11 +echo "$MSG" > "$1"
12 +echo "$MSG" >& 2
13 +EOF
14 +chmod a+x fake_editor.sh
15 +VISUAL=./fake_editor.sh
16 +export VISUAL
17 +
18 +test_expect_success 'cannot annotate non-existing HEAD' '
19 + (MSG=3 && export MSG && test_must_fail git notes edit)
20 +'
21 +
22 +test_expect_success setup '
23 + : > a1 &&
24 + git add a1 &&
25 + test_tick &&
26 + git commit -m 1st &&
27 + : > a2 &&
28 + git add a2 &&
29 + test_tick &&
30 + git commit -m 2nd
31 +'
32 +
33 +test_expect_success 'need valid notes ref' '
34 + (MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
35 + test_must_fail git notes edit) &&
36 + (MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
37 + test_must_fail git notes show)
38 +'
39 +
40 +test_expect_success 'refusing to edit in refs/heads/' '
41 + (MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
42 + export MSG GIT_NOTES_REF &&
43 + test_must_fail git notes edit)
44 +'
45 +
46 +test_expect_success 'refusing to edit in refs/remotes/' '
47 + (MSG=1 GIT_NOTES_REF=refs/remotes/bogus &&
48 + export MSG GIT_NOTES_REF &&
49 + test_must_fail git notes edit)
50 +'
51 +
52 +# 1 indicates caught gracefully by die, 128 means git-show barked
53 +test_expect_success 'handle empty notes gracefully' '
54 + git notes show ; test 1 = $?
55 +'
56 +
57 +test_expect_success 'create notes' '
58 + git config core.notesRef refs/notes/commits &&
59 + MSG=b1 git notes edit &&
60 + test ! -f .git/new-notes &&
61 + test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
62 + test b1 = $(git notes show) &&
63 + git show HEAD^ &&
64 + test_must_fail git notes show HEAD^
65 +'
66 +
67 +cat > expect << EOF
68 +commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
69 +Author: A U Thor <author@example.com>
70 +Date: Thu Apr 7 15:14:13 2005 -0700
71 +
72 + 2nd
73 +
74 +Notes:
75 + b1
76 +EOF
77 +
78 +test_expect_success 'show notes' '
79 + ! (git cat-file commit HEAD | grep b1) &&
80 + git log -1 > output &&
81 + test_cmp expect output
82 +'
83 +test_expect_success 'create multi-line notes (setup)' '
84 + : > a3 &&
85 + git add a3 &&
86 + test_tick &&
87 + git commit -m 3rd &&
88 + MSG="b3
89 +c3c3c3c3
90 +d3d3d3" git notes edit
91 +'
92 +
93 +cat > expect-multiline << EOF
94 +commit 1584215f1d29c65e99c6c6848626553fdd07fd75
95 +Author: A U Thor <author@example.com>
96 +Date: Thu Apr 7 15:15:13 2005 -0700
97 +
98 + 3rd
99 +
100 +Notes:
101 + b3
102 + c3c3c3c3
103 + d3d3d3
104 +EOF
105 +
106 +printf "\n" >> expect-multiline
107 +cat expect >> expect-multiline
108 +
109 +test_expect_success 'show multi-line notes' '
110 + git log -2 > output &&
111 + test_cmp expect-multiline output
112 +'
113 +
114 +test_done