Skip to content

Commit 3d6a316

Browse files
dyronegitster
authored andcommitted
notes: introduce "--no-separator" option
Sometimes, the user may want to add or append multiple notes without any separator to be added between them. Disscussion: https://public-inbox.org/git/[email protected]/ Signed-off-by: Teng Long <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c4e2aa7 commit 3d6a316

File tree

3 files changed

+79
-18
lines changed

3 files changed

+79
-18
lines changed

Documentation/git-notes.txt

Lines changed: 6 additions & 5 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] [--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>] [<object>]
1313
'git notes' copy [-f] ( --stdin | <from-object> [<to-object>] )
14-
'git notes' append [--allow-empty] [--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>] [<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>
@@ -171,10 +171,11 @@ OPTIONS
171171
Allow an empty note object to be stored. The default behavior is
172172
to automatically remove empty notes.
173173

174-
--separator <paragraph-break>::
174+
--[no-]separator, --separator=<paragraph-break>::
175175
Specify a string used as a custom inter-paragraph separator
176-
(a newline is added at the end as needed). Defaults to a
177-
blank line.
176+
(a newline is added at the end as needed). If `--no-separator`, no
177+
separators will be added between paragraphs. Defaults to a blank
178+
line.
178179

179180
--[no-]stripspace::
180181
Strip leading and trailing whitespace from the note message.

builtin/notes.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
#include "worktree.h"
2929
#include "write-or-die.h"
3030

31-
static char *separator = "\n";
31+
static const char *separator = "\n";
3232
static const char * const git_notes_usage[] = {
3333
N_("git notes [--ref <notes-ref>] [list [<object>]]"),
34-
N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--separator=<paragraph-break>] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
34+
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>]"),
3535
N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
36-
N_("git notes [--ref <notes-ref>] append [--allow-empty] [--separator=<paragraph-break>] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
36+
N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
3737
N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
3838
N_("git notes [--ref <notes-ref>] show [<object>]"),
3939
N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
@@ -239,8 +239,11 @@ static void write_note_data(struct note_data *d, struct object_id *oid)
239239

240240
static void append_separator(struct strbuf *message)
241241
{
242-
size_t sep_len = strlen(separator);
243-
if (sep_len && separator[sep_len - 1] == '\n')
242+
size_t sep_len = 0;
243+
244+
if (!separator)
245+
return;
246+
else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n')
244247
strbuf_addstr(message, separator);
245248
else
246249
strbuf_addf(message, "%s%s", separator, "\n");
@@ -249,8 +252,8 @@ static void append_separator(struct strbuf *message)
249252
static void concat_messages(struct note_data *d)
250253
{
251254
struct strbuf msg = STRBUF_INIT;
252-
253255
size_t i;
256+
254257
for (i = 0; i < d->msg_nr ; i++) {
255258
if (d->buf.len)
256259
append_separator(&d->buf);
@@ -341,6 +344,16 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset
341344
return parse_reuse_arg(opt, arg, unset);
342345
}
343346

347+
static int parse_separator_arg(const struct option *opt, const char *arg,
348+
int unset)
349+
{
350+
if (unset)
351+
*(const char **)opt->value = NULL;
352+
else
353+
*(const char **)opt->value = arg ? arg : "\n";
354+
return 0;
355+
}
356+
344357
static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
345358
{
346359
struct strbuf buf = STRBUF_INIT;
@@ -481,8 +494,10 @@ static int add(int argc, const char **argv, const char *prefix)
481494
OPT_BOOL(0, "allow-empty", &allow_empty,
482495
N_("allow storing empty note")),
483496
OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
484-
OPT_STRING(0, "separator", &separator, N_("separator"),
485-
N_("insert <paragraph-break> between paragraphs")),
497+
OPT_CALLBACK_F(0, "separator", &separator,
498+
N_("<paragraph-break>"),
499+
N_("insert <paragraph-break> between paragraphs"),
500+
PARSE_OPT_OPTARG, parse_separator_arg),
486501
OPT_BOOL(0, "stripspace", &d.stripspace,
487502
N_("remove unnecessary whitespace")),
488503
OPT_END()
@@ -654,8 +669,10 @@ static int append_edit(int argc, const char **argv, const char *prefix)
654669
parse_reuse_arg),
655670
OPT_BOOL(0, "allow-empty", &allow_empty,
656671
N_("allow storing empty note")),
657-
OPT_STRING(0, "separator", &separator, N_("separator"),
658-
N_("insert <paragraph-break> between paragraphs")),
672+
OPT_CALLBACK_F(0, "separator", &separator,
673+
N_("<paragraph-break>"),
674+
N_("insert <paragraph-break> between paragraphs"),
675+
PARSE_OPT_OPTARG, parse_separator_arg),
659676
OPT_BOOL(0, "stripspace", &d.stripspace,
660677
N_("remove unnecessary whitespace")),
661678
OPT_END()

t/t3301-notes.sh

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ test_expect_success 'create note with combination of -m and -F' '
382382
'
383383

384384
test_expect_success 'create note with combination of -m and -F and --separator' '
385+
test_when_finished git notes remove HEAD &&
385386
cat >expect-combine_m_and_F <<-\EOF &&
386387
foo
387388
-------
@@ -395,7 +396,22 @@ test_expect_success 'create note with combination of -m and -F and --separator'
395396
EOF
396397
echo "xyzzy" >note_a &&
397398
echo "zyxxy" >note_b &&
398-
git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" --separator "-------" &&
399+
git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" --separator="-------" &&
400+
git notes show >actual &&
401+
test_cmp expect-combine_m_and_F actual
402+
'
403+
404+
test_expect_success 'create note with combination of -m and -F and --no-separator' '
405+
cat >expect-combine_m_and_F <<-\EOF &&
406+
foo
407+
xyzzy
408+
bar
409+
zyxxy
410+
baz
411+
EOF
412+
echo "xyzzy" >note_a &&
413+
echo "zyxxy" >note_b &&
414+
git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" --no-separator &&
399415
git notes show >actual &&
400416
test_cmp expect-combine_m_and_F actual
401417
'
@@ -541,7 +557,7 @@ test_expect_success 'listing non-existing notes fails' '
541557
test_must_be_empty actual
542558
'
543559

544-
test_expect_success 'append: specify an empty separator' '
560+
test_expect_success 'append: specify a separator with an empty arg' '
545561
test_when_finished git notes remove HEAD &&
546562
cat >expect <<-\EOF &&
547563
notes-1
@@ -555,6 +571,33 @@ test_expect_success 'append: specify an empty separator' '
555571
test_cmp expect actual
556572
'
557573

574+
test_expect_success 'append: specify a separator without arg' '
575+
test_when_finished git notes remove HEAD &&
576+
cat >expect <<-\EOF &&
577+
notes-1
578+
579+
notes-2
580+
EOF
581+
582+
git notes add -m "notes-1" &&
583+
git notes append --separator -m "notes-2" &&
584+
git notes show >actual &&
585+
test_cmp expect actual
586+
'
587+
588+
test_expect_success 'append: specify as --no-separator' '
589+
test_when_finished git notes remove HEAD &&
590+
cat >expect <<-\EOF &&
591+
notes-1
592+
notes-2
593+
EOF
594+
595+
git notes add -m "notes-1" &&
596+
git notes append --no-separator -m "notes-2" &&
597+
git notes show >actual &&
598+
test_cmp expect actual
599+
'
600+
558601
test_expect_success 'append: specify separator with line break' '
559602
test_when_finished git notes remove HEAD &&
560603
cat >expect <<-\EOF &&
@@ -615,7 +658,7 @@ test_expect_success 'append note with combination of -m and -F and --separator'
615658
616659
echo "f-notes-1" >note_a &&
617660
echo "f-notes-2" >note_b &&
618-
git notes append -m "m-notes-1" -F note_a -m "m-notes-2" -F note_b -m "m-notes-3" --separator "-------" &&
661+
git notes append -m "m-notes-1" -F note_a -m "m-notes-2" -F note_b -m "m-notes-3" --separator="-------" &&
619662
git notes show >actual &&
620663
test_cmp expect-combine_m_and_F actual
621664
'

0 commit comments

Comments
 (0)