]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf: accept a comment string for strbuf_stripspace()
authorJeff King <peff@peff.net>
Tue, 12 Mar 2024 09:17:27 +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_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>
13 files changed:
builtin/am.c
builtin/branch.c
builtin/commit.c
builtin/notes.c
builtin/rebase.c
builtin/stripspace.c
builtin/tag.c
builtin/worktree.c
gpg-interface.c
rebase-interactive.c
sequencer.c
strbuf.c
strbuf.h

index d1990d7edcbe37467c59a420a9b03a5894e6da93..5bc72d78229e30bf8fe63ff833cdae957f6b6b65 100644 (file)
@@ -1286,7 +1286,7 @@ static int parse_mail(struct am_state *state, const char *mail)
 
        strbuf_addstr(&msg, "\n\n");
        strbuf_addbuf(&msg, &mi.log_message);
-       strbuf_stripspace(&msg, '\0');
+       strbuf_stripspace(&msg, NULL);
 
        assert(!state->author_name);
        state->author_name = strbuf_detach(&author_name, NULL);
index cfb63cce5fb9dff64106907947d0df25a2c25489..c03c0407d1d0b6b4e504ab5413dee8239eae1056 100644 (file)
@@ -678,7 +678,7 @@ static int edit_branch_description(const char *branch_name)
                strbuf_release(&buf);
                return -1;
        }
-       strbuf_stripspace(&buf, comment_line_char);
+       strbuf_stripspace(&buf, comment_line_str);
 
        strbuf_addf(&name, "branch.%s.description", branch_name);
        if (buf.len || exists)
index d8abbe48b17b9b63f47b59013ed61bcfcde4ddd4..583bf04be5f7b1e55b7a6a2f8531012a47386c08 100644 (file)
@@ -890,7 +890,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
        s->hints = 0;
 
        if (clean_message_contents)
-               strbuf_stripspace(&sb, '\0');
+               strbuf_stripspace(&sb, NULL);
 
        if (signoff)
                append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
index caf20fd5bdd4b7c1cb44191d58ecbb389643b748..ae981085eaffe46c58cde18380430afe57c05d1c 100644 (file)
@@ -223,7 +223,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
                        die(_("please supply the note contents using either -m or -F option"));
                }
                if (d->stripspace)
-                       strbuf_stripspace(&d->buf, comment_line_char);
+                       strbuf_stripspace(&d->buf, comment_line_str);
        }
 }
 
@@ -264,7 +264,7 @@ static void concat_messages(struct note_data *d)
                if ((d->stripspace == UNSPECIFIED &&
                     d->messages[i]->stripspace == STRIPSPACE) ||
                    d->stripspace == STRIPSPACE)
-                       strbuf_stripspace(&d->buf, 0);
+                       strbuf_stripspace(&d->buf, NULL);
                strbuf_reset(&msg);
        }
        strbuf_release(&msg);
index 6ead9465a42c6d3c6ff7a547bb42ed9dd919dd90..bf7840212992962a87e8644f9ee14c8a95b43bc0 100644 (file)
@@ -204,7 +204,7 @@ static int edit_todo_file(unsigned flags)
        if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
                return error_errno(_("could not read '%s'."), todo_file);
 
-       strbuf_stripspace(&todo_list.buf, comment_line_char);
+       strbuf_stripspace(&todo_list.buf, comment_line_str);
        res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
        if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
                                            NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
index 7b700a9fb1c2a355af6ee6935edbeec49f3f4135..434ac490cb11d4083b98e0fbc878afec5d494263 100644 (file)
@@ -59,7 +59,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 
        if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
                strbuf_stripspace(&buf,
-                         mode == STRIP_COMMENTS ? comment_line_char : '\0');
+                         mode == STRIP_COMMENTS ? comment_line_str : NULL);
        else
                comment_lines(&buf);
 
index 19a7e06bf414ccf811c93f0ac941a142a7fabef2..07327d3c040f444e16b371b4b5d7dbbc5bb91f94 100644 (file)
@@ -310,7 +310,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 
        if (opt->cleanup_mode != CLEANUP_NONE)
                strbuf_stripspace(buf,
-                 opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
+                 opt->cleanup_mode == CLEANUP_ALL ? comment_line_str : NULL);
 
        if (!opt->message_given && !buf->len)
                die(_("no tag message?"));
