1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/rdoc] Skip non-date logs by git-log

`RDoc::Parser::ChangeLog` mis-parses ChangeLog generated by
git-log, because of too heuristic `Time.parse`.

For instance, "commit 8187228de0142d3ac7950b7d977c2849e934c637"
results in "8187-08-16", that is, day 228 in the year 8187.

https://github.com/ruby/rdoc/commit/9711e6f6d9
This commit is contained in:
Nobuyoshi Nakada 2021-01-23 17:28:59 +09:00
parent d5d1c41728
commit 52ebaf718e
2 changed files with 24 additions and 14 deletions

View file

@ -1,5 +1,4 @@
# frozen_string_literal: true
require 'time'
##
# A ChangeLog file parser.
@ -106,14 +105,32 @@ class RDoc::Parser::ChangeLog < RDoc::Parser
entries.group_by do |title, _|
begin
time = @time_cache[title]
(time || Time.parse(title)).strftime '%Y-%m-%d'
(time || parse_date(title)).strftime '%Y-%m-%d'
rescue NoMethodError, ArgumentError
time, = title.split ' ', 2
Time.parse(time).strftime '%Y-%m-%d'
parse_date(time).strftime '%Y-%m-%d'
end
end
end
##
# Parse date in ISO-8601, RFC-2822, or default of Git
def parse_date(date)
case date
when /\A\s*(\d+)-(\d+)-(\d+)(?: (\d+):(\d+):(\d+) *([-+]\d\d)(\d\d))?\b/
Time.new($1, $2, $3, $4, $5, $6, ("#{$7}:#{$8}" if $7))
when /\A\s*\w{3}, +(\d+) (\w{3}) (\d+) (\d+):(\d+):(\d+) *(?:([-+]\d\d)(\d\d))\b/
Time.new($3, $2, $1, $4, $5, $6, ("#{$7}:#{$8}" if $7))
when /\A\s*\w{3} (\w{3}) +(\d+) (\d+) (\d+):(\d+):(\d+) *(?:([-+]\d\d)(\d\d))\b/
Time.new($3, $1, $2, $4, $5, $6, ("#{$7}:#{$8}" if $7))
when /\A\s*\w{3} (\w{3}) +(\d+) (\d+):(\d+):(\d+) (\d+)\b/
Time.new($6, $1, $2, $3, $4, $5)
else
raise ArgumentError, "bad date: #{date}"
end
end
##
# Parses the entries in the ChangeLog.
#
@ -152,19 +169,10 @@ class RDoc::Parser::ChangeLog < RDoc::Parser
entry_name = $&
begin
time = Time.parse entry_name
time = parse_date entry_name
@time_cache[entry_name] = time
# HACK Ruby 1.8 does not raise ArgumentError for Time.parse "Other"
entry_name = nil unless entry_name =~ /#{time.year}/
rescue NoMethodError
# HACK Ruby 2.1.2 and earlier raises NoMethodError if time part is absent
entry_name.split ' ', 2
rescue ArgumentError
if /out of range/ =~ $!.message
Time.parse(entry_name.split(' ', 2)[0]) rescue entry_name = nil
else
entry_name = nil
end
entry_name = nil
end
entry_body = []

View file

@ -212,6 +212,8 @@ Mon Dec 3 20:28:02 2012 Koichi Sasada <ko1@atdot.net>
change condition of using `opt_send_simple'.
More method invocations can be simple.
commit\ 8187228de0142d3ac7950b7d977c2849e934c637
Other note that will be ignored
ChangeLog