Skip to content

Commit f7ab5c7

Browse files
committed
custom pretty format: tolerate empty e-mail address
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>
1 parent 5682694 commit f7ab5c7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

pretty.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,27 @@ static void format_person_part(struct strbuf *sb, char part,
292292
/* parse name */
293293
for (end = 0; end < len && msg[end] != '<'; end++)
294294
; /* do nothing */
295+
/*
296+
* If it does not even have a '<' and '>', that is
297+
* quite a bogus commit author and we discard it;
298+
* this is in line with add_user_info() that is used
299+
* in the normal codepath. When end points at the '<'
300+
* that we found, it should have matching '>' later,
301+
* which means start (beginning of email address) must
302+
* be strictly below len.
303+
*/
295304
start = end + 1;
305+
if (start >= len - 1)
306+
return;
296307
while (end > 0 && isspace(msg[end - 1]))
297308
end--;
298309
if (part == 'n') { /* name */
299310
strbuf_add(sb, msg, end);
300311
return;
301312
}
302313

303-
if (start >= len)
304-
return;
305-
306314
/* parse email */
307-
for (end = start + 1; end < len && msg[end] != '>'; end++)
315+
for (end = start; end < len && msg[end] != '>'; end++)
308316
; /* do nothing */
309317

310318
if (end >= len)

t/t6006-rev-list-format.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ commit 131a310eb913d107dd3c09a65d1651175898735d
139139
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
140140
EOF
141141

142+
test_expect_success 'empty email' '
143+
test_tick &&
144+
C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
145+
A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
146+
test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
147+
echo "Eh? $A" >failure
148+
false
149+
}
150+
'
151+
142152
test_done

0 commit comments

Comments
 (0)