aboutsummaryrefslogtreecommitdiffstats
path: root/Perl/Local/Cvsinfo.pm
blob: 3e798917e86df2789d937c6aa1e3ec89328e6385 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl -w

##  Copyright (C) 2001  Denis Barbier <barbier@debian.org>
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.

=head1 NAME

Local::Cvsinfo - retrieve CVS related informations from a checked out copy

=head1 SYNOPSIS

 use Local::Cvsinfo;
 my $cvs = Local::Cvsinfo->new();
 $cvs->readinfo('.', recursive => 1, verbose => 1 );
 my $top = $cvs->topdir();
 my $rev = $cvs->revision('foo/bar');

=head1 DESCRIPTION

This module retrieves CVS related informations from a checked out
working directory, by scanning the F<CVS/*> files found within.

=head1 METHODS

=over 4

=cut

package Local::Cvsinfo;
use Carp;
use strict;

=item new

This is the constructor.

   my $cvs = Local::Cvsinfo->new();

=cut

sub new {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = {
                TOP      => '',
                IGNORE   => [],
                DIRS     => [],
                FILES    => {},
                GLOBAL_OPTIONS => {},
                SKIP     => sub {},
        };
        bless ($self, $class);
        return $self;
}

=item options

Without arguments, return a reference to a hash array containing the
list of options.
If there is an argument, set some global options for the timelife of
this object.  Argument is a hash array, currently C<recursive>,
C<verbose>, C<skipdir> and C<matchfile> keys are recognized.  Processing
is recursive (resp. verbose) if C<recursive> (resp. C<verbose>) is set
to a non-null value.  The C<skipdir> and C<matchfile> values must be
arrays containing Perl regular expressions, the former specifies
directory to skip in recursive mode (C<CVS> directories are always
skipped), and the latter specifies which files do match (default: all).

   $cvs->options(recursive => 1, matchfile => [ '\.c$' ]);
   while (($key, $val) = each %{$cvs->options()}) {
       print $key . ":" . $val . "\n";
   }

=cut

sub options {
        my $self = shift;
        my %arg  = @_;
        if (!@_) {
                return $self->{GLOBAL_OPTIONS};
        }
        $self->{GLOBAL_OPTIONS} = \%arg;
        $self->{GLOBAL_OPTIONS}->{matchfile} ||= [ '' ];
        $self->{OPTIONS} = $self->{GLOBAL_OPTIONS};
        $self->_fix_skip();
}

sub _verbose {
        my $self = shift;
        return unless $self->{OPTIONS}->{verbose};
        print STDERR "Verbose: ".$_[0] . "\n";
}

=item readinfo

This is where processing is done.  First argument is a directory name,
the F<CVS/Entries> and F<CVS/Repository> files will be searched in that
directory and informations on entries defined within are internally
stored, unless this entry has been discarded by an C<skipdir> attribute.
Optional remaining arguments are a hash array overriding global options.

   $cvs->readinfo("src");
   $cvs->readinfo("src", recursive => 1);

=cut

sub readinfo {
        my $self    = shift;
        my $dir     = shift;
        my %options = @_;
        my @heredir = ();
        my ($entry, $oldoptions, $line);

        $dir =~ s|/+$||;
        -r $dir."/CVS/Entries" or do {
                carp "Unable to read file $dir/CVS/Entries ...  skipped";
                return;
        };
        -r $dir."/CVS/Repository" or do {
                carp "Unable to read file $dir/CVS/Repository ...  skipped";
                return;
        };

        #   Set options
        $oldoptions = $self->{OPTIONS};
        $self->{OPTIONS} = $self->{GLOBAL_OPTIONS};
        if (%options) {
                foreach (keys %options) {
                        $self->{OPTIONS}->{$_} = $options{$_};
                }
        }
        $self->_fix_skip()
                unless ref($oldoptions->{skipdir}) eq "ARRAY"
                and ref($oldoptions->{matchfile}) eq "ARRAY"
                and ref($self->{OPTIONS}->{skipdir}) eq "ARRAY"
                and ref($self->{OPTIONS}->{matchfile}) eq "ARRAY"
                and join("\n", $oldoptions->{skipdir}) eq join("\n", $self->{OPTIONS}->{skipdir})
                and join("\n", $oldoptions->{matchfile}) eq join("\n", $self->{OPTIONS}->{matchfile});

        #   Read CVS/Repository and CVS/Root to determine top-level
        #   directory
        open(REP, "< $dir/CVS/Repository")
                or croak "Unable to read file $dir/CVS/Repository\n";
        $self->_verbose("Reading $dir/CVS/Repository");
        $line = <REP>;
        close(REP);
        if ($line =~ m#^/#) {
                #   Absolute path, we must read CVS/Root
                open(ROOT, "< $dir/CVS/Root")
                        or croak "Unable to read file $dir/CVS/Root\n";
                $self->_verbose("Reading $dir/CVS/Root");
                my $root = <ROOT>;
                close(ROOT);
                chomp $root;
                $root =~ s/^.*://;
                $line =~ s#^$root/##
                        or croak "Unable to determine toplevel CVS directory\n";
        }
        chomp $line;
        $line =~ s{[^/]+}{..}g;
        $line =~ s{^\.\.}{.};
        $line =~ s{^\./}{};
        $self->{TOP} = $line;

        #   Read CVS/Entries
        open(ENTRIES, "< $dir/CVS/Entries")
                or croak "Unable to read file $dir/CVS/Entries\n";
        $self->_verbose("Reading $dir/CVS/Entries");
        my @entries = <ENTRIES>;
        close(ENTRIES);
        #   Entries are sorted so that DIRS is also sorted.
        foreach (sort @entries) {
                chomp;
                if (m|^D/([^/]+)/|) {
                        $entry = $dir."/".$1;
                        next if $self->_skippable($entry) or ! -d $entry;
                        push (@{$self->{DIRS}}, $entry);
                        push (@heredir, $entry);
                        $self->_verbose("Found directory: $entry");
                } elsif (m|^/([^/]+)/([^/]+)/([^/]+)/([^/]*)/(?:T[^/]+)?$|) {
                        $entry = $dir."/".$1;
                        next if $self->_skippable($entry) or ! -f $entry;
                        $self->{FILES}->{$entry} = {
                                REV     => $2,
                                DATE    => $3,
                                KEYWORD => $4,
                        };
                        $self->_verbose("Found file $entry, rev. $2, date $3");
                } elsif (m|^D$|) {
                        #  Hmmm, what is this entry for?
                } else {
                        carp "Unable to parse line:\n\t$_\n";
                }
        }
        return unless $self->{OPTIONS}->{recursive};
        foreach my $d (@heredir) {
                next if $self->_skippable($d);
                $self->readinfo($d, %options);
        }
}

#   Set $self->{SKIP} according to $self->{OPTIONS}->{skipdir} and
#   $self->{OPTIONS}->{matchfile}
sub _fix_skip {
        my $self = shift;
        if (ref($self->{OPTIONS}) eq "HASH") {
                my $sub = "\$_ = shift; if (-d \$_) { return 1 if m{^(.*/)?CVS\$};";
                ref($self->{OPTIONS}->{skipdir}) eq "ARRAY" and do {
                        foreach (@{$self->{OPTIONS}->{skipdir}}) {
                                $sub .= "return 1 if m{/$_\$};";
                        }
                };
                $sub .= "return 0; }";
                ref($self->{OPTIONS}->{matchfile}) eq "ARRAY" and do {
                        foreach (@{$self->{OPTIONS}->{matchfile}}) {
                                $sub .= "return 0 if m{$_};";
                        }
                };
                $sub .= "return 1";
                $self->{SKIP} = eval "sub {$sub}";
        } else {
                $self->{SKIP} = sub {};
        }
}

