Skip to content

Commit 5f5ef92

Browse files
committed
Merge pull request #11 from nnsathish/master
OTWO-2996 Default to EPOCH time when the git parser don't find a date
2 parents 6490fb7 + becdca2 commit 5f5ef92

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

lib/scm/parsers/git_styled_parser.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def self.internal_parse(io, opts)
4242
elsif line =~ /^Author: (.+)$/
4343
e.author_name = $1
4444
elsif line =~ /^Date: (.*)$/
45-
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_date($1)
4647
elsif line == "__BEGIN_COMMENT__"
4748
state = :message
4849
elsif line =~ /^AuthorEmail: (.+)$/
@@ -79,5 +80,10 @@ def self.internal_parse(io, opts)
7980

8081
yield e if e
8182
end
83+
84+
def self.parse_date(date)
85+
t = Time.rfc2822(date) rescue Time.at(0)
86+
t.utc
87+
end
8288
end
8389
end

test/unit/git_styled_parser_test.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
module Scm::Parsers
4+
class GitStyledParserTest < Scm::Test
5+
6+
def test_empty_array
7+
assert_equal([], GitStyledParser.parse(''))
8+
end
9+
10+
def test_log_parser_nil_date
11+
sample_log = <<-SAMPLE
12+
__BEGIN_COMMIT__
13+
Commit: 1df547800dcd168e589bb9b26b4039bff3a7f7e4
14+
Author: Jason Allen
15+
AuthorEmail: jason@ohloh.net
16+
Date:
17+
__BEGIN_COMMENT__
18+
moving COPYING
19+
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+
38+
__END_COMMENT__
39+
SAMPLE
40+
41+
commits = GitStyledParser.parse(sample_log)
42+
assert_equal 1, commits.size
43+
assert_equal Time.utc(1970,1,1,0,0,0), commits[0].author_date
44+
end
45+
46+
def test_log_parser_default
47+
sample_log = <<SAMPLE
48+
__BEGIN_COMMIT__
49+
Commit: 1df547800dcd168e589bb9b26b4039bff3a7f7e4
50+
Author: Jason Allen
51+
AuthorEmail: jason@ohloh.net
52+
Date: Fri, 14 Jul 2006 16:07:15 -0700
53+
__BEGIN_COMMENT__
54+
moving COPYING
55+
56+
__END_COMMENT__
57+
58+
:000000 100755 0000000000000000000000000000000000000000 a7b13ff050aed1191c45d7a5db9a50edcdc5755f A COPYING
59+
60+
__BEGIN_COMMIT__
61+
Commit: 2e9366dd7a786fdb35f211fff1c8ea05c51968b1
62+
Author: Robin Luckey
63+
AuthorEmail: robin@ohloh.net
64+
Date: Sun, 11 Jun 2006 11:34:17 -0700
65+
__BEGIN_COMMENT__
66+
added some documentation and licensing info
67+
68+
__END_COMMENT__
69+
70+
:100644 100644 d4a46caf1891fccebb726504f34794a0ca5d2e42 41dc0d12cb9eaa30e57aa7126b1227ba320ad297 M README
71+
:100644 000000 41dc0d12cb9eaa30e57aa7126b1227ba320ad297 0000000000000000000000000000000000000000 D helloworld.c
72+
SAMPLE
73+
74+
commits = GitStyledParser.parse(sample_log)
75+
76+
assert commits
77+
assert_equal 2, commits.size
78+
79+
assert_equal '1df547800dcd168e589bb9b26b4039bff3a7f7e4', commits[0].token
80+
assert_equal 'Jason Allen', commits[0].author_name
81+
assert_equal 'jason@ohloh.net', commits[0].author_email
82+
assert_equal "moving COPYING\n", commits[0].message
83+
assert_equal Time.utc(2006,7,14,23,7,15), commits[0].author_date
84+
assert_equal 1, commits[0].diffs.size
85+
86+
assert_equal "A", commits[0].diffs[0].action
87+
assert_equal "COPYING", commits[0].diffs[0].path
88+
89+
assert_equal '2e9366dd7a786fdb35f211fff1c8ea05c51968b1', commits[1].token
90+
assert_equal 'Robin Luckey', commits[1].author_name
91+
assert_equal 'robin@ohloh.net', commits[1].author_email
92+
assert_equal "added some documentation and licensing info\n", commits[1].message # Note \n at end of comment
93+
assert_equal Time.utc(2006,6,11,18,34,17), commits[1].author_date
94+
assert_equal 2, commits[1].diffs.size
95+
96+
assert_equal "M", commits[1].diffs[0].action
97+
assert_equal "README", commits[1].diffs[0].path
98+
assert_equal "D", commits[1].diffs[1].action
99+
assert_equal "helloworld.c", commits[1].diffs[1].path
100+
end
101+
102+
end
103+
end

0 commit comments

Comments
 (0)