Skip to content

Commit c4e2aa7

Browse files
dyronegitster
authored andcommitted
notes.c: introduce "--[no-]stripspace" option
This commit introduces a new option "--[no-]stripspace" to git notes append, git notes edit, and git notes add. This option allows users to control whether the note message need to stripped out. For the consideration of backward compatibility, let's look at the behavior about "stripspace" in "git notes" command: 1. "Edit Message" case: using the default editor to edit the note message. In "edit" case, the edited message will always be stripped out, the implementation which can be found in the "prepare_note_data()". In addition, the "-c" option supports to reuse an existing blob as a note message, then open the editor to make a further edition on it, the edited message will be stripped. This commit doesn't change the default behavior of "edit" case by using an enum "notes_stripspace", only when "--no-stripspace" option is specified, the note message will not be stripped out. If you do not specify the option or you specify "--stripspace", clearly, the note message will be stripped out. 2. "Assign Message" case: using the "-m"/"-F"/"-C" option to specify the note message. In "assign" case, when specify message by "-m" or "-F", the message will be stripped out by default, but when specify message by "-C", the message will be copied verbatim, in other word, the message will not be stripped out. One more thing need to note is "the order of the options matter", that is, if you specify "-C" before "-m" or "-F", the reused message by "-C" will be stripped out together, because everytime concat "-m" or "-F" message, the concated message will be stripped together. Oppositely, if you specify "-m" or "-F" before "-C", the reused message by "-C" will not be stripped out. This commit doesn't change the default behavior of "assign" case by extending the "stripspace" field in "struct note_msg", so we can distinguish the different behavior of "-m"/"-F" and "-C" options when we need to parse and concat the message. Signed-off-by: Teng Long <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7d87ad commit c4e2aa7

File tree

3 files changed

+332
-19
lines changed

3 files changed

+332
-19
lines changed

Documentation/git-notes.txt

Lines changed: 19 additions & 6 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] [--allow-empty] [--separator=<paragraph-break>] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
12+
'git notes' add [-f] [--allow-empty] [--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>] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
15-
'git notes' edit [--allow-empty] [<object>]
14+
'git notes' append [--allow-empty] [--separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
15+
'git notes' edit [--allow-empty] [<object>] [--[no-]stripspace]
1616
'git notes' show [<object>]
1717
'git notes' merge [-v | -q] [-s <strategy> ] <notes-ref>
1818
'git notes' merge --commit [-v | -q]
@@ -141,20 +141,26 @@ OPTIONS
141141
If multiple `-m` options are given, their values
142142
are concatenated as separate paragraphs.
143143
Lines starting with `#` and empty lines other than a
144-
single line between paragraphs will be stripped out.
144+
single line between paragraphs will be stripped out,
145+
if you wish to keep them verbatim, use `--no-stripspace`.
145146

146147
-F <file>::
147148
--file=<file>::
148149
Take the note message from the given file. Use '-' to
149150
read the note message from the standard input.
150151
Lines starting with `#` and empty lines other than a
151-
single line between paragraphs will be stripped out.
152+
single line between paragraphs will be stripped out,
153+
if you wish to keep them verbatim, use with
154+
`--no-stripspace` option.
152155

153156
-C <object>::
154157
--reuse-message=<object>::
155158
Take the given blob object (for example, another note) as the
156159
note message. (Use `git notes copy <object>` instead to
157-
copy notes between objects.)
160+
copy notes between objects.). By default, message will be
161+
copied verbatim, but if you wish to strip out the lines
162+
starting with `#` and empty lines other than a single line
163+
between paragraphs, use with`--stripspace` option.
158164

159165
-c <object>::
160166
--reedit-message=<object>::
@@ -170,6 +176,13 @@ OPTIONS
170176
(a newline is added at the end as needed). Defaults to a
171177
blank line.
172178

179+
--[no-]stripspace::
180+
Strip leading and trailing whitespace from the note message.
181+
Also strip out empty lines other than a single line between
182+
paragraphs. For lines starting with `#` will be stripped out
183+
in non-editor cases like "-m", "-F" and "-C", but not in
184+
editor case like "git notes edit", "-c", etc.
185+
173186
--ref <ref>::
174187
Manipulate the notes tree in <ref>. This overrides
175188
`GIT_NOTES_REF` and the "core.notesRef" configuration. The ref

