-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_parser.rb
80 lines (68 loc) · 2.05 KB
/
inventory_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# This class was taken from the darcs-ruby project.
# This class holds the patch's name, log, author, date, and other
# meta-info.
class InventoryParser
def initialize(date, name, author, log = nil, inverted = false)
if date.kind_of?(String)
@date = parse_date(date)
else
@date = date
end
@name = name
@author = author
@log = log
@inverted = inverted
end
attr_reader :date, :name, :author, :log
def inverted?
@inverted
end
# Reads a patch from a stream using the inventory format
def self.read(f)
header_regexp =
/^\[([^\n]+)\n([^\*]+)\*([-\*])(\d{14})(?:\n((?:^\ [^\n]*\n)+))?\]/
lines = f.readline
until match_data = header_regexp.match(lines)
lines += f.readline
end
short_message = $1
author_email = $2
inverted = $3 == "-" ? true : false
date = $4
long_message = $5
if long_message
stripped = ""
long_message.each_line {|l| stripped += l[1..-1] }
long_message = stripped
end
start_of_match, end_of_match = match_data.offset(0)
# push the remaining part of the last 10 lines back onto the stream.
lines[end_of_match..-1].reverse.each_byte {|b| f.ungetc b}
return self.new(date, short_message, author_email, long_message, inverted)
end
# Retrieve the patch's date in string timestamp format
def timestamp
date.strftime("%Y%m%d%H%M%S")
end
# Retrieve the patch's name
def filename
author_hash = SHA1.new(author).to_s[0..4]
hash = SHA1.new(name + author + timestamp +
(log.nil? ? '' : log.gsub(/\n/, '')) +
(inverted? ? 't' : 'f'))
"#{timestamp}-#{author_hash}-#{hash}.gz"
end
def to_s
if log
the_log = log.gsub(/[\n\s]+$/, '').gsub(/\n/, "\n ")
end
"[#{name}\n#{author}**#{timestamp}" +
(log.nil? ? '' : "\n " + the_log + "\n") + "]"
end
protected
def parse_date(str)
# Format is YYYYMMDDHHMMSS
Time.gm(str[0..3].to_i, str[4..5].to_i, str[6..7].to_i, str[8..9].to_i,
str[10..11].to_i, str[12..13].to_i)
end
end