Debian Codegraph @

debian/contrib/svn-clean

raw
1 #!/usr/bin/perl
2
3 # svn-clean - Wipes out unversioned files from SVN working copy.
4 # Copyright (C) 2004, 2005, 2006 Simon Perreault
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 use strict;
21 use Cwd;
22 use File::Path;
23 use Getopt::Long;
24 use Pod::Usage;
25
26 # Try to use SVN module if available.
27 my $use_svn_module = eval { require SVN::Client };
28
29 my $CWD = getcwd;
30
31 my @exclude = ();
32 my $force = 0;
33 my $quiet = 0;
34 my $print = 0;
35 my $help = 0;
36 my $man = 0;
37 my $nonrecursive = 0;
38 my @paths = ($CWD);
39 GetOptions(
40 "exclude=s" => \@exclude,
41 "force" => \$force,
42 "non-recursive|N" => \$nonrecursive,
43 "quiet" => \$quiet,
44 "print" => \$print,
45 "help|?" => \$help,
46 "man" => \$man
47 ) or pod2usage(2);
48 pod2usage(1) if $help;
49 pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
50 @paths = map { Cwd::abs_path($_) } @ARGV if @ARGV;
51
52 # Precompile regexes.
53 $_ = qr/$_/ foreach @exclude;
54
55 if ($use_svn_module) {
56
57 # Create SVN client object. No need for connection to remote server.
58 my $ctx = new SVN::Client;
59
60 # Call handler function with status info for each file.
61 $ctx->status( $_, undef, \&clean, !$nonrecursive, 1, 0, 1 )
62 for @paths;
63 }
64 else {
65 warn "Warning: Not using SVN Perl modules, this might be slow.\n"
66 unless $quiet;
67
68 # Build svn client command
69 my @command = qw(svn status --no-ignore -v);
70 if ($nonrecursive) {
71 push @command, '-N';
72 }
73
74 # Main file-wiping loop.
75 if ( $^O eq 'MSWin32' ) {
76
77 # Perl on Windows currently doesn't have list pipe opens.
78 open SVN, join( ' ', @command, @paths ) . '|'
79 or die "Can't call program \"svn\": $!\n";
80 }
81 else {
82 open SVN, "-|", @command, @paths
83 or die "Can't call program \"svn\": $!\n";
84 }
85 LINE: while (<SVN>) {
86 if (/^([\?ID])/) {
87 my $file = (split)[-1];
88 foreach my $ex (@exclude) {
89 if ( $file =~ $ex ) {
90 print "excluded $file\n" unless $quiet or $print;
91 next LINE;
92 }
93 }
94 if ( $1 eq 'D' ) {
95 next unless -f $file;
96 }
97 else {
98 next unless -e $file;
99 }
100 if ($print) {
101 print "$file\n";
102 }
103 else {
104 rmtree( $file, !$quiet, !$force );
105 }
106 }
107 }
108 }
109
110 # Main file-wiping function.
111 sub clean {
112 my ( $path, $status ) = @_;
113
114 # Use relative path for pretty-printing.
115 if ( $path =~ s/^\Q$CWD\E\/?//o ) {
116
117 # If the substitution succeeded, we should have a relative path. Make
118 # sure we don't delete critical stuff.
119 return if $path =~ /^\//;
120 }
121
122 # Find files needing to be removed.
123 if ( $status->text_status == $SVN::Wc::Status::unversioned
124 or $status->text_status == $SVN::Wc::Status::ignored
125 or $status->text_status == $SVN::Wc::Status::deleted )
126 {
127 foreach my $ex (@exclude) {
128 if ( $path =~ $ex ) {
129 print "excluded $path\n" unless $quiet or $print;
130 return;
131 }
132 }
133
134 # Make sure the file exists before removing it. Do not remove deleted
135 # directories as they are needed to remove the files they contain when
136 # committing.
137 lstat $path or stat $path;
138 if (
139 -e _
140 and ( not -d _
141 or $status->text_status != $SVN::Wc::Status::deleted )
142 )
143 {
144 if ($print) {
145 print "$path\n";
146 }
147 else {
148 rmtree( $path, !$quiet, !$force );
149 }
150 }
151 }
152 }
153
154 __END__
155
156 =head1 NAME
157
158 svn-clean - Wipes out unversioned files from Subversion working copy
159
160 =head1 SYNOPSIS
161
162 svn-clean [options] [directory or file ...]
163
164 =head1 DESCRIPTION
165
166 B<svn-clean> will scan the given files and directories recursively and find
167 unversioned files and directories (files and directories that are not present in
168 the Subversion repository). After the scan is done, these files and directories
169 will be deleted.
170
171 If no file or directory is given, B<svn-clean> defaults to the current directory
172 (".").
173
174 B<svn-clean> uses the SVN Perl modules if they are available. This is much
175 faster than parsing the output of the B<svn> command-line client.
176
177 =head1 OPTIONS
178
179 =over 8
180
181 =item B<-e>, B<--exclude>
182
183 A regular expression for filenames to be excluded. For example, the following
184 command will skip files ending in ".zip":
185
186 =over 8
187
188 svn-clean --exclude '\.zip$'
189
190 =back
191
192 Multiple exclude patterns can be specified. If at least one matches, then the
193 file is skipped. For example, the following command will skip files ending in
194 ".jpg" or ".png":
195
196 =over 8
197
198 svn-clean --exclude '\.jpg$' --exclude '\.png$'
199
200 =back
201
202 The following command will skip the entire "build" subdirectory:
203
204 =over 8
205
206 svn-clean --exclude '^build(/|$)'
207
208 =back
209
210 =item B<-f>, B<--force>
211
212 Files to which you do not have delete access (if running under VMS) or write
213 access (if running under another OS) will not be deleted unless you use this
214 option.
215
216 =item B<-N>, B<--non-recursive>
217
218 Do not search recursively for unversioned files and directories. Unversioned
219 directories will still be deleted along with all their contents.
220
221 =item B<-q>, B<--quiet>
222
223 Do not print progress info. In particular, do not print a message each time a
224 file is examined, giving the name of the file, and indicating whether "rmdir" or
225 "unlink" is used to remove it, or that it's skipped.
226
227 =item B<-p>, B<--print>
228
229 Do not delete anything. Instead, print the name of every file and directory that
230 would have been deleted.
231
232 =item B<-?>, B<-h>, B<--help>
233
234 Prints a brief help message and exits.
235
236 =item B<--man>
237
238 Prints the manual page and exits.
239
240 =back
241
242 =head1 AUTHOR
243
244 Simon Perreault <[email protected]>
245
246 =cut