~ overflow ~

SVN folder cleaning (svn wipe)

by z3n on Dec.04, 2010, under Coding

Problem:

You did a svn checkout on this nice project but now you don’t want that .svn folders all over the place, after all you won’t be commiting it. Suddenly you find out that there’s thousands of folders with thousands of files.

Solution:

I wrote this little php cli script to fix this:

<?php

// (c) z3n - R1V1@101203 - www.overflow.biz - rodrigo.orph@gmail.com

function _o($msg) {
	echo "\r" . substr($msg,0, 79) . str_repeat(" ", 79 - strlen($msg));
}

function rddir($dir) { /* v2.17 */
	global $tfile,$tdir;

	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if ($file != "." && $file != "..") {
					if (is_dir($dir . $file))
						$tdir[] = $dir . $file;
					else
						$tfile[] = $file;
				}
			}
			closedir($dh);
		}
	}
}

if (!isset($argv[1]))
	die("Usage: " . $_SERVER['PHP_SELF'] . " <path/to/svn_root/>");

// browse dirs searching for .svn

for (
	$tdir = array($argv[1]),
	$tfile = array(),
	$svn = array(),
	$i = 0;$i < count($tdir);$i++)
{
	rddir($tdir[$i]);
	_o("Reading: ".$tdir[$i]." ...");

	foreach ($tfile as $entry) {
		if ($entry != "." && $entry != "..") {
			$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
			if ($entry == ".svn" && !in_array($fn, $svn))
				$svn[] = $fn;

			if (is_dir($fn) && $entry != ".svn")
				$tdir[] = $fn;
		}
	}

	$tfile = array();
}

_o("Found: " . count($svn) . " .svn folders, collecting files...");

$files = array();
$dirs = array();
$t = 0;

// collect files from .svn folders

foreach ($svn as $dir) {
	for ($tdir = array($dir), $tfile = array(),$i = 0;$i < count($tdir);$i++) {
		rddir($tdir[$i]);
		_o("[" . $t . "] - Collecting: ". $tdir[$i] ."...");

		foreach ($tfile as $entry) {
			if ($entry != "." && $entry != "..") {
				$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
				$t++;

				if (is_dir($fn)) {
					$tdir[] = $fn;
					$dirs[] = $fn;
				} else {
					$files[] = $fn;
				}
			}
		}
	}
}

$i = count($files) + count($dirs) + count($svn);

// delete files

foreach ($files as $file) {
	_o("[" . $i . "] Deleting Files: " . $file);
	unlink($file);
	$i--;
}

// delete subfolders

rsort($dirs);

foreach ($dirs as $dir) {
	_o("[" . $i . "] Deleting Dir: " . $dir);
	rmdir($dir);
	$i--;
}

// delete root .svn folders

foreach ($svn as $dir) {
	_o("[" . $i . "] Deleting Roots: " . $dir);
	rmdir($dir);
	$i--;
}

echo "\nDone!";

?>
:, , ,


No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!