strbuf: remove global variable

As a library that only interacts with other primitives, strbuf should
not utilize the comment_line_char global variable within its
functions. Therefore, add an additional parameter for functions that use
comment_line_char and refactor callers to pass it in instead.
strbuf_stripspace() removes the skip_comments boolean and checks if
comment_line_char is a non-NUL character to determine whether to skip
comments or not.

Signed-off-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Calvin Wan
2023-06-06 19:48:43 +00:00
committed by Junio C Hamano
parent aba0706832
commit 787cb8a48a
16 changed files with 90 additions and 64 deletions

View File

@ -11,6 +11,7 @@
#include "config.h"
#include "builtin.h"
#include "editor.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
#include "notes.h"
@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
if (strbuf_read(&buf, show.out, 0) < 0)
die_errno(_("could not read 'show' output"));
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
write_or_die(fd, cbuf.buf, cbuf.len);
strbuf_release(&cbuf);
@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
copy_obj_to_fd(fd, old_note);
strbuf_addch(&buf, '\n');
strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
comment_line_char);
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
write_or_die(fd, buf.buf, buf.len);
write_commented_object(fd, object);
@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
if (launch_editor(d->edit_path, &d->buf, NULL)) {
die(_("please supply the note contents using either -m or -F option"));
}
strbuf_stripspace(&d->buf, 1);
strbuf_stripspace(&d->buf, comment_line_char);
}
}
@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
if (d->buf.len)
strbuf_addch(&d->buf, '\n');
strbuf_addstr(&d->buf, arg);
strbuf_stripspace(&d->buf, 0);
strbuf_stripspace(&d->buf, '\0');
d->given = 1;
return 0;
@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
die_errno(_("cannot read '%s'"), arg);
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
die_errno(_("could not open or read '%s'"), arg);
strbuf_stripspace(&d->buf, 0);
strbuf_stripspace(&d->buf, '\0');
d->given = 1;
return 0;