~ubuntu-branches/ubuntu/trusty/subversion/trusty-proposed

« back to all changes in this revision

Viewing changes to debian/contrib/svn-clean

  • Committer: Package Import Robot
  • Author(s): Andy Whitcroft
  • Date: 2012-06-21 15:36:36 UTC
  • mfrom: (0.4.13 sid)
  • Revision ID: package-import@ubuntu.com-20120621153636-amqqmuidgwgxz1ly
Tags: 1.7.5-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Create pot file on build.
  - Build a python-subversion-dbg package.
  - Build-depend on python-dbg.
  - Build-depend on default-jre-headless/-jdk.
  - Do not apply java-build patch.
  - debian/rules: Manually create the doxygen output directory, otherwise
    we get weird build failures when running parallel builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
my %svn_clean_ignore;
 
56
if ($use_svn_module) {
 
57
 
 
58
    # Create SVN client object. No need for connection to remote server.
 
59
    my $ctx = new SVN::Client;
 
60
 
 
61
    for my $path (@paths) {
 
62
        my $ign = $ctx->propget('svn-clean:ignore', $path, undef, 1);
 
63
        for my $dir (keys %$ign) {
 
64
            for (split /\n/, $ign->{$dir}) {
 
65
                for (glob "$dir/$_") {
 
66
                    $svn_clean_ignore{$_} = 1 if -e $_;
 
67
                }
 
68
            }
 
69
        }
 
70
        # Call handler function with status info for each file.
 
71
        $ctx->status( $path, undef, \&clean, !$nonrecursive, 1, 0, 1 );
 
72
    }
 
73
    exit 0;
 
74
}
 
75
else {
 
76
    warn "Warning: Not using SVN Perl modules, this might be slow.\n"
 
77
      unless $quiet;
 
78
 
 
79
    my @command = qw(svn propget svn-clean:ignore);
 
80
    push @command, '-R' unless $nonrecursive;
 
81
    open PG, '-|', @command, @paths;
 
82
    my $dir;
 
83
    while (<PG>) {
 
84
        if (s/(.*?) - //) {
 
85
            $dir = $1;
 
86
        }
 
87
        chomp;
 
88
        for (glob "$dir/$_") {
 
89
            $svn_clean_ignore{$_} = 1 if -e $_;
 
90
        }
 
91
    }
 
92
    close PG;
 
93
 
 
94
    @command = qw(svn status --no-ignore -v);
 
95
    if ($nonrecursive) {
 
96
        push @command, '-N';
 
97
    }
 
98
 
 
99
    # Main file-wiping loop.
 
100
    if ( $^O eq 'MSWin32' ) {
 
101
 
 
102
        # Perl on Windows currently doesn't have list pipe opens.
 
103
        open SVN, join( ' ', @command, @paths ) . '|'
 
104
          or die "Can't call program \"svn\": $!\n";
 
105
    }
 
106
    else {
 
107
        open SVN, "-|", @command, @paths
 
108
          or die "Can't call program \"svn\": $!\n";
 
109
    }
 
110
  LINE: while (<SVN>) {
 
111
        if (/^([\?ID])/) {
 
112
            my $file = (split)[-1];
 
113
            next if $svn_clean_ignore{$file};
 
114
            foreach my $ex (@exclude) {
 
115
                if ( $file =~ $ex ) {
 
116
                    print "excluded $file\n" unless $quiet or $print;
 
117
                    next LINE;
 
118
                }
 
119
            }
 
120
            if ( $1 eq 'D' ) {
 
121
                next unless -f $file;
 
122
            }
 
123
            else {
 
124
                next unless -e $file;
 
125
            }
 
126
            if ($print) {
 
127
                print "$file\n";
 
128
            }
 
129
            else {
 
130
                rmtree( $file, !$quiet, !$force );
 
131
            }
 
132
        }
 
133
    }
 
134
}
 
135
 
 
136
# Main file-wiping function.
 
