Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Forked from tbroyer/git-svn-diff.sh
Created February 18, 2011 03:32
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save markjaquith/833214 to your computer and use it in GitHub Desktop.
Save markjaquith/833214 to your computer and use it in GitHub Desktop.
#!/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
# modified by t.broyer@ltgt.net - fixes diffs that introduce new files
# modified by m@rkj.me - fix sed syntax issue in OS X
#
# 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 find-rev $(git rev-list --date-order --max-count=1 $TRACKING_BRANCH)`
# 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.*/===================================================================/"
@vadz
Copy link

vadz commented Apr 23, 2013

In spite of several fixes, this still doesn't seem to handle new files correctly. This gist does work for me however (notably, it uses "working copy" for the new files instead of "revision 0"), so I ended up combining git commands from this one with sed commands from the other one.

@rage-shadowman
Copy link

See my branch for a much cleaner way to get the tip of the tracking branch (using git log the same way that git does to determine which branch to dcommit to -- as per the CAVEATS section of the git-svn man page).

@rage-shadowman
Copy link

The reason it doesn't handle new files correctly is because of the first line of the sed command not adding a space between ---/+++ and \1.

See https://gist.github.com/rage-shadowman/6325382 for an updated version of this which gives identical output (in all of my tests so far) to svn diff.

@jekkos
Copy link

jekkos commented Nov 6, 2013

Seems the script doesn't take chmod messages into account...
new file mode 100755

@songpp
Copy link

songpp commented Jun 29, 2015

on my laptop(osx 10.10.3) sed doesn't write newline ('\n') correctly.

change the line #27

sed -e "/--- \/dev\/null/{ N; s|^--- /dev/null\n+++ \(.*\)|---\1    (revision 0)\n+++\1 (revision 0)|;}" \

to

sed -e '/^--- \/dev\/null/{ N; s|^--- /dev/null\n+++ \(.*\)|---\1   (revision 0)\
+++\1   (revision 0)|;}' \

that is, change double quote to single quote and replace the '\n' by a backslash followed by a newline character.
fixes it.

@vitkarpov
Copy link

vitkarpov commented Jun 13, 2017

Thanks for the script!

Also found that (revision ...) sticks to the path, so the latter is incorrect.

--- -e "s/^--- .*/&(revision $REV)/" \
+++ -e "s/^--- .*/& (revision $REV)/" \

A whitespace is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment