blob: de4ff1cd4663a85fc96134a0d0b113cd2168016c [file] [log] [blame]
Michael Gottesman19c37352013-04-26 00:58:45 +00001#!/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 log -1 $COMMIT | grep git-svn-id | tr -s "@" " " | cut -f 4 -d " ")
18
19if [ -z "$SVN_REVISION" ]; then
20 echo "Error! Given commit is not a git-svn revision!"
21 exit 1
22fi
23
24# Grab the one line message for our revert commit message.
25ONE_LINE_MSG=$(git log --oneline $COMMIT -1 | cut -f2- -d " ")
26
27# Revert the commit.
28git revert --no-commit $COMMIT 2>/dev/null
29if [ $? -ne 0 ]; then
30 echo "Error! Failed to revert commit $COMMIT. Resetting to head."
31 git reset --hard HEAD
32 exit 1
33fi
34
35# Create a template in our .git directory.
36TEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
37cat > $TEMPLATE <<EOF
38Revert "$ONE_LINE_MSG"
39
40This reverts commit r$SVN_REVISION.
41EOF
42
43# Begin the commit but give our user an opportunity to edit it.
44git commit --file="$TEMPLATE" --edit
45if [ $? -ne 0 ]; then
46 echo "Error! Failed to commit reverting commit for commit $COMMIT. Reverting to head."
47 git reset --hard HEAD
48 rm -rf $TEMPLATE
49 exit 1
50fi
51
52rm -rf $TEMPLATE
53