Skip to content

Commit d73a5b9

Browse files
jherlandgitster
authored andcommitted
builtin/notes: add --allow-empty, to allow storing empty notes
Although the "git notes" man page advertises that we support binary-safe notes addition (using the -C option), we currently do not support adding the empty note (i.e. using the empty blob to annotate an object). Instead, an empty note is always treated as an intent to remove the note altogether. Introduce the --allow-empty option to the add/append/edit subcommands, to explicitly allow an empty note to be stored into the notes tree. Also update the documentation, and add test cases for the new option. Reported-by: James H. Fisher <jhf@trifork.com> Improved-by: Kyle J. McKay <mackyle@gmail.com> Improved-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 52694cd commit d73a5b9

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

Documentation/git-notes.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git notes' [list [<object>]]
12-
'git notes' add [-f] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
12+
'git notes' add [-f] [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
1313
'git notes' copy [-f] ( --stdin | <from-object> <to-object> )
14-
'git notes' append [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
15-
'git notes' edit [<object>]
14+
'git notes' append [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
15+
'git notes' edit [--allow-empty] [<object>]
1616
'git notes' show [<object>]
1717
'git notes' merge [-v | -q] [-s <strategy> ] <notes-ref>
1818
'git notes' merge --commit [-v | -q]
@@ -155,6 +155,10 @@ OPTIONS
155155
Like '-C', but with '-c' the editor is invoked, so that
156156
the user can further edit the note message.
157157

158+
--allow-empty::
159+
Allow an empty note object to be stored. The default behavior is
160+
to automatically remove empty notes.
161+
158162
--ref <ref>::
159163
Manipulate the notes tree in <ref>. This overrides
160164
'GIT_NOTES_REF' and the "core.notesRef" configuration. The ref
@@ -287,7 +291,7 @@ arbitrary files using 'git hash-object':
287291
------------
288292
$ cc *.c
289293
$ blob=$(git hash-object -w a.out)
290-
$ git notes --ref=built add -C "$blob" HEAD
294+
$ git notes --ref=built add --allow-empty -C "$blob" HEAD
291295
------------
292296

293297
(You cannot simply use `git notes --ref=built add -F a.out HEAD`

builtin/notes.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
static const char * const git_notes_usage[] = {
2424
N_("git notes [--ref <notes_ref>] [list [<object>]]"),
25-
N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
25+
N_("git notes [--ref <notes_ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
2626
N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
27-
N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28-
N_("git notes [--ref <notes_ref>] edit [<object>]"),
27+
N_("git notes [--ref <notes_ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28+
N_("git notes [--ref <notes_ref>] edit [--allow-empty] [<object>]"),
2929
N_("git notes [--ref <notes_ref>] show [<object>]"),
3030
N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
3131
N_("git notes merge --commit [-v | -q]"),
@@ -381,7 +381,7 @@ static int append_edit(int argc, const char **argv, const char *prefix);
381381

382382
static int add(int argc, const char **argv, const char *prefix)
383383
{
384-
int force = 0;
384+
int force = 0, allow_empty = 0;
385385
const char *object_ref;
386386
struct notes_tree *t;
387387
unsigned char object[20], new_note[20];
@@ -400,6 +400,8 @@ static int add(int argc, const char **argv, const char *prefix)
400400
{ OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
401401
N_("reuse specified note object"), PARSE_OPT_NONEG,
402402
parse_reuse_arg},
403+
OPT_BOOL(0, "allow-empty", &allow_empty,
404+
N_("allow storing empty note")),
403405
OPT__FORCE(&force, N_("replace existing notes")),
404406
OPT_END()
405407
};
@@ -445,7 +447,7 @@ static int add(int argc, const char **argv, const char *prefix)
445447
}
446448

447449
prepare_note_data(object, &d, note);
448-
if (d.buf.len) {
450+
if (d.buf.len || allow_empty) {
449451
write_note_data(&d, new_note);
450452
if (add_note(t, object, new_note, combine_notes_overwrite))
451453
die("BUG: combine_notes_overwrite failed");
@@ -540,6 +542,7 @@ static int copy(int argc, const char **argv, const char *prefix)
540542

541543
static int append_edit(int argc, const char **argv, const char *prefix)
542544
{
545+
int allow_empty = 0;
543546
const char *object_ref;
544547
struct notes_tree *t;
545548
unsigned char object[20], new_note[20];
@@ -560,6 +563,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
560563
{ OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
561564
N_("reuse specified note object"), PARSE_OPT_NONEG,
562565
parse_reuse_arg},
566+
OPT_BOOL(0, "allow-empty", &allow_empty,
567+
N_("allow storing empty note")),
563568
OPT_END()
564569
};
565570
int edit = !strcmp(argv[0], "edit");
@@ -602,7 +607,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
602607
free(prev_buf);
603608
}
604609

605-
if (d.buf.len) {
610+
if (d.buf.len || allow_empty) {
606611
write_note_data(&d, new_note);
607612
if (add_note(t, object, new_note, combine_notes_overwrite))
608613
die("BUG: combine_notes_overwrite failed");

t/t3301-notes.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,8 @@ test_expect_success 'git notes get-ref (--ref)' '
12421242
test_expect_success 'setup testing of empty notes' '
12431243
test_unconfig core.notesRef &&
12441244
test_commit 16th &&
1245-
empty_blob=$(git hash-object -w /dev/null)
1245+
empty_blob=$(git hash-object -w /dev/null) &&
1246+
echo "$empty_blob" >expect_empty
12461247
'
12471248

12481249
while read cmd
@@ -1252,6 +1253,13 @@ do
12521253
MSG= git notes $cmd &&
12531254
test_must_fail git notes list HEAD
12541255
"
1256+
1257+
test_expect_success "'git notes $cmd --allow-empty' stores empty note" "
1258+
test_might_fail git notes remove HEAD &&
1259+
MSG= git notes $cmd --allow-empty &&
1260+
git notes list HEAD >actual &&
1261+
test_cmp expect_empty actual
1262+
"
12551263
done <<\EOF
12561264
add
12571265
add -F /dev/null

0 commit comments

Comments
 (0)