119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman#!/bin/bash
219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ $# -ne 1 ]; then
419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "Invalid arguments!"
519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "$0 <commit to revert>"
619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
719c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
919c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ -n "$(git status -uno -s --porcelain)" ]; then
1019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "You have unstashed changes. Please stash and then revert."
1119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    git status -uno
1219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
1319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
1419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
1519c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanCOMMIT=$1
1619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
17276b8549ee177ee584699638dd17eb358e25ace1Michael GottesmanSVN_REVISION=$(git svn find-rev "$COMMIT")
18276b8549ee177ee584699638dd17eb358e25ace1Michael Gottesmanif [ $? -ne 0 ]; then
19276b8549ee177ee584699638dd17eb358e25ace1Michael Gottesman    echo "Error! Could not find an svn revision for commit $COMMIT!"
2019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
2119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
2219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
2319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Grab the one line message for our revert commit message.
2419c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanONE_LINE_MSG=$(git log --oneline $COMMIT -1 | cut -f2- -d " ")
2519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
2619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Revert the commit.
2719c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmangit revert --no-commit $COMMIT 2>/dev/null
2819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ $? -ne 0 ]; then
2919c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "Error! Failed to revert commit $COMMIT. Resetting to head."
3019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    git reset --hard HEAD
3119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
3219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
3319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
3419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Create a template in our .git directory.
3519c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanTEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
3619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmancat > $TEMPLATE <<EOF
3719c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanRevert "$ONE_LINE_MSG"
3819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
3919c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanThis reverts commit r$SVN_REVISION.
4019c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanEOF
4119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
4219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Begin the commit but give our user an opportunity to edit it.
4319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmangit commit --file="$TEMPLATE" --edit
4419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ $? -ne 0 ]; then
4519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "Error! Failed to commit reverting commit for commit $COMMIT. Reverting to head."
4619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    git reset --hard HEAD
4719c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    rm -rf $TEMPLATE
4819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
4919c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
5019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
5119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanrm -rf $TEMPLATE
5219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
53