Unsvn - a way to un-checkout an svn repository

Sometimes other developers push up .svn directories to production. This drives me nuts. Not only does it create clutter but the directory contains sensitive information about where your SVN repository is located and who the user is.

Removing these directories is a pain to do manually, so why bother? The C# code below will remove all .svn directories & all the files beneath them for any given path. See the source below or just download the Windows Binary.

using System;
using System.IO;

namespace UnSvn
{
class Program
{
static void Main(string[] args)
{
if (args == null || args.Length != 1)
{
Usage();
return;
}

DirectoryInfo root = new DirectoryInfo(args[0]);

try
{
if(!Process(root))
{
Console.WriteLine("Nothing to do");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}

static void Usage()
{
Console.WriteLine("usage: unsvn [/path/to/directory]");
Console.WriteLine("unsvn will remove all \".svn\" directories and files from the given path");
}

static bool Process(DirectoryInfo di)
{
bool removed = false;

if (String.Equals(di.Name, ".svn", StringComparison.OrdinalIgnoreCase))
{
SetNormal(di);
Console.WriteLine(String.Format("Removed: {0}", di.FullName));
Directory.Delete(di.FullName, true);
removed = true;
}
else
{
foreach (DirectoryInfo di2 in di.GetDirectories())
{
removed = removed | Process(di2);
}
}

return removed;
}

static void SetNormal(DirectoryInfo di)
{
foreach (FileInfo fi in di.GetFiles())
{
File.SetAttributes(fi.FullName, FileAttributes.Normal);
}

foreach (DirectoryInfo di2 in di.GetDirectories())
{
SetNormal(di2);
}
}
}
}


And for those on Macs, below is a Python implementation (that has not been fully tested, but works well enough for me).

#!/usr/bin/python
import os, sys

def main():
if len(sys.argv) != 2: usage()
elif unsvn(sys.argv[1]) == False:
print "Nothing to do"
print

def usage():
print "usage: unsvn [/path/to/directory]"
print "unsvn will remove all \".svn\" directories and files from the given path"

def unsvn(path):
removed = False
for file in os.listdir(path):
subpath = os.path.join(path, file)
if file == ".svn":
removeDir(subpath)
removed = True
print "Removed: " + subpath
elif os.path.isdir(subpath):
removed = removed or unsvn(subpath)
return removed

def removeDir(path):
if os.path.isdir(path) == False:
os.unlink(path)
return
for file in os.listdir(path):
removeDir(os.path.join(path, file))
os.rmdir(path)

main()

Comments

Popular posts from this blog

Fluent NHibernate - Incorrect syntax near the keyword 'Group'

Fluent NHibernate + Encrypting Values

MapReduce in C# using Task Parallel Library