blob: f15e7abfb3f1015bbb073b6865b3b0c289ec8e79 [file] [log] [blame]
Michael Gottesmand8134202013-04-26 00:58:45 +00001#!/bin/bash
2
3if [ $# -ne 1 ]; then
4 echo "Invalid arguments!"
Renato Golindbcc2a92014-10-08 09:32:47 +00005 echo "$0 <rNNNNNN | git-hash>"
Michael Gottesmand8134202013-04-26 00:58:45 +00006 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
Renato Golindbcc2a92014-10-08 09:32:47 +000016OTHER=$(git svn find-rev "$COMMIT")
Michael Gottesman68be5202013-04-26 03:27:39 +000017if [ $? -ne 0 ]; then
Renato Golindbcc2a92014-10-08 09:32:47 +000018 echo "Error! Could not find an svn/git revision for commit $COMMIT!"
Michael Gottesmand8134202013-04-26 00:58:45 +000019 exit 1
20fi
21
Renato Golindbcc2a92014-10-08 09:32:47 +000022if [ -n "$(echo $COMMIT | grep '^r[0-9]\+')" ]; then
23 SVN=`echo $COMMIT | sed -e 's/^r//'`
24 GIT=$OTHER
25else
26 SVN=$OTHER
27 GIT=$COMMIT
28fi
29
Michael Gottesmand8134202013-04-26 00:58:45 +000030# Grab the one line message for our revert commit message.
Renato Golindbcc2a92014-10-08 09:32:47 +000031ONE_LINE_MSG=$(git log --oneline $GIT -1 | cut -f2- -d " ")
Michael Gottesmand8134202013-04-26 00:58:45 +000032
33# Revert the commit.
Renato Golindbcc2a92014-10-08 09:32:47 +000034git revert --no-commit $GIT 2>/dev/null
Michael Gottesmand8134202013-04-26 00:58:45 +000035if [ $? -ne 0 ]; then
Renato Golindbcc2a92014-10-08 09:32:47 +000036 echo "Error! Failed to revert commit r$SVN. Resetting to head."
Michael Gottesmand8134202013-04-26 00:58:45 +000037 git reset --hard HEAD
38 exit 1
39fi
40
41# Create a template in our .git directory.
42TEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
43cat > $TEMPLATE <<EOF
44Revert "$ONE_LINE_MSG"
45
Renato Golindbcc2a92014-10-08 09:32:47 +000046This reverts commit r$SVN.
Michael Gottesmand8134202013-04-26 00:58:45 +000047EOF
48
49# Begin the commit but give our user an opportunity to edit it.
50git commit --file="$TEMPLATE" --edit
51if [ $? -ne 0 ]; then
Renato Golindbcc2a92014-10-08 09:32:47 +000052 echo "Error! Failed to commit reverting commit for commit r$SVN. Reverting to head."
Michael Gottesmand8134202013-04-26 00:58:45 +000053 git reset --hard HEAD
54 rm -rf $TEMPLATE
55 exit 1
56fi
57
58rm -rf $TEMPLATE
59