How to make diffs of SVN and Git files with vimdiff

Version control systems like Git or SVN have lots of advantages. One can easily fork a project or create a new branch to test new code. I use Git for all my programs, reports and for my thesis. But sometimes it is necessary to see the difference of two versions of a file. One can use git diff or svn diff but in my opinion the output of these tools are not very nice and it is not easy to see the difference of the files.
In this post I will show you how you can display the difference of two versions of a (SVN or Git) file. The tool I’ll use for this purpose is vimdiff. It’s also possible to use gvimdiff for graphical vim, just replace vimdiff with gvimdiff.

Git:

To use vimdiff as git diff tool is very easy and there are several ways to use vimdiff as git-diff tool. I will show you two of them.

Example:

git difftool --tool=vimdiff --no-prompt

This diff shows the difference between the changes you made and the current index (staging area for the next commit). You can find two links at the end of this post for more information about the usage of git diff.

If you want to use vimdiff always, you just need to tell git to use it as diff tool:

git config --global diff.tool vimdiff
git config --global merge.tool vimdiff

This will set vimdiff as git diff tool for all your git repositories. Omit the –global option, if you don’t want to use vimdiff for all git repositories.
If you also want to avoid the prompt of git to launch vimdiff you can set the difftool.promt to false:

git config --global difftool.prompt false

Now you can use git-difftool without the –tool option, e.g.:

git difftool HEAD HEAD^

This displays the difference of HEAD (left side) and one version behind HEAD (right side). That’s it.

SVN:

To use vimdiff as svn diff tool we need a little helper script. I saved this script in /home/funkym0nk3y/bin, i.e. in my bin folder within my home folder:

#!/bin/sh

# Configure your favorite diff program here.
DIFF="/usr/bin/vimdiff"

# Subversion provides the paths we need as the sixth and seventh
# parameters.
LEFT=${6}
RIGHT=${7}

# Call the diff command (change the following line to make sense for
# your merge program).
$DIFF $LEFT $RIGHT

# Return an errorcode of 0 if no differences were detected, 1 if some were.
# Any other errorcode will be treated as fatal.

I found this script on a website, the link is at the end of this post. Another link about the usage of svn diff is also at the end of this post.

Examples:
To get the difference of a file program.py at revision 100 and revision 99, we can use the following command:

svn diff --diff-cmd=/home/funkym0nk3y/bin/svnvimdiff.sh -c 100 program.py

In this case the old version (revision 99) is on the left side and the new version (revision 100) of program.py is on the right side. If you want to change the order of files (left: new file, right: old file), you just have to switch the Parameter ${6} and ${7} in the script.

To get the difference of a file program.py at revision 100 and revision 97, we can use the following command:

svn diff --diff-cmd=/home/funkym0nk3y/bin/svnvimdiff.sh -r 100:97 program.py

In this case the new file (revision 100) is on the left side and the old file (revision 97) is on the right side. If you want to change the order of the files, switch the parameters in the script (see above). Another way to change the order (left: old file, right: new file) is to switch the revision numbers, i.e.

svn diff --diff-cmd=/home/funkym0nk3y/bin/svnvimdiff.sh -r 97:100 program.py

But it’s not very handy to type the complete command because it’s very long. We can shorten this command by changing the subversion config file and set svnvimdiff.sh as default diff tool. The subversion config file is located at $HOME/.subversion/config. Here is an excerpt of my config file:

[helpers]
diff-cmd = /home/funkym0nk3y/bin/svnvimdiff.sh

Now you can use the svn diff command without the diff-cmd option to display diffs with vimdiff, e.g.:

svn diff -c 100 program.py

Sources:
Git and vimdiff
git diff man page
git diff man page (another one)
SVN-Book: svn-diff
SVN diff Bash Script

Advertisements

Posted on September 3, 2013, in Command-Line, Configure, Network, Programming, Software and tagged , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: