Skip to content

Commit d9246d4

Browse files
jherlandgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5b0c24 commit d9246d4

File tree

3 files changed

+107
-9
lines changed

3 files changed

+107
-9
lines changed

Documentation/git-notes.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-notes - Add/inspect commit notes
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git-notes' (edit | show) [commit]
11+
'git-notes' (edit [-F <file> | -m <msg>] | show) [commit]
1212

1313
DESCRIPTION
1414
-----------
@@ -33,6 +33,20 @@ show::
3333
Show the notes for a given commit (defaults to HEAD).
3434

3535

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+
3650
Author
3751
------
3852
Written by Johannes Schindelin <[email protected]>

git-notes.sh

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,59 @@
11
#!/bin/sh
22

3-
USAGE="(edit | show) [commit]"
3+
USAGE="(edit [-F <file> | -m <msg>] | show) [commit]"
44
. git-sh-setup
55

6-
test -n "$3" && usage
7-
86
test -z "$1" && usage
97
ACTION="$1"; shift
108

119
test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="$(git config core.notesref)"
1210
test -z "$GIT_NOTES_REF" && GIT_NOTES_REF="refs/notes/commits"
1311

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+
1457
COMMIT=$(git rev-parse --verify --default HEAD "$@") ||
1558
die "Invalid commit: $@"
1659

@@ -29,19 +72,24 @@ edit)
2972
test -f "$GIT_INDEX_FILE" && rm "$GIT_INDEX_FILE"
3073
' 0
3174

32-
GIT_NOTES_REF= git log -1 $COMMIT | sed "s/^/#/" > "$MSG_FILE"
33-
3475
CURRENT_HEAD=$(git show-ref "$GIT_NOTES_REF" | cut -f 1 -d ' ')
3576
if [ -z "$CURRENT_HEAD" ]; then
3677
PARENT=
3778
else
3879
PARENT="-p $CURRENT_HEAD"
3980
git read-tree "$GIT_NOTES_REF" || die "Could not read index"
40-
git cat-file blob :$COMMIT >> "$MSG_FILE" 2> /dev/null
4181
fi
4282

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
4593

4694
grep -v ^# < "$MSG_FILE" | git stripspace > "$MSG_FILE".processed
4795
mv "$MSG_FILE".processed "$MSG_FILE"

t/t3301-notes.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,41 @@ test_expect_success 'show multi-line notes' '
110110
git log -2 > output &&
111111
test_cmp expect-multiline output
112112
'
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 <[email protected]>
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+
'
113149

114150
test_done

0 commit comments

Comments
 (0)