Skip to content

Commit b7d87ad

Browse files
dyronegitster
authored andcommitted
notes.c: append separator instead of insert by pos
Rename "insert_separator" to "append_separator" and also remove the "postion" argument, this serves two purpose: The first is that when specifying more than one "-m" ( like "-F", etc) to "git notes add" or "git notes append", the order of them matters, which means we need to append the each separator and message in turn, so we don't have to make the caller specify the position, the "append" operation is enough and clear. The second is that when we execute the "git notes append" subcommand, we need to combine the "prev_note" and "current_note" to get the final result. Before, we inserted a newline character at the beginning of "current_note". Now, we will append a newline to the end of "prev_note" instead, this will give the consisitent results. Signed-off-by: Teng Long <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90bc19b commit b7d87ad

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

builtin/notes.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ static void write_note_data(struct note_data *d, struct object_id *oid)
229229
}
230230
}
231231

232-
static void insert_separator(struct strbuf *message, size_t pos)
232+
static void append_separator(struct strbuf *message)
233233
{
234234
size_t sep_len = strlen(separator);
235235
if (sep_len && separator[sep_len - 1] == '\n')
236-
strbuf_insertstr(message, pos, separator);
236+
strbuf_addstr(message, separator);
237237
else
238-
strbuf_insertf(message, pos, "%s%s", separator, "\n");
238+
strbuf_addf(message, "%s%s", separator, "\n");
239239
}
240240

241241
static void concat_messages(struct note_data *d)
@@ -245,7 +245,7 @@ static void concat_messages(struct note_data *d)
245245
size_t i;
246246
for (i = 0; i < d->msg_nr ; i++) {
247247
if (d->buf.len)
248-
insert_separator(&d->buf, d->buf.len);
248+
append_separator(&d->buf);
249249
strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len);
250250
strbuf_addbuf(&d->buf, &msg);
251251
if (d->messages[i]->stripspace)
@@ -680,14 +680,17 @@ static int append_edit(int argc, const char **argv, const char *prefix)
680680
/* Append buf to previous note contents */
681681
unsigned long size;
682682
enum object_type type;
683-
char *prev_buf = repo_read_object_file(the_repository, note,
684-
&type, &size);
683+
struct strbuf buf = STRBUF_INIT;
684+
char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
685685

686-
if (d.buf.len && prev_buf && size)
687-
insert_separator(&d.buf, 0);
688686
if (prev_buf && size)
689-
strbuf_insert(&d.buf, 0, prev_buf, size);
687+
strbuf_add(&buf, prev_buf, size);
688+
if (d.buf.len && prev_buf && size)
689+
append_separator(&buf);
690+
strbuf_insert(&d.buf, 0, buf.buf, buf.len);
691+
690692
free(prev_buf);
693+
strbuf_release(&buf);
691694
}
692695

693696
if (d.buf.len || allow_empty) {

0 commit comments

Comments
 (0)