Skip to content

Instantly share code, notes, and snippets.

@barratis
Forked from rage-shadowman/git-svn-diff.sh
Last active June 2, 2018 12:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barratis/10013729 to your computer and use it in GitHub Desktop.
Save barratis/10013729 to your computer and use it in GitHub Desktop.
fix sed command line
#!/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
#
# 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 $FROM..$TO |
sed -b -e "/--- \/dev\/null/ { N; s|^--- /dev/null\n+++ \(.*\)|--- \1 (revision 0)\n+++ \1 (working copy)|;n}" \
-e "/--- .*/ { N; s|^--- \(.*\)\n+++ /dev/null|--- \1 (revision $SVN_REV)\n+++ \1 (working copy)|; t; s|^--- \(.*\)\n+++ \(.*\)|--- \1 (revision $SVN_REV)\n+++ \2 (working copy)|; n}" \
-e "s/^\(@@.*@@\).*/\1/" \
-e "s/^diff --git [^[:space:]]*/Index:/" \
-e "s/^index.*/===================================================================/" \
-e "/^new file mode [0-9]\+$/d" \
-e "/^deleted file mode [0-9]\+$/d"
@fzakaria
Copy link

the sed command doesn't work on OSX.

$ git-svn-diff.sh                                                                                                                                                                                         
sed: illegal option -- b
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
fatal: ..: '..' is outside repository

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