137
sub clean {
 
138
    my ( $path, $status ) = @_;
 
139
    return if $svn_clean_ignore{$path};
 
140
 
 
141
    # Use relative path for pretty-printing.
 
142
    if ( $path =~ s/^\Q$CWD\E\/?//o ) {
 
143
 
 
144
        # If the substitution succeeded, we should have a relative path. Make
 
145
        # sure we don't delete critical stuff.
 
146
        return if $path =~ /^\//;
 
147
    }
 
148
 
 
149
    # Find files needing to be removed.
 
150
    if (   $status->text_status == $SVN::Wc::Status::unversioned
 
151
        or $status->text_status == $SVN::Wc::Status::ignored
 
152
        or $status->text_status == $SVN::Wc::Status::deleted )
 
153
    {
 
154
        foreach my $ex (@exclude) {
 
155
            if ( $path =~ $ex ) {
 
156
                print "excluded $path\n" unless $quiet or $print;
 
157
                return;
 
158
            }
 
159
        }
 
160
 
 
161
        # Make sure the file exists before removing it. Do not remove deleted
 
162
        # directories as they are needed to remove the files they contain when
 
163
        # committing.
 
164
        lstat $path or stat $path;
 
165
        if (
 
166
            -e _
 
167
            and ( not -d _
 
168
                or $status->text_status != $SVN::Wc::Status::deleted )
 
169
          )
 
170
        {
 
171
            if ($print) {
 
172
                print "$path\n";
 
173
            }
 
174
            else {
 
175
                rmtree( $path, !$quiet, !$force );
 
176
            }
 
177
        }
 
178
    }
 
179
}
 
180
 
 
181
__END__
 
182
 
 
183
=head1 NAME
 
184
 
 
185
svn-clean - Wipes out unversioned files from Subversion working copy
 
186
 
 
187
=head1 SYNOPSIS
 
188
 
 
189
svn-clean [options] [directory or file ...]
 
190
 
 
191
=head1 DESCRIPTION
 
192
 
 
193
B<svn-clean> will scan the given files and directories recursively and find
 
194
unversioned files and directories (files and directories that are not present in
 
195
the Subversion repository). After the scan is done, these files and directories
 
196
will be deleted.  Files which match patterns in the I<svn-clean:ignore> dir
 
197
property will be spared, much as the I<svn:ignore> property works for B<svn
 
198
status>.
 
199
 
 
200
If no file or directory is given, B<svn-clean> defaults to the current directory
 
201
(".").
 
202
 
 
203
B<svn-clean> uses the SVN Perl modules if they are available. This is much
 
204
faster than parsing the output of the B<svn> command-line client.
 
205
 
 
206
=head1 OPTIONS
 
207
 
 
208
=over 8
 
209
 
 
210
=item B<-e>, B<--exclude>
 
211
 
 
212
A regular expression for filenames to be exluded. For example, the following
 
213
command will skip files ending in ".zip":
 
214
 
 
215
=over 8
 
216
 
 
217
svn-clean --exclude '\.zip$'
 
218
 
 
219
=back
 
220
 
 
221
Multiple exclude patterns can be specified. If at least one matches, then the
 
222
file is skipped. For example, the following command will skip files ending in
 
223
".jpg" or ".png":
 
224
 
 
225
=over 8
 
226
 
 
227
svn-clean --exclude '\.jpg$' --exclude '\.png$'
 
228
 
 
229
=back
 
230
 
 
231
The following command will skip the entire "build" subdirectory:
 
232
 
 
233
=over 8
 
234
 
 
235
svn-clean --exclude '^build(/|$)'
 
236
 
 
237
=back
 
238
 
 
239
=item B<-f>, B<--force>
 
240
 
 
241
Files to which you do not have delete access (if running under VMS) or write
 
242
access (if running under another OS) will not be deleted unless you use this
 
243
option.
 
244
 
 
245
=item B<-N>, B<--non-recursive>
 
246
 
 
247
Do not search recursively for unversioned files and directories. Unversioned
 
248
directories will still be deleted along with all their contents.
 
249
 
 
250
=item B<-q>, B<--quiet>
 
251
 
 
252
Do not print progress info. In particular, do not print a message each time a
 
253
file is examined, giving the name of the file, and indicating whether "rmdir" or
 
254
"unlink" is used to remove it, or that it's skipped.
 
255
 
 
256
=item B<-p>, B<--print>
 
257
 
 
258
Do not delete anything. Instead, print the name of every file and directory that
 
259
would have been deleted.
 
260
 
 
261
=item B<-?>, B<-h>, B<--help>
 
262
 
 
263
Prints a brief help message and exits.
 
264
 
 
265
=item B<--man>
 
266
 
 
267
Prints the manual page and exits.
 
268
 
 
269
=back
 
270
 
 
271
=head1 AUTHOR
 
272
 
 
273
Simon Perreault <nomis80@nomis80.org>
 
274
 
 
275
=cut