The Truth is Out There!
Erik's Weblog
 

Custom Software Solutions for Everyday Problems

shim

February 14, 2006

[@124]

Subversion diff with vim

Once in a while I have to use Subversion on a Linux box. Not a big deal, except for diffs. The standard svn diff output is not the most readable. Vim, or more exactly vimdiff, does a terrific job at displaying differences between files side-by-side. Here's a little Bourne shell script I wrote that uses vimdiff to view the differences between a local file and the latest revision in the repository:

Nothing too fancy. It uses svn cat to get the latest rev, saves it to a temporary file, and opens both files in vimdiff:
The temp file is deleted as soon as vimdiff is quit.
[@389]

I also just found svncommand.vim, a nifty Subversion integration plugin.

Tags: , ,
shim


 

svn diff --diff-cmd vimdiff
will do it.

# Tue, Feb 14 2006 at 18:25:22 PST

The --diff-cmd option returns arguments that are not understood by vimdiff. Someone wrote a python hack to re-arrange the parameters, but I find my script much easier to use.

E.

# Tue, Feb 14 2006 at 18:55:58 PST

There is a really nice Linux tool for visual diffs, it is called meld (http://meld.sourceforge.net/).

Give it a try, it does understand Subversion and all you have to do is:
$ meld <your_file>

It can also show diffs for a whole folder structure, just use a folder name instead of a file name.

# Wed, Feb 15 2006 at 17:20:01 PST

Your script works better if the extension of the temporary file is the same as for the original file.
That way syntax highlighting works also for the temporary file. I modified the temp file name generation in your script like so:

TEMP=/tmp/tmp.$$.$1

Thanks for creating you script. It works great. And I'm happy to be able to use vimdiff, which I'm familiar with.

# Mon, Apr 3 2006 at 09:20:35 PDT

Thanks, Fernando. I've changed the script accordingly.

# Mon, Apr 3 2006 at 10:23:08 PDT
Francesc writes:

Good tip!

I've found it has problems using directories (ie, 'svndiff lib/foo.php') but it works if you change temp file name with this:

TEMP=/tmp/tmp.$$.`basename $1`

# Thu, May 11 2006 at 03:27:04 PDT

Thanks, Francesc. I'll update the script with your suggestion.

# Thu, May 11 2006 at 10:44:27 PDT
fREW Schmidt writes:

Hi Erik,
I don't know if you actually do any editing when you use this script.  I personally don't so I found it useful to do the following:

vimdiff -R $TEMP $1

Only because I tend to leave files open in vim in between commits and I got sick of the error message and whatnot.

# Sat, Nov 25 2006 at 20:36:25 PST
wulf writes:

I added a option -r maybe it is also interesting for you or others.


You will find my version here
http://gpl.coulmann.de/svn_diff

# Sun, Mar 25 2007 at 03:20:09 PDT
Balbir Singh writes:

I would suggest a trap in the begining to make sure the temp file is removed no matter how the script exits.

e.g.

trap 'rm -f $tempfile' 0 1 2 15

Thanks for the idea.

# Fri, Apr 6 2007 at 20:01:52 PDT

Enter Your Comment:

  (e.g. Jon Q. User)
  (e.g. http://www.example.com or jon@foo.com)
 
 
(URL allowed — [url="http://www.example.com/"]title[/url] or [url]http://example.com/[/url] — rel="nofollow" enabled)

Comment Preview:

   

This article was published on February 14, 2006. RSSAtom
Editor: Erik C. Thauvin