Posts Tagged ‘svn’

Remove files recursively..

// March 10th, 2009 // Comments Off on Remove files recursively.. // powershell, Source Control

I had a problem a couple of weeks ago, when my main Subversion repository completely died out of data corruption. I was trying to check-in my latest changes, but I just couldn’t. Nothing was lost since I do regular backups of my repositories. So I was in good shape. I thought to myself this was the best chance to migrate to TFS, which I have been wanting to do for a while.

Note: I did not leave subversion in favor of TFS because of this corruption. This data corruption was expected to happen because the machine usually gets killed in the middle of backup or by the occasional  “Windows Update”; so the repository was destined to die eventually. I could have fixed the repository in no time with the tools that Subversion provides or simply by restoring backups. But I moved to TFS mainly because of the project integration in all aspects and not only source control, and because most of my clients use TFS, so integration with their systems is also easier.

Anyways I needed a clean copy of my newest changes so that I could import it into TFS. I felt lazy at the moment and I chose the complex solution, clean up my subversion repository copy. My project has a LOT of folders, so deleting the .svn folders became a HUGE effort. I decided to use a simple shell command to do that, like I just to do in linux:

rm -rf `find . -type d -name .svn`

So I started looking on google to see what was the way to it in Powershell and I found really complex solutions like:

$fso = New-Object -com "Scripting.FileSystemObject";
$folder = $fso.GetFolder("C:\\Test\\");

foreach ($subfolder in $folder.SubFolders) {
    if ($subfolder.Name -like "*.svn"){
        remove-item $subfolder.Path -Verbose
    }
}

Source: Here

It really scared me! I thought that, if that was the easiest way to delete recursively, then powershell is doomed. Thanks to google, I did find a simpler way to do this:

dir . -recurse .svn -fo | remove-item -force

Which is quite elegant and fast! Sometimes I just don’t understand why it is so difficult to come up with the simple answer. It happens to me quite often. It is extremely easy to come up with a complex solution…but to actually have a simple solution is always difficult. This is only one example.

Anyways this did the trick and it works perfectly.

[Update]: I forgot to add the ‘-fo’ option which shows the hidden files (in this case the .svn folders). Otherwise this would not work.

Subversion Revision Number through Powershell

// August 1st, 2008 // 2 Comments » // powershell

For a project I am working on, I needed to get the latest revision of the project. I need this in order to building the assemblies with the latest revision in the assembly info and also to added it the final install package. I am currently using powershell to do all my builds, and I enjoy it very much. There is a lot of power in it.

Here is the script:

# GetLatestRevisionNumber.ps1
#
# Description :
#   Script that returns the latest revision of the selected project.
#
# Author(s):
#   Alejandro Espinoza (aespinoza AT structum.com.mx)
#
# Usage: GetLatestRevision.ps1 c:\svn\svn.exe svn://localhost/MyProject
param([string] $svnExe, [string] $svnServer)

### Method for returning the latest repository revision.
function GetLatestRevisionNumber()
{
    $revLinePattern = "(.*?)(Revision: )(\d+\.?\d*)(.*?)";
    $revNumberPattern = "(\d+\.?\d*)";

    $result = &$svnExe info $svnServer;

    $revision = 0;

    if([regex]::Match($result, $pattern)) {
        $matches = [regex]::Split($result,$revLinePattern);
        $revision = $matches[3];
    }
    return $revision;
}

GetReposRevision;

To use this script just call like this: GetLatestRevision.ps1 c:\svn\svn.exe svn://localhost/MyProject. It only requires the subversion executable path and the projects repository path.

What the script does is get the project’s information and parses the revision number from it. It is quite simple; I would love to have a better way to get it, but this works.