builtin/notes.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,21 @@ static const char * const git_notes_get_ref_usage[] = {
101101
static const char note_template[] =
102102
N_("Write/edit the notes for the following object:");
103103

104+
enum notes_stripspace {
105+
UNSPECIFIED = -1,
106+
NO_STRIPSPACE = 0,
107+
STRIPSPACE = 1,
108+
};
109+
104110
struct note_msg {
105-
int stripspace;
111+
enum notes_stripspace stripspace;
106112
struct strbuf buf;
107113
};
108114

109115
struct note_data {
110116
int given;
111117
int use_editor;
118+
int stripspace;
112119
char *edit_path;
113120
struct strbuf buf;
114121
struct note_msg **messages;
@@ -213,7 +220,8 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
213220
if (launch_editor(d->edit_path, &d->buf, NULL)) {
214221
die(_("please supply the note contents using either -m or -F option"));
215222
}
216-
strbuf_stripspace(&d->buf, 1);
223+
if (d->stripspace)
224+
strbuf_stripspace(&d->buf, 1);
217225
}
218226
}
219227

@@ -248,7 +256,9 @@ static void concat_messages(struct note_data *d)
248256
append_separator(&d->buf);
249257
strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len);
250258
strbuf_addbuf(&d->buf, &msg);
251-
if (d->messages[i]->stripspace)
259+
if ((d->stripspace == UNSPECIFIED &&
260+
d->messages[i]->stripspace == STRIPSPACE) ||
261+
d->stripspace == STRIPSPACE)
252262
strbuf_stripspace(&d->buf, 0);
253263
strbuf_reset(&msg);
254264
}
@@ -266,7 +276,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
266276
strbuf_addstr(&msg->buf, arg);
267277
ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
268278
d->messages[d->msg_nr - 1] = msg;
269-
msg->stripspace = 1;
279+
msg->stripspace = STRIPSPACE;
270280
return 0;
271281
}
272282

@@ -286,7 +296,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
286296

287297
ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
288298
d->messages[d->msg_nr - 1] = msg;
289-
msg->stripspace = 1;
299+
msg->stripspace = STRIPSPACE;
290300
return 0;
291301
}
292302

@@ -319,7 +329,7 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
319329
msg->buf.len = len;
320330
ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
321331
d->messages[d->msg_nr - 1] = msg;
322-
msg->stripspace = 0;
332+
msg->stripspace = NO_STRIPSPACE;
323333
return 0;
324334
}
325335

@@ -453,7 +463,7 @@ static int add(int argc, const char **argv, const char *prefix)
453463
struct notes_tree *t;
454464
struct object_id object, new_note;
455465
const struct object_id *note;
456-
struct note_data d = { .buf = STRBUF_INIT };
466+
struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
457467

458468
struct option options[] = {
459469
OPT_CALLBACK_F('m', "message", &d, N_("message"),
@@ -473,6 +483,8 @@ static int add(int argc, const char **argv, const char *prefix)
473483
OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
474484
OPT_STRING(0, "separator", &separator, N_("separator"),
475485
N_("insert <paragraph-break> between paragraphs")),
486+
OPT_BOOL(0, "stripspace", &d.stripspace,
487+
N_("remove unnecessary whitespace")),
476488
OPT_END()
477489
};
478490

@@ -626,7 +638,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
626638
const struct object_id *note;
627639
char *logmsg;
628640
const char * const *usage;
629-
struct note_data d = { .buf = STRBUF_INIT };
641+
struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
630642
struct option options[] = {
631643
OPT_CALLBACK_F('m', "message", &d, N_("message"),
632644
N_("note contents as a string"), PARSE_OPT_NONEG,
@@ -644,6 +656,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
644656
N_("allow storing empty note")),
645657
OPT_STRING(0, "separator", &separator, N_("separator"),
646658
N_("insert <paragraph-break> between paragraphs")),
659+
OPT_BOOL(0, "stripspace", &d.stripspace,
660+
N_("remove unnecessary whitespace")),
647661
OPT_END()
648662
};
649663
int edit = !strcmp(argv[0], "edit");

0 commit comments

Comments
 (0)