Saturday, November 10, 2007

Unix Command To Delete Corrupted Subversion Meta-data

Did you ever find yourself in a situation where you have projects checked-out from source control and the directory-based source control meta-data (from CVS or Subversion) has somehow become irreparably corrupted (or the fix to the corruption is non-obvious)? You do not want to lose local working-copy changes, but because of the corruption you cannot rebuild the meta-data, re-connect to source control, or overlay freshly-updated project files from the source control system without the potential loss of your local workspace changes?
A solution to this problem is to delete only the project's meta-data directories, back-up the project files to a temporary directory, re-check out the project code from the source control system, and overlay the backed-up project files onto the pristine project (which has uncorrupted meta-data).

This is the simple Unix command I use for deleting all the Subversion meta-data in a directory and in its subdirectories:

  • find . -name .svn -type d -print | xargs rm -r -f

It is pretty straightforward to interpret what is happening with this command. It is finding any directory (-type d) named .svn in the current directory (.) and its subdirectories. Each of those directories is deleted (rm -r) without prompting (-f). -print | xargs can be interpretted as use each result from the find command as input to the rm command.

find is very robust in what it can take as input, so a more general version of this line could be rewritten as:

  • find </path/to/a/directory> -name <a_regex> -type <d | f> -print | xargs rm -r -f

It can then be used to delete from a branch of the filesystem heirarchy any files or directories whose names match some pattern.


marsosx said...

Fantastic, thank you! What a powerful command - it saved my day when I just couldn't get "svn export" to work (still new to Subversion)!

Tim Myer said...

Very happy to be of help. Thanks for the feedback!