git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Roger Leigh <rleigh@debian.org>
To: git@vger.kernel.org
Cc: Roger Leigh <rleigh@debian.org>
Subject: [PATCH] builtin-mailinfo.c: Trim only first pair of square brackets in subject
Date: Mon, 29 Jun 2009 22:17:00 +0100	[thread overview]
Message-ID: <1246310220-16909-1-git-send-email-rleigh@debian.org> (raw)
In-Reply-To: <7vfxdkez96.fsf@alter.siamese.dyndns.org>

Use a regular expression to match text after "Re:" or any text in the
first pair of square brackets such as "[PATCH n/m]".  This replaces
the complex hairy string munging with a simple single  pattern match.

Signed-off-by: Roger Leigh <rleigh@debian.org>
---
 builtin-mailinfo.c |   61 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
index 92637ac..6d19046 100644
--- a/builtin-mailinfo.c
+++ b/builtin-mailinfo.c
@@ -219,35 +219,42 @@ static int is_multipart_boundary(const struct strbuf *line)
 
 static void cleanup_subject(struct strbuf *subject)
 {
-	char *pos;
-	size_t remove;
-	while (subject->len) {
-		switch (*subject->buf) {
-		case 'r': case 'R':
-			if (subject->len <= 3)
-				break;
-			if (!memcmp(subject->buf + 1, "e:", 2)) {
-				strbuf_remove(subject, 0, 3);
-				continue;
-			}
-			break;
-		case ' ': case '\t': case ':':
-			strbuf_remove(subject, 0, 1);
-			continue;
-		case '[':
-			if ((pos = strchr(subject->buf, ']'))) {
-				remove = pos - subject->buf;
-				if (remove <= (subject->len - remove) * 2) {
-					strbuf_remove(subject, 0, remove + 1);
-					continue;
-				}
-			} else
-				strbuf_remove(subject, 0, 1);
-			break;
-		}
+	int status;
+	regex_t regex;
+	regmatch_t match[4];
+
+	/* Strip off 'Re:' and/or the first text in square brackets, such as
+	   '[PATCH]' at the start of the mail Subject. */
+	status = regcomp(&regex,
+			 "^([Rr]e:)?([^]]*\\[[^]]+\\])(.*)$",
+			 REG_EXTENDED);
+
+	if (status) {
+		/* Compiling the regex failed.  Find out why and tell
+		   the user.  This is always a bug in the code. */
+		int esize = regerror(status, &regex, NULL, 0);
+		struct strbuf etext = STRBUF_INIT;
+
+		strbuf_grow(&etext, esize);
+		regerror(status, &regex, etext.buf, esize);
+		fprintf (stderr,
+			 "Error compiling regular expression: %s\n",
+			 etext.buf);
+		strbuf_release(&etext);
+		exit(1);
+	}
+
+	/* Store any matches in match. */
+	status = regexec(&regex, subject->buf, 4, match, 0);
+
+	/* If there was a match for \3 in the regex, trim the subject
+	   to this match. */
+	if (!status && match[3].rm_so > 0) {
+		strbuf_remove(subject, 0, match[3].rm_so);
 		strbuf_trim(subject);
-		return;
 	}
+
+	return;
 }
 
 static void cleanup_space(struct strbuf *sb)
-- 
1.6.3.3

  parent reply	other threads:[~2009-06-29 21:17 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-28 19:38 git mailinfo strips important context from patch subjects Roger Leigh
2009-06-28 20:02 ` Jeff King
2009-06-28 23:04   ` Junio C Hamano
2009-06-29  9:53     ` Andreas Ericsson
2009-06-29  9:55       ` [PATCH] mailinfo: Remove only one set of square brackets Andreas Ericsson
2009-06-29 16:09         ` Junio C Hamano
2009-06-30  5:33         ` Jeff King
2009-06-29 21:17     ` Roger Leigh [this message]
2009-06-29 21:26       ` [PATCH] builtin-mailinfo.c: Trim only first pair of square brackets in subject Jakub Narebski
2009-06-29 21:49         ` Roger Leigh
2009-09-22 10:39       ` Neil Roberts
2009-09-22 12:56         ` [PATCH] builtin-mailinfo.c: Improve the regexp for cleaning up the subject Neil Roberts
2009-09-22 16:15         ` [PATCH] builtin-mailinfo.c: Trim only first pair of square brackets in subject Junio C Hamano
2009-09-22 16:51           ` Neil Roberts
2009-09-23  0:26           ` Jason Holden
2009-06-29 21:34     ` [PATCH 2/2] builtin-mailinfo.c: Free regular expression after use Roger Leigh
2009-06-29 21:36     ` git mailinfo strips important context from patch subjects Roger Leigh
2009-06-28 20:07 ` [PATCH] " Paolo Bonzini
2009-06-29  9:19   ` Andreas Ericsson
2009-06-29 10:21     ` Paolo Bonzini
2009-06-29 10:54       ` Andreas Ericsson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1246310220-16909-1-git-send-email-rleigh@debian.org \
    --to=rleigh@debian.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).