Skip to content

Commit dab0b9e

Browse files
devdekunlettaylorr
authored andcommitted
notes: teach the -e option to edit messages in editor
Notes can be added to a commit using: - "-m" to provide a message on the command line. - -C to copy a note from a blob object. - -F to read the note from a file. When these options are used, Git does not open an editor, it simply takes the content provided via these options and attaches it to the commit as a note. Improve flexibility to fine-tune the note before finalizing it by allowing the messages to be prefilled in the editor and edited after the messages have been provided through -[mF]. Signed-off-by: Abraham Samuel Adekunle <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 34b6ce9 commit dab0b9e

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

Documentation/git-notes.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git notes' [list [<object>]]
12-
'git notes' add [-f] [--allow-empty] [--[no-]separator | --separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
12+
'git notes' add [-f] [--allow-empty] [--[no-]separator | --separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [-e] [<object>]
1313
'git notes' copy [-f] ( --stdin | <from-object> [<to-object>] )
14-
'git notes' append [--allow-empty] [--[no-]separator | --separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
14+
'git notes' append [--allow-empty] [--[no-]separator | --separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [-e] [<object>]
1515
'git notes' edit [--allow-empty] [<object>] [--[no-]stripspace]
1616
'git notes' show [<object>]
1717
'git notes' merge [-v | -q] [-s <strategy> ] <notes-ref>
@@ -67,7 +67,9 @@ add::
6767
the existing notes will be opened in the editor (like the `edit`
6868
subcommand). If you specify multiple `-m` and `-F`, a blank
6969
line will be inserted between the messages. Use the `--separator`
70-
option to insert other delimiters.
70+
option to insert other delimiters. You can use `-e` to edit and
71+
fine-tune the message(s) supplied from `-m` and `-F` options
72+
interactively (using an editor) before adding the note.
7173

7274
copy::
7375
Copy the notes for the first object onto the second object (defaults to
@@ -93,6 +95,8 @@ append::
9395
an existing note, a blank line is added before each new
9496
message as an inter-paragraph separator. The separator can
9597
be customized with the `--separator` option.
98+
Edit the notes to be appended given by `-m` and `-F` options with
99+
`-e` interactively (using an editor) before appending the note.
96100

97101
edit::
98102
Edit the notes for a given object (defaults to HEAD).

builtin/notes.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
static const char *separator = "\n";
3333
static const char * const git_notes_usage[] = {
3434
N_("git notes [--ref <notes-ref>] [list [<object>]]"),
35-
N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
35+
N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>] [-e]"),
3636
N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
37-
N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
37+
N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>] [-e]"),
3838
N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
3939
N_("git notes [--ref <notes-ref>] show [<object>]"),
4040
N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
@@ -489,6 +489,8 @@ static int add(int argc, const char **argv, const char *prefix)
489489
OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
490490
N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
491491
parse_reedit_arg),
492+
OPT_BOOL('e', "edit", &d.use_editor,
493+
N_("edit note message in editor")),
492494
OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
493495
N_("reuse specified note object"), PARSE_OPT_NONEG,
494496
parse_reuse_arg),
@@ -667,6 +669,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
667669
OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
668670
N_("reuse specified note object"), PARSE_OPT_NONEG,
669671
parse_reuse_arg),
672+
OPT_BOOL('e', "edit", &d.use_editor,
673+
N_("edit note message in editor")),
670674
OPT_BOOL(0, "allow-empty", &allow_empty,
671675
N_("allow storing empty note")),
672676
OPT_CALLBACK_F(0, "separator", &separator,

t/t3301-notes.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,4 +1567,67 @@ test_expect_success 'empty notes do not invoke the editor' '
15671567
git notes remove HEAD
15681568
'
15691569

1570+
test_expect_success 'git notes add with -m/-F invokes editor with -e' '
1571+
test_commit 19th &&
1572+
echo "edited" >expect &&
1573+
MSG="$(cat expect)" git notes add -m "initial" -e &&
1574+
git notes show >actual &&
1575+
test_cmp expect actual &&
1576+
git notes remove HEAD &&
1577+
1578+
# Add a note using -F and edit it
1579+
echo "initial" >note_file &&
1580+
MSG="$(cat expect)" git notes add -F note_file -e &&
1581+
git notes show >actual &&
1582+
test_cmp expect actual
1583+
'
1584+
1585+
test_expect_success 'git notes append with -m/-F invokes the editor with -e' '
1586+
test_commit 20th &&
1587+
cat >expect <<-EOF &&
1588+
initial
1589+
1590+
edited
1591+
EOF
1592+
git notes add -m "initial" &&
1593+
MSG="edited" git notes append -m "appended" -e &&
1594+
1595+
# Verify the note content was appended and edited
1596+
git notes show >actual &&
1597+
test_cmp expect actual &&
1598+
git notes remove HEAD &&
1599+
1600+
# Append a note using -F and edit it
1601+
echo "note from file" >note_file &&
1602+
git notes add -m "initial" &&
1603+
MSG="edited" git notes append -F note_file -e &&
1604+
1605+
# Verify notes from file has been edited in editor and appended
1606+
git notes show >actual &&
1607+
test_cmp expect actual
1608+
'
1609+
1610+
test_expect_success 'git notes with a combination of -m, -F and -e invokes editor' '
1611+
test_commit 21st &&
1612+
echo "foo-file-1" >note_1 &&
1613+
echo "foo-file-2" >note_2 &&
1614+
echo "edited" >expect &&
1615+
1616+
MSG=$(cat expect) git notes append -F note_1 -m "message-1" -F note_2 -e &&
1617+
1618+
# Verify that combined messages from file and -m have been edited
1619+
git notes show >actual &&
1620+
test_cmp expect actual
1621+
'
1622+
test_expect_success 'git notes append aborts when editor fails with -e' '
1623+
test_commit 22nd &&
1624+
echo "foo-file-1" >note_1 &&
1625+
1626+
# Try to append a note with -F and -e, but make the editor fail
1627+
test_env GIT_EDITOR="false" test_must_fail git notes append -F note_1 -e &&
1628+
1629+
# Verify that no note was added due to editor failure
1630+
test_must_fail git notes show
1631+
'
1632+
15701633
test_done

0 commit comments

Comments
 (0)