Art Of Programming

musings by Dmytrii Nagirniak

Extract Changed Files Only With SVN

I often need to get the whole directory tree of changed file. ONLY changed files. I used to do it by filtering the files in Total Commander by modification date.

Now I’ve just prepared small script to do that for me using SVN and SharpScript. You may tune it for your need, but here’s the good starting point for that.

Requirements:

  • ScriptSharp installed in Program Files.
  • SVN client.
  • Resulting folder is always ‘Changes’.
  • Place these files in the same folder that is under SVN.

 

Usage: run get-svn-status.bat (or just double click it).

get-svn-status.bat:

del svn-changes.txt
svn status >> svn-changes.txt
"C:\Program Files\Menees\SharpScript\SharpScriptConsole.exe" grabChanges.scs

 

grabChanges.scs:

using SharpScript;
using System;
using System.IO;
using System.Linq;

class Program
{
    static bool IsChange(string line) {
        char mark = line[0]; // Rip off with nulls
        // http://knaddison.com/technology/svn-status-code-cheat-sheet
        switch (mark) {
            case 'M': // Modified
            case 'A': // Added
            case 'C': // Confilict
            case 'G': // merGed from repo to working copy            
            return true;
        }
        return false;
    }
    
    public static void Main(string[] arArgs)
    {
        var folder = "Changes";
        try {
            Directory.Delete(folder, true);
        } catch(DirectoryNotFoundException) {}; // We know why
        Directory.CreateDirectory(folder);
        
        using(var fs = File.OpenText("svn-changes.txt")) {
            string line = "";
            while ((line = fs.ReadLine()) != null) {
                if (!IsChange(line)) continue;
                Script.Echo(line);
                var filePath = line.Substring(7);
                var dest = folder + "\\" + filePath;
                
                Directory.CreateDirectory(Path.GetDirectoryName(dest));                
                File.Copy(filePath, dest, true/*overwrite*/);
            }            
        }
        
        Script.Echo("Done");
    }
}

 

 

Enjoy.

Comments

Dmytrii Nagirniak
Hi Alex,

Thanks for the info.
I’ll try this out.
Alexander
Yes. Pinging.
There is a set of unix POSIX utils ported to WIndows, and having not that much deps. This package is called UnixUtils - http://unxutils.sourceforge.net/

This package includes most of the hardly used utilities, and Bourne shell implementation so you can run unix script on your win machine (surely, not often :)

The only thing required to do is to dl them, unpack somewhere and add /bin folder to your PATH environment variable.

Believe, having all this small text-processing utilities is VERY handsome for development, whatever you do.

Just to get some info on how to use these utils, search for unix Man page.
Dmytrii Nagirniak
Alex,
I have to do it on Windows, so Ubuntu is not for me.
I believe there should be something already available, but I just wrote it for my particular case.
Anywat, if you’ll see something like that for Win, please ping me.

Cheers.
Alexander
And, btw, found two more things:

1. Not sure you really need, but you always append the modified file list to the file, so after you commit come changes, make more changes again and run your script once more, you’ll get ‘old’ files copied to the result folder, thous they are already in the repo

2. When checking the modification flag of svn status command, you don’t check for ‘?’ (unversioned) files. They are usually treated as modified, too (they are new)

Surely, these two messages are just IMHO - I can not be sure about your intentions :)


THanks, anyway!
Alexander
Errr.
So, the code of the command itself follows:

cp `svn stat | grep ^[MG?AC] | awk ‘{print $2}’` ~/test
Alexander
When i hear about scripting support in the OS, I always think that my Ubuntu decision is Awesome!

You could do it within Ubuntu with the following command
lexaux@lexaux-laptop:12:33:38:main $cp `svn stat | grep ^[MG?AC] | awk ‘{print $2}’` ~/test

Comments