Does anybody know a way to recursively remove all files in a working copy that are not under version control? (I need this to get more reliable results in my automatic build
I would add this as a comment to Thomas Watnedal's answer , but can't yet.
A minor issue with it (which won't affect Windows) is that it only checks for files or directories. For Unix like systems where symbolic links may be present, it is necessary to change the line:
if os.path.isfile(fullpath):
to
if os.path.isfile(fullpath) or os.path.islink(fullpath):
to also remove links.
For me, changing the last line if match: removeall(match.group(1))
into
if match:
print "Removing " + match.group(1)
removeall(match.group(1))
so that it displays what it is removing was useful too.
Depending on the use case, the ?[\?ID]
part of the regular expression may be better as ?[\?I]
, as the D
also removes deleted files, which were under version control. I want to use this to build in a clean, checked in folder, so there should be no files in a D
state.
A clean way to do this in PERL would be:
#!/usr/bin/perl
use IO::CaptureOutput 'capture_exec'
my $command = sprintf ("svn status --no-ignore | grep '^?' | sed -n 's/^\?//p'");
my ( $stdout, $stderr, $success, $exit_code ) = capture_exec ( $command );
my @listOfFiles = split ( ' ', $stdout );
foreach my $file ( @listOfFiles )
{ # foreach ()
$command = sprintf ("rm -rf %s", $file);
( $stdout, $stderr, $success, $exit_code ) = capture_exec ( $command );
} # foreach ()
this works for me in bash:
svn status | egrep '^\?' | cut -c8- | xargs rm
Seth Reno's is better:
svn status | grep ^\? | cut -c9- | xargs -d \\n rm -r
It handles unversioned folders and spaces in filenames
As per comments below, this only works on files that subversion doesn't know about (status=?). Anything that subversion does know about (including Ignored files/folders) will not be deleted.
If you are using subversion 1.9 or greater you can simply use the svn cleanup command with --remove-unversioned and --remove-ignored options
See: svn-clean
Subversion 1.9.0 introduced option to remove unversioned items [1]
svn cleanup --remove-unversioned
[1] https://subversion.apache.org/docs/release-notes/1.9.html#svn-cleanup-options
@zhoufei I tested your answer and here is updated version:
FOR /F "tokens=1* delims= " %%G IN ('svn st %~1 ^| findstr "^?"') DO del /s /f /q "%%H"
FOR /F "tokens=1* delims= " %%G IN ('svn st %~1 ^| findstr "^?"') DO rd /s /q "%%H"
%
marks in front of G and H%~1
can be used any directory name, I used this as a function in a bat file, so %~1
is first input paramter