Skip to content

Commit b1b0e1e

Browse files
Sathishkumar NatesanPeter Degen-Portnoy
authored andcommitted
OTWO-2996 Default to EPOCH time when the git parser don't find a date
Currently, it defaults to Time.now.utc which is not reasonably fair. Also git commands like git log/show defaults to epoch time.
1 parent 17e2556 commit b1b0e1e

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

lib/scm/parsers/git_styled_parser.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ 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+
# 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
4648
elsif line == "__BEGIN_COMMENT__"
4749
state = :message
4850
elsif line =~ /^AuthorEmail: (.+)$/

test/unit/git_styled_parser_test.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_no_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_default
29+
sample_log = <<SAMPLE
30+
__BEGIN_COMMIT__
31+
Commit: 1df547800dcd168e589bb9b26b4039bff3a7f7e4
32+
Author: Jason Allen
33+
AuthorEmail: jason@ohloh.net
34+
Date: Fri, 14 Jul 2006 16:07:15 -0700
35+
__BEGIN_COMMENT__
36+
moving COPYING
37+
38+
__END_COMMENT__
39+
40+
:000000 100755 0000000000000000000000000000000000000000 a7b13ff050aed1191c45d7a5db9a50edcdc5755f A COPYING
41+
42+
__BEGIN_COMMIT__
43+
Commit: 2e9366dd7a786fdb35f211fff1c8ea05c51968b1
44+
Author: Robin Luckey
45+
AuthorEmail: robin@ohloh.net
46+
Date: Sun, 11 Jun 2006 11:34:17 -0700
47+
__BEGIN_COMMENT__
48+
added some documentation and licensing info
49+
50+
__END_COMMENT__
51+
52+
:100644 100644 d4a46caf1891fccebb726504f34794a0ca5d2e42 41dc0d12cb9eaa30e57aa7126b1227ba320ad297 M README
53+
:100644 000000 41dc0d12cb9eaa30e57aa7126b1227ba320ad297 0000000000000000000000000000000000000000 D helloworld.c
54+
SAMPLE
55+
56+
commits = GitStyledParser.parse(sample_log)
57+
58+
assert commits
59+
assert_equal 2, commits.size
60+
61+
assert_equal '1df547800dcd168e589bb9b26b4039bff3a7f7e4', commits[0].token
62+
assert_equal 'Jason Allen', commits[0].author_name
63+
assert_equal 'jason@ohloh.net', commits[0].author_email
64+
assert_equal "moving COPYING\n", commits[0].message
65+
assert_equal Time.utc(2006,7,14,23,7,15), commits[0].author_date
66+
assert_equal 1, commits[0].diffs.size
67+
68+
assert_equal "A", commits[0].diffs[0].action
69+
assert_equal "COPYING", commits[0].diffs[0].path
70+
71+
assert_equal '2e9366dd7a786fdb35f211fff1c8ea05c51968b1', commits[1].token
72+
assert_equal 'Robin Luckey', commits[1].author_name
73+
assert_equal 'robin@ohloh.net', commits[1].author_email
74+
assert_equal "added some documentation and licensing info\n", commits[1].message # Note \n at end of comment
75+
assert_equal Time.utc(2006,6,11,18,34,17), commits[1].author_date
76+
assert_equal 2, commits[1].diffs.size
77+
78+
assert_equal "M", commits[1].diffs[0].action
79+
assert_equal "README", commits[1].diffs[0].path
80+
assert_equal "D", commits[1].diffs[1].action
81+
assert_equal "helloworld.c", commits[1].diffs[1].path
82+
end
83+
84+
end
85+
end

0 commit comments

Comments
 (0)