custom pretty format: tolerate empty e-mail address
authorJunio C Hamano <gitster@pobox.com>
Sun, 6 Jan 2008 12:21:07 +0000 (04:21 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Jan 2008 02:41:43 +0000 (18:41 -0800)
When e-mail address is empty (e.g. "A U Thor <>"), --pretty=format
misparsed the commit header and did not pick up the date field correctly.

Noticed by Marco, fixed slightly differently with additional sanity
check and with a test.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
pretty.c
t/t6006-rev-list-format.sh
index 5b1078beb6b27f08b196d0e35b4acfb72df643b7..b987ff245b310a6693dc69ba8c71ef2915da7864 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -292,7 +292,18 @@ static void format_person_part(struct strbuf *sb, char part,
        /* parse name */
        for (end = 0; end < len && msg[end] != '<'; end++)
                ; /* do nothing */
+       /*
+        * If it does not even have a '<' and '>', that is
+        * quite a bogus commit author and we discard it;
+        * this is in line with add_user_info() that is used
+        * in the normal codepath.  When end points at the '<'
+        * that we found, it should have matching '>' later,
+        * which means start (beginning of email address) must
+        * be strictly below len.
+        */
        start = end + 1;
+       if (start >= len - 1)
+               return;
        while (end > 0 && isspace(msg[end - 1]))
                end--;
        if (part == 'n') {      /* name */
@@ -300,11 +311,8 @@ static void format_person_part(struct strbuf *sb, char part,
                return;
        }
 
-       if (start >= len)
-               return;
-
        /* parse email */
-       for (end = start + 1; end < len && msg[end] != '>'; end++)
+       for (end = start; end < len && msg[end] != '>'; end++)
                ; /* do nothing */
 
        if (end >= len)
index 1e4541afea07daa094895244f0e49803623cd1cd..0dc915ea67b21d07d8e4dca44767906e05ad2278 100755 (executable)
@@ -139,4 +139,14 @@ commit 131a310eb913d107dd3c09a65d1651175898735d
 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 EOF
 
+test_expect_success 'empty email' '
+       test_tick &&
+       C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
+       A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
+       test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
+               echo "Eh? $A" >failure
+               false
+       }
+'
+
 test_done