+Id string
[nethome.git] / bin / cvsutil
1 #! /usr/bin/perl
2 #
3 #       $Id$
4
5 use strict;
6 use warnings;
7
8 use Getopt::Long;
9 use Cwd qw(chdir fastgetcwd);
10 use Errno qw(ENOENT);
11
12 use constant ENTRIES  =>"CVS/Entries";
13 use constant CVSIGNORE=>".cvsignore";
14 use constant ROOT     =>"CVS/Root";
15
16 my($opt_files,$opt_ignores,$opt_workings,$opt_dirs);
17 my($opt_rm,$opt_print);
18 our($opt_root); # undefined=>no rooting, ""=>print roots or first phase for root checking, ne ""=>set roots
19 my($opt_verbose,$opt_fatal);
20 my($opt_force); # set roots without their checking
21 my(@opt_ignore);
22
23 $Getopt::Long::ignorecase=0;
24 $Getopt::Long::bundling=1;
25 die if !GetOptions_shortfilter(
26                 "f|files!"   ,\$opt_files,
27                 "i|ignores!" ,\$opt_ignores,
28                 "w|workings!",\$opt_workings,
29                 "d|dirs!"    ,\$opt_dirs,
30                 "r|rm!"      ,\$opt_rm,
31                 "p|print!"   ,\$opt_print,
32                   "root:s"   ,\$opt_root,
33                 "v|verbose!" ,\$opt_verbose,
34                   "fatal!"   ,\$opt_fatal,
35                   "force!"   ,\$opt_force,
36                 "I|ignore=s" ,\@opt_ignore,
37                 );
38
39 die "--root possible only with (optional and ignored) -d|--dirs"
40                 if defined $opt_root && ($opt_files || $opt_ignores || $opt_workings || $opt_dirs || $opt_rm || $opt_print);
41 die "-d|--dirs forbidden with -r|--rm"
42                 if $opt_dirs && $opt_rm;
43 die "Nothing to do (no -r|--rm, no -p|--print)"
44                 if !$opt_rm && !$opt_print && !defined $opt_root;
45 die "Nothing to process (no -f|--files, no -i|--ignores, no -w|--workings, no -d|--dirs)"
46                 if !defined $opt_root && (!$opt_files && !$opt_ignores && !$opt_workings && !$opt_dirs);
47
48 my @all_ignore=("CVS");
49 for (@opt_ignore) {
50         push @all_ignore,$_;
51         @all_ignore=() if $_ eq "!";
52         }
53
54 my($root_contents,@root_dirs);
55
56 @ARGV=(".") if !@ARGV;
57 our(@dir_dirs)=@ARGV;
58 our(@dir_files,@dir_ignores,@dir_workings,@dir_victims);
59 our($dir_dirname)="";
60 unless ((defined $opt_root && $opt_root ne "") && $opt_force) {
61         local($opt_root)=$opt_root;
62         $opt_root="" if defined $opt_root && $opt_root ne "";
63         localdircore();
64         }
65
66 actionrootfinal() if defined $opt_root && $opt_root eq "";
67 if (defined $opt_root && $opt_root ne "") {
68         die "Non-matching \"".ROOT."\" contents, use --force for override" if !@root_dirs && !$opt_force;
69         localdircore();
70         }
71
72 exit 0;
73
74 sub mayfatal
75 {
76 my($msg,%opts)=@_;
77
78         my $errstr=$!;
79         $msg.=" in \"".fastgetcwd."\" (CVS \"$dir_dirname\")".($opts{"noerrno"} ? "" : ": $errstr");
80         die $msg if $opt_fatal;
81         warn $msg;
82 }
83
84 sub fordirs
85 {
86 my($func,@dirs)=@_;
87
88         my $origdir=fastgetcwd;
89         for (@dirs) {
90                 if (!chdir $_) {
91                         mayfatal "Unable to process directory \"$_\"";
92                         next;
93                         }
94                 &$func($_);
95                 chdir $origdir;
96                 }
97 }
98
99 sub localdir
100 {
101 my($localdirname)=@_;
102
103         verbose("localdir(\"$localdirname\") entry");
104
105         local(@dir_dirs,@dir_files,@dir_ignores,@dir_workings,@dir_victims);
106         local($dir_dirname)=$dir_dirname.$localdirname."/";
107
108         localreaddir() or return;
109
110         localdircore();
111
112         verbose("localdir(\"$localdirname\") exit");
113 }
114
115 sub localdircore
116 {
117         localvictims() if !defined $opt_root;
118         localaction();
119
120         fordirs \&localdir,@dir_dirs;
121 }
122
123 sub localreaddir
124 {
125         local *E;
126         if (!open E,ENTRIES) {
127                 mayfatal "File \"".ENTRIES."\" cannot be opened";
128                 return 0;
129                 }
130         while (<E>) {
131                 chomp;
132                 do { push @dir_dirs ,$1; next; } if m#^D/([^/]*)/#;
133                 do { push @dir_files,$1; next; } if m#^/([^/]*)/# ;
134                 next if /^D$/;
135                 mayfatal "File ".ENTRIES." contains invalid line \"$_\"",("noerrno"=>1);
136                 }
137         close E;
138
139         return 1 if defined $opt_root;
140
141         local *I;
142         if (open I,CVSIGNORE) {
143                 while (<I>) {
144                         while (/\S+/g) {
145                                 for (<$&>) {
146                                         push @dir_ignores,$_ if -e $_;
147                                         }
148                                 }
149                         }
150                 close I;
151                 }
152         else {
153                 mayfatal "File \"".CVSIGNORE."\" cannot be opened" if !$!{ENOENT};
154                 }
155
156         local *D;
157         if (!opendir D,".") {
158                 mayfatal "Cannot read directory \".\"";
159                 return 0;
160                 }
161         @dir_workings=readdir D;
162         closedir D;
163         my %delworkings=map { $_=>1; } @dir_workings;
164         for (@dir_dirs,@dir_files,@dir_ignores,@all_ignore,".","..") {
165                 delete $delworkings{$_};
166                 }
167         @dir_workings=keys %delworkings;
168         return 1;
169 }
170
171 sub localvictims
172 {
173         push @dir_victims,@dir_files    if $opt_files;
174         push @dir_victims,@dir_ignores  if $opt_ignores;
175         push @dir_victims,@dir_workings if $opt_workings;
176         push @dir_victims,@dir_dirs     if $opt_dirs;
177 }
178
179 sub localactionprint
180 {
181 my($filename)=@_;
182
183         mayfatal "File \"$filename\" does not exist",("noerrno"=>1) if !-e $filename;
184         print "${dir_dirname}$filename\n";
185 }
186
187 sub localactionrm
188 {
189 my($filename)=@_;
190
191         if (!unlink $filename) {
192                 mayfatal "File \"$_\" cannot be removed" if !$!{ENOENT};
193                 }
194 }
195
196 sub localactionrootset
197 {
198         local *R;
199         if (!open R,'+<',ROOT) {
200                 mayfatal "File \"".ROOT."\" cannot be written";
201                 return;
202                 }
203         print R "$opt_root\n";
204         truncate R,tell R;
205         close R;
206 }
207
208 sub localactionrootprint
209 {
210         local *R;
211         if (!open R,ROOT) {
212                 mayfatal "File \"".ROOT."\" cannot be opened";
213                 return;
214                 }
215         local $/=undef;
216         localactionrootprintcheck(<R>);
217         close R;
218 }
219
220 sub localactionrootprintcheck
221 {
222 my($contents)=@_;
223
224         if (!defined $root_contents) {
225                 push @root_dirs,$dir_dirname;
226                 $root_contents=$contents;
227                 return;
228                 }
229         if (@root_dirs && $root_contents eq $contents) {
230                 push @root_dirs,$dir_dirname;
231                 return;
232                 }
233         if (@root_dirs) {
234                 print map "$_: $root_contents",@root_dirs;
235                 @root_dirs=();
236                 }
237         print "$dir_dirname: $contents";
238 }
239
240 sub actionrootfinal
241 {
242         return if !@root_dirs;
243         print $root_contents;
244 }
245
246 sub localaction
247 {
248         if ("" ne $dir_dirname && defined $opt_root) {
249                 localactionrootset()   if defined $opt_root && "" ne $opt_root;
250                 localactionrootprint() if defined $opt_root && "" eq $opt_root;
251                 return;
252                 }
253         for (@dir_victims) {
254                 localactionprint $_ if $opt_print;
255                 localactionrm    $_ if $opt_rm;
256                 }
257 }
258
259 sub verbose
260 {
261 my($msg)=@_;
262
263         return if !$opt_verbose;
264         print fastgetcwd.": $msg\n";
265 }
266
267 sub GetOptions_shortfilter
268 {
269         my @r;
270         while ($_=shift) {
271                 if (/^(\w)\|/) {
272                         my $ref=shift;
273                         push @r,$1,$ref,$',$ref;
274                         next;
275                         }
276                 push @r,$_;
277                 }
278         return GetOptions @r;
279 }