Also available at

Also available at my website http://tosh.me/ and on Twitter @toshafanasiev

Monday, 25 January 2010

Removing Subversion bindings

There's no rocket science here, but if you find yourself de-svn-ifying directory trees on an even vaguely regular basis, you'll want some automated way of doing it.

If you use Tortoise SVN ( http://tortoisesvn.tigris.org/ ), and you don't mind copying the contents of the directory you are trying to unbind, a very simple solution is to right click in the directory and choose 'export' from the Tortoise menu ( or use svn export on the command line ) - this will export ( i.e. copy directory structure minus bindings ) the tree to the location you specify. It should be noted, however, that it exports the tree in the state in which it finds it, not a copy of the repository you checked out of - any changes you have made locally are exported ( though unversioned files are not ).

If you want to avoid the copy of an export, or otherwise want to remove the source control bindings in place, a script may be the answer.

I'm not much of a shell scripter and I'm a huge fan of Python ( http://python.org/ ) so I wrote a Python script for removing Subversion ( http://subversion.tigris.org/ ) bindings which I have found so useful that I'm sharing it.

Feedback is extremely welcome, but please use with caution - it does remove entire directories.


'''
this utility script was written by tosh afanasiev.
it comes with no warrantee of any sort.

http://tosh.me/
'''
import os, shutil, stat

SVN_DIR = '.svn'

def remove_bindings( dirname, binding_dir=SVN_DIR ):
    '''
    deletes all svn binding directories in a directory tree.
    a different name can be specified for @binding_dir,
    with the result that 
    directories with that name will be removed.
    '''
    
    # walk the directory
    for root, dirs, files in os.walk( dirname ):

        # test for a binding directory
        if binding_dir in dirs:

            # if found, walk the binding directory
            path = os.path.join( root, binding_dir )
            for broot, bdirs, bfiles in os.walk( path ):
                for f in bfiles:
                    # ensure that all files are writeable
                    os.chmod(
                      os.path.join( broot, f )
                    , stat.S_IWRITE
                    )

            # and finally remove the binding directory
            shutil.rmtree( path )

def main():
    '''
    this function is called if you execute the
    script rather than import it
    '''
    dirname = raw_input( 'directory name:\n' )
    remove_bindings( dirname )

if __name__ == '__main__':
    main()

2 comments:

  1. Alternatively, use TortoiseSVN. :-)

    http://tortoisesvn.net/unversion.html

    ReplyDelete
  2. Thanks for sharing this informative post. This article shares some rapid-fire advice for busy developers who need to get the most out of Subversion straight away Remove a binding

    ReplyDelete