File Coverage

perllib/Arch/RevisionBunches.pm
Criterion Covered Total %
statement 9 157 5.7
branch 0 62 0.0
condition 0 54 0.0
subroutine 3 14 21.4
pod 9 9 100.0
total 21 296 7.0


line stmt bran cond sub pod time code
1             # Arch Perl library, Copyright (C) 2004 Mikhael Goikhman
2             #
3             # This program is free software; you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by
5             # the Free Software Foundation; either version 2 of the License, or
6             # (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program; if not, write to the Free Software
15             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16              
17 1     1   26 use 5.005;
  1         4  
  1         51  
18 1     1   8 use strict;
  1         2  
  1         56  
19              
20             package Arch::RevisionBunches;
21              
22 1     1   8 use Arch::Util qw(date2daysago);
  1         1  
  1         1486  
23              
24             sub new ($%) {
25 0     0 1   my $class = shift;
26 0           my %args = @_;
27              
28 0           my $self = {
29             bunched_revision_descs => [],
30             new_revision_descs => [],
31             bunches => [],
32             versions => [],
33             filepaths => [],
34             bunch_size => 50,
35             max_sumlen => undef,
36             version => undef,
37             final_revision => undef,
38             final_filepath => undef,
39             cb_remove_all_bunches => undef,
40             cb_add_new_bunch => undef,
41             };
42 0           bless $self, $class;
43 0           return $self->init(%args);
44             }
45              
46             sub init ($%) {
47 0     0 1   my $self = shift;
48 0           my %args = @_;
49              
50 0           while (my ($key, $value) = each %args) {
51 0 0         unless (exists $self->{$key}) {
52 0           warn "Arch::RevisionBunches: unknown key $key, ignoring\n";
53 0           next;
54             }
55 0           $self->{$key} = $value;
56             }
57 0 0 0       $self->{max_sumlen} = undef if $args{max_sumlen} &&
      0        
58             !($args{max_sumlen} =~ /^\d+$/ && $args{max_sumlen} > 5);
59 0           return $self;
60             }
61              
62             sub add_revision_descs ($$%) {
63 0     0 1   my $self = shift;
64 0           my $revision_descs = shift;
65 0           my %args = @_;
66 0           my $max_sumlen = $self->{max_sumlen};
67              
68 0           foreach (@$revision_descs) {
69 0 0 0       substr($_->{summary}, $max_sumlen - 3) = "..."
70             if $max_sumlen && length($_->{summary}) > $max_sumlen;
71 0           foreach my $arg (keys %args) {
72 0           $_->{$arg} = $args{$arg};
73             }
74             }
75 0           push @{$self->{new_revision_descs}}, @$revision_descs;
  0            
76 0           return $self;
77             }
78              
79             sub get ($) {
80 0     0 1   my $self = shift;
81 0 0         $self->rebunch if @{$self->{new_revision_descs}};
  0            
82 0 0         return wantarray? @{$self->{bunches}}: $self->{bunches};
  0            
83             }
84              
85             sub _set_bunch_interval_data ($$;$) {
86 0     0     my $self = shift;
87 0   0       my $bunch = shift || die;
88 0   0       my $start_index = shift || 0;
89              
90 0           my $nr = @{$bunch->{revision_descs}};
  0            
91 0           my $i = 1;
92 0 0         my $idx2 = $nr > 1 ? -1 : undef;
93 0           foreach my $idx (0, $idx2) {
94 0   0       my $revision_desc = defined $idx && $bunch->{revision_descs}->[$idx];
95 0 0         $bunch->{"name$i"} = $revision_desc? $revision_desc->{name}: undef;
96 0 0         my ($daysago, $time, $tz) = $revision_desc?
97             date2daysago($revision_desc->{date}): (undef) x 3;
98 0           $bunch->{"daysago$i"} = $daysago;
99 0           $bunch->{"time$i"} = $time;
100 0           $bunch->{"tz$i"} = $tz;
101 0           $i++;
102             }
103 0           $bunch->{size} = $nr;
104              
105 0           my %creators = ();
106 0           my $nm = 0;
107 0           foreach my $revision_desc (@{$bunch->{revision_descs}}) {
  0            
108 0   0       my $creator = $revision_desc->{creator} || "";
109 0   0       my $email = $revision_desc->{email} || "";
110 0   0       my $entry = $creators{$creator,$email} ||= [ $creator, $email, 0 ];
111 0           $entry->[2]++;
112 0 0         $nm++ if $revision_desc->{is_merged};
113             }
114 0           my $nc = $bunch->{num_creators} = scalar keys %creators;
115 0 0         $bunch->{num_other_creators} = $nc? $nc - 1: 0;
116 0           ($bunch->{main_creator}, $bunch->{main_email}) =
117 0 0         @{ (sort { $b->[2] <=> $a->[2] } values %creators)[0] || [] };
  0            
118 0 0         $bunch->{creator} = $bunch->{main_creator} . ($nc == 1? "": " among $nc creators");
119              
120 0           $bunch->{name} = $bunch->{name1};
121 0           $bunch->{date} = $bunch->{daysago1};
122 0 0         if ($bunch->{name2}) {
123 0           $bunch->{name} .= " .. $bunch->{name2}";
124 0           $bunch->{date} .= " .. $bunch->{daysago2}";
125             }
126 0           $bunch->{date} .= " days ago";
127 0 0         $bunch->{summary} = "$nr revision" . ($nr == 1? "": "s");
128 0 0 0       $bunch->{summary} .= ' (missing)' if $bunch->{is_missing} && $nm < $nr;
129 0 0         $bunch->{summary} .= " ($nm merged)" if $nm > 0;
130              
131 0 0         $self->{cb_add_new_bunch}->($bunch, $start_index) if $self->{cb_add_new_bunch};
132             }
133              
134             sub _invalidate_bunches ($) {
135 0     0     my $self = shift;
136              
137 0 0         $self->{cb_remove_all_bunches}->() if $self->{cb_remove_all_bunches};
138              
139 0           unshift @{$self->{new_revision_descs}}, @{$self->{bunched_revision_descs}};
  0            
  0            
140 0           @{$self->{bunched_revision_descs}} = ();
  0            
141 0           @{$self->{bunches}} = ();
  0            
142 0           @{$self->{versions}} = ();
  0            
143 0           @{$self->{filepaths}} = ();
  0            
144             }
145              
146             sub rebunch ($;$) {
147 0     0 1   my $self = shift;
148 0           my $bunch_size = shift;
149 0           my $change_size = !!$bunch_size;
150 0   0       $bunch_size ||= $self->{bunch_size} || die "No bunch size given";
      0        
151              
152 0 0         if ($change_size) {
153 0           $self->{bunch_size} = $bunch_size;
154 0           $self->_invalidate_bunches;
155             }
156 0 0         goto RETURN unless @{$self->{new_revision_descs}};
  0            
157              
158 0           my $last_bunch = $self->{bunches}->[-1];
159 0 0         my $start_index = $last_bunch? @{$last_bunch->{revision_descs}}: 0;
  0            
160 0           my $multi_version = !$self->{version};
161 0           while (my $rd = shift @{$self->{new_revision_descs}}) {
  0            
162 0           my $version = $rd->{version};
163 0           my $is_missing = $rd->{is_missing};
164 0           my $has_is_missing = defined $is_missing;
165 0           my $filepath = $rd->{filepath};
166 0           my $has_filepath = defined $filepath;
167 0           my $is_bunch_property_changed = 0;
168              
169 0 0 0       if ($last_bunch && $multi_version && $last_bunch->{version} ne $version) {
      0        
170 0           push @{$self->{versions}}, $version;
  0            
171 0           $is_bunch_property_changed = 1;
172             }
173 0 0 0       if ($last_bunch && $has_is_missing && $last_bunch->{is_missing} ne $is_missing) {
      0        
174 0           $is_bunch_property_changed = 1;
175             }
176 0 0 0       if (!$last_bunch || $has_filepath && $last_bunch->{filepath} ne $filepath) {
      0        
177 0           push @{$self->{filepaths}}, $filepath;
  0            
178 0           $is_bunch_property_changed = 1;
179             }
180              
181 0 0 0       if (
      0        
182 0           !$last_bunch || $is_bunch_property_changed ||
183             @{$last_bunch->{revision_descs}} >= $bunch_size
184             ) {
185 0 0         $self->_set_bunch_interval_data($last_bunch, $start_index) if $last_bunch;
186 0           $start_index = 0;
187 0           $last_bunch = { revision_descs => [] };
188 0 0         $last_bunch->{version} = $version if $multi_version;
189 0 0         $last_bunch->{filepath} = $filepath if $has_filepath;
190 0 0         $last_bunch->{is_missing} = $is_missing if $has_is_missing;
191 0           push @{$self->{bunches}}, $last_bunch;
  0            
192             }
193 0           push @{$last_bunch->{revision_descs}}, $rd;
  0            
194 0           push @{$self->{bunched_revision_descs}}, $rd;
  0            
195             }
196 0 0         $self->_set_bunch_interval_data($last_bunch, $start_index) if $last_bunch;
197              
198 0           RETURN:
199 0 0         return wantarray? @{$self->{bunches}}: $self->{bunches};
200             }
201              
202             sub clear ($) {
203 0     0 1   my $self = shift;
204              
205 0           $self->_invalidate_bunches;
206 0           @{$self->{new_revision_descs}} = @{$self->{bunched_revision_descs}} = ();
  0            
  0            
207 0           return $self;
208             }
209              
210             sub reverse_revision_descs ($) {
211 0     0 1   my $self = shift;
212              
213 0           $self->_invalidate_bunches;
214 0           @{$self->{new_revision_descs}} = reverse(@{$self->{new_revision_descs}});
  0            
  0            
215 0           return $self;
216             }
217              
218             sub versions ($) {
219 0     0 1   my $self = shift;
220 0           return $self->{versions};
221             }
222              
223             sub filepaths ($) {
224 0     0 1   my $self = shift;
225 0           return $self->{filepaths};
226             }
227              
228             1;
229              
230             __END__