1#!/bin/bash
2
3if [ $# -ne 1 ]; then
4    echo "Invalid arguments!"
5    echo "$0 <commit to revert>"
6    exit 1
7fi
8
9if [ -n "$(git status -uno -s --porcelain)" ]; then
10    echo "You have unstashed changes. Please stash and then revert."
11    git status -uno
12    exit 1
13fi
14
15COMMIT=$1
16
17SVN_REVISION=$(git svn find-rev "$COMMIT")
18if [ $? -ne 0 ]; then
19    echo "Error! Could not find an svn revision for commit $COMMIT!"
20    exit 1
21fi
22
23# Grab the one line message for our revert commit message.
24ONE_LINE_MSG=$(git log --oneline $COMMIT -1 | cut -f2- -d " ")
25
26# Revert the commit.
27git revert --no-commit $COMMIT 2>/dev/null
28if [ $? -ne 0 ]; then
29    echo "Error! Failed to revert commit $COMMIT. Resetting to head."
30    git reset --hard HEAD
31    exit 1
32fi
33
34# Create a template in our .git directory.
35TEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
36cat > $TEMPLATE <<EOF
37Revert "$ONE_LINE_MSG"
38
39This reverts commit r$SVN_REVISION.
40EOF
41
42# Begin the commit but give our user an opportunity to edit it.
43git commit --file="$TEMPLATE" --edit
44if [ $? -ne 0 ]; then
45    echo "Error! Failed to commit reverting commit for commit $COMMIT. Reverting to head."
46    git reset --hard HEAD
47    rm -rf $TEMPLATE
48    exit 1
49fi
50
51rm -rf $TEMPLATE
52
53