strbuf: accept a comment string for strbuf_add_commented_lines()

As part of our transition to multi-byte comment characters, let's take a NUL-terminated string pointer for strbuf_add_commented_lines() rather than a single character. All of the callers have to be adjusted; most can just pass comment_line_str rather than comment_line_char. And now our "cheat" in strbuf_commented_addf() can go away, as we can take the full string from it. 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 a1bb146aaf551db4ff9b4aafaaa2f7a9f9574f93
8 files changed +21 -31
builtin/notes.c
+4 -4
@@ -179,7 +179,7 @@ static void write_commented_object(int fd, const struct object_id *object)
179
180 if (strbuf_read(&buf, show.out, 0) < 0)
181 die_errno(_("could not read 'show' output"));
182 - strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
182 + strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
183 write_or_die(fd, cbuf.buf, cbuf.len);
184
185 strbuf_release(&cbuf);
@@ -207,10 +207,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
207 copy_obj_to_fd(fd, old_note);
208
209 strbuf_addch(&buf, '\n');
210 - strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
210 + strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
211 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
212 - comment_line_char);
213 - strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
212 + comment_line_str);
213 + strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
214 write_or_die(fd, buf.buf, buf.len);
215
216 write_commented_object(fd, object);
builtin/stripspace.c
+1 -1
@@ -13,7 +13,7 @@ static void comment_lines(struct strbuf *buf)
13 size_t len;
14
15 msg = strbuf_detach(buf, &len);
16 - strbuf_add_commented_lines(buf, msg, len, comment_line_char);
16 + strbuf_add_commented_lines(buf, msg, len, comment_line_str);
17 free(msg);
18 }
19
fmt-merge-msg.c
+3 -3
@@ -510,7 +510,7 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
510 if (sig->len) {
511 strbuf_addch(tagbuf, '\n');
512 strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
513 - comment_line_char);
513 + comment_line_str);
514 }
515 }
516
@@ -557,7 +557,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
557 strbuf_add_commented_lines(&tagline,
558 origins.items[first_tag].string,
559 strlen(origins.items[first_tag].string),
560 - comment_line_char);
560 + comment_line_str);
561 strbuf_insert(&tagbuf, 0, tagline.buf,
562 tagline.len);
563 strbuf_release(&tagline);
@@ -566,7 +566,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
566 strbuf_add_commented_lines(&tagbuf,
567 origins.items[i].string,
568 strlen(origins.items[i].string),
569 - comment_line_char);
569 + comment_line_str);
570 fmt_tag_signature(&tagbuf, &sig, buf, len);
571 }
572 strbuf_release(&payload);
rebase-interactive.c
+3 -3
@@ -78,7 +78,7 @@ void append_todo_help(int command_count,
78 shortrevisions, shortonto, command_count);
79 }
80
81 - strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
81 + strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
82
83 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
84 msg = _("\nDo not remove any line. Use 'drop' "
@@ -87,7 +87,7 @@ void append_todo_help(int command_count,
87 msg = _("\nIf you remove a line here "
88 "THAT COMMIT WILL BE LOST.\n");
89
90 - strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
90 + strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
91
92 if (edit_todo)
93 msg = _("\nYou are editing the todo file "
@@ -98,7 +98,7 @@ void append_todo_help(int command_count,
98 msg = _("\nHowever, if you remove everything, "
99 "the rebase will be aborted.\n\n");
100
101 - strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
101 + strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
102 }
103
104 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
sequencer.c
+4 -4
@@ -1851,7 +1851,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
1851 s += count;
1852 len -= count;
1853 }
1854 - strbuf_add_commented_lines(buf, s, len, comment_line_char);
1854 + strbuf_add_commented_lines(buf, s, len, comment_line_str);
1855 }
1856
1857 /* Does the current fixup chain contain a squash command? */
@@ -1950,7 +1950,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
1950 strbuf_addf(buf, _(nth_commit_msg_fmt),
1951 ++opts->current_fixup_count + 1);
1952 strbuf_addstr(buf, "\n\n");
1953 - strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
1953 + strbuf_add_commented_lines(buf, body, commented_len, comment_line_str);
1954 /* buf->buf may be reallocated so store an offset into the buffer */
1955 fixup_off = buf->len;
1956 strbuf_addstr(buf, body + commented_len);
@@ -2041,7 +2041,7 @@ static int update_squash_messages(struct repository *r,
2041 strbuf_addstr(&buf, "\n\n");
2042 if (is_fixup_flag(command, flag))
2043 strbuf_add_commented_lines(&buf, body, strlen(body),
2044 - comment_line_char);
2044 + comment_line_str);
2045 else
2046 strbuf_addstr(&buf, body);
2047
@@ -2061,7 +2061,7 @@ static int update_squash_messages(struct repository *r,
2061 ++opts->current_fixup_count + 1);
2062 strbuf_addstr(&buf, "\n\n");
2063 strbuf_add_commented_lines(&buf, body, strlen(body),
2064 - comment_line_char);
2064 + comment_line_str);
2065 } else
2066 return error(_("unknown command: %d"), command);
2067 repo_unuse_commit_buffer(r, commit, message);
strbuf.c
+3 -13
@@ -359,13 +359,9 @@ static void add_lines(struct strbuf *out,
359 }
360
361 void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
362 - size_t size, char comment_prefix)
362 + size_t size, const char *comment_prefix)
363 {
364 - char prefix[2];
365 -
366 - prefix[0] = comment_prefix;
367 - prefix[1] = '\0';
368 - add_lines(out, prefix, buf, size, 1);
364 + add_lines(out, comment_prefix, buf, size, 1);
365 }
366
367 void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix,
@@ -379,13 +375,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix,
375 strbuf_vaddf(&buf, fmt, params);
376 va_end(params);
377
382 - /*
383 - * TODO Our commented_lines helper does not yet understand
384 - * comment strings. But since we know that the strings are
385 - * always single-char, we can cheat for the moment, and
386 - * fix this later.
387 - */
388 - strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix[0]);
378 + strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix);
379 if (incomplete_line)
380 sb->buf[--sb->len] = '\0';
381
strbuf.h
+1 -1
@@ -288,7 +288,7 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
288 */
289 void strbuf_add_commented_lines(struct strbuf *out,
290 const char *buf, size_t size,
291 - char comment_prefix);
291 + const char *comment_prefix);
292
293
294 /**
wt-status.c
+2 -2
@@ -1028,7 +1028,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
1028 if (s->display_comment_prefix) {
1029 size_t len;
1030 summary_content = strbuf_detach(&summary, &len);
1031 - strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
1031 + strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
1032 free(summary_content);
1033 }
1034
@@ -1104,7 +1104,7 @@ void wt_status_append_cut_line(struct strbuf *buf)
1104 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1105
1106 strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
1107 - strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
1107 + strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
1108 }
1109
1110 void wt_status_add_cut_line(FILE *fp)