strbuf: accept a comment string for strbuf_stripspace()

As part of our transition to multi-byte comment characters, let's take a NUL-terminated string pointer for strbuf_stripspace(), rather than a single character. We can continue to support its feature of ignoring comments by accepting a NULL pointer (as opposed to the current behavior of a NUL byte). All of the callers have to be adjusted, but they can all just pass comment_line_str (or NULL). Inside the function we detect comments by comparing the first byte of a line to the comment character. We'll adjust that to use starts_with(), which will match multiple bytes (though for now, of course, we still only allow a single byte, so it's academic). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 12, 2024 at 05:17 UTC 2982b65690d7a043275558c74202a89b0450cbf5
13 files changed +20 -20
builtin/am.c
+1 -1
@@ -1286,7 +1286,7 @@ static int parse_mail(struct am_state *state, const char *mail)
1286
1287 strbuf_addstr(&msg, "\n\n");
1288 strbuf_addbuf(&msg, &mi.log_message);
1289 - strbuf_stripspace(&msg, '\0');
1289 + strbuf_stripspace(&msg, NULL);
1290
1291 assert(!state->author_name);
1292 state->author_name = strbuf_detach(&author_name, NULL);
builtin/branch.c
+1 -1
@@ -678,7 +678,7 @@ static int edit_branch_description(const char *branch_name)
678 strbuf_release(&buf);
679 return -1;
680 }
681 - strbuf_stripspace(&buf, comment_line_char);
681 + strbuf_stripspace(&buf, comment_line_str);
682
683 strbuf_addf(&name, "branch.%s.description", branch_name);
684 if (buf.len || exists)
builtin/commit.c
+1 -1
@@ -890,7 +890,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
890 s->hints = 0;
891
892 if (clean_message_contents)
893 - strbuf_stripspace(&sb, '\0');
893 + strbuf_stripspace(&sb, NULL);
894
895 if (signoff)
896 append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
builtin/notes.c
+2 -2
@@ -223,7 +223,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
223 die(_("please supply the note contents using either -m or -F option"));
224 }
225 if (d->stripspace)
226 - strbuf_stripspace(&d->buf, comment_line_char);
226 + strbuf_stripspace(&d->buf, comment_line_str);
227 }
228 }
229
@@ -264,7 +264,7 @@ static void concat_messages(struct note_data *d)
264 if ((d->stripspace == UNSPECIFIED &&
265 d->messages[i]->stripspace == STRIPSPACE) ||
266 d->stripspace == STRIPSPACE)
267 - strbuf_stripspace(&d->buf, 0);
267 + strbuf_stripspace(&d->buf, NULL);
268 strbuf_reset(&msg);
269 }
270 strbuf_release(&msg);
builtin/rebase.c
+1 -1
@@ -204,7 +204,7 @@ static int edit_todo_file(unsigned flags)
204 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
205 return error_errno(_("could not read '%s'."), todo_file);
206
207 - strbuf_stripspace(&todo_list.buf, comment_line_char);
207 + strbuf_stripspace(&todo_list.buf, comment_line_str);
208 res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
209 if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
210 NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
builtin/stripspace.c
+1 -1
@@ -59,7 +59,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
59
60 if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
61 strbuf_stripspace(&buf,
62 - mode == STRIP_COMMENTS ? comment_line_char : '\0');
62 + mode == STRIP_COMMENTS ? comment_line_str : NULL);
63 else
64 comment_lines(&buf);
65
builtin/tag.c
+1 -1
@@ -310,7 +310,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
310
311 if (opt->cleanup_mode != CLEANUP_NONE)
312 strbuf_stripspace(buf,
313 - opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
313 + opt->cleanup_mode == CLEANUP_ALL ? comment_line_str : NULL);
314
315 if (!opt->message_given && !buf->len)
316 die(_("no tag message?"));
builtin/worktree.c
+1 -1
@@ -657,7 +657,7 @@ static int can_use_local_refs(const struct add_opts *opts)
657 strbuf_add_real_path(&path, get_worktree_git_dir(NULL));
658 strbuf_addstr(&path, "/HEAD");
659 strbuf_read_file(&contents, path.buf, 64);
660 - strbuf_stripspace(&contents, 0);
660 + strbuf_stripspace(&contents, NULL);
661 strbuf_strip_suffix(&contents, "\n");
662
663 warning(_("HEAD points to an invalid (or orphaned) reference.\n"
gpg-interface.c
+2 -2
@@ -586,8 +586,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
586 }
587 }
588
589 - strbuf_stripspace(&ssh_keygen_out, '\0');
590 - strbuf_stripspace(&ssh_keygen_err, '\0');
589 + strbuf_stripspace(&ssh_keygen_out, NULL);
590 + strbuf_stripspace(&ssh_keygen_err, NULL);
591 /* Add stderr outputs to show the user actual ssh-keygen errors */
592 strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len);
593 strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len);
rebase-interactive.c
+1 -1
@@ -130,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
130 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
131 return -2;
132
133 - strbuf_stripspace(&new_todo->buf, comment_line_char);
133 + strbuf_stripspace(&new_todo->buf, comment_line_str);
134 if (initial && new_todo->buf.len == 0)
135 return -3;
136
sequencer.c
+3 -3
@@ -1152,7 +1152,7 @@ void cleanup_message(struct strbuf *msgbuf,
1152 strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
1153 if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
1154 strbuf_stripspace(msgbuf,
1155 - cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
1155 + cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL);
1156 }
1157
1158 /*
@@ -1184,7 +1184,7 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
1184 return 0;
1185
1186 strbuf_stripspace(&tmpl,
1187 - cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
1187 + cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL);
1188 if (!skip_prefix(sb->buf, tmpl.buf, &start))
1189 start = sb->buf;
1190 strbuf_release(&tmpl);
@@ -1557,7 +1557,7 @@ static int try_to_commit(struct repository *r,
1557
1558 if (cleanup != COMMIT_MSG_CLEANUP_NONE)
1559 strbuf_stripspace(msg,
1560 - cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
1560 + cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL);
1561 if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
1562 res = 1; /* run 'git commit' to display error message */
1563 goto out;
strbuf.c
+3 -3
@@ -1001,10 +1001,10 @@ static size_t cleanup(char *line, size_t len)
1001 *
1002 * If last line does not have a newline at the end, one is added.
1003 *
1004 - * Pass a non-NUL comment_prefix to skip every line starting
1004 + * Pass a non-NULL comment_prefix to skip every line starting
1005 * with it.
1006 */
1007 -void strbuf_stripspace(struct strbuf *sb, char comment_prefix)
1007 +void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
1008 {
1009 size_t empties = 0;
1010 size_t i, j, len, newlen;
@@ -1018,7 +1018,7 @@ void strbuf_stripspace(struct strbuf *sb, char comment_prefix)
1018 len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
1019
1020 if (comment_prefix && len &&
1021 - sb->buf[i] == comment_prefix) {
1021 + starts_with(sb->buf + i, comment_prefix)) {
1022 newlen = 0;
1023 continue;
1024 }
strbuf.h
+2 -2
@@ -513,11 +513,11 @@ int strbuf_getcwd(struct strbuf *sb);
513 int strbuf_normalize_path(struct strbuf *sb);
514
515 /**
516 - * Strip whitespace from a buffer. If comment_prefix is non-NUL,
516 + * Strip whitespace from a buffer. If comment_prefix is non-NULL,
517 * then lines beginning with that character are considered comments,
518 * thus removed.
519 */
520 -void strbuf_stripspace(struct strbuf *buf, char comment_prefix);
520 +void strbuf_stripspace(struct strbuf *buf, const char *comment_prefix);
521
522 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
523 {