thinkt4nk
8/3/2011 - 2:48 PM

Remove svn traces from directory structure

Remove svn traces from directory structure

<?php
/**
 * Script to recursively remove all traces of svn from a directory tree
 * Author: Ryan Bales, Creative Anvil 2011
 */
ini_set('memory_limit','512M');

function usage()
{
	ob_start(); ?>

			::UnSvn::
Script to recursively remove all traces of svn from a directory tree

Usage: /path/to/php unsvn.php /path/to/directory
<?php echo ob_get_clean();
}
function rrmdir($dir) { 
   if (is_dir($dir)) { 
     $objects = scandir($dir); 
     foreach ($objects as $object) { 
       if ($object != "." && $object != "..") { 
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
       } 
     } 
     reset($objects); 
     rmdir($dir); 
   } 
} 
function unsvn($files)
{
	foreach( $files as $file ) {
		$filename = str_replace(dirname($file),'',$file);
		if( $filename == '/.' || $filename == '/..' ) {	
			continue;
		} elseif ( $filename == '/.svn' && is_dir($file) ) {
			echo "deleting file {$file}\n";	
			rrmdir($file);
		} else {
			if( is_dir($file) ) {
				// recurse
				$inner_files = scandir($file);
				$dirname = $file;
				$filepaths = array();
				foreach( $inner_files as $inner_file ) {
					$filepaths[] = "{$dirname}/{$inner_file}";
				}
				unsvn($filepaths);		
			}
		}		
	}
}


if( count($argv) > 1 ) {
	if( is_dir($argv[1]) ) {
		unsvn(array($argv[1]));
	} else {
		usage();
	}
} else {
	usage();
}