3 # svn-clean - Wipes out unversioned files from SVN working copy.
4 # Copyright (C) 2004, 2005, 2006 Simon Perreault
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.
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.
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.
26 # Try to use SVN module if available.
27 my $use_svn_module = eval { require SVN
::Client
};
40 "exclude=s" => \
@exclude,
42 "non-recursive|N" => \
$nonrecursive,
48 pod2usage
(1) if $help;
49 pod2usage
( -exitstatus
=> 0, -verbose
=> 2 ) if $man;
50 $path = Cwd
::abs_path
( $ARGV[0] ) if @ARGV;
53 $_ = qr/$_/ foreach @exclude;
55 if ($use_svn_module) {
57 # Create SVN client object. No need for connection to remote server.
58 my $ctx = new SVN
::Client
;
60 # Call handler function with status info for each file.
61 $ctx->status( $path, undef, \
&clean
, !$nonrecursive, 1, 0, 1 );
64 warn "Warning: Not using SVN Perl modules, this might be slow.\n"
67 # Build svn client command
68 my @command = qw(svn status --no-ignore -v);
73 # Main file-wiping loop.
74 if ( $^O
eq 'MSWin32' ) {
76 # Perl on Windows currently doesn't have list pipe opens.
77 open SVN
, join( ' ', @command, @ARGV ) . '|'
78 or die "Can't call program \"svn\": $!\n";
81 open SVN
, "-|", @command, @ARGV
82 or die "Can't call program \"svn\": $!\n";
86 my $file = (split)[-1];
87 foreach my $ex (@exclude) {
89 print "excluded $file\n" unless $quiet or $print;
103 rmtree
( $file, !$quiet, !$force );
109 # Main file-wiping function.
111 my ( $path, $status ) = @_;
113 # Use relative path for pretty-printing.
114 if ( $path =~ s/^\Q$CWD\E\/?//o
) {
116 # If the substitution succeeded, we should have a relative path. Make
117 # sure we don't delete critical stuff.
118 return if $path =~ /^\//;
121 # Find files needing to be removed.
122 if ( $status->text_status == $SVN::Wc
::Status
::unversioned
123 or $status->text_status == $SVN::Wc
::Status
::ignored
124 or $status->text_status == $SVN::Wc
::Status
::deleted
)
126 foreach my $ex (@exclude) {
127 if ( $path =~ $ex ) {
128 print "excluded $path\n" unless $quiet or $print;
133 # Make sure the file exists before removing it. Do not remove deleted
134 # directories as they are needed to remove the files they contain when
136 lstat $path or stat $path;
140 or $status->text_status != $SVN::Wc
::Status
::deleted
)
147 rmtree
( $path, !$quiet, !$force );
157 svn-clean - Wipes out unversioned files from Subversion working copy
161 svn-clean [options] [directory or file ...]
165 B<svn-clean> will scan the given files and directories recursively and find
166 unversioned files and directories (files and directories that are not present in
167 the Subversion repository). After the scan is done, these files and directories
170 If no file or directory is given, B<svn-clean> defaults to the current directory
173 B<svn-clean> uses the SVN Perl modules if they are available. This is much
174 faster than parsing the output of the B<svn> command-line client.
180 =item B<-e>, B<--exclude>
182 A regular expression for filenames to be exluded. For example, the following
183 command will skip files ending in ".zip":
187 svn-clean --exclude '\.zip$'
191 Multiple exclude patterns can be specified. If at least one matches, then the
192 file is skipped. For example, the following command will skip files ending in
197 svn-clean --exclude '\.jpg$' --exclude '\.png$'
201 The following command will skip the entire "build" subdirectory:
205 svn-clean --exclude '^build(/|$)'
209 =item B<-f>, B<--force>
211 Files to which you do not have delete access (if running under VMS) or write
212 access (if running under another OS) will not be deleted unless you use this
215 =item B<-N>, B<--non-recursive>
217 Do not search recursively for unversioned files and directories. Unversioned
218 directories will still be deleted along with all their contents.
220 =item B<-q>, B<--quiet>
222 Do not print progress info. In particular, do not print a message each time a
223 file is examined, giving the name of the file, and indicating whether "rmdir" or
224 "unlink" is used to remove it, or that it's skipped.
226 =item B<-p>, B<--print>
228 Do not delete anything. Instead, print the name of every file and directory that
229 would have been deleted.
231 =item B<-?>, B<-h>, B<--help>
233 Prints a brief help message and exits.
237 Prints the manual page and exits.
243 Simon Perreault <nomis80@nomis80.org>