From patchwork Thu Mar 7 09:15:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585227 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1398E839E3 for ; Thu, 7 Mar 2024 09:15:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709802950; cv=none; b=JpK85Rbc68JZjo+yM2i8MnhXyjA40Gvz11wQO+SOF+nRSzSqpXvAP02WfNrKw9lPaNooQGeHRNAVkqmP8F8mEXKudCD2Nh6bKUp57axzMqzrx4biBseRSmIObR5aPZjN30qzdZGDJPsoE5lBk9NbGRet6Og/Ce8kF57LjOEP1Xk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709802950; c=relaxed/simple; bh=WZwXQB5LdEQa9DMMM96SK3oQehHJk6F6W2dNg+gAsvI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PAFJ3sCojicSf1rVxoMHINiDQyDo5p8y8+CHHeykEke92teLsdyW1laIJCQ4OKF27QpiNuy7BLIE1OIp38Vk8pbskFnNQQCv9/FoF6XijSo+XG6ffT0sOqskVGXP2JRW4dJ+BRwTYRf9dF4I5l7P7CSpSKCaVRVJEiXUypoWXpo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6821 invoked by uid 109); 7 Mar 2024 09:15:48 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:15:48 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12530 invoked by uid 111); 7 Mar 2024 09:15:52 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:15:52 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:15:47 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 01/15] strbuf: simplify comment-handling in add_lines() helper Message-ID: <20240307091547.GA2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> In strbuf_add_commented_lines(), we prepare two strings with potential prefixes: one with just the comment char, and one with an additional space. In the add_lines() helper, we use the one without the extra space for blank lines or lines starting with a tab. While passing in two separate prefixes to the helper is very flexible, it's more flexibility than we actually use (or are likely to use, since the rules inside add_lines() only make sense if "prefix2" is a variant of "prefix1" without the extra space). And setting up the two strings makes refactoring in strbuf_add_commented_lines() awkward. Instead, let's pass in a single string, and just let add_lines() add the extra space to the result as appropriate. We do still need to pass in a flag to trigger this behavior. The helper is shared by strbuf_add_lines(), which passes in a NULL "prefix2" to inhibit this extra handling. Signed-off-by: Jeff King --- strbuf.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/strbuf.c b/strbuf.c index 7827178d8e..689d8acd5e 100644 --- a/strbuf.c +++ b/strbuf.c @@ -340,18 +340,17 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...) } static void add_lines(struct strbuf *out, - const char *prefix1, - const char *prefix2, - const char *buf, size_t size) + const char *prefix, + const char *buf, size_t size, + int space_after_prefix) { while (size) { - const char *prefix; const char *next = memchr(buf, '\n', size); next = next ? (next + 1) : (buf + size); - prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t')) - ? prefix2 : prefix1); strbuf_addstr(out, prefix); + if (space_after_prefix && buf[0] != '\n' && buf[0] != '\t') + strbuf_addch(out, ' '); strbuf_add(out, buf, next - buf); size -= next - buf; buf = next; @@ -362,14 +361,11 @@ static void add_lines(struct strbuf *out, void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size, char comment_line_char) { - static char prefix1[3]; - static char prefix2[2]; + static char prefix[2]; - if (prefix1[0] != comment_line_char) { - xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char); - xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char); - } - add_lines(out, prefix1, prefix2, buf, size); + if (prefix[0] != comment_line_char) + xsnprintf(prefix, sizeof(prefix), "%c", comment_line_char); + add_lines(out, prefix, buf, size, 1); } void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, @@ -750,7 +746,7 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) void strbuf_add_lines(struct strbuf *out, const char *prefix, const char *buf, size_t size) { - add_lines(out, prefix, NULL, buf, size); + add_lines(out, prefix, buf, size, 0); } void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s) From patchwork Thu Mar 7 09:16:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585229 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85EBE745FE for ; Thu, 7 Mar 2024 09:16:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803016; cv=none; b=dCO9r8Reab8SK8HMA2JvtSUnub++IRHnjdoXUlsy0KKE9JYz11ZZGHso8rxFhhEBXrNS/uqQWOidmuNkapATJmuYlrqCdUYsKvIulmSLd3HtP/3VBNfoaQaIVL7T8cXz62iLmTQ5Jd0BSeWaokVMHg3hm05iWiChCQw2tm7RShI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803016; c=relaxed/simple; bh=aXEB4GukL7MpNBxHsjGcuhkuWKjeN9GSO/o0embAoyk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OJJeBwNoOV6AqmBKgGV+PXY39m12KRbAYgyjSvbcR85WouOaPdrx1O+otml2J8l3kvR4uavlRQDygXJSgAAtBbyAWRLxQuiWK75ajNFoaZDV06ZVmKz7JjM0HhtWDY8JktaAHlQ2jR7aHcR0zwlty3VvCexzhicWWVexw4y9cz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6841 invoked by uid 109); 7 Mar 2024 09:16:53 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:16:53 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12547 invoked by uid 111); 7 Mar 2024 09:16:58 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:16:58 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:16:52 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 02/15] strbuf: avoid static variables in strbuf_add_commented_lines() Message-ID: <20240307091652.GB2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> In strbuf_add_commented_lines(), we have to convert the single-byte comment_line_char into a string to pass to add_lines(). We cache the created string using a static-local variable. But this makes the function non-reentrant, and it's doubtful that this provides any real performance benefit given that we know the string always contains a single character. So let's just create it from scratch each time, and to give the compiler the maximal opportunity to make it fast we'll ditch the over-complicated xsnprintf() and just assign directly into the array. Signed-off-by: Jeff King --- In the long run we'll end up just passing in the comment-string that the caller gives us, so this patch could arguably be dropped until that point. strbuf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strbuf.c b/strbuf.c index 689d8acd5e..ca80a2c77e 100644 --- a/strbuf.c +++ b/strbuf.c @@ -361,10 +361,10 @@ static void add_lines(struct strbuf *out, void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size, char comment_line_char) { - static char prefix[2]; + char prefix[2]; - if (prefix[0] != comment_line_char) - xsnprintf(prefix, sizeof(prefix), "%c", comment_line_char); + prefix[0] = comment_line_char; + prefix[1] = '\0'; add_lines(out, prefix, buf, size, 1); } From patchwork Thu Mar 7 09:18:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585230 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08BE482D99 for ; Thu, 7 Mar 2024 09:18:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803112; cv=none; b=R9qoDQgTbTFXODbPM2ComgPXOToVB0BXt7Ymp436sYThMdH7lQ69mgUtmlCzgK7NrAL0v24OJpSSwcmOQRcDcSX0ZlFgjUsYuXfjoIp0JTB5rNm+kE2LGIOjMx83Ju3qs6BzN+zs5gz0UrevEtnT0YuVRbh2w3qlZJELiOtvWyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803112; c=relaxed/simple; bh=qgQw+fB7qAXZZDiFpttQrbe5VHSSuiEsWorR/DoVs88=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OOIljqe8Wnq5uTWa6XNKpUlX/KIN5qKQNshNzF87xsz1sUrT9mvwo6HdX2gzxE54kgrkJ6XZCfoR76U3wxbdfORFmUxa6p0Z7M6di07bNN4mG6ExKbvz5U0sc3DxAQ0+LJTr7CCpZSblnkfxGhTWPWaWh+y5e5J2sC63LJ8CUis= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6862 invoked by uid 109); 7 Mar 2024 09:18:30 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:18:30 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12565 invoked by uid 111); 7 Mar 2024 09:18:34 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:18:34 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:18:29 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 03/15] commit: refactor base-case of adjust_comment_line_char() Message-ID: <20240307091829.GC2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> When core.commentChar is set to "auto", we check a set of candidate characters against the proposed buffer to see which if any can be used without ambiguity. But before we do that, we optimize for the common case that the default "#" is fine by just seeing if it is present in the buffer at all. The way we do this is a bit subtle, though: we assign the candidate character to comment_line_char preemptively, then check if it works, and return if it does. The subtle part is that sometimes setting comment_line_char is important (after we return, the important outcome is the fact that we have set the variable) and sometimes it is useless (if our optimization fails, we go on to do the more careful checks and eventually assign something else instead). To make it more clear what is happening (and to make further refactoring of comment_line_char easier), let's check our candidate character directly, and then assign as part of returning if it worked out. Signed-off-by: Jeff King --- builtin/commit.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 6d1fa71676..d496980421 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -684,9 +684,10 @@ static void adjust_comment_line_char(const struct strbuf *sb) char *candidate; const char *p; - comment_line_char = candidates[0]; - if (!memchr(sb->buf, comment_line_char, sb->len)) + if (!memchr(sb->buf, candidates[0], sb->len)) { + comment_line_char = candidates[0]; return; + } p = sb->buf; candidate = strchr(candidates, *p); From patchwork Thu Mar 7 09:19:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585231 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E53AA839E3 for ; Thu, 7 Mar 2024 09:19:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803165; cv=none; b=iXdQeLcAMbRQcjWLWwg0NJ7RC04h9wrqEuYuOgvIa9tfQFX7Pg33wurw8uRBxMg6XWB+eGc4nrbD3YxZuTY8rlS5ukyW7CtH2DTC3MahA4S/Vgb7cH5kzDaIkvzmcD1OqVmaRoBur1BDAsgbV+vFVz72Zwn8i0TWLnRaPDGFH+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803165; c=relaxed/simple; bh=XU73t0aytWrPkUc0m+/TL/QAtYhe0OToIAHc7v1fV+4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XoZ9wtno/wq85O70LLQ0TYWX1EkN7V2g3TvYv1od7NiO74N+bWm8redymAemVw+xWntzYRb8j5lx67Gs8PQyMp1l/bvslOC9CXkasy4S+zTH8f1sq702AMURxXa9p2yYdfplG9BEt1wZr5S2gE6Aifjp4v63+dnJcJmDAmLHvOo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6876 invoked by uid 109); 7 Mar 2024 09:19:23 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:19:23 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12572 invoked by uid 111); 7 Mar 2024 09:19:27 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:19:27 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:19:22 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 04/15] strbuf: avoid shadowing global comment_line_char name Message-ID: <20240307091922.GD2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> Several comment-related strbuf functions take a comment_line_char parameter. There's also a global comment_line_char variable, which is closely related (most callers pass it in as this parameter). Let's avoid shadowing the global name. This makes it more obvious that we're not using the global value, and it will be especially helpful as we refactor the global in future patches (in particular, any macro trickery wouldn't work because the preprocessor doesn't respect scope). We'll use "comment_prefix". That should be descriptive enough, and as a bonus is more neutral with respect to the "char" type (since we'll eventually swap it out for a string). Signed-off-by: Jeff King --- strbuf.c | 16 ++++++++-------- strbuf.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/strbuf.c b/strbuf.c index ca80a2c77e..a33aed6c07 100644 --- a/strbuf.c +++ b/strbuf.c @@ -359,16 +359,16 @@ static void add_lines(struct strbuf *out, } void strbuf_add_commented_lines(struct strbuf *out, const char *buf, - size_t size, char comment_line_char) + size_t size, char comment_prefix) { char prefix[2]; - prefix[0] = comment_line_char; + prefix[0] = comment_prefix; prefix[1] = '\0'; add_lines(out, prefix, buf, size, 1); } -void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, +void strbuf_commented_addf(struct strbuf *sb, char comment_prefix, const char *fmt, ...) { va_list params; @@ -379,7 +379,7 @@ void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, strbuf_vaddf(&buf, fmt, params); va_end(params); - strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char); + strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix); if (incomplete_line) sb->buf[--sb->len] = '\0'; @@ -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_line_char to skip every line starting + * Pass a non-NUL comment_prefix to skip every line starting * with it. */ -void strbuf_stripspace(struct strbuf *sb, char comment_line_char) +void strbuf_stripspace(struct strbuf *sb, char comment_prefix) { size_t empties = 0; size_t i, j, len, newlen; @@ -1017,8 +1017,8 @@ void strbuf_stripspace(struct strbuf *sb, char comment_line_char) eol = memchr(sb->buf + i, '\n', sb->len - i); len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; - if (comment_line_char && len && - sb->buf[i] == comment_line_char) { + if (comment_prefix && len && + sb->buf[i] == comment_prefix) { newlen = 0; continue; } diff --git a/strbuf.h b/strbuf.h index e959caca87..860fcec5fb 100644 --- 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_line_char); + char comment_prefix); /** @@ -379,7 +379,7 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...); * blank to the buffer. */ __attribute__((format (printf, 3, 4))) -void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...); +void strbuf_commented_addf(struct strbuf *sb, char comment_prefix, const char *fmt, ...); __attribute__((format (printf,2,0))) void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap); @@ -513,11 +513,11 @@ int strbuf_getcwd(struct strbuf *sb); int strbuf_normalize_path(struct strbuf *sb); /** - * Strip whitespace from a buffer. If comment_line_char is non-NUL, + * Strip whitespace from a buffer. If comment_prefix is non-NUL, * then lines beginning with that character are considered comments, * thus removed. */ -void strbuf_stripspace(struct strbuf *buf, char comment_line_char); +void strbuf_stripspace(struct strbuf *buf, char comment_prefix); static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) { From patchwork Thu Mar 7 09:20:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585232 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89C5582D83 for ; Thu, 7 Mar 2024 09:20:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803228; cv=none; b=Gy03UA/Ka+uxaDOr9V9pVj6zPfE4++zUyLKp5oQzgD7qRpD16rUmrXTvL73z6FXcuho48NlP/guQrxW4XYp9St8ibk+0ZvEk2HgM6ouJdfaHqz6rFSN2d0SD85PwkqcWlnqtV+abRGHmFG46mta1bYK8lbBAIDP2CL/T8ZVNr3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803228; c=relaxed/simple; bh=98qbewk4yJQQw7FLerZA4S0+D0u/XuiU1A5Fmw6oSvA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dsJFFfK0DECLEpoc43XBQRMs6kirouw1R9q0Wuhx2Q4q/GCTOgAwepenuDzsV73y1dgLGEXqdCRkgVv+pNJS6oTdidPFr71tcthYhOdu2eIYsMg+6FF/HhGuXIQTCPJmt76JI/KyaE64viX+U08rgbyXGvnO2KvJPDFHrnGTNAc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6897 invoked by uid 109); 7 Mar 2024 09:20:25 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:20:25 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12611 invoked by uid 111); 7 Mar 2024 09:20:30 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:20:30 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:20:24 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 05/15] environment: store comment_line_char as a string Message-ID: <20240307092024.GE2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> We'd like to eventually support multi-byte comment prefixes, but the comment_line_char variable is referenced in many spots, making the transition difficult. Let's start by storing the character in a NUL-terminated string. That will let us switch code over incrementally to the string format, and we can easily support the existing code with a macro wrapper (since we'll continue to allow only a single-byte prefix, this will behave identically). Once all references to the "char" variable have been converted, we can drop it and enable longer strings. We'll still have to touch all of the spots that create or set the variable in this patch, but there are only a few (reading the config, and the "auto" character selector). Signed-off-by: Jeff King --- builtin/commit.c | 4 ++-- config.c | 2 +- environment.c | 2 +- environment.h | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index d496980421..d8abbe48b1 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -685,7 +685,7 @@ static void adjust_comment_line_char(const struct strbuf *sb) const char *p; if (!memchr(sb->buf, candidates[0], sb->len)) { - comment_line_char = candidates[0]; + comment_line_str = xstrfmt("%c", candidates[0]); return; } @@ -706,7 +706,7 @@ static void adjust_comment_line_char(const struct strbuf *sb) if (!*p) die(_("unable to select a comment character that is not used\n" "in the current commit message")); - comment_line_char = *p; + comment_line_str = xstrfmt("%c", *p); } static void prepare_amend_commit(struct commit *commit, struct strbuf *sb, diff --git a/config.c b/config.c index 3cfeb3d8bd..e12ea68f24 100644 --- a/config.c +++ b/config.c @@ -1566,7 +1566,7 @@ static int git_default_core_config(const char *var, const char *value, else if (!strcasecmp(value, "auto")) auto_comment_line_char = 1; else if (value[0] && !value[1]) { - comment_line_char = value[0]; + comment_line_str = xstrfmt("%c", value[0]); auto_comment_line_char = 0; } else return error(_("core.commentChar should only be one ASCII character")); diff --git a/environment.c b/environment.c index 90632a39bc..0a9f5db407 100644 --- a/environment.c +++ b/environment.c @@ -110,7 +110,7 @@ int protect_ntfs = PROTECT_NTFS_DEFAULT; * The character that begins a commented line in user-editable file * that is subject to stripspace. */ -char comment_line_char = '#'; +const char *comment_line_str = "#"; int auto_comment_line_char; /* Parallel index stat data preload? */ diff --git a/environment.h b/environment.h index e5351c9dd9..3496474cce 100644 --- a/environment.h +++ b/environment.h @@ -8,7 +8,8 @@ struct strvec; * The character that begins a commented line in user-editable file * that is subject to stripspace. */ -extern char comment_line_char; +#define comment_line_char (comment_line_str[0]) +extern const char *comment_line_str; extern int auto_comment_line_char; /* From patchwork Thu Mar 7 09:21:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585246 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 011F583CA6 for ; Thu, 7 Mar 2024 09:21:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803289; cv=none; b=p1wY40+JsPw8z09/onhkRAAMStbBtFqZBq8voIYHMvlbPAj/pmPc0LEF9FZx5a93S8uhkkelgwc8N06y/9fzOkeBQkVMSzBHHRf2edOHkhILJP81/RHeo6qixCVAc+QHpxP9wU41ExknE3jgXre3MM2nRMES+Nzcu7XdGqqKQ4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803289; c=relaxed/simple; bh=Miqnq3yFK7cDbiF+dkXslX4a6cSUIZ9DjQqv6so46Mw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HEaHZ4JcclWb3Ev+i24LLacFR2PcO5yDrZGmqREDXGddJOAXavEHXn78KLLVCPATpYcnRfwOAvWLoCaTlDI+LQaTlWnIzSpxnu4SrDJZG2a32eDik5djUN9bIXU8N95ouJLsWC1LQ4KADvqomI8QAMkFMl8dXKoFusuOqJZQq2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6920 invoked by uid 109); 7 Mar 2024 09:21:27 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:21:27 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12621 invoked by uid 111); 7 Mar 2024 09:21:31 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:21:31 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:21:26 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 06/15] strbuf: accept a comment string for strbuf_stripspace() Message-ID: <20240307092126.GF2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> 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 --- builtin/branch.c | 2 +- builtin/notes.c | 2 +- builtin/rebase.c | 2 +- builtin/stripspace.c | 2 +- builtin/tag.c | 2 +- rebase-interactive.c | 2 +- sequencer.c | 6 +++--- strbuf.c | 6 +++--- strbuf.h | 4 ++-- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index cfb63cce5f..c03c0407d1 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -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) diff --git a/builtin/notes.c b/builtin/notes.c index caf20fd5bd..5223a3f350 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -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); } } diff --git a/builtin/rebase.c b/builtin/rebase.c index 6ead9465a4..bf78402129 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -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))) diff --git a/builtin/stripspace.c b/builtin/stripspace.c index 7b700a9fb1..434ac490cb 100644 --- a/builtin/stripspace.c +++ b/builtin/stripspace.c @@ -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); diff --git a/builtin/tag.c b/builtin/tag.c index 19a7e06bf4..07327d3c04 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -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?")); diff --git a/rebase-interactive.c b/rebase-interactive.c index d9718409b3..6dfc33e4e3 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -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; diff --git a/sequencer.c b/sequencer.c index f49a871ac0..6a1b7b200e 100644 --- a/sequencer.c +++ b/sequencer.c @@ -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; diff --git a/strbuf.c b/strbuf.c index a33aed6c07..e9b6127e76 100644 --- 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; } diff --git a/strbuf.h b/strbuf.h index 860fcec5fb..dc4710adbb 100644 --- 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) { From patchwork Thu Mar 7 09:22:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585247 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8592B77F32 for ; Thu, 7 Mar 2024 09:22:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803333; cv=none; b=NFnM0Xi2U+hWIaItEoVtCXf9j8y20Y5oEKa8sQU2rJjQJOekhXWXANYuHTlA3R5JVjJW68YBqTdp5M+fsk0UM7JoVp8eMvIa51Cb3OhUJGA681QgyCv3TbV7Nb+xaBSwDby5gRkPPh5bYoEnzGuop9xM34nZAK++urtMZcDD56o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803333; c=relaxed/simple; bh=2GKCY9AP/kCoFXmk2Y3s/y31IYN18lwAL16qOlK2X1g=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iN82wErvieygqTEqTLcNhMnKoYi5P/hJoeEaGBL6r3/RDBRsqzwgdTAYHdJIBHk9E3MnY9FfWS3zQqFiR6DDsqDBSemKW1ZhXOaY1a2e8UcJKureOu3HHvVOJHQEt6BTfHaihA4Kgskq+Pk52qPWAEDTVQ5JYzydgKPNdgXEtGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6935 invoked by uid 109); 7 Mar 2024 09:22:10 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:22:10 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12629 invoked by uid 111); 7 Mar 2024 09:22:14 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:22:14 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:22:09 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 07/15] strbuf: accept a comment string for strbuf_commented_addf() Message-ID: <20240307092209.GG2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> As part of our transition to multi-byte comment characters, let's take a NUL-terminated string pointer for strbuf_commented_addf() rather than a single character. All of the callers have to be adjusted, but they can just pass comment_line_str rather than comment_line_char. Note that we rely on strbuf_add_commented_lines() under the hood, so we'll cheat a bit to squeeze our string into a single character (for now the two are equivalent, and we'll address this TODO in the next patch). Signed-off-by: Jeff King --- add-patch.c | 8 ++++---- builtin/branch.c | 2 +- builtin/merge.c | 8 ++++---- builtin/tag.c | 4 ++-- rebase-interactive.c | 2 +- sequencer.c | 4 ++-- strbuf.c | 10 ++++++++-- strbuf.h | 2 +- wt-status.c | 2 +- 9 files changed, 24 insertions(+), 18 deletions(-) diff --git a/add-patch.c b/add-patch.c index 68f525b35c..7390677795 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1105,11 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) size_t i; strbuf_reset(&s->buf); - strbuf_commented_addf(&s->buf, comment_line_char, + strbuf_commented_addf(&s->buf, comment_line_str, _("Manual hunk edit mode -- see bottom for " "a quick guide.\n")); render_hunk(s, hunk, 0, 0, &s->buf); - strbuf_commented_addf(&s->buf, comment_line_char, + strbuf_commented_addf(&s->buf, comment_line_str, _("---\n" "To remove '%c' lines, make them ' ' lines " "(context).\n" @@ -1118,13 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) s->mode->is_reverse ? '+' : '-', s->mode->is_reverse ? '-' : '+', comment_line_char); - strbuf_commented_addf(&s->buf, comment_line_char, "%s", + strbuf_commented_addf(&s->buf, comment_line_str, "%s", _(s->mode->edit_hunk_hint)); /* * TRANSLATORS: 'it' refers to the patch mentioned in the previous * messages. */ - strbuf_commented_addf(&s->buf, comment_line_char, + strbuf_commented_addf(&s->buf, comment_line_str, _("If it does not apply cleanly, you will be " "given an opportunity to\n" "edit again. If all lines of the hunk are " diff --git a/builtin/branch.c b/builtin/branch.c index c03c0407d1..8904a1e5d9 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -667,7 +667,7 @@ static int edit_branch_description(const char *branch_name) exists = !read_branch_desc(&buf, branch_name); if (!buf.len || buf.buf[buf.len-1] != '\n') strbuf_addch(&buf, '\n'); - strbuf_commented_addf(&buf, comment_line_char, + strbuf_commented_addf(&buf, comment_line_str, _("Please edit the description for the branch\n" " %s\n" "Lines starting with '%c' will be stripped.\n"), diff --git a/builtin/merge.c b/builtin/merge.c index 935c8a57dd..6d048fb628 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -852,15 +852,15 @@ static void prepare_to_commit(struct commit_list *remoteheads) strbuf_addch(&msg, '\n'); if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { wt_status_append_cut_line(&msg); - strbuf_commented_addf(&msg, comment_line_char, "\n"); + strbuf_commented_addf(&msg, comment_line_str, "\n"); } - strbuf_commented_addf(&msg, comment_line_char, + strbuf_commented_addf(&msg, comment_line_str, _(merge_editor_comment)); if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) - strbuf_commented_addf(&msg, comment_line_char, + strbuf_commented_addf(&msg, comment_line_str, _(scissors_editor_comment)); else - strbuf_commented_addf(&msg, comment_line_char, + strbuf_commented_addf(&msg, comment_line_str, _(no_scissors_editor_comment), comment_line_char); } if (signoff) diff --git a/builtin/tag.c b/builtin/tag.c index 07327d3c04..1c708785bf 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -291,10 +291,10 @@ static void create_tag(const struct object_id *object, const char *object_ref, struct strbuf buf = STRBUF_INIT; strbuf_addch(&buf, '\n'); if (opt->cleanup_mode == CLEANUP_ALL) - strbuf_commented_addf(&buf, comment_line_char, + strbuf_commented_addf(&buf, comment_line_str, _(tag_template), tag, comment_line_char); else - strbuf_commented_addf(&buf, comment_line_char, + strbuf_commented_addf(&buf, comment_line_str, _(tag_template_nocleanup), tag, comment_line_char); write_or_die(fd, buf.buf, buf.len); strbuf_release(&buf); diff --git a/rebase-interactive.c b/rebase-interactive.c index 6dfc33e4e3..affc93a8e4 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -71,7 +71,7 @@ void append_todo_help(int command_count, if (!edit_todo) { strbuf_addch(buf, '\n'); - strbuf_commented_addf(buf, comment_line_char, + strbuf_commented_addf(buf, comment_line_str, Q_("Rebase %s onto %s (%d command)", "Rebase %s onto %s (%d commands)", command_count), diff --git a/sequencer.c b/sequencer.c index 6a1b7b200e..852c3f9f4e 100644 --- a/sequencer.c +++ b/sequencer.c @@ -667,11 +667,11 @@ void append_conflicts_hint(struct index_state *istate, } strbuf_addch(msgbuf, '\n'); - strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n"); + strbuf_commented_addf(msgbuf, comment_line_str, "Conflicts:\n"); for (i = 0; i < istate->cache_nr;) { const struct cache_entry *ce = istate->cache[i++]; if (ce_stage(ce)) { - strbuf_commented_addf(msgbuf, comment_line_char, + strbuf_commented_addf(msgbuf, comment_line_str, "\t%s\n", ce->name); while (i < istate->cache_nr && !strcmp(ce->name, istate->cache[i]->name)) diff --git a/strbuf.c b/strbuf.c index e9b6127e76..76d02e0920 100644 --- a/strbuf.c +++ b/strbuf.c @@ -368,7 +368,7 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, add_lines(out, prefix, buf, size, 1); } -void strbuf_commented_addf(struct strbuf *sb, char comment_prefix, +void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix, const char *fmt, ...) { va_list params; @@ -379,7 +379,13 @@ void strbuf_commented_addf(struct strbuf *sb, char comment_prefix, strbuf_vaddf(&buf, fmt, params); va_end(params); - strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix); + /* + * 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]); if (incomplete_line) sb->buf[--sb->len] = '\0'; diff --git a/strbuf.h b/strbuf.h index dc4710adbb..b128ca539a 100644 --- a/strbuf.h +++ b/strbuf.h @@ -379,7 +379,7 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...); * blank to the buffer. */ __attribute__((format (printf, 3, 4))) -void strbuf_commented_addf(struct strbuf *sb, char comment_prefix, const char *fmt, ...); +void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix, const char *fmt, ...); __attribute__((format (printf,2,0))) void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap); diff --git a/wt-status.c b/wt-status.c index b5a29083df..2be2eb094c 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1103,7 +1103,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_char, "%s", cut_line); + strbuf_commented_addf(buf, comment_line_str, "%s", cut_line); strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char); } From patchwork Thu Mar 7 09:23:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585248 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0DE482D7C for ; Thu, 7 Mar 2024 09:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803395; cv=none; b=g5n02dNmyOZgaRIhh3Th/ov/WTVeQ4o6DvMbG3iG95bPPodx0Sc9WK0ZF6HQlsAKsp+AA03ohqeSX9eXS9d66uQs5DcOZElnbr1eNP7fiua+XUwejxs5iSZc0DtJTmPSvHKKyQAlAut3iv12B4HtvZhkxDsBpi6CseTQIMiLka4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803395; c=relaxed/simple; bh=8bVLZk8lmn2ZVTwFtX+ylsp+7f2Vy4mssttsK5o05IA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FEYGubchhOQNiVxq7fapcqy/FB+5+zF2Bg1ExFTnlraoJWBU3oqE/Q8HTFq2HuOZD+puvcPzTB/8sXZcBPqG9MMRyeh21F441w/TqwUb46bC2DqJt3yuw3v7gv1Otepj9T9HRL382cith+qaKyWQpFzKOuUY/jKGe9jLjMqtiyM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6956 invoked by uid 109); 7 Mar 2024 09:23:13 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:23:13 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12636 invoked by uid 111); 7 Mar 2024 09:23:17 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:23:17 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:23:12 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 08/15] strbuf: accept a comment string for strbuf_add_commented_lines() Message-ID: <20240307092312.GH2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> 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 --- This could also be squashed into the previous patch. I wasn't sure if it would be more overwhelming to have so many changes intermingled, or if the "cheat" / "uncheat" back-and-forth would be too confusing. Pick your poison. builtin/notes.c | 8 ++++---- builtin/stripspace.c | 2 +- fmt-merge-msg.c | 6 +++--- rebase-interactive.c | 6 +++--- sequencer.c | 8 ++++---- strbuf.c | 16 +++------------- strbuf.h | 2 +- wt-status.c | 4 ++-- 8 files changed, 21 insertions(+), 31 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 5223a3f350..1a67f01d00 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -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); diff --git a/builtin/stripspace.c b/builtin/stripspace.c index 434ac490cb..e5626e5126 100644 --- a/builtin/stripspace.c +++ b/builtin/stripspace.c @@ -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); } diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index 66e47449a0..79e8aad086 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -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); diff --git a/rebase-interactive.c b/rebase-interactive.c index affc93a8e4..c343e16fcd 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -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, diff --git a/sequencer.c b/sequencer.c index 852c3f9f4e..032e213a3f 100644 --- a/sequencer.c +++ b/sequencer.c @@ -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); diff --git a/strbuf.c b/strbuf.c index 76d02e0920..7c8f582127 100644 --- 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'; diff --git a/strbuf.h b/strbuf.h index b128ca539a..58dddf2777 100644 --- 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); /** diff --git a/wt-status.c b/wt-status.c index 2be2eb094c..6b81f5349c 100644 --- a/wt-status.c +++ b/wt-status.c @@ -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) From patchwork Thu Mar 7 09:23:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585249 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E6FE7A14C for ; Thu, 7 Mar 2024 09:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803410; cv=none; b=qS2uJfE24Qb+QKpwStvob3RGzc6c8/vNEOGVmi3aLwhknK+LhrH2bOd98RiFkNE4lln1hKHUNV7gmMYF11EIByBtWOt/9Ne9ToTu5AJPXZninQyc0Y12Kh6+3OP37BGPFV8cWiBfvZVAMIuSdXFY3T1Ud2tH5kfNPQX7KucxPSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803410; c=relaxed/simple; bh=7K1sSiwpT4U0ZFyiKyXkKaj14z+Z52BPTCBLiHz7WEE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ZpBmSGqY30+j4dqwNnDA97YZ1tISRHvAjSufIA284Rvq6mlEBfat+hEEtQB79klSdKzZ0pQjdeQQzBUEMmPxSoS57p4L8V089D5y21xyyA3q2XeX3f+24TPsrFJqtJylE610NO/Z9JnNR4AgVlA2n4Uo8Nyi2S0WdgEjiscpj74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6971 invoked by uid 109); 7 Mar 2024 09:23:27 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:23:27 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12643 invoked by uid 111); 7 Mar 2024 09:23:31 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:23:31 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:23:26 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 09/15] prefer comment_line_str to comment_line_char for printing Message-ID: <20240307092326.GI2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> As part of our transition to multi-byte comment characters, we should use the string variable rather than the historical character variable. All of the sites adjusted here are just swapping out "%c" for "%s" in format strings, or strbuf_addch() for strbuf_addstr(). The type system and printf-attribute give the compiler enough information to make sure our formats and variable changes all match (especially important for cases where the format string is defined far away from its use, like prepare_to_commit() in commit.c). Signed-off-by: Jeff King --- add-patch.c | 4 ++-- builtin/branch.c | 4 ++-- builtin/commit.c | 12 ++++++------ builtin/merge.c | 4 ++-- builtin/tag.c | 8 ++++---- fmt-merge-msg.c | 2 +- sequencer.c | 20 ++++++++++---------- wt-status.c | 10 +++++----- 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/add-patch.c b/add-patch.c index 7390677795..4a10237d50 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1114,10 +1114,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) "To remove '%c' lines, make them ' ' lines " "(context).\n" "To remove '%c' lines, delete them.\n" - "Lines starting with %c will be removed.\n"), + "Lines starting with %s will be removed.\n"), s->mode->is_reverse ? '+' : '-', s->mode->is_reverse ? '-' : '+', - comment_line_char); + comment_line_str); strbuf_commented_addf(&s->buf, comment_line_str, "%s", _(s->mode->edit_hunk_hint)); /* diff --git a/builtin/branch.c b/builtin/branch.c index 8904a1e5d9..1cdcae8454 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -670,8 +670,8 @@ static int edit_branch_description(const char *branch_name) strbuf_commented_addf(&buf, comment_line_str, _("Please edit the description for the branch\n" " %s\n" - "Lines starting with '%c' will be stripped.\n"), - branch_name, comment_line_char); + "Lines starting with '%s' will be stripped.\n"), + branch_name, comment_line_str); write_file_buf(edit_description(), buf.buf, buf.len); strbuf_reset(&buf); if (launch_editor(edit_description(), &buf, NULL)) { diff --git a/builtin/commit.c b/builtin/commit.c index d8abbe48b1..8519a004d0 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -910,18 +910,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix, struct ident_split ci, ai; const char *hint_cleanup_all = allow_empty_message ? _("Please enter the commit message for your changes." - " Lines starting\nwith '%c' will be ignored.\n") : + " Lines starting\nwith '%s' will be ignored.\n") : _("Please enter the commit message for your changes." - " Lines starting\nwith '%c' will be ignored, and an empty" + " Lines starting\nwith '%s' will be ignored, and an empty" " message aborts the commit.\n"); const char *hint_cleanup_space = allow_empty_message ? _("Please enter the commit message for your changes." " Lines starting\n" - "with '%c' will be kept; you may remove them" + "with '%s' will be kept; you may remove them" " yourself if you want to.\n") : _("Please enter the commit message for your changes." " Lines starting\n" - "with '%c' will be kept; you may remove them" + "with '%s' will be kept; you may remove them" " yourself if you want to.\n" "An empty message aborts the commit.\n"); if (whence != FROM_COMMIT) { @@ -945,12 +945,12 @@ static int prepare_to_commit(const char *index_file, const char *prefix, fprintf(s->fp, "\n"); if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL) - status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char); + status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str); else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { if (whence == FROM_COMMIT && !merge_contains_scissors) wt_status_add_cut_line(s->fp); } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */ - status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char); + status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str); /* * These should never fail because they come from our own diff --git a/builtin/merge.c b/builtin/merge.c index 6d048fb628..ba4308883f 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -821,7 +821,7 @@ static const char scissors_editor_comment[] = N_("An empty message aborts the commit.\n"); static const char no_scissors_editor_comment[] = -N_("Lines starting with '%c' will be ignored, and an empty message aborts\n" +N_("Lines starting with '%s' will be ignored, and an empty message aborts\n" "the commit.\n"); static void write_merge_heads(struct commit_list *); @@ -861,7 +861,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) _(scissors_editor_comment)); else strbuf_commented_addf(&msg, comment_line_str, - _(no_scissors_editor_comment), comment_line_char); + _(no_scissors_editor_comment), comment_line_str); } if (signoff) append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0); diff --git a/builtin/tag.c b/builtin/tag.c index 1c708785bf..721d07a589 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -158,11 +158,11 @@ static int do_sign(struct strbuf *buffer) static const char tag_template[] = N_("\nWrite a message for tag:\n %s\n" - "Lines starting with '%c' will be ignored.\n"); + "Lines starting with '%s' will be ignored.\n"); static const char tag_template_nocleanup[] = N_("\nWrite a message for tag:\n %s\n" - "Lines starting with '%c' will be kept; you may remove them" + "Lines starting with '%s' will be kept; you may remove them" " yourself if you want to.\n"); static int git_tag_config(const char *var, const char *value, @@ -292,10 +292,10 @@ static void create_tag(const struct object_id *object, const char *object_ref, strbuf_addch(&buf, '\n'); if (opt->cleanup_mode == CLEANUP_ALL) strbuf_commented_addf(&buf, comment_line_str, - _(tag_template), tag, comment_line_char); + _(tag_template), tag, comment_line_str); else strbuf_commented_addf(&buf, comment_line_str, - _(tag_template_nocleanup), tag, comment_line_char); + _(tag_template_nocleanup), tag, comment_line_str); write_or_die(fd, buf.buf, buf.len); strbuf_release(&buf); } diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index 79e8aad086..ae201e21db 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -321,7 +321,7 @@ static void credit_people(struct strbuf *out, skip_prefix(me, them->items->string, &me) && starts_with(me, " <"))) return; - strbuf_addf(out, "\n%c %s ", comment_line_char, label); + strbuf_addf(out, "\n%s %s ", comment_line_str, label); add_people_count(out, them); } diff --git a/sequencer.c b/sequencer.c index 032e213a3f..241e185f87 100644 --- a/sequencer.c +++ b/sequencer.c @@ -663,7 +663,7 @@ void append_conflicts_hint(struct index_state *istate, if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { strbuf_addch(msgbuf, '\n'); wt_status_append_cut_line(msgbuf); - strbuf_addch(msgbuf, comment_line_char); + strbuf_addstr(msgbuf, comment_line_str); } strbuf_addch(msgbuf, '\n'); @@ -1946,7 +1946,7 @@ static int append_squash_message(struct strbuf *buf, const char *body, (starts_with(body, "squash!") || starts_with(body, "fixup!")))) commented_len = commit_subject_length(body); - strbuf_addf(buf, "\n%c ", comment_line_char); + strbuf_addf(buf, "\n%s ", comment_line_str); strbuf_addf(buf, _(nth_commit_msg_fmt), ++opts->current_fixup_count + 1); strbuf_addstr(buf, "\n\n"); @@ -2006,7 +2006,7 @@ static int update_squash_messages(struct repository *r, eol = buf.buf[0] != comment_line_char ? buf.buf : strchrnul(buf.buf, '\n'); - strbuf_addf(&header, "%c ", comment_line_char); + strbuf_addf(&header, "%s ", comment_line_str); strbuf_addf(&header, _(combined_commit_msg_fmt), opts->current_fixup_count + 2); strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); @@ -2032,9 +2032,9 @@ static int update_squash_messages(struct repository *r, repo_unuse_commit_buffer(r, head_commit, head_message); return error(_("cannot write '%s'"), rebase_path_fixup_msg()); } - strbuf_addf(&buf, "%c ", comment_line_char); + strbuf_addf(&buf, "%s ", comment_line_str); strbuf_addf(&buf, _(combined_commit_msg_fmt), 2); - strbuf_addf(&buf, "\n%c ", comment_line_char); + strbuf_addf(&buf, "\n%s ", comment_line_str); strbuf_addstr(&buf, is_fixup_flag(command, flag) ? _(skip_first_commit_msg_str) : _(first_commit_msg_str)); @@ -2056,7 +2056,7 @@ static int update_squash_messages(struct repository *r, if (command == TODO_SQUASH || is_fixup_flag(command, flag)) { res = append_squash_message(&buf, body, command, opts, flag); } else if (command == TODO_FIXUP) { - strbuf_addf(&buf, "\n%c ", comment_line_char); + strbuf_addf(&buf, "\n%s ", comment_line_str); strbuf_addf(&buf, _(skip_nth_commit_msg_fmt), ++opts->current_fixup_count + 1); strbuf_addstr(&buf, "\n\n"); @@ -5659,8 +5659,8 @@ static int make_script_with_merges(struct pretty_print_context *pp, oid_to_hex(&commit->object.oid), oneline.buf); if (is_empty) - strbuf_addf(&buf, " %c empty", - comment_line_char); + strbuf_addf(&buf, " %s empty", + comment_line_str); FLEX_ALLOC_STR(entry, string, buf.buf); oidcpy(&entry->entry.oid, &commit->object.oid); @@ -5750,7 +5750,7 @@ static int make_script_with_merges(struct pretty_print_context *pp, entry = oidmap_get(&state.commit2label, &commit->object.oid); if (entry) - strbuf_addf(out, "\n%c Branch %s\n", comment_line_char, entry->string); + strbuf_addf(out, "\n%s Branch %s\n", comment_line_str, entry->string); else strbuf_addch(out, '\n'); @@ -5887,7 +5887,7 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, oid_to_hex(&commit->object.oid)); pretty_print_commit(&pp, commit, out); if (is_empty) - strbuf_addf(out, " %c empty", comment_line_char); + strbuf_addf(out, " %s empty", comment_line_str); strbuf_addch(out, '\n'); } if (skipped_commit) diff --git a/wt-status.c b/wt-status.c index 6b81f5349c..b66c30775b 100644 --- a/wt-status.c +++ b/wt-status.c @@ -70,7 +70,7 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color, strbuf_vaddf(&sb, fmt, ap); if (!sb.len) { if (s->display_comment_prefix) { - strbuf_addch(&sb, comment_line_char); + strbuf_addstr(&sb, comment_line_str); if (!trail) strbuf_addch(&sb, ' '); } @@ -85,7 +85,7 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color, strbuf_reset(&linebuf); if (at_bol && s->display_comment_prefix) { - strbuf_addch(&linebuf, comment_line_char); + strbuf_addstr(&linebuf, comment_line_str); if (*line != '\n' && *line != '\t') strbuf_addch(&linebuf, ' '); } @@ -1090,7 +1090,7 @@ size_t wt_status_locate_end(const char *s, size_t len) const char *p; struct strbuf pattern = STRBUF_INIT; - strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line); + strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line); if (starts_with(s, pattern.buf + 1)) len = 0; else if ((p = strstr(s, pattern.buf))) @@ -1214,8 +1214,8 @@ static void wt_longstatus_print_tracking(struct wt_status *s) "%s%.*s", comment_line_string, (int)(ep - cp), cp); if (s->display_comment_prefix) - color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c", - comment_line_char); + color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s", + comment_line_str); else fputs("\n", s->fp); strbuf_release(&sb); From patchwork Thu Mar 7 09:24:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585250 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7068183A01 for ; Thu, 7 Mar 2024 09:24:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803460; cv=none; b=WSDTxgqwQ+pg2u1tIE+Ndgh2V+2jTc6JBig3K2Rx4xONaLqSMm2kputf2xNiqr4aHYWqj1gIAt+7HX9UsOsPFJkIm+hHEb+VGS344exkblB0TFV8Wojf2m9FrLVJQLJFMEKTH31l7UsPsfmAubtqf4Z4JP8oijiS6GjhIoJzm7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803460; c=relaxed/simple; bh=pp9ZP8ntWbKCVIScueQ85+Nxx3QM8WrJ64rzCW/qB/s=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=P6qFdN7Ec1c1fvl0LePmUkyKAScTQ+7K2JTQhAeAQMw0RN8P/geuFT5QIJN5KWBcWN/soB1KoHN8l4ZI0a8S8lRwztgOJtLaFe/CJL9dRqApdgvUgUy7IOqBvMyJ51XbRmRq5LheSMfVl5fBbqy1/vKmXF5PXBkQPozAP8yzSJc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 6995 invoked by uid 109); 7 Mar 2024 09:24:17 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:24:17 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12651 invoked by uid 111); 7 Mar 2024 09:24:22 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:24:21 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:24:16 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 10/15] find multi-byte comment chars in NUL-terminated strings Message-ID: <20240307092416.GJ2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> Several parts of the code need to identify lines that begin with the comment character, and do so with a simple byte equality check. As part of the transition to handling multi-byte characters, we need to match all of the bytes. For cases where we are looking in a NUL-terminated string, we can just use starts_with(), which checks all of the characters in comment_line_str. Note that we can drop the "line.len" check in wt-status.c's read_rebase_todolist(). The starts_with() function handles the case of an empty haystack buffer (it will always return false for a non-empty prefix). Signed-off-by: Jeff King --- I think the main way these hunks could be wrong is if the buffer is not in fact NUL-terminated. In most cases we're working with a strbuf, though. add-patch.c | 2 +- sequencer.c | 2 +- trailer.c | 2 +- wt-status.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/add-patch.c b/add-patch.c index 4a10237d50..d599ca53e1 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1139,7 +1139,7 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) for (i = 0; i < s->buf.len; ) { size_t next = find_next_line(&s->buf, i); - if (s->buf.buf[i] != comment_line_char) + if (!starts_with(s->buf.buf + i, comment_line_str)) strbuf_add(&s->plain, s->buf.buf + i, next - i); i = next; } diff --git a/sequencer.c b/sequencer.c index 241e185f87..991a2dbe96 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2003,7 +2003,7 @@ static int update_squash_messages(struct repository *r, return error(_("could not read '%s'"), rebase_path_squash_msg()); - eol = buf.buf[0] != comment_line_char ? + eol = !starts_with(buf.buf, comment_line_str) ? buf.buf : strchrnul(buf.buf, '\n'); strbuf_addf(&header, "%s ", comment_line_str); diff --git a/trailer.c b/trailer.c index ef9df4af55..fe18faf6c5 100644 --- a/trailer.c +++ b/trailer.c @@ -1013,7 +1013,7 @@ static void parse_trailers(struct trailer_info *info, for (i = 0; i < info->trailer_nr; i++) { int separator_pos; char *trailer = info->trailers[i]; - if (trailer[0] == comment_line_char) + if (starts_with(trailer, comment_line_str)) continue; separator_pos = find_separator(trailer, separators); if (separator_pos >= 1) { diff --git a/wt-status.c b/wt-status.c index b66c30775b..084bfc584f 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1382,7 +1382,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines) git_path("%s", fname)); } while (!strbuf_getline_lf(&line, f)) { - if (line.len && line.buf[0] == comment_line_char) + if (starts_with(line.buf, comment_line_str)) continue; strbuf_trim(&line); if (!line.len) From patchwork Thu Mar 7 09:26:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585253 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B37F463409 for ; Thu, 7 Mar 2024 09:26:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803601; cv=none; b=cbb9TPp4QCPMITsVEbuPt5/uVN/AVFJDiC0LMBc3dLjourv0ab9ortVlFMdpEDfza/5GiL/4LonqQg4fx7/C+kqm7GfldhEfcGlQacuwNXswfg+sooxcmYtCaOvWAc5+ey+nUoU4K/PitP44E3SGB8k3MTaJB7sQa/sesOmLxMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803601; c=relaxed/simple; bh=2AtI8LQdQY2VP8k2/VQY1qOwqY415H/HnQrNlgUDHvU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rgNCFP5Eci7U+fn3aMUeob5Bz6yyMwfbRftBUpZ6JbVpoaDQGCEb4XeJDHaodPcgNeAKhtunrOlQO0ozHczyV44v4GrIuO/6wsvymTmmk85FRfwFTXLXH0utjK+hh/yFQ/KaHx3gbc9KpbfMGTt8ex3yEw5Kk+PEI+GdcizJRCE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 7015 invoked by uid 109); 7 Mar 2024 09:26:39 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:26:39 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12710 invoked by uid 111); 7 Mar 2024 09:26:43 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:26:43 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:26:38 -0500 From: Jeff King To: git@vger.kernel.org Cc: =?utf-8?b?UmVuw6k=?= Scharfe , Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 11/15] find multi-byte comment chars in unterminated buffers Message-ID: <20240307092638.GK2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> As with the previous patch, we need to swap out single-byte matching for something like starts_with() to match all bytes of a multi-byte comment character. But for cases where the buffer is not NUL-terminated (and we instead have an explicit size or end pointer), it's not safe to use starts_with(), as it might walk off the end of the buffer. Let's introduce a new starts_with_mem() that does the same thing but also accepts the length of the "haystack" str and makes sure not to walk past it. Note that in most cases the existing code did not need a length check at all, since it was written in a way that knew we had at least one byte available (and that was all we checked). So I had to read each one to find the appropriate bounds. The one exception is sequencer.c's add_commented_lines(), where we can actually get rid of the length check. Just like starts_with(), our starts_with_mem() handles an empty haystack variable by not matching (assuming a non-empty prefix). A few notes on the implementation of starts_with_mem(): - it would be equally correct to take an "end" pointer (and indeed, many of the callers have this and have to subtract to come up with the length). I think taking a ptr/size combo is a more usual interface for our codebase, though, and has the added benefit that the function signature makes it harder to mix up the three parameters. - we could obviously build starts_with() on top of this by passing strlen(str) as the length. But it's possible that starts_with() is a relatively hot code path, and it should not pay that penalty (it can generally return an answer proportional to the size of the prefix, not the whole string). - it naively feels like xstrncmpz() should be able to do the same thing, but that's not quite true. If you pass the length of the haystack buffer, then strncmp() finds that a shorter prefix string is "less than" than the haystack, even if the haystack starts with the prefix. If you pass the length of the prefix, then you risk reading past the end of the haystack if it is shorter than the prefix. So I think we really do need a new function. Signed-off-by: Jeff King --- Arguably starts_with() and this new function should both be inlined, like we do for skip_prefix(), but I think that's out of scope for this series. And it's possible I was simply too dumb to figure out xstrncmpz() here. I'm waiting for René to show up and tell me how to do it. ;) IMHO this is the trickiest commit of the whole series, as it would be easy to get the length computations subtly wrong. commit.c | 3 ++- sequencer.c | 4 ++-- strbuf.c | 11 +++++++++++ strbuf.h | 1 + trailer.c | 4 ++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/commit.c b/commit.c index ef679a0b93..531a666cba 100644 --- a/commit.c +++ b/commit.c @@ -1796,7 +1796,8 @@ size_t ignored_log_message_bytes(const char *buf, size_t len) else next_line++; - if (buf[bol] == comment_line_char || buf[bol] == '\n') { + if (starts_with_mem(buf + bol, cutoff - bol, comment_line_str) || + buf[bol] == '\n') { /* is this the first of the run of comments? */ if (!boc) boc = bol; diff --git a/sequencer.c b/sequencer.c index 991a2dbe96..664986e3b2 100644 --- a/sequencer.c +++ b/sequencer.c @@ -1840,7 +1840,7 @@ static int is_fixup_flag(enum todo_command command, unsigned flag) static void add_commented_lines(struct strbuf *buf, const void *str, size_t len) { const char *s = str; - while (len > 0 && s[0] == comment_line_char) { + while (starts_with_mem(s, len, comment_line_str)) { size_t count; const char *n = memchr(s, '\n', len); if (!n) @@ -2562,7 +2562,7 @@ static int parse_insn_line(struct repository *r, struct todo_item *item, /* left-trim */ bol += strspn(bol, " \t"); - if (bol == eol || *bol == '\r' || *bol == comment_line_char) { + if (bol == eol || *bol == '\r' || starts_with_mem(bol, eol - bol, comment_line_str)) { item->command = TODO_COMMENT; item->commit = NULL; item->arg_offset = bol - buf; diff --git a/strbuf.c b/strbuf.c index 7c8f582127..291bdc2a65 100644 --- a/strbuf.c +++ b/strbuf.c @@ -24,6 +24,17 @@ int istarts_with(const char *str, const char *prefix) return 0; } +int starts_with_mem(const char *str, size_t len, const char *prefix) +{ + const char *end = str + len; + for (; ; str++, prefix++) { + if (!*prefix) + return 1; + else if (str == end || *str != *prefix) + return 0; + } +} + int skip_to_optional_arg_default(const char *str, const char *prefix, const char **arg, const char *def) { diff --git a/strbuf.h b/strbuf.h index 58dddf2777..3156d6ea8c 100644 --- a/strbuf.h +++ b/strbuf.h @@ -673,6 +673,7 @@ char *xstrfmt(const char *fmt, ...); int starts_with(const char *str, const char *prefix); int istarts_with(const char *str, const char *prefix); +int starts_with_mem(const char *str, size_t len, const char *prefix); /* * If the string "str" is the same as the string in "prefix", then the "arg" diff --git a/trailer.c b/trailer.c index fe18faf6c5..f59c90b4b5 100644 --- a/trailer.c +++ b/trailer.c @@ -882,7 +882,7 @@ static size_t find_trailer_block_start(const char *buf, size_t len) /* The first paragraph is the title and cannot be trailers */ for (s = buf; s < buf + len; s = next_line(s)) { - if (s[0] == comment_line_char) + if (starts_with_mem(s, buf + len - s, comment_line_str)) continue; if (is_blank_line(s)) break; @@ -902,7 +902,7 @@ static size_t find_trailer_block_start(const char *buf, size_t len) const char **p; ssize_t separator_pos; - if (bol[0] == comment_line_char) { + if (starts_with_mem(bol, buf + end_of_title - bol, comment_line_str)) { non_trailer_lines += possible_continuation_lines; possible_continuation_lines = 0; continue; From patchwork Thu Mar 7 09:27:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585254 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABFA1224CE for ; Thu, 7 Mar 2024 09:27:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803671; cv=none; b=ZmpnJGbj7PcQ+wVvyCGrYpEfOOrm1La361RN35b1Gv3jK/1HwthP6qZZ3LW/V5KYqcHFuZ8kYCPYmYVs4kwCvxUgJx7Qg7IMFhdUp2oK0112OHG2NgbNJakpsyilfaoeeK0VRgORHkPLcEEfAHFO6kvktLhwM6hBg7CR2i7oPW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803671; c=relaxed/simple; bh=QfEZIegp3HP32a7NJYKye70G8SiE56O+uklQvk79zzo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TrdKpJdZ5HDoDELIaczJz6X6IC1luDXoLF/F3QedQ3KN400yqjOQWkTmvgDowGJmJgqg7zwVF6DBhvr1oD4B34/GbDXoV0qJdxjevhFRgxajJeYlOG7eUT4iDL9G+PWGEcMmwounsfj2bo3R7UZYc3I5LVp/K9hD2G/hduETW5g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 7039 invoked by uid 109); 7 Mar 2024 09:27:48 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:27:48 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12719 invoked by uid 111); 7 Mar 2024 09:27:53 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:27:53 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:27:47 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 12/15] sequencer: handle multi-byte comment characters when writing todo list Message-ID: <20240307092747.GL2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> We already match multi-byte comment characters in parse_insn_line(), thanks to the previous commit, yielding a TODO_COMMENT entry. But in todo_list_to_strbuf(), we may call command_to_char() to convert that back into something we can output. We can't just return comment_line_char anymore, since it may require multiple bytes. Instead, we'll return "0" for this case, which is the same thing we'd return for a command which does not have a single-letter abbreviation (e.g., "revert" or "noop"). In that case the caller then falls back to outputting the full name via command_to_string(). So we can handle TODO_COMMENT there, returning the full string. Note that there are many other callers of command_to_string(), which will now behave differently if they pass TODO_COMMENT. But we would not expect that to happen; prior to this commit, the function just calls die() in this case. And looking at those callers, that makes sense; e.g., do_pick_commit() will only be called when servicing a pick command, and should never be called for a comment in the first place. Signed-off-by: Jeff King --- sequencer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sequencer.c b/sequencer.c index 664986e3b2..9e2851428b 100644 --- a/sequencer.c +++ b/sequencer.c @@ -1779,14 +1779,16 @@ static const char *command_to_string(const enum todo_command command) { if (command < TODO_COMMENT) return todo_command_info[command].str; + if (command == TODO_COMMENT) + return comment_line_str; die(_("unknown command: %d"), command); } static char command_to_char(const enum todo_command command) { if (command < TODO_COMMENT) return todo_command_info[command].c; - return comment_line_char; + return 0; } static int is_noop(const enum todo_command command) From patchwork Thu Mar 7 09:28:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585255 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D683B76C8D for ; Thu, 7 Mar 2024 09:28:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803715; cv=none; b=h8o3ANm5L+U8oxgDOmzHA9TkbGAHNVxB+9CjmxHVZr++j4FL30GjCulKu/NQ43kcVUy5XJV/N2OV7ST6uSSwoDtspX0S6PXL7438Tz2DKxnbDb4wisKUJsO0qlBiOi85/76pftRuZWCKWWeaOFuwrDfysDU2qVnA1C49TMzW8ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803715; c=relaxed/simple; bh=OR6hKLTAS2an3PtrDNC/XwutHWUFLLezfxlIQVF/V+Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=SBUqwcHQjcRE06/EXndUYESA6XRYWolZ9u21kZdoV3WpvwiZjHGZih3t2aoRw5X1rkCPW6HwrDQp5zgZjUAVIUQL/XvzhI7KB9ayQbdJ0K6lSaDY+i7NEy2bcg+qKu+F98lDoMhzS2lIY+4Oij26no7MbqrpVWg7XfhNfCBeNQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 7051 invoked by uid 109); 7 Mar 2024 09:28:33 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:28:33 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12726 invoked by uid 111); 7 Mar 2024 09:28:37 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:28:37 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:28:32 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 13/15] wt-status: drop custom comment-char stringification Message-ID: <20240307092832.GM2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> In wt_longstatus_print_tracking() we may conditionally show a comment prefix based on the wt_status->display_comment_prefix flag. We handle that by creating a local "comment_line_string" that is either the empty string or the comment character followed by a space. For a single-byte comment, the maximum length of this string is 2 (plus a NUL byte). But to handle multi-byte comment characters, it can be arbitrarily large. One way to handle this is to just call xstrfmt("%s ", comment_line_str), and then free it when we're done. But we can simplify things further by just conditionally switching between our prefix string and an empty string when formatting. We couldn't just do that with the previous code, because the comment character was a single byte. There's no way to have a "%c" format switch between some character and "no character at all". Whereas with "%s" you can switch between some string and the empty string. So now that we have a comment string and not a comment char, we can just use it directly when formatting. Do note that we have to also conditionally add the trailing space at the same time. Signed-off-by: Jeff King --- I had hoped to clean this up as a preparatory commit, but it really is awkward until we can make use of comment_line_str, for the reasons given above. wt-status.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/wt-status.c b/wt-status.c index 084bfc584f..823e8e81b0 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1176,8 +1176,6 @@ static void wt_longstatus_print_tracking(struct wt_status *s) struct strbuf sb = STRBUF_INIT; const char *cp, *ep, *branch_name; struct branch *branch; - char comment_line_string[3]; - int i; uint64_t t_begin = 0; assert(s->branch && !s->is_initial); @@ -1202,16 +1200,11 @@ static void wt_longstatus_print_tracking(struct wt_status *s) } } - i = 0; - if (s->display_comment_prefix) { - comment_line_string[i++] = comment_line_char; - comment_line_string[i++] = ' '; - } - comment_line_string[i] = '\0'; - for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), - "%s%.*s", comment_line_string, + "%s%s%.*s", + s->display_comment_prefix ? comment_line_str : "", + s->display_comment_prefix ? " " : "", (int)(ep - cp), cp); if (s->display_comment_prefix) color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s", From patchwork Thu Mar 7 09:30:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585256 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B12982D8F for ; Thu, 7 Mar 2024 09:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803808; cv=none; b=P7NVb4DwZ600tVoi8T7ACs3pN2VvqyUiza0uJ/1aszWCRgS41CO8qhsUwjVONrRrNqryVy1sv39PooNEQEgVgu4iy7ogc4cBJokvcPTRvHtqN2st/flcByFQu53vSoQtlhDagz0ccKwbz1f9jzpbOpjv+0OqAojAQbE0sfkN8R8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803808; c=relaxed/simple; bh=7adLsxql+gbei+StrLQJfoBHp+rDPeG+wWsHgOqvqBU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=t0NiRGpWwccEJW438bh4A37KKo3FdSQOn5B+TX8VfctHh3tPpdhJ0d/iyANwZW7xEi2vqVXnZD0P5vNf98sl6V1Fvs5Om1APgiLv1v2VvoHYxt450uvvcuj/HO/vGKsT1pT1bnLjJvmBerQHOStDwQX6jcGqtPU9NrMPDPEBIKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 7074 invoked by uid 109); 7 Mar 2024 09:30:06 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:30:06 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12765 invoked by uid 111); 7 Mar 2024 09:30:10 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:30:10 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:30:05 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 14/15] environment: drop comment_line_char compatibility macro Message-ID: <20240307093005.GN2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> There is no longer any code which references the single-byte comment_line_char. Let's drop it, clearing the way for true multi-byte entries in comment_line_str. It's possible there are topics in flight that have added new references to comment_line_char. But we would prefer to fail compilation (and then fix it) upon merging with this, rather than have them quietly ignore the bytes after the first. Signed-off-by: Jeff King --- I did merge against 'next' and there are no such topics. And likewise "log -Scomment_line_char next..seen" shows nothing. But as somebody who maintained a long-running fork for many years, who knows what people are carrying in their private trees. ;) environment.h | 1 - 1 file changed, 1 deletion(-) diff --git a/environment.h b/environment.h index 3496474cce..a8b06674eb 100644 --- a/environment.h +++ b/environment.h @@ -8,7 +8,6 @@ struct strvec; * The character that begins a commented line in user-editable file * that is subject to stripspace. */ -#define comment_line_char (comment_line_str[0]) extern const char *comment_line_str; extern int auto_comment_line_char; From patchwork Thu Mar 7 09:34:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff King X-Patchwork-Id: 13585257 Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4213882D80 for ; Thu, 7 Mar 2024 09:34:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.231.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709804074; cv=none; b=IHUTNX83ckNtKqJJFhqMwLGIFX/VitSIEfN9EYPeK+H1PIDiK15GBo1k7tqNNPwr2hara06Mt3yTlP3tp7jHEe5pwfVV14gcavm4bxN6IVfMuYPcdjSWfvJL1U5OfiRxfdZYak9IpHMSb0xH6uvUnl/osyhDKj2XSzu2XX683cA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709804074; c=relaxed/simple; bh=/Ba/CJnRjD6xbO3Rp7enVJMnM/3qiiQdsIop7T2dX7Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ubqQPCQb68HpfcMYT+OrFXcikoXRe3igDJF+Ybhze558SRAxpdU3lTYAKlV2hLd1F4ScBxcwP3KqFivwT9WN0qjcHMsjq8RHTjeO0MGysaPMEZaCQ/VqzRhGDEcrRaMcPZVc3hWUReLf28RmOuU9KDJ803InSwNB6/GGCHBOsIU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net; spf=pass smtp.mailfrom=peff.net; arc=none smtp.client-ip=104.130.231.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=peff.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peff.net Received: (qmail 7101 invoked by uid 109); 7 Mar 2024 09:34:32 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Thu, 07 Mar 2024 09:34:32 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 12778 invoked by uid 111); 7 Mar 2024 09:34:36 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Thu, 07 Mar 2024 04:34:36 -0500 Authentication-Results: peff.net; auth=none Date: Thu, 7 Mar 2024 04:34:31 -0500 From: Jeff King To: git@vger.kernel.org Cc: Junio C Hamano , Dragan Simic , Kristoffer Haugsbakk , Manlio Perillo Subject: [PATCH 15/15] config: allow multi-byte core.commentChar Message-ID: <20240307093431.GO2080210@coredump.intra.peff.net> References: <20240307091407.GA2072522@coredump.intra.peff.net> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240307091407.GA2072522@coredump.intra.peff.net> Now that all of the code handles multi-byte comment characters, it's safe to allow users to set them. There is one special case I kept: we still will not allow an empty string for the commentChar. While it might make sense in some contexts (e.g., output where you don't want any comment prefix), there are plenty where it will behave badly (e.g., all of our starts_with() checks will indicate that every line is a comment!). It might be reasonable to assign some meaningful semantics, but it would probably involve checking how each site behaves. In the interim let's forbid it and we can loosen things later. Since comment_line_str is used in many parts of the code, it's hard to cover all possibilities with tests. We can convert the existing double-semicolon prefix test to show that "git status" works. And we'll give it a more challenging case in t7507, where we confirm that git-commit strips out the commit template along with any --verbose text when reading the edited commit message back in. That covers the basics, though it's possible there could be issues in more exotic spots (e.g., the sequencer todo list uses its own code). Signed-off-by: Jeff King --- Obviously everything works using the "str" variant with a single character, and many tests are already covering that. You can swap out the default to "foo>" or something and run the test suite, but there are many spots that hard-code "#" in their expectations. I do think it's an acceptable risk, though; for the most part you'd only find new bugs if you set a multi-byte core.commentChar, which was simply not allowed before. So we're more likely to see bugs in the new feature than regression of existing cases. Documentation/config/core.txt | 4 +++- config.c | 6 +++--- t/t0030-stripspace.sh | 5 +++++ t/t7507-commit-verbose.sh | 10 ++++++++++ t/t7508-status.sh | 4 +++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Documentation/config/core.txt b/Documentation/config/core.txt index 0e8c2832bf..c86b8c8408 100644 --- a/Documentation/config/core.txt +++ b/Documentation/config/core.txt @@ -523,7 +523,9 @@ core.commentChar:: Commands such as `commit` and `tag` that let you edit messages consider a line that begins with this character commented, and removes them after the editor returns - (default '#'). + (default '#'). Note that this option can take values larger than + a byte (whether a single multi-byte character, or you + could even go wild with a multi-character sequence). + If set to "auto", `git-commit` would select a character that is not the beginning character of any line in existing commit messages. diff --git a/config.c b/config.c index e12ea68f24..4dea34936c 100644 --- a/config.c +++ b/config.c @@ -1565,11 +1565,11 @@ static int git_default_core_config(const char *var, const char *value, return config_error_nonbool(var); else if (!strcasecmp(value, "auto")) auto_comment_line_char = 1; - else if (value[0] && !value[1]) { - comment_line_str = xstrfmt("%c", value[0]); + else if (value[0]) { + comment_line_str = xstrdup(value); auto_comment_line_char = 0; } else - return error(_("core.commentChar should only be one ASCII character")); + return error(_("core.commentChar must have at least one character")); return 0; } diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh index d1b3be8725..9cdf2bddbd 100755 --- a/t/t0030-stripspace.sh +++ b/t/t0030-stripspace.sh @@ -401,6 +401,11 @@ test_expect_success 'strip comments with changed comment char' ' test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)" ' +test_expect_success 'empty commentchar is forbidden' ' + test_must_fail git -c core.commentchar= stripspace -s 2>err && + grep "core.commentChar must have at least one character" err +' + test_expect_success '-c with single line' ' printf "# foo\n" >expect && printf "foo" | git stripspace -c >actual && diff --git a/t/t7507-commit-verbose.sh b/t/t7507-commit-verbose.sh index c3281b192e..4c7db19ce7 100755 --- a/t/t7507-commit-verbose.sh +++ b/t/t7507-commit-verbose.sh @@ -101,6 +101,16 @@ test_expect_success 'verbose diff is stripped out with set core.commentChar' ' test_grep "Aborting commit due to empty commit message." err ' +test_expect_success 'verbose diff is stripped with multi-byte comment char' ' + ( + GIT_EDITOR=cat && + export GIT_EDITOR && + test_must_fail git -c core.commentchar="foo>" commit -a -v >out 2>err + ) && + grep "^foo> " out && + test_grep "Aborting commit due to empty commit message." err +' + test_expect_success 'status does not verbose without --verbose' ' git status >actual && ! grep "^diff --git" actual diff --git a/t/t7508-status.sh b/t/t7508-status.sh index a3c18a4fc2..10ed8b32bc 100755 --- a/t/t7508-status.sh +++ b/t/t7508-status.sh @@ -1403,7 +1403,9 @@ test_expect_success "status (core.commentchar with submodule summary)" ' test_expect_success "status (core.commentchar with two chars with submodule summary)" ' test_config core.commentchar ";;" && - test_must_fail git -c status.displayCommentPrefix=true status + sed "s/^/;/" expect.double && + git -c status.displayCommentPrefix=true status >output && + test_cmp expect.double output ' test_expect_success "--ignore-submodules=all suppresses submodule summary" '