sub _skippable {
        my $self = shift;
        return 0 unless &{$self->{SKIP}}($_[0]);
        $self->_verbose("Skipping $_[0]");
        return 1;
}

=item reset

Clear all data set by previous call to C<readinfo>.

   $cvs->readinfo("src");
   $cvs->reset();
   $cvs->readinfo("doc");

=cut

sub reset {
        my $self = shift;
        $self->{DIRS}  = [];
        $self->{FILES} = {};
}

=item topdir

Return relative path of the top directory CVS checked out copy.
This path is the one when C<readinfo> was called.

   my $root = $cvs->topdir();

=cut

sub topdir {
        my $self = shift;
        return $self->{TOP};
}

=item removefile

Remove an entry from the file list.

   $cvs->removefile("src/main.c");

=cut

sub removefile {
        my $self = shift;
        delete $self->{FILES}->{$_[0]};
}

=item dirs

Return a reference to the list of directories contained in current
working directory.

   foreach (@{$cvs->dirs()}) {
        print "Found directory: $_\n";
   }

=cut

sub dirs {
        my $self = shift;
        return $self->{DIRS};
}

=item files

Return a reference to file list.

   foreach (@{$cvs->files()}) {
        print "Found file: $_\n";
   }

=cut

sub files {
        my $self = shift;
        return [keys %{$self->{FILES}}];
}

=item revision

First argument is a filename.  If there is no more argument, the CVS
revision of this file is returned, otherwise it is set to the 2nd
argument.

   my $rev = $cvs->revision("src/foo.c");

=cut

sub revision {
        my $self = shift;
        my $file = shift;
        return undef if !defined($self->{FILES}->{$file});
        $self->{FILES}->{$file}->{REV} = $_[0] if @_;
        return $self->{FILES}->{$file}->{REV};
}

=item date

First argument is a filename.  If there is no more argument, the latest
commit date of this file is returned (in a format similar to the
C<date> command output), otherwise it is set to the 2nd argument.

   my $date = $cvs->date("src/foo.c");

=cut

sub date {
        my $self = shift;
        my $file = shift;
        return undef if !defined($self->{FILES}->{$file});
        $self->{FILES}->{$file}->{DATE} = $_[0] if @_;
        return $self->{FILES}->{$file}->{DATE};
}

=item keyword

First argument is a filename.  If there is no more argument, the keyword
substitution method (see the B<-k> flag of the C<cvs> command) for this
file is returned, otherwise it is set to the 2nd argument.

   my $kflag = $cvs->keyword("src/foo.c");

=cut

sub keyword {
        my $self = shift;
        my $file = shift;
        return undef if !defined($self->{FILES}->{$file});
        $self->{FILES}->{$file}->{KEYWORD} = $_[0] if @_;
        return $self->{FILES}->{$file}->{KEYWORD};
}

=back

=head1 AUTHOR

Copyright (C) 2001  Denis Barbier <barbier@debian.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut

1;

© 2014-2024 Faster IT GmbH | imprint | privacy policy