Skip to content

Instantly share code, notes, and snippets.

@quodlibetor
Created July 31, 2014 17:06
Show Gist options
  • Save quodlibetor/4a9f5e488ce5ac51fd8a to your computer and use it in GitHub Desktop.
Save quodlibetor/4a9f5e488ce5ac51fd8a to your computer and use it in GitHub Desktop.
git-svndiff
#!/bin/bash
#
# git-svn-diff
# Generate an SVN-compatible diff against the tip of the tracking branch
# for subgit this expects that you have added the following line to your
# .git/config, and to have run git-fetch recently:
#
# fetch = +refs/svn/map:refs/notes/commits
#
# from http://subgit.com/book-remote/index.html#N2048A
set -e
handle_error() {
echo "FAILED: line $1, exit code $2"
exit 1
}
git_svn_tracking() {
git config --get svn-remote.svn.fetch | sed -e 's/.*:refs\/remotes\///'
}
subgit_tracking() {
git rev-parse --abbrev-ref --symbolic-full-name HEAD@{upstream}
}
subgit_rev() {
git notes show $1 | cut -d ' ' -f1 | tr -d 'r'
}
git_svn_rev() {
tracking=$1
git svn find-rev $( git rev-list --date-order --max-count=1 $tracking 2>/dev/null)
}
git_root() {
git rev-parse --show-toplevel
}
git_svn_repo() {
svn_dir=`git_root`/.git/svn
if [ -d $svn_dir ] ; then
echo -n 1
fi
}
in_git_svn_repo=$( git_svn_repo )
if [ -n "$in_git_svn_repo" ] ; then
TRACKING=$( git_svn_tracking )
else
TRACKING=$( subgit_tracking )
fi
if [ -n "$in_git_svn_repo" ] ; then
SVN_REV=$( git_svn_rev $TRACKING )
else
SVN_REV=$( subgit_rev $TRACKING )
fi
if [[ -z "$SVN_REV" ]] ; then
echo "couldn't find SVN revision for diff"
exit 1
fi
if [ -n "$1" ] ; then
BASE=$1
else
BASE=$TRACKING
fi
diff_base=$(git rev-list --date-order --max-count=1 $BASE)
git diff --no-prefix $diff_base | \
sed -e "s/^+++ .*/& (working copy)/" -e "s/^--- .*/& (revision $SVN_REV)/" \
-e "s/^diff --git [^[:space:]]*/Index:/" \
-e "s/^index.*/===================================================================/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment