]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf: accept a comment string for strbuf_add_commented_lines()
authorJeff King <peff@peff.net>
Tue, 12 Mar 2024 09:17:32 +0000 (05:17 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Mar 2024 20:28:10 +0000 (13:28 -0700)
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>
builtin/notes.c
builtin/stripspace.c
fmt-merge-msg.c
rebase-interactive.c
sequencer.c
strbuf.c
strbuf.h
wt-status.c

index ae981085eaffe46c58cde18380430afe57c05d1c..cb011303e68d7725b8ed7bed0f2069e81859d692 100644 (file)
@@ -179,7 +179,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, comment_line_char);
+       strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
        write_or_die(fd, cbuf.buf, cbuf.len);
 
        strbuf_release(&cbuf);
@@ -207,10 +207,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"), comment_line_char);
+               strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
                strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
-                                          comment_line_char);
-               strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+                                          comment_line_str);
+               strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
                write_or_die(fd, buf.buf, buf.len);
 
                write_commented_object(fd, object);
index 434ac490cb11d4083b98e0fbc878afec5d494263..e5626e5126695b43aea014e3852ea325f055e62b 100644 (file)
@@ -13,7 +13,7 @@ static void comment_lines(struct strbuf *buf)
        size_t len;
 
        msg = strbuf_detach(buf, &len);
-       strbuf_add_commented_lines(buf, msg, len, comment_line_char);
+       strbuf_add_commented_lines(buf, msg, len, comment_line_str);
        free(msg);
 }
 
index 66e47449a092d67b66a172a0e325593d5e494efa..79e8aad086108e76f7a95e4595fc27259b05afee 100644 (file)
@@ -510,7 +510,7 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
        if (sig->len) {
                strbuf_addch(tagbuf, '\n');
                strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
-                                          comment_line_char);
+                                          comment_line_str);
        }
 }
 
@@ -557,7 +557,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
                                strbuf_add_commented_lines(&tagline,
                                                origins.items[first_tag].string,
                                                strlen(origins.items[first_tag].string),
-                                               comment_line_char);
+                                               comment_line_str);
                                strbuf_insert(&tagbuf, 0, tagline.buf,
                                              tagline.len);
                                strbuf_release(&tagline);
@@ -566,7 +566,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
                        strbuf_add_commented_lines(&tagbuf,
                                        origins.items[i].string,
                                        strlen(origins.items[i].string),
-                                       comment_line_char);
+                                       comment_line_str);
                        fmt_tag_signature(&tagbuf, &sig, buf, len);
                }
                strbuf_release(&payload);
index affc93a8e4d27e76c71b877862710af684fe1e7f..c343e16fcddddec8601a2278ae3b5e39dbab770c 100644 (file)
@@ -78,7 +78,7 @@ void append_todo_help(int command_count,
                                      shortrevisions, shortonto, command_count);
        }
 
-       strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
+       strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
 
        if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
                msg = _("\nDo not remove any line. Use 'drop' "
@@ -87,7 +87,7 @@ void append_todo_help(int command_count,
                msg = _("\nIf you remove a line here "
                         "THAT COMMIT WILL BE LOST.\n");
 
-       strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
+       strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
 
        if (edit_todo)
                msg = _("\nYou are editing the todo file "
@@ -98,7 +98,7 @@ void append_todo_help(int command_count,
                msg = _("\nHowever, if you remove everything, "
                        "the rebase will be aborted.\n\n");
 
-       strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
+       strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
index 852c3f9f4e2975cf1e38a9c7d7c67969f9736d3d..032e213a3ff4ca5571a98b6693d30b55f33ad053 100644 (file)
@@ -1851,7 +1851,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
                s += count;
                len -= count;
        }
-       strbuf_add_commented_lines(buf, s, len, comment_line_char);
+       strbuf_add_commented_lines(buf, s, len, comment_line_str);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1950,7 +1950,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
        strbuf_addf(buf, _(nth_commit_msg_fmt),
                    ++opts->current_fixup_count + 1);
        strbuf_addstr(buf, "\n\n");
-       strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
+       strbuf_add_commented_lines(buf, body, commented_len, comment_line_str);
        /* buf->buf may be reallocated so store an offset into the buffer */
        fixup_off = buf->len;
        strbuf_addstr(buf, body + commented_len);
@@ -2041,7 +2041,7 @@ static int update_squash_messages(struct repository *r,
                strbuf_addstr(&buf, "\n\n");
                if (is_fixup_flag(command, flag))
                        strbuf_add_commented_lines(&buf, body, strlen(body),
-                                                  comment_line_char);
+                                                  comment_line_str);
                else
                        strbuf_addstr(&buf, body);
 
@@ -2061,7 +2061,7 @@ static int update_squash_messages(struct repository *r,
                            ++opts->current_fixup_count + 1);
                strbuf_addstr(&buf, "\n\n");
                strbuf_add_commented_lines(&buf, body, strlen(body),
-                                          comment_line_char);
+                                          comment_line_str);
        } else
                return error(_("unknown command: %d"), command);
        repo_unuse_commit_buffer(r, commit, message);
index 76d02e0920a8c91c2d8c18eb41a32e0d26186b4d..7c8f58212758b120b759074d8c7fa5f7da239490 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -359,13 +359,9 @@ static void add_lines(struct strbuf *out,
 }
 
 void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
-                               size_t size, char comment_prefix)
+                               size_t size, const char *comment_prefix)
 {
-       char prefix[2];
-
-       prefix[0] = comment_prefix;
-       prefix[1] = '\0';
-       add_lines(out, prefix, buf, size, 1);
+       add_lines(out, comment_prefix, buf, size, 1);
 }
 
 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,
        strbuf_vaddf(&buf, fmt, params);
        va_end(params);
 
-       /*
-        * TODO Our commented_lines helper does not yet understand
-        * comment strings. But since we know that the strings are
-        * always single-char, we can cheat for the moment, and
-        * fix this later.
-        */
-       strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix[0]);
+       strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix);
        if (incomplete_line)
                sb->buf[--sb->len] = '\0';
 
index b128ca539a3ea7def36ad303c0550196f412780b..58dddf2777b56777f3f711bb2112d96b488520aa 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -288,7 +288,7 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  */
 void strbuf_add_commented_lines(struct strbuf *out,
                                const char *buf, size_t size,
-                               char comment_prefix);
+                               const char *comment_prefix);
 
 
 /**
index 2be2eb094c7231f8d45e368490f5ef44d9c0331a..6b81f5349cf772f442e59ca93f787b03badcb865 100644 (file)
@@ -1028,7 +1028,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
        if (s->display_comment_prefix) {
                size_t len;
                summary_content = strbuf_detach(&summary, &len);
-               strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
+               strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
                free(summary_content);
        }
 
@@ -1104,7 +1104,7 @@ void wt_status_append_cut_line(struct strbuf *buf)
        const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
        strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
-       strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
+       strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
 }
 
 void wt_status_add_cut_line(FILE *fp)