Pavel P
Pavel P

Reputation: 16996

svn: cleanup/revert recursively a folder to make sure that it has no files that aren't part of SVN

When I recursively cleanup a folder in SVN I have some files that aren't part of SVN (e.g. svn status lists them with a ?). So, my question is: how do I tell SVN to delete all these files, so that my svn status would return empty, e.g. identical to SVN server.

one way would be:

rm -r /path/to/localcopy/somefolder && svn update /path/to/localcopy/somefolder

but this isn't acceptable, because that somefolder contains LOTS of files that I don't want to checkout everytime.

Upvotes: 1

Views: 803

Answers (1)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24083

EDIT: Updated to work with file paths containing whitespace, except for newlines. Tested in bash 3.2.17 on Mac OS X.

Also see these questions.

#!/bin/bash

OLD_IFS=$IFS
IFS=$'\n'
for path in $(svn st | awk '/^\?/ { $1=""; print substr($0, 2) }'); do
    rm -r "$path"
done
IFS=$OLD_IFS

Let's break this down.

OLD_IFS=$IFS
IFS=$'\n'

Saves the old value of $IFS (the shell's input field separator) and changes the value to a newline. This will let us loop over things containing spaces, only going to the next iteration when we hit a newline character.

for path in $( ...

Loop through the results of the command substitution inside $( ).

svn st | awk '/^\?/ ...

Prints all lines from svn st (same as svn status) that begin with ?.

{ $1=""; print substr($0, 2) }'

Removes ? and leading whitespaces, leaving only the un-versioned paths in your working copy.

    rm -r "$path"

Delete the path, passing -r in case the path is a directory. There's no need to check if the path is a directory because an un-versioned directory can't contain versioned content.

IFS=$OLD_IFS

Restore the value of the shell's input field separator.

Upvotes: 1

Related Questions