Skip to content

Commit 8b426c8

Browse files
ddissgitster
authored andcommitted
notes: do not trigger editor when adding an empty note
With "git notes add -C $blob", the given blob contents are to be made into a note without involving an editor. But when "--allow-empty" is given, the editor is invoked, which can cause problems for non-interactive callers[1]. This behaviour started with 90bc19b (notes.c: introduce '--separator=<paragraph-break>' option, 2023-05-27), which changed editor invocation logic to check for a zero length note_data buffer. Restore the original behaviour of "git note" that takes the contents given via the "-m", "-C", "-F" options without invoking an editor, by checking for any prior parameter callbacks, indicated by a non-zero note_data.msg_nr. Remove the now-unneeded note_data.given flag. Add a test for this regression by checking whether GIT_EDITOR is invoked alongside "git notes add -C $empty_blob --allow-empty" [1] ddiss/icyci#12 Signed-off-by: David Disseldorp <[email protected]> [jc: enhanced the test with -m/-F options] Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39bf06a commit 8b426c8

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

builtin/notes.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ struct note_msg {
114114
};
115115

116116
struct note_data {
117-
int given;
118117
int use_editor;
119118
int stripspace;
120119
char *edit_path;
@@ -193,15 +192,15 @@ static void write_commented_object(int fd, const struct object_id *object)
193192
static void prepare_note_data(const struct object_id *object, struct note_data *d,
194193
const struct object_id *old_note)
195194
{
196-
if (d->use_editor || !d->given) {
195+
if (d->use_editor || !d->msg_nr) {
197196
int fd;
198197
struct strbuf buf = STRBUF_INIT;
199198

200199
/* write the template message before editing: */
201200
d->edit_path = git_pathdup("NOTES_EDITMSG");
202201
fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
203202

204-
if (d->given)
203+
if (d->msg_nr)
205204
write_or_die(fd, d->buf.buf, d->buf.len);
206205
else if (old_note)
207206
copy_obj_to_fd(fd, old_note);
@@ -515,7 +514,6 @@ static int add(int argc, const char **argv, const char *prefix)
515514

516515
if (d.msg_nr)
517516
concat_messages(&d);
518-
d.given = !!d.buf.len;
519517

520518
object_ref = argc > 1 ? argv[1] : "HEAD";
521519

@@ -528,7 +526,7 @@ static int add(int argc, const char **argv, const char *prefix)
528526
if (note) {
529527
if (!force) {
530528
free_notes(t);
531-
if (d.given) {
529+
if (d.msg_nr) {
532530
free_note_data(&d);
533531
return error(_("Cannot add notes. "
534532
"Found existing notes for object %s. "
@@ -690,14 +688,14 @@ static int append_edit(int argc, const char **argv, const char *prefix)
690688
usage_with_options(usage, options);
691689
}
692690

693-
if (d.msg_nr)
691+
if (d.msg_nr) {
694692
concat_messages(&d);
695-
d.given = !!d.buf.len;
696-
697-
if (d.given && edit)
698-
fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
699-
"for the 'edit' subcommand.\n"
700-
"Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
693+
if (edit)
694+
fprintf(stderr, _("The -m/-F/-c/-C options have been "
695+
"deprecated for the 'edit' subcommand.\n"
696+
"Please use 'git notes add -f -m/-F/-c/-C' "
697+
"instead.\n"));
698+
}
701699

702700
object_ref = 1 < argc ? argv[1] : "HEAD";
703701

t/t3301-notes.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,4 +1557,14 @@ test_expect_success 'empty notes are displayed by git log' '
15571557
test_cmp expect actual
15581558
'
15591559

1560+
test_expect_success 'empty notes do not invoke the editor' '
1561+
test_commit 18th &&
1562+
GIT_EDITOR="false" git notes add -C "$empty_blob" --allow-empty &&
1563+
git notes remove HEAD &&
1564+
GIT_EDITOR="false" git notes add -m "" --allow-empty &&
1565+
git notes remove HEAD &&
1566+
GIT_EDITOR="false" git notes add -F /dev/null --allow-empty &&
1567+
git notes remove HEAD
1568+
'
1569+
15601570
test_done

0 commit comments

Comments
 (0)