index 9c76b62b02da037db84ef633100cc6d9136eb0cf..f0aa962cf816b5e3ed24bb86c09fb8e7dc3c1262 100644 (file)
@@ -657,7 +657,7 @@ static int can_use_local_refs(const struct add_opts *opts)
                        strbuf_add_real_path(&path, get_worktree_git_dir(NULL));
                        strbuf_addstr(&path, "/HEAD");
                        strbuf_read_file(&contents, path.buf, 64);
-                       strbuf_stripspace(&contents, 0);
+                       strbuf_stripspace(&contents, NULL);
                        strbuf_strip_suffix(&contents, "\n");
 
                        warning(_("HEAD points to an invalid (or orphaned) reference.\n"
index 95e764acb14b3e069a255a8261a7d724c5f8d1ad..b5993385ff521da302eea04148d1055ff8d6b5ff 100644 (file)
@@ -586,8 +586,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
                }
        }
 
-       strbuf_stripspace(&ssh_keygen_out, '\0');
-       strbuf_stripspace(&ssh_keygen_err, '\0');
+       strbuf_stripspace(&ssh_keygen_out, NULL);
+       strbuf_stripspace(&ssh_keygen_err, NULL);
        /* Add stderr outputs to show the user actual ssh-keygen errors */
        strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len);
        strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len);
index d9718409b3d7862fee0eaa7e2255e83f50379656..6dfc33e4e391c33486e40746553809946e1ef0f1 100644 (file)
@@ -130,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
        if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
                return -2;
 
-       strbuf_stripspace(&new_todo->buf, comment_line_char);
+       strbuf_stripspace(&new_todo->buf, comment_line_str);
        if (initial && new_todo->buf.len == 0)
                return -3;
 
index f49a871ac0666b10393331ed3c9b611921f1039e..6a1b7b200ee5c1f1384eea0fd033e535c41abac7 100644 (file)
@@ -1152,7 +1152,7 @@ void cleanup_message(struct strbuf *msgbuf,
                strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
        if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
                strbuf_stripspace(msgbuf,
-                 cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
+                 cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL);
 }
 
 /*
@@ -1184,7 +1184,7 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
                return 0;
 
        strbuf_stripspace(&tmpl,
-         cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
+         cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL);
        if (!skip_prefix(sb->buf, tmpl.buf, &start))
                start = sb->buf;
        strbuf_release(&tmpl);
@@ -1557,7 +1557,7 @@ static int try_to_commit(struct repository *r,
 
        if (cleanup != COMMIT_MSG_CLEANUP_NONE)
                strbuf_stripspace(msg,
-                 cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
+                 cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_str : NULL);
        if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
                res = 1; /* run 'git commit' to display error message */
                goto out;
index a33aed6c07a1e7e1cd3204d012bed12ed9f2e06b..e9b6127e766d4cef9d868b1c6d90b705d5abea8c 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -1001,10 +1001,10 @@ static size_t cleanup(char *line, size_t len)
  *
  * If last line does not have a newline at the end, one is added.
  *
- * Pass a non-NUL comment_prefix to skip every line starting
+ * Pass a non-NULL comment_prefix to skip every line starting
  * with it.
  */
-void strbuf_stripspace(struct strbuf *sb, char comment_prefix)
+void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
 {
        size_t empties = 0;
        size_t i, j, len, newlen;
@@ -1018,7 +1018,7 @@ void strbuf_stripspace(struct strbuf *sb, char comment_prefix)
                len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
 
                if (comment_prefix && len &&
-                   sb->buf[i] == comment_prefix) {
+                   starts_with(sb->buf + i, comment_prefix)) {
                        newlen = 0;
                        continue;
                }
index 860fcec5fb545604f294e4dd48ce718e6280c214..dc4710adbb496a5765e560ceeb816601b7f0f722 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -513,11 +513,11 @@ int strbuf_getcwd(struct strbuf *sb);
 int strbuf_normalize_path(struct strbuf *sb);
 
 /**
- * Strip whitespace from a buffer. If comment_prefix is non-NUL,
+ * Strip whitespace from a buffer. If comment_prefix is non-NULL,
  * then lines beginning with that character are considered comments,
  * thus removed.
  */
-void strbuf_stripspace(struct strbuf *buf, char comment_prefix);
+void strbuf_stripspace(struct strbuf *buf, const char *comment_prefix);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {