7 use Cwd qw(chdir fastgetcwd);
10 use constant ENTRIES =>"CVS/Entries";
11 use constant CVSIGNORE=>".cvsignore";
12 use constant ROOT =>"CVS/Root";
14 my($opt_files,$opt_ignores,$opt_workings,$opt_dirs);
15 my($opt_rm,$opt_print);
16 our($opt_root); # undefined=>no rooting, ""=>print roots or first phase for root checking, ne ""=>set roots
17 my($opt_verbose,$opt_fatal);
18 my($opt_force); # set roots without their checking
21 $Getopt::Long::ignorecase=0;
22 $Getopt::Long::bundling=1;
23 die if !GetOptions_shortfilter(
24 "f|files!" ,\$opt_files,
25 "i|ignores!" ,\$opt_ignores,
26 "w|workings!",\$opt_workings,
27 "d|dirs!" ,\$opt_dirs,
29 "p|print!" ,\$opt_print,
31 "v|verbose!" ,\$opt_verbose,
32 "fatal!" ,\$opt_fatal,
33 "force!" ,\$opt_force,
34 "I|ignore=s" ,\@opt_ignore,
37 die "--root possible only with (optional and ignored) -d|--dirs"
38 if defined $opt_root && ($opt_files || $opt_ignores || $opt_workings || $opt_dirs || $opt_rm || $opt_print);
39 die "-d|--dirs forbidden with -r|--rm"
40 if $opt_dirs && $opt_rm;
41 die "Nothing to do (no -r|--rm, no -p|--print)"
42 if !$opt_rm && !$opt_print && !defined $opt_root;
43 die "Nothing to process (no -f|--files, no -i|--ignores, no -w|--workings, no -d|--dirs)"
44 if !defined $opt_root && (!$opt_files && !$opt_ignores && !$opt_workings && !$opt_dirs);
46 my @all_ignore=("CVS");
49 @all_ignore=() if $_ eq "!";
52 my($root_contents,@root_dirs);
54 @ARGV=(".") if !@ARGV;
56 our(@dir_files,@dir_ignores,@dir_workings,@dir_victims);
58 unless ((defined $opt_root && $opt_root ne "") && $opt_force) {
59 local($opt_root)=$opt_root;
60 $opt_root="" if defined $opt_root && $opt_root ne "";
64 actionrootfinal() if defined $opt_root && $opt_root eq "";
65 if (defined $opt_root && $opt_root ne "") {
66 die "Non-matching \"".ROOT."\" contents, use --force for override" if !@root_dirs && !$opt_force;
77 $msg.=" in \"".fastgetcwd."\" (CVS \"$dir_dirname\")".($opts{"noerrno"} ? "" : ": $errstr");
78 die $msg if $opt_fatal;
86 my $origdir=fastgetcwd;
89 mayfatal "Unable to process directory \"$_\"";
101 verbose("localdir(\"$localdirname\") entry");
103 local(@dir_dirs,@dir_files,@dir_ignores,@dir_workings,@dir_victims);
104 local($dir_dirname)=$dir_dirname.$localdirname."/";
106 localreaddir() or return;
110 verbose("localdir(\"$localdirname\") exit");
115 localvictims() if !defined $opt_root;
118 fordirs \&localdir,@dir_dirs;
124 if (!open E,ENTRIES) {
125 mayfatal "File \"".ENTRIES."\" cannot be opened";
130 do { push @dir_dirs ,$1; next; } if m#^D/([^/]*)/#;
131 do { push @dir_files,$1; next; } if m#^/([^/]*)/# ;
133 mayfatal "File ".ENTRIES." contains invalid line \"$_\"",("noerrno"=>1);
137 return 1 if defined $opt_root;
140 if (open I,CVSIGNORE) {
144 push @dir_ignores,$_ if -e $_;
151 mayfatal "File \"".CVSIGNORE."\" cannot be opened" if !$!{ENOENT};
155 if (!opendir D,".") {
156 mayfatal "Cannot read directory \".\"";
159 @dir_workings=readdir D;
161 my %delworkings=map { $_=>1; } @dir_workings;
162 for (@dir_dirs,@dir_files,@dir_ignores,@all_ignore,".","..") {
163 delete $delworkings{$_};
165 @dir_workings=keys %delworkings;
171 push @dir_victims,@dir_files if $opt_files;
172 push @dir_victims,@dir_ignores if $opt_ignores;
173 push @dir_victims,@dir_workings if $opt_workings;
174 push @dir_victims,@dir_dirs if $opt_dirs;
181 mayfatal "File \"$filename\" does not exist",("noerrno"=>1) if !-e $filename;
182 print "${dir_dirname}$filename\n";
189 if (!unlink $filename) {
190 mayfatal "File \"$_\" cannot be removed" if !$!{ENOENT};
194 sub localactionrootset
197 if (!open R,'>',ROOT) {
198 mayfatal "File \"".ROOT."\" cannot be written";
201 print R "$opt_root\n";
205 sub localactionrootprint
209 mayfatal "File \"".ROOT."\" cannot be opened";
213 localactionrootprintcheck(<R>);
217 sub localactionrootprintcheck
221 if (!defined $root_contents) {
222 push @root_dirs,$dir_dirname;
223 $root_contents=$contents;
226 if (@root_dirs && $root_contents eq $contents) {
227 push @root_dirs,$dir_dirname;
231 print map "$_: $root_contents",@root_dirs;
234 print "$dir_dirname: $contents";
239 return if !@root_dirs;
240 print $root_contents;
245 if ("" ne $dir_dirname && defined $opt_root) {
246 localactionrootset() if defined $opt_root && "" ne $opt_root;
247 localactionrootprint() if defined $opt_root && "" eq $opt_root;
251 localactionprint $_ if $opt_print;
252 localactionrm $_ if $opt_rm;
260 return if !$opt_verbose;
261 print fastgetcwd.": $msg\n";
264 sub GetOptions_shortfilter
270 push @r,$1,$ref,$',$ref;
275 return GetOptions @r;