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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
############################################################################
# Copyright 2009 Benjamin Kellermann #
# #
# This file is part of dudle. #
# #
# Dudle is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Affero General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# Dudle is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
# License for more details. #
# #
# You should have received a copy of the GNU Affero General Public License #
# along with dudle. If not, see <http://www.gnu.org/licenses/>. #
############################################################################
require "time"
class LogEntry
attr_accessor :rev, :timestamp, :comment
def initialize(rev,timestamp,comment)
@rev = rev
@timestamp = timestamp
@comment = comment
end
def to_html(link = true)
ret = "<tr><td>"
ret += "<a href='?revision=#{@rev}' >" if link
ret += "#{@rev}"
ret += "</a>" if link
ret += "</td>"
ret += "<td>#{@timestamp.strftime('%d.%m, %H:%M')}</td>"
ret += "<td class='historycomment'>#{CGI.escapeHTML(@comment)}</td>"
ret += "</tr>"
ret
end
end
class Log
def initialize(a = [])
@log = a
end
def min
ret = @log[0]
@log.each{|l|
ret.rev = l if l.rev < ret.rev
}
ret
end
def max
ret = @log[-1]
@log.each{|l|
ret.rev = l if l.rev > ret.rev
}
ret
end
def [](revision)
if revision.class == Fixnum
@log.each{|l|
return l if l.rev == revision
}
raise "No such revision found #{revision}"
else
min_r, max_r = "",""
@log.each_with_index{|l,i|
min_r = i if l.rev == revision.min
max_r = i if l.rev == revision.max
}
min_r = @log.index(min) if min_r == ""
max_r = @log.index(max) if max_r == ""
return Log.new(@log[min_r..max_r])
end
end
def add(revision,timestamp,comment)
@log << LogEntry.new(revision,timestamp,comment)
@log.sort!{|a,b| a.rev <=> b.rev}
end
def to_html(notlinkrevision)
ret = "<table><tr><th>Version</th><th>Date</th><th>Comment</th></tr>"
self.each do |l|
ret += l.to_html(notlinkrevision != l.rev)
end
ret += "</table>"
ret
end
def each
@log.each{|e| yield(e)}
end
def collect
@log.collect{|e| yield(e)}
end
def comment_matches(regex)
Log.new(@log.collect{|e| e if e.comment =~ regex}.compact)
end
def flatten
h = []
minrev = min.rev
rev = max.rev
while rev > minrev
elem = self[rev]
prevrev = elem.comment.scan(/^Reverted Poll to revision (\d*)$/).flatten[0]
if prevrev
rev = prevrev.to_i
else
h << elem
rev += -1
end
end
h.sort!{|a,b| a.rev <=> b.rev}
a = []
begin
a << h.pop
end while a.last.comment =~ /^Column .*$/
a.pop
a.sort!{|a1,b1| a1.rev <=> b1.rev}
Log.new(a)
end
end
if __FILE__ == $0
require "test/unit"
class Log_test < Test::Unit::TestCase
def test_indexes
l = Log.new
l.add(10,Time.now,"baz 10")
20.times{|i|
l.add(i,Time.now,"foo #{i}") unless i == 10
}
assert_equal(0,l.min.rev)
assert_equal(19,l.max.rev)
assert_equal("baz 10",l[10].comment)
p = l[9..11]
assert_equal([9,10,11],[p[9].rev,p[10].rev,p[11].rev])
assert_equal([10],l.comment_matches(/^baz \d*$/).collect{|e| e.rev})
end
end
end
|