@matheusbsbs / matheus-bagliano / commits / d9246d4303
Fork 0

Teach "-m <msg>" and "-F <file>" to "git notes edit"

The "-m" and "-F" options are already the established method (in both git-commit and git-tag) to specify a commit/tag message without invoking the editor. This patch teaches "git notes edit" to respect the same options for specifying a notes message without invoking the editor. Multiple "-m" and/or "-F" options are concatenated as separate paragraphs. The patch also updates the "git notes" documentation and adds selftests for the new functionality. Unfortunately, the added selftests include a couple of lines with trailing whitespace (without these the test will fail). This may cause git to warn about "whitespace errors". This patch has been improved by the following contributions: - Thomas Rast: fix trailing whitespace in t3301 Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Oct 9, 2009 at 12:22 UTC d9246d4303f441c0e30614cd3a97a80fbe9354b6
3 files changed +107 -9
Documentation/git-notes.txt
+15 -1
@@ -8,7 +8,7 @@ git-notes - Add/inspect commit notes
8 SYNOPSIS
9 --------
10 [verse]
11 -'git-notes' (edit | show) [commit]
11 +'git-notes' (edit [-F <file> | -m <msg>] | show) [commit]
12
13 DESCRIPTION
14 -----------
@@ -33,6 +33,20 @@ show::
33 Show the notes for a given commit (defaults to HEAD).
34
35
36 +OPTIONS
37 +-------
38 +-m <msg>::
39 + Use the given note message (instead of prompting).
40 + If multiple `-m` (or `-F`) options are given, their
41 + values are concatenated as separate paragraphs.
42 +
43 +-F <file>::
44 + Take the note message from the given file. Use '-' to
45 + read the note message from the standard input.
46 + If multiple `-F` (or `-m`) options are given, their
47 + values are concatenated as separate paragraphs.
48 +
49 +
50 Author
51 ------
52 Written by Johannes Schindelin <johannes.schindelin@gmx.de>
git-notes.sh
+56 -8
@@ -1,16 +1,59 @@
1 #!/bin/sh
2
3 -USAGE="(edit | show) [commit]"
3 +USAGE="(edit [-F <file> | -m <msg>] | show) [commit]"
4 . git-sh-setup
5
6 -test -n "$3" && usage
7 -
6 test -z "$1" && usage
7 ACTION="$1"; shift
8
9 test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="$(git config core.notesref)"
10 test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="refs/notes/commits"
11
12 +MESSAGE=
13 +while test $# != 0
14 +do
15 + case "$1" in
16 + -m)
17 + test "$ACTION" = "edit" || usage
18 + shift
19 + if test "$#" = "0"; then
20 + die "error: option -m needs an argument"
21 + else
22 + if [ -z "$MESSAGE" ]; then
23 + MESSAGE="$1"
24 + else
25 + MESSAGE="$MESSAGE
26 +
27 +$1"
28 + fi
29 + shift
30 + fi
31 + ;;
32 + -F)
33 + test "$ACTION" = "edit" || usage
34 + shift
35 + if test "$#" = "0"; then
36 + die "error: option -F needs an argument"
37 + else
38 + if [ -z "$MESSAGE" ]; then
39 + MESSAGE="$(cat "$1")"
40 + else
41 + MESSAGE="$MESSAGE
42 +
43 +$(cat "$1")"
44 + fi
45 + shift
46 + fi
47 + ;;
48 + -*)
49 + usage
50 + ;;
51 + *)
52 + break
53 + ;;
54 + esac
55 +done
56 +
57 COMMIT=$(git rev-parse --verify --default HEAD "$@") ||
58 die "Invalid commit: $@"
59
@@ -29,19 +72,24 @@ edit)
72 test -f "$GIT_INDEX_FILE" && rm "$GIT_INDEX_FILE"
73 ' 0
74
32 - GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MSG_FILE"
33 -
75 CURRENT_HEAD=$(git show-ref "$GIT_NOTES_REF" | cut -f 1 -d ' ')
76 if [ -z "$CURRENT_HEAD" ]; then
77 PARENT=
78 else
79 PARENT="-p $CURRENT_HEAD"
80 git read-tree "$GIT_NOTES_REF" || die "Could not read index"
40 - git cat-file blob :$COMMIT >> "$MSG_FILE" 2> /dev/null
81 fi
82
43 - core_editor="$(git config core.editor)"
44 - ${GIT_EDITOR:-${core_editor:-${VISUAL:-${EDITOR:-vi}}}} "$MSG_FILE"
83 + if [ -z "$MESSAGE" ]; then
84 + GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MSG_FILE"
85 + if [ ! -z "$CURRENT_HEAD" ]; then
86 + git cat-file blob :$COMMIT >> "$MSG_FILE" 2> /dev/null
87 + fi
88 + core_editor="$(git config core.editor)"
89 + ${GIT_EDITOR:-${core_editor:-${VISUAL:-${EDITOR:-vi}}}} "$MSG_FILE"
90 + else
91 + echo "$MESSAGE" > "$MSG_FILE"
92 + fi
93
94 grep -v ^# < "$MSG_FILE" | git stripspace > "$MSG_FILE".processed
95 mv "$MSG_FILE".processed "$MSG_FILE"
t/t3301-notes.sh
+36
@@ -110,5 +110,41 @@ test_expect_success 'show multi-line notes' '
110 git log -2 > output &&
111 test_cmp expect-multiline output
112 '
113 +test_expect_success 'create -m and -F notes (setup)' '
114 + : > a4 &&
115 + git add a4 &&
116 + test_tick &&
117 + git commit -m 4th &&
118 + echo "xyzzy" > note5 &&
119 + git notes edit -m spam -F note5 -m "foo
120 +bar
121 +baz"
122 +'
123 +
124 +whitespace=" "
125 +cat > expect-m-and-F << EOF
126 +commit 15023535574ded8b1a89052b32673f84cf9582b8
127 +Author: A U Thor <author@example.com>
128 +Date: Thu Apr 7 15:16:13 2005 -0700
129 +
130 + 4th
131 +
132 +Notes:
133 + spam
134 +$whitespace
135 + xyzzy
136 +$whitespace
137 + foo
138 + bar
139 + baz
140 +EOF
141 +
142 +printf "\n" >> expect-m-and-F
143 +cat expect-multiline >> expect-m-and-F
144 +
145 +test_expect_success 'show -m and -F notes' '
146 + git log -3 > output &&
147 + test_cmp expect-m-and-F output
148 +'
149
150 test_done