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 90bc19b3ae (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] https://github.com/ddiss/icyci/issues/12

Signed-off-by: David Disseldorp <ddiss@suse.de>
[jc: enhanced the test with -m/-F options]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
David Disseldorp 2024-07-29 17:14:00 +02:00 committed by Junio C Hamano
parent 39bf06adf9
commit 8b426c84f3
2 changed files with 20 additions and 12 deletions

View File

@ -114,7 +114,6 @@ struct note_msg {
}; };


struct note_data { struct note_data {
int given;
int use_editor; int use_editor;
int stripspace; int stripspace;
char *edit_path; char *edit_path;
@ -193,7 +192,7 @@ static void write_commented_object(int fd, const struct object_id *object)
static void prepare_note_data(const struct object_id *object, struct note_data *d, static void prepare_note_data(const struct object_id *object, struct note_data *d,
const struct object_id *old_note) const struct object_id *old_note)
{ {
if (d->use_editor || !d->given) { if (d->use_editor || !d->msg_nr) {
int fd; int fd;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;


@ -201,7 +200,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
d->edit_path = git_pathdup("NOTES_EDITMSG"); d->edit_path = git_pathdup("NOTES_EDITMSG");
fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600); fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);


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


if (d.msg_nr) if (d.msg_nr)
concat_messages(&d); concat_messages(&d);
d.given = !!d.buf.len;


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


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


if (d.msg_nr) if (d.msg_nr) {
concat_messages(&d); concat_messages(&d);
d.given = !!d.buf.len; if (edit)

fprintf(stderr, _("The -m/-F/-c/-C options have been "
if (d.given && edit) "deprecated for the 'edit' subcommand.\n"
fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " "Please use 'git notes add -f -m/-F/-c/-C' "
"for the 'edit' subcommand.\n" "instead.\n"));
"Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); }


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



View File

@ -1557,4 +1557,14 @@ test_expect_success 'empty notes are displayed by git log' '
test_cmp expect actual test_cmp expect actual
' '


test_expect_success 'empty notes do not invoke the editor' '
test_commit 18th &&
GIT_EDITOR="false" git notes add -C "$empty_blob" --allow-empty &&
git notes remove HEAD &&
GIT_EDITOR="false" git notes add -m "" --allow-empty &&
git notes remove HEAD &&
GIT_EDITOR="false" git notes add -F /dev/null --allow-empty &&
git notes remove HEAD
'

test_done test_done