Skip to content

Instantly share code, notes, and snippets.

@jfthuong
Created October 20, 2015 10:49
Show Gist options
  • Save jfthuong/2566a1032ae6719803d2 to your computer and use it in GitHub Desktop.
Save jfthuong/2566a1032ae6719803d2 to your computer and use it in GitHub Desktop.
Script to remove unversioned files and perform svn revert
#!python
import os
import re
import sys
import subprocess
'''
svn_revert_clean.py - script to remove unversioned files and perform svn revert
Usage:
python svn_revert_clean.py [dir]
'''
def remove_unversioned(path):
'''Recursively remove all unversioned files/folders'''
#File
if not os.path.isdir(path):
os.remove(path)
print "Removed %s" % path
return
#Directory
files=os.listdir(path)
for x in files:
fullpath=os.path.join(path, x)
if os.path.isfile(fullpath):
os.remove(fullpath)
print "Removed %s" % fullpath
elif os.path.isdir(fullpath):
remove_unversioned(fullpath)
os.rmdir(path)
if __name__=='__main__':
#Get argument folder
folder = sys.argv[1] if len(sys.argv)>1 else '.'
#SVN revert
subprocess.call('svn revert -R "%s"' % folder)
#Remove unversioned files
unversionedRex = re.compile('^ ?[\?ID] *[1-9 ]*[a-zA-Z]* +(.*)')
for l in os.popen('svn status --no-ignore -v "%s"' % folder).readlines():
match = unversionedRex.match(l)
if match: remove_unversioned(match.group(1))
#-END-#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment