Skip to content

Commit 36381a6

Browse files
Sathishkumar NatesanPeter Degen-Portnoy
authored andcommitted
OTWO-2996 Use strict time format check using Time.rfc2822 to handle bogus dates
1 parent b1b0e1e commit 36381a6

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/scm/parsers/git_styled_parser.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ def self.internal_parse(io, opts)
4242
elsif line =~ /^Author: (.+)$/
4343
e.author_name = $1
4444
elsif line =~ /^Date: (.*)$/
45-
# default to EPOCH when there's no date info
46-
e.author_date = Time.at(0).utc if $1.strip.empty?
47-
e.author_date ||= Time.parse($1).utc # Note strongly: MUST be RFC2822 format to parse properly
45+
# MUST be RFC2822 format to parse properly, else defaults to epoch time
46+
e.author_date = parse_commit_date($1)
4847
elsif line == "__BEGIN_COMMENT__"
4948
state = :message
5049
elsif line =~ /^AuthorEmail: (.+)$/
@@ -81,5 +80,10 @@ def self.internal_parse(io, opts)
8180

8281
yield e if e
8382
end
83+
84+
def self.parse_commit_date(date)
85+
t = Time.rfc2822(date) rescue Time.at(0)
86+
t.utc
87+
end
8488
end
8589
end

test/unit/git_styled_parser_test.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_empty_array
77
assert_equal([], GitStyledParser.parse(''))
88
end
99

10-
def test_log_parser_no_date
10+
def test_log_parser_nil_date
1111
sample_log = <<-SAMPLE
1212
__BEGIN_COMMIT__
1313
Commit: 1df547800dcd168e589bb9b26b4039bff3a7f7e4
@@ -17,6 +17,24 @@ def test_log_parser_no_date
1717
__BEGIN_COMMENT__
1818
moving COPYING
1919
20+
__END_COMMENT__
21+
SAMPLE
22+
23+
commits = GitStyledParser.parse(sample_log)
24+
assert_equal 1, commits.size
25+
assert_equal Time.utc(1970,1,1,0,0,0), commits[0].author_date
26+
end
27+
28+
def test_log_parser_bogus_date
29+
sample_log = <<-SAMPLE
30+
__BEGIN_COMMIT__
31+
Commit: 1df547800dcd168e589bb9b26b4039bff3a7f7e4
32+
Author: Jason Allen
33+
AuthorEmail: jason@ohloh.net
34+
Date: Mon, Jan 01 2012 05:00:00 -0500
35+
__BEGIN_COMMENT__
36+
moving COPYING
37+
2038
__END_COMMENT__
2139
SAMPLE
2240

0 commit comments

Comments
 (0)