tests: fix just-introduced typo in rm/ext3-perf
[coreutils.git] / build-aux / cvsu
blob49be49650d1e3a81349123b1fc1d65bda79ef81a
1 #! /usr/bin/perl -w
3 # cvsu - do a quick check to see what files are out of date.
5 # Copyright (C) 2000-2005 Pavel Roskin <proski@gnu.org>
6 # Initially written by Tom Tromey <tromey@cygnus.com>
7 # Completely rewritten by Pavel Roskin <proski@gnu.org>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 require 5.004;
24 use Getopt::Long;
25 use File::Basename;
26 use Time::Local;
27 use strict;
29 use vars qw($list_types %messages %options @batch_list $batch_cmd
30 $no_recurse $explain_type $find_mode $short_print
31 $no_cvsignore $nolinks $file $single_filename $curr_dir
32 @common_ignores $ignore_rx %entries %subdirs %removed);
34 use constant SUBDIR_FOUND => 1;
35 use constant SUBDIR_CVS => 2;
37 # This list comes from the CVS manual.
38 use constant STANDARD_IGNORES =>
39 ('RCS', 'SCCS', 'CVS', 'CVS.adm', 'RCSLOG', 'cvslog.*', 'tags',
40 'TAGS', '.make.state', '.nse_depinfo', '*~', '#*', '.#*', ',*',
41 "_\$*", "*\$", '*.old', '*.bak', '*.BAK', '*.orig', '*.rej',
42 '.del-*', '*.a', '*.olb', '*.o', '*.obj', '*.so', '*.exe',
43 '*.Z', '*.elc', '*.ln', 'core');
45 # 3-letter month names in POSIX locale, for fast date decoding
46 my %months = (
47 "Jan" => 0,
48 "Feb" => 1,
49 "Mar" => 2,
50 "Apr" => 3,
51 "May" => 4,
52 "Jun" => 5,
53 "Jul" => 6,
54 "Aug" => 7,
55 "Sep" => 8,
56 "Oct" => 9,
57 "Nov" => 10,
58 "Dec" => 11
61 # print usage information and exit
62 sub usage ()
64 print "Usage:\n" .
65 " cvsu [OPTIONS] [FILE] ...\n" .
66 "Options:\n" .
67 " --local Disable recursion\n" .
68 " --explain Verbosely print status of files\n" .
69 " --find Emulate find - filenames only\n" .
70 " --short Don't print paths\n" .
71 " --ignore Don't read .cvsignore\n" .
72 " --messages List known file types and long messages\n" .
73 " --nolinks Disable recognizing hard and soft links\n" .
74 " --types=[^]LIST Print only file types [not] from LIST\n" .
75 " --batch=COMMAND Execute this command on files\n" .
76 " --help Print this usage information\n" .
77 " --version Print version number\n" .
78 "Abbreviations and short options are supported\n";
79 exit 0;
82 # print version information and exit
83 sub version ()
85 print "cvsu - CVS offline examiner, version 0.2.3\n";
86 exit 0;
89 # If types begin with '^', make inversion
90 sub adjust_types ()
92 if ($list_types =~ m{^\^(.*)$}) {
93 $list_types = "";
94 foreach (keys %messages) {
95 $list_types .= $_
96 if (index ($1, $_) < 0);
101 # list known messages and exit
102 sub list_messages ()
104 my $default_mark;
105 print "Recognizable file types are:\n";
106 foreach (sort keys %messages) {
107 if (index($list_types, $_) >= 0) {
108 $default_mark = "*";
109 } else {
110 $default_mark = " ";
112 print " $default_mark $_ $messages{$_}\n";
114 print "* indicates file types listed by default\n";
115 exit 0;
118 # Initialize @common_ignores
119 # Also read $HOME/.cvsignore and append it to @common_ignores
120 sub init_ignores ()
122 my $HOME = $ENV{"HOME"};
124 push @common_ignores, STANDARD_IGNORES;
126 unless (defined($HOME)) {
127 return;
130 my $home_cvsignore = "${HOME}/.cvsignore";
132 if (-f "$home_cvsignore") {
134 unless (open (CVSIGNORE, "< $home_cvsignore")) {
135 error ("couldn't open $home_cvsignore: $!");
138 while (<CVSIGNORE>) {
139 push (@common_ignores, split);
142 close (CVSIGNORE);
145 my $CVSIGNOREENV = $ENV{"CVSIGNORE"};
147 unless (defined($CVSIGNOREENV)) {
148 return;
151 my @ignores_var = split (/ /, $CVSIGNOREENV);
152 push (@common_ignores, @ignores_var);
156 # Print message and exit (like "die", but without raising an exception).
157 # Newline is added at the end.
158 sub error ($)
160 print STDERR "cvsu: ERROR: " . shift(@_) . "\n";
161 exit 1;
164 # execute commands from @exec_list with $exec_cmd
165 sub do_batch ()
167 my @cmd_list = split (' ', $batch_cmd);
168 system (@cmd_list, @batch_list);
171 # print files status
172 # Parameter 1: status in one-letter representation
173 sub file_status ($)
175 my $type = shift (@_);
176 my $item;
177 my $pathfile;
179 return
180 if $ignore_rx ne '' && $type =~ /[?SLD]/ && $file =~ /$ignore_rx/;
182 return
183 if (index($list_types, $type) < 0);
185 $pathfile = $curr_dir . $file;
187 if (defined($batch_cmd)) {
188 push (@batch_list, $pathfile);
189 # 1000 items in the command line might be too much for HP-UX
190 if ($#batch_list > 1000) {
191 do_batch();
192 undef @batch_list;
196 if ($short_print) {
197 $item = $file;
198 } else {
199 $item = $pathfile;
202 if ($find_mode) {
203 print "$item\n";
204 } else {
205 $type = $messages{$type}
206 if ($explain_type);
207 print "$type $item\n";
211 # load entries from CVS/Entries and CVS/Entries.Log
212 # Parameter 1: file name for CVS/Entries
213 # Return: list of entries in the format used in CVS/Entries
214 sub load_entries ($);
215 sub load_entries ($)
217 my $entries_file = shift (@_);
218 my $entries_log_file = "$entries_file.Log";
219 my %ent = ();
221 unless (open (ENTRIES, "< $entries_file")) {
222 error ("couldn't open $entries_file: $!");
224 while (<ENTRIES>) {
225 chomp;
226 $ent{$_} = 1;
228 close (ENTRIES);
230 if (open (ENTRIES, "< $entries_log_file")) {
231 while (<ENTRIES>) {
232 chomp;
233 if ( m{^A (.+)} ) {
234 $ent{$1} = 1;
235 } elsif ( m{^R (.+)} ) {
236 delete $ent{$1};
237 } else {
238 # Note: "cvs commit" helps even when you are offline
239 error ("$entries_log_file:$.: unrecognizable line, " .
240 "try \"cvs commit\"");
243 close (ENTRIES);
246 return keys %ent;
249 # process one directory
250 # Parameter 1: directory name
251 sub process_arg ($);
252 sub process_arg ($)
254 my $arg = shift (@_);
255 my %found_files = ();
257 # $file, $curr_dir, and $ignore_rx must be seen in file_status
258 local $file = "";
259 local $ignore_rx = "";
260 local $single_filename = 0;
262 if ( $arg eq "" or -d $arg ) {
263 $curr_dir = $arg;
264 my $real_curr_dir = $curr_dir eq "" ? "." : $curr_dir;
266 error ("$real_curr_dir is not a directory")
267 unless ( -d $real_curr_dir );
269 # Scan present files.
270 file_status (".");
271 opendir (DIR, $real_curr_dir) ||
272 error ("couldn't open directory $real_curr_dir: $!");
273 foreach (readdir (DIR)) {
274 $found_files {$_} = 1;
276 closedir (DIR);
277 } else {
278 $single_filename = basename $arg;
279 $curr_dir = dirname $arg;
280 $found_files{$single_filename} = 1 if lstat $arg;
283 $curr_dir .= "/"
284 unless ( $curr_dir eq "" || $curr_dir =~ m{/$} );
286 # Scan CVS/Entries.
287 my %entries = ();
288 my %subdirs = ();
289 my %removed = ();
291 foreach ( load_entries ("${curr_dir}CVS/Entries") ) {
292 if ( m{^D/([^/]+)/} ) {
293 $subdirs{$1} = SUBDIR_FOUND if !$single_filename;
294 } elsif ( m{^/([^/]+)/([^/])[^/]*/([^/]*)/} ) {
295 if ( !$single_filename or $single_filename eq $1 ) {
296 $entries{$1} = $3;
297 $removed{$1} = 1
298 if $2 eq '-';
300 } elsif ( m{^D$} ) {
301 next;
302 } else {
303 error ("${curr_dir}CVS/Entries: unrecognizable line");
307 if ( $single_filename && !$entries{$single_filename} &&
308 !$found_files{$single_filename} ) {
309 error ("nothing known about $arg");
312 # Scan .cvsignore if any
313 unless ($no_cvsignore) {
314 my (@ignore_list) = ();
316 if (-f "${curr_dir}.cvsignore") {
317 open (CVSIGNORE, "< ${curr_dir}.cvsignore")
318 || error ("couldn't open ${curr_dir}.cvsignore: $!");
319 while (<CVSIGNORE>) {
320 push (@ignore_list, split);
322 close (CVSIGNORE);
325 my ($iter);
326 foreach $iter (@ignore_list, @common_ignores) {
327 if ($iter eq '!') {
328 $ignore_rx = ''
329 } else {
330 if ($ignore_rx eq '') {
331 $ignore_rx = '^(';
332 } else {
333 $ignore_rx .= '|';
335 $ignore_rx .= glob_to_rx ($iter);
338 $ignore_rx .= ')$'
339 if $ignore_rx ne '';
342 # File is missing
343 foreach $file (sort keys %entries) {
344 unless ($found_files{$file}) {
345 if ($removed{$file}) {
346 file_status("R");
347 } else {
348 file_status("U");
353 foreach $file (sort keys %found_files) {
354 next if ($file eq '.' || $file eq '..');
355 lstat ($curr_dir . $file) ||
356 error ("lstat() failed on $curr_dir . $file");
357 if (! $nolinks && -l _) {
358 file_status ("L");
359 } elsif (-d _) {
360 if ($file eq 'CVS') {
361 file_status ("C");
362 } elsif ($subdirs{$file}) {
363 $subdirs{$file} = SUBDIR_CVS;
364 } else {
365 file_status ("D"); # Unknown directory
367 } elsif (! (-f _) && ! (-l _)) {
368 file_status ("S"); # This must be something very special
369 } elsif (! $nolinks && (stat _) [3] > 1 ) {
370 file_status ("H"); # Hard link
371 } elsif (! $entries{$file}) {
372 file_status ("?");
373 } elsif ($entries{$file} =~ /^Initial |^dummy /) {
374 file_status ("A");
375 } elsif ($entries{$file} =~ /^Result of merge/) {
376 file_status ("G");
377 } elsif ($entries{$file} !~
378 /^(...) (...) (..) (..):(..):(..) (....)$/) {
379 error ("Invalid timestamp for $curr_dir$file: $entries{$file}");
380 } else {
381 my $cvtime = timegm($6, $5, $4, $3, $months{$2}, $7 - 1900);
382 my $mtime = (stat _) [9];
383 if ($cvtime == $mtime) {
384 file_status ("F");
385 } elsif ($cvtime < $mtime) {
386 file_status ("M");
387 } else {
388 file_status ("O");
393 # Now do directories.
394 unless ($no_recurse) {
395 my $save_curr_dir = $curr_dir;
396 foreach $file (sort keys %subdirs) {
397 if ($subdirs{$file} == SUBDIR_FOUND) {
398 $curr_dir = $save_curr_dir;
399 file_status ("X");
400 } elsif ($subdirs{$file} == SUBDIR_CVS) {
401 process_arg ($save_curr_dir . $file)
407 # Turn a glob into a regexp without recognizing square brackets.
408 sub glob_to_rx_simple ($)
410 my ($expr) = @_;
411 # Quote all non-word characters, convert ? to . and * to .*
412 $expr =~ s/(\W)/\\$1/g;
413 $expr =~ s/\\\*/.*/g;
414 $expr =~ s/\\\?/./g;
415 return $expr;
418 # Turn a glob into a regexp
419 sub glob_to_rx ($)
421 my $result = '';
422 my ($expr) = @_;
423 # Find parts in square brackets and copy them literally
424 # Text outside brackets is processed by glob_to_rx_simple()
425 while ($expr ne '') {
426 if ($expr =~ /^(.*?)(\[.*?\])(.*)/) {
427 $expr = $3;
428 $result .= glob_to_rx_simple ($1) . $2;
429 } else {
430 $result .= glob_to_rx_simple ($expr);
431 last;
434 return $result;
437 sub Main ()
439 # types of files to be listed
440 $list_types = "^.FCL";
442 # long status messages
443 %messages = (
444 "?" => "Unlisted file",
445 "." => "Known directory",
446 "F" => "Up-to-date file",
447 "C" => "CVS admin directory",
448 "M" => "Modified file",
449 "S" => "Special file",
450 "D" => "Unlisted directory",
451 "L" => "Symbolic link",
452 "H" => "Hard link",
453 "U" => "Lost file",
454 "X" => "Lost directory",
455 "A" => "Newly added",
456 "O" => "Older copy",
457 "G" => "Result of merge",
458 "R" => "Removed file"
461 undef @batch_list; # List of files for batch processing
462 undef $batch_cmd; # Command to be executed on files
463 $no_recurse = 0; # If this is set, do only local files
464 $explain_type = 0; # Verbosely print status of files
465 $find_mode = 0; # Don't print status at all
466 $short_print = 0; # Print only filenames without path
467 $no_cvsignore = 0; # Ignore .cvsignore
468 $nolinks = 0; # Do not test for soft- or hard-links
469 my $want_msg = 0; # List possible filetypes and exit
470 my $want_help = 0; # Print help and exit
471 my $want_ver = 0; # Print version and exit
473 my %options = (
474 "types=s" => \$list_types,
475 "batch=s" => \$batch_cmd,
476 "local" => \$no_recurse,
477 "explain" => \$explain_type,
478 "find" => \$find_mode,
479 "short" => \$short_print,
480 "ignore" => \$no_cvsignore,
481 "messages" => \$want_msg,
482 "nolinks" => \$nolinks,
483 "help" => \$want_help,
484 "version" => \$want_ver
487 GetOptions(%options);
489 adjust_types();
491 list_messages() if $want_msg;
492 usage() if $want_help;
493 version() if $want_ver;
495 unless ($no_cvsignore) {
496 init_ignores();
499 if ($#ARGV < 0) {
500 @ARGV = ("");
503 foreach (@ARGV) {
504 process_arg ($_);
507 if ($#batch_list >= 0) {
508 do_batch();
512 Main();