public
Last active — forked from markjaquith/git-svn-diff.sh

fixed bug with diff of newly added files

  • Download Gist
git-svn-diff.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/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
# modified by rage-shadowman - cleaned up finding of SVN info and handling of path parameters
# modified by tianyapiaozi - cleaned up some diff context lines
#
# Generate an SVN-compatible diff against the tip of the tracking branch
 
# Usage: git-svn-diff.sh FROM TO
# or: git-svn-diff.sh TO
# or: git-svn-diff.sh
#
# Gets the SVN diff from the latest dcommitted version of FROM to the latest version of TO
 
usage_exit ()
{
echo
echo "Gets the SVN compatible diff from the latest dcommitted version of FROM to the latest version of TO"
echo
echo "Usage: $0 FROM TO"
echo " or: $0 TO"
echo " or: $0"
echo
echo "If FROM is not supplied we will use the latest dcommitted version of HEAD"
echo
echo "If TO is not supplied we will use the latest (possibly non-committed) version of HEAD"
echo
exit 1;
}
 
FROM=${2:+$1}
TO=${2:-$1}
 
# make sure FROM and TO exist or were not specified
if ! git show-branch $FROM >/dev/null 2>&1
then
usage_exit
fi
 
if ! git show-branch $TO >/dev/null 2>&1
then
usage_exit
fi
 
LATEST_DCOMMIT_HASH=`git log $FROM --grep=^git-svn-id: --first-parent -1 --pretty=format:'%H'`
SVN_REV=`git svn find-rev $LATEST_DCOMMIT_HASH`
 
# do the diff and masssage into SVN format
git diff --no-prefix $LATEST_DCOMMIT_HASH $TO |
sed -e "/--- \/dev\/null/{ N; s|^--- /dev/null\n+++ \(.*\)|--- \1 (revision 0)\n+++ \1 (working copy)|;}" \
-e "s/^--- .*/& (revision $SVN_REV)/" \
-e "s/^+++ .*/& (working copy)/" \
-e "s/^\(@@.*@@\).*/\1/" \
-e "s/^diff --git [^[:space:]]*/Index:/" \
-e "s/^index.*/===================================================================/" \
-e "/^new file mode [0-9]\+$/d"

Updated to get starting SHA1 hash and SVN revision using the same mechanism that Git uses to determine where to commit on dcommit. See: https://www.kernel.org/pub/software/scm/git/docs/git-svn.html#_caveats

Added ability to specify from/to branches with usage check.

Quickly fixed this script to handle rename and deletion correctly: https://gist.github.com/barratis/10013729

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.