Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
#
# git-svn-diff originally by (http://mojodna.net/2009/02/24/my-work-git-workflow.html)
# modified by mike@mikepearce.net
# modified by aconway@[redacted] - handle diffs that introduce new files
#
# Generate an SVN-compatible diff against the tip of the tracking branch
# Get the tracking branch (if we're on a branch)
TRACKING_BRANCH=`git svn info | grep URL | sed -e 's/.*\/branches\///'`
# If the tracking branch has 'URL' at the beginning, then the sed wasn't successful and
# we'll fall back to the svn-remote config option
if [[ "$TRACKING_BRANCH" =~ URL.* ]]
then
TRACKING_BRANCH=`git config --get svn-remote.svn.fetch | sed -e 's/.*:refs\/remotes\///'`
fi
# Get the highest revision number
REV=`git svn info | grep 'Last Changed Rev:' | sed -E 's/^.*: ([[:digit:]]*)/\1/'`
# Then do the diff from the highest revision on the current branch
# and masssage into SVN format
git diff --no-prefix $(git rev-list --date-order --max-count=1 $TRACKING_BRANCH) $* |
sed -e "/--- \/dev\/null/{ N; s|^--- /dev/null\n+++ \(.*\)|---\1 (revision 0)\n+++\1 (revision 0)|;}" \
-e "s/^--- .*/& (revision $REV)/" \
-e "s/^+++ .*/& (working copy)/" \
-e "s/^diff --git [^[:space:]]*/Index:/" \
-e "s/^index.*/===================================================================/"
@tbroyer

This comment has been minimized.

Copy link

commented Jan 25, 2011

Hi, there's a small bug in handling of diffs that introduce new files: the --- and +++ are missing from the rewritten lines (see my fork at https://gist.github.com/795208 for the fix, see http://codereview.appspot.com/3282042/patch/71001/72004 for an example of a broken patch)

@markjaquith

This comment has been minimized.

Copy link

commented Feb 18, 2011

Line 25 has a sed syntax issue that prevents it from running on my OS X setup. I get this error:

sed: 1: "/--- \/dev\/null/{ N; s ...": bad flag in substitute command: '}'

You need a semicolon before the ending curly brace. Fixed here in my fork.

@mojodna

This comment has been minimized.

Copy link
Owner Author

commented Feb 21, 2011

Thanks for the bug reports! They should be fixed now.

@sebmartin

This comment has been minimized.

Copy link

commented Apr 14, 2011

I found that the $REV value is incorrect if you're not working with the latest svn revision.
I corrected it to use git svn info instead like this:

REV=`git svn info | grep 'Last Changed Rev:' | sed -E 's/^.*: ([[:digit:]]*)/\1/'\`
@mojodna

This comment has been minimized.

Copy link
Owner Author

commented Apr 14, 2011

Updated.

@sebmartin

This comment has been minimized.

Copy link

commented Apr 21, 2011

Bah! There is a run-away slash at the end of the line that I gave you. It should read:

REV=`git svn info | grep 'Last Changed Rev:' | sed -E 's/^.*: ([[:digit:]]*)/\1/'`
@mojodna

This comment has been minimized.

Copy link
Owner Author

commented Apr 21, 2011

Fixed, thanks!

@nmacinnis

This comment has been minimized.

Copy link

commented Nov 23, 2011

Forked to make the script a little friendlier to windows and tortoiseSVN (specifically, not referencing /dev/null on deletes) - https://gist.github.com/1387609

@DanCraft99

This comment has been minimized.

Copy link

commented Apr 23, 2014

Line 26/27/28/29 has a sed syntax issue that prevents it from running on my OS X setup. Can you remove the empty space after each .

After removing the empty spaces, the script work nicely. Thank you.

@anshulguleria

This comment has been minimized.

Copy link

commented Feb 8, 2016

Removed the trailing spaces here https://gist.github.com/anshulguleria/7f107ce67a0fe23c1b0b
Was causing issue in linux also. Please add this change to your gist too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.