CommuterJoy » Logbook

« logbook home

Posted by mattc at May 21, 08 09:37 AM ... Comments (0)

I feel a bit dumb for only having just discovered the patch command.

My local file system is full of copies of bits of code that have gradually morphed from what I set out to do to what I ended up with, a path strewn with fruitless (but brave, nonetheless!) diversions. Using Subversion does solve the problem managing ever changing files over time it's pretty bothersome to keep jumping around the revision history in your local working copy or even attempting to compare and run multiple versions of the same file at the same time. Even remembering which revision does what is a bit of a struggle unless you've got a good commit message convention.

I think patch can make this easier by allowing you to store your experiments from the main trunk code as a little library of diff snippets.

Eg.

Let's say you have a JavaScript file called 'original',

-- original --
// returns a charArray of a string
String.prototype.toCharArray = function(){
        var a = this.split("");
        return a;
}

If you copied the above file and added an experiment to it you might end up with this,

-- new --
// returns a charArray of a string
String.prototype.toCharArray = function(){
        if ( this.length == 0 ) // don't want empty arrays
                return false;
        var a = this.split("");
        return a;
}

You can now diff the two files and store the result as a patch file ...

diff orginal new > lengthcheck.patch

... the contents of which looks something like this,

-- lengthcheck.patch --
2a3,4
>       if ( this.length == 0 ) // don't want empty arrays
>               return false;

At some later date you can patch your code with following command.

patch -b orginal lengthcheck.patch

The -b switch makes a backup of your code. Patch will prompt you if it finds a problem and store any rejected patches in a seperate file for you to inspect.

The idea being that in the course of, say, a 2 day hacking session, you can keep the core code in your SVN trunk directory while the deviations, for better or worse, can be stored in a library of diff's that you can periodically merge in and out of your mainline development.


Comments (0)

Post Your Comments

random bookmark
link summary month October 2009 (1)
September 2009 (14)
August 2009 (16)
July 2009 (21)
June 2009 (24)
May 2009 (16)
April 2009 (2)
March 2009 (22)
February 2009 (11)
January 2009 (11)
December 2008 (9)
November 2008 (16)
October 2008 (18)
September 2008 (11)
August 2008 (12)
July 2008 (20)
June 2008 (15)
May 2008 (27)
April 2008 (9)
March 2008 (10)
February 2008 (8)
January 2008 (8)
December 2007 (12)
November 2007 (10)
October 2007 (10)
September 2007 (6)
August 2007 (13)
July 2007 (8)
June 2007 (10)
May 2007 (12)
April 2007 (5)
March 2007 (12)
February 2007 (13)
January 2007 (22)
December 2006 (21)
November 2006 (28)
August 2006 (1)
category code (15)
food (4)
notes (4)
photo (18)
project (2)
quote (12)
sketch (13)
soup (10)
travel (2)