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 <abrahamadekunle50@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Abraham Samuel Adekunle committed Oct 21, 2024 at 18:12 UTC dab0b9e176d64f7687029b176d96eddfbf74db64
3 files changed +76 -5
Documentation/git-notes.txt
+7 -3
@@ -9,9 +9,9 @@ SYNOPSIS
9 --------
10 [verse]
11 '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>]
13 '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>]
15 'git notes' edit [--allow-empty] [<object>] [--[no-]stripspace]
16 'git notes' show [<object>]
17 'git notes' merge [-v | -q] [-s <strategy> ] <notes-ref>
@@ -67,7 +67,9 @@ add::
67 the existing notes will be opened in the editor (like the `edit`
68 subcommand). If you specify multiple `-m` and `-F`, a blank
69 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.
73
74 copy::
75 Copy the notes for the first object onto the second object (defaults to
@@ -93,6 +95,8 @@ append::
95 an existing note, a blank line is added before each new
96 message as an inter-paragraph separator. The separator can
97 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.
100
101 edit::
102 Edit the notes for a given object (defaults to HEAD).
builtin/notes.c
+6 -2
@@ -32,9 +32,9 @@
32 static const char *separator = "\n";
33 static const char * const git_notes_usage[] = {
34 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]"),
36 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]"),
38 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
39 N_("git notes [--ref <notes-ref>] show [<object>]"),
40 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)
489 OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
490 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
491 parse_reedit_arg),
492 + OPT_BOOL('e', "edit", &d.use_editor,
493 + N_("edit note message in editor")),
494 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
495 N_("reuse specified note object"), PARSE_OPT_NONEG,
496 parse_reuse_arg),
@@ -667,6 +669,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
669 OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
670 N_("reuse specified note object"), PARSE_OPT_NONEG,
671 parse_reuse_arg),
672 + OPT_BOOL('e', "edit", &d.use_editor,
673 + N_("edit note message in editor")),
674 OPT_BOOL(0, "allow-empty", &allow_empty,
675 N_("allow storing empty note")),
676 OPT_CALLBACK_F(0, "separator", &separator,
t/t3301-notes.sh
+63
@@ -1567,4 +1567,67 @@ test_expect_success 'empty notes do not invoke the editor' '
1567 git notes remove HEAD
1568 '
1569
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 +
1633 test_done