Skip to content

Commit 17635fc

Browse files
committed
mailinfo: -b option keeps [bracketed] strings that is not a [PATCH] marker
By default, we remove leading [bracketed] [strings] from the Subject: header when coming up with the summary of the patch. This is because there are mailing lists etc that add their own headers to the subject, and they know they can add things in brackets. The most obvious example is the Linux kernel security list. Their emails look like Subject: [Security] [patch] random: make get_random_int() more random and other people mangle Subject: themselves in a similar way, e.g.: Subject: [PATCH -rc] [BUGFIX] x86: fix kernel_trap_sp() Subject: [BUGFIX][PATCH] fix bad page removal from LRU (Was Re: [RFC][PATCH] .. even though "fix" is more than enough cue to mark it as a [BUGFIX]. Some projects however want to keep these bracketed strings. With this option, we remove only [bracketed strings that contain word PATCH], so we will turn things like these [PATCH] [mailinfo] -b ... [PATCH v2] [mailinfo] -b ... [PATCH (v2) 1/4] [mailinfo] -b ... into [mailinfo] -b ... This lacks tests and integration to the "git am" toolchain to be useful, but it is a start. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a53e9d commit 17635fc

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

Documentation/git-mailinfo.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-mailinfo - Extracts patch and authorship from a single e-mail message
88

99
SYNOPSIS
1010
--------
11-
'git mailinfo' [-k] [-u | --encoding=<encoding> | -n] <msg> <patch>
11+
'git mailinfo' [-k|-b] [-u | --encoding=<encoding> | -n] <msg> <patch>
1212

1313

1414
DESCRIPTION
@@ -32,6 +32,11 @@ OPTIONS
3232
munging, and is most useful when used to read back
3333
'git-format-patch -k' output.
3434

35+
-b::
36+
When -k is not in effect, all leading strings bracketed with '['
37+
and ']' pairs are stripped. This option limits the stripping to
38+
only the pairs whose bracketed string contains the word "PATCH".
39+
3540
-u::
3641
The commit log message, author name and author email are
3742
taken from the e-mail, and after minimally decoding MIME

builtin-mailinfo.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
static FILE *cmitmsg, *patchfile, *fin, *fout;
1111

1212
static int keep_subject;
13+
static int keep_non_patch_brackets_in_subject;
1314
static const char *metainfo_charset;
1415
static struct strbuf line = STRBUF_INIT;
1516
static struct strbuf name = STRBUF_INIT;
@@ -219,35 +220,41 @@ static int is_multipart_boundary(const struct strbuf *line)
219220

220221
static void cleanup_subject(struct strbuf *subject)
221222
{
222-
char *pos;
223-
size_t remove;
224-
while (subject->len) {
225-
switch (*subject->buf) {
223+
size_t at = 0;
224+
225+
while (at < subject->len) {
226+
char *pos;
227+
size_t remove;
228+
229+
switch (subject->buf[at]) {
226230
case 'r': case 'R':
227-
if (subject->len <= 3)
231+
if (subject->len <= at + 3)
228232
break;
229-
if (!memcmp(subject->buf + 1, "e:", 2)) {
230-
strbuf_remove(subject, 0, 3);
233+
if (!memcmp(subject->buf + at + 1, "e:", 2)) {
234+
strbuf_remove(subject, at, 3);
231235
continue;
232236
}
237+
at++;
233238
break;
234239
case ' ': case '\t': case ':':
235-
strbuf_remove(subject, 0, 1);
240+
strbuf_remove(subject, at, 1);
236241
continue;
237242
case '[':
238-
if ((pos = strchr(subject->buf, ']'))) {
239-
remove = pos - subject->buf;
240-
if (remove <= (subject->len - remove) * 2) {
241-
strbuf_remove(subject, 0, remove + 1);
242-
continue;
243-
}
244-
} else
245-
strbuf_remove(subject, 0, 1);
246-
break;
243+
pos = strchr(subject->buf + at, ']');
244+
if (!pos)
245+
break;
246+
remove = pos - subject->buf + at + 1;
247+
if (!keep_non_patch_brackets_in_subject ||
248+
(7 <= remove &&
249+
memmem(subject->buf + at, remove, "PATCH", 5)))
250+
strbuf_remove(subject, at, remove);
251+
else
252+
at += remove;
253+
continue;
247254
}
248-
strbuf_trim(subject);
249-
return;
255+
break;
250256
}
257+
strbuf_trim(subject);
251258
}
252259

253260
static void cleanup_space(struct strbuf *sb)
@@ -931,7 +938,7 @@ static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
931938
}
932939

933940
static const char mailinfo_usage[] =
934-
"git mailinfo [-k] [-u | --encoding=<encoding> | -n] msg patch <mail >info";
941+
"git mailinfo [-k|-b] [-u | --encoding=<encoding> | -n] msg patch <mail >info";
935942

936943
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
937944
{
@@ -948,6 +955,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
948955
while (1 < argc && argv[1][0] == '-') {
949956
if (!strcmp(argv[1], "-k"))
950957
keep_subject = 1;
958+
else if (!strcmp(argv[1], "-b"))
959+
keep_non_patch_brackets_in_subject = 1;
951960
else if (!strcmp(argv[1], "-u"))
952961
metainfo_charset = def_charset;
953962
else if (!strcmp(argv[1], "-n"))

0 commit comments

Comments
 (0)