Skip to contentSkip to conversation

blame: Improve parsing for emails with spaces

PATCH In review Josh Stone 4 participants 6 messages Jun 15, 2016

lore
Share & tools
lore

Josh Stone

author
#0 lore raw

PATCH 1/1 blame: Improve parsing for emails with spaces

One of my git repositories has some old commits where the authors obfuscated their email address as <author at example dot com>. To handle this, blame needs to look for the leading '<' when scanning to split the "name <email>", rather then only a space delimiter.

---
 builtin/blame.c     |    2 +-
 t/annotate-tests.sh |   12 +++++++++++-
 t/t8002-blame.sh    |    2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)
builtin/blame.c +1 -1
index f6b03f7..41525f1 100644
@@ -1377,7 +1377,7 @@ static void get_ac_line(const char *inbuf, const char *what,
 	timepos = tmp;
 
 	*tmp = 0;
-	while (person < tmp && *tmp != ' ')
+	while (person < tmp && !(*tmp == ' ' && tmp[1] == '<'))
 		tmp--;
 	if (tmp <= person)
 		return;
t/annotate-tests.sh +11 -1
index d34208c..abb1885 100644
@@ -1,5 +1,5 @@
 # This file isn't used as a test script directly, instead it is
-# sourced from t8001-annotate.sh and t8001-blame.sh.
+# sourced from t8001-annotate.sh and t8002-blame.sh.
 
 check_count () {
 	head=
@@ -124,3 +124,13 @@ test_expect_success \
 test_expect_success \
     'some edit' \
     'check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1'
+
+test_expect_success \
+    'an obfuscated email added' \
+    'sed -e "1i No robots allowed" < file > file.new &&
+     mv file.new file &&
+     GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" git commit -a -m "norobots"'
+
+test_expect_success \
+    'obfuscated email parsed' \
+    'check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1'
t/t8002-blame.sh +1 -1
index d3a51e1..e2896cf 100755
@@ -8,7 +8,7 @@ PROG='git blame -c'
 
 PROG='git blame -c -e'
 test_expect_success 'Blame --show-email works' '
-    check_count "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1
+    check_count "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<[email protected]>" 1 "<E at test dot git>" 1
 '
 
 test_done
Signature · 1 line
1.7.4.4
#1 lore raw
Quoted 4 lines · Josh Stone

One of my git repositories has some old commits where the authors obfuscated their email address as <author at example dot com>. To handle this, blame needs to look for the leading '<' when scanning to split the "name <email>", rather then only a space delimiter.

Hmm. That address seems bogus, and I wonder if we should be rejecting it at commit time. Still, it is something we may run across in existing repositories, so handling it more gracefully makes sense.

builtin/blame.c static void get_ac_line(const char *inbuf, const char *what, · line 1383

Quoted 13 lines
builtin/blame.c +1 -1
index f6b03f7..41525f1 100644
@@ -1377,7 +1377,7 @@ static void get_ac_line(const char *inbuf, const char *what,
 	timepos = tmp;
 
 	*tmp = 0;
-	while (person < tmp && *tmp != ' ')
+	while (person < tmp && !(*tmp == ' ' && tmp[1] == '<'))
 		tmp--;
 	if (tmp <= person)
 		return;

The fix looks fine. It's a little gross that we are parsing the commit headers ourselves here, as opposed to using some library function provided for commits. I think we could eliminate some code by using the parser in pretty.c, but it might need some refactoring to do so efficiently.

Looking over the other places we parse author info, I don't think the same problem exists elsewhere. Most other places parse from left to right, and just go to the closing ">".

-Peff

#2 lore raw
Quoted 6 lines · Jeff King
Quoted 4 lines · Josh Stone

One of my git repositories has some old commits where the authors obfuscated their email address as <author at example dot com>. To handle this, blame needs to look for the leading '<' when scanning to split the "name <email>", rather then only a space delimiter.

Given that we enclose the e-mail inside "<>" pair and excise "<" from author names in fmt_ident(), I think it makes sense to look for " <" like this patch does.

Quoted 3 lines

Hmm. That address seems bogus, and I wonder if we should be rejecting it at commit time. Still, it is something we may run across in existing repositories, so handling it more gracefully makes sense.

Perhaps but within reason.

What new types of breakages are we proposing to tolerate, what breakages are we declaring not worth fixing, and what is the price of not loosening this? Without this patch, such a broken commit will result in the author email shown somewhat broken, but the original is already broken to begin with, and also the entry for the blamed line will come with its commit object name anyway, so I do not think it is such a big deal.

It would be an entirely different issue if the command barfed and refused to blame the file.

Quoted 3 lines

Looking over the other places we parse author info, I don't think the same problem exists elsewhere. Most other places parse from left to right, and just go to the closing ">".

Because fmt_ident() removes "<" or ">" from given email address, I think it is sufficient.

It would be nice to have an abstraction around the parsing in a way similar to fmt_ident() is an abstraction around the formatting, but that would be a separate topic.

#3 lore raw
Quoted 4 lines · Junio C Hamano
Quoted 3 lines

Hmm. That address seems bogus, and I wonder if we should be rejecting it at commit time. Still, it is something we may run across in existing repositories, so handling it more gracefully makes sense.

Quoted 8 lines

Perhaps but within reason.

What new types of breakages are we proposing to tolerate, what breakages are we declaring not worth fixing, and what is the price of not loosening this? Without this patch, such a broken commit will result in the author email shown somewhat broken, but the original is already broken to begin with, and also the entry for the blamed line will come with its commit object name anyway, so I do not think it is such a big deal.

I'm pretty sure such an address would make a non-rfc822-compliant "from" header when used with format-patch. But given that it is obviously a bogus address, I don't think there's much we can do anyway, and anyone looking at will say "Oh, that's wrong". So it's probably not a big deal.

-Peff

author
#4 lore raw
In reply to Junio C Hamano
Quoted 2 lines · Junio C Hamano

It would be an entirely different issue if the command barfed and refused to blame the file.

Yeah - the command doesn't fail outright. Anything that builds on blame/annotate would get bad data, but for me it's more of a formatting issue. In my actual bad commit, the author's "name <broken email" takes up 61 characters, making the whole blame output tediously wide.

Thanks for taking the patch.

Josh

#5 lore raw
In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 2 lines · Jeff King

On Thu, Apr 21, 2011 at 03:07:36PM -0700, Josh Stone wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

One of my git repositories has some old commits where the authors obfuscated their email address as <author at example dot com>. To handle this, blame needs to look for the leading '<' when scanning to split the "name <email>", rather then only a space delimiter.

Given that we enclose the e-mail inside "<>" pair and excise "<" from author names in fmt_ident(), I think it makes sense to look for " <" like this patch does.

Will queue. Thanks.

Author: Josh Stone <[email protected]>
Date:   Thu Apr 21 15:07:36 2011 -0700

blame: tolerate bogus e-mail addresses a bit better

The names and e-mails are sanitized by fmt_ident() when creating commits, so that they do not contain "<" nor ">", and the "committer" and "author" lines in the commit object will always be in the form:

("author" | "committer") name SP "<" email ">" SP timestamp SP zone

When parsing the email part out, the current code looks for SP starting from the end of the email part, but the author could obfuscate the address as "author at example dot com".

We should instead look for SP followed by "<", to match the logic of the side that formats these lines.