Skip to content

Commit 8f80c61

Browse files
committed
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]>
1 parent 15030f9 commit 8f80c61

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,4 +1567,75 @@ 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+
MSG="Edited notes message" git notes add -m "Initial notes message" -e &&
1573+
echo "Edited notes message" >expect &&
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 "Note from file" >note_file &&
1580+
MSG="Edited note from file" git notes add -F note_file -e &&
1581+
echo "Edited note from file" >expect &&
1582+
git notes show >actual &&
1583+
test_cmp expect actual
1584+
'
1585+
1586+
test_expect_success 'git notes append with -m/-F invokes the editor with -e' '
1587+
test_commit 20th &&
1588+
git notes add -m "Initial note message" &&
1589+
MSG="Appended edited note message" git notes append -m "New appended note" -e &&
1590+
1591+
# Verify the note content was appended and edited
1592+
1593+
cat >expect <<-EOF &&
1594+
Initial note message
1595+
1596+
Appended edited note message
1597+
EOF
1598+
git notes show >actual &&
1599+
test_cmp expect actual &&
1600+
git notes remove HEAD &&
1601+
1602+
# Append a note using -F and edit it
1603+
echo "Note from file" >note_file &&
1604+
git notes add -m "Initial note message" &&
1605+
MSG="Appended edited note from file" git notes append -F note_file -e &&
1606+
1607+
# Verify notes from file has been edited in editor and appended
1608+
cat >expect <<-EOF &&
1609+
Initial note message
1610+
1611+
Appended edited note from file
1612+
EOF
1613+
git notes show >actual &&
1614+
test_cmp expect actual
1615+
'
1616+
1617+
test_expect_success 'git notes with a combination of -m, -F and -e invokes editor' '
1618+
test_commit 21st &&
1619+
echo "foo-file-1" >note_1 &&
1620+
echo "foo-file-2" >note_2 &&
1621+
1622+
MSG="Collapsed edited notes" git notes append -F note_1 -m "message-1" -F note_2 -e &&
1623+
1624+
# Verify that combined messages from file and -m have been edited
1625+
1626+
echo "Collapsed edited notes" >expect &&
1627+
git notes show >actual &&
1628+
test_cmp expect actual
1629+
'
1630+
test_expect_success 'git notes append aborts when editor fails with -e' '
1631+
test_commit 22nd &&
1632+
echo "foo-file-1" >note_1 &&
1633+
1634+
# Try to append a note with -F and -e, but make the editor fail
1635+
test_env GIT_EDITOR="false" test_must_fail git notes append -F note_1 -e &&
1636+
1637+
# Verify that no note was added due to editor failure
1638+
test_must_fail git notes show
1639+
'
1640+
15701641
test_done

0 commit comments

Comments
 (0)