blob: 4185ee7ae87afd1484a7a353516f0ae472ba3489 [file] [log] [blame]
Michael Gottesman19c37352013-04-26 00:58:45 +00001#!/bin/bash
2
3if [ $# -ne 1 ]; then
4 echo "Invalid arguments!"
Renato Golin9e9c2af2014-10-08 09:32:47 +00005 echo "$0 <rNNNNNN | git-hash>"
Michael Gottesman19c37352013-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 Golin9e9c2af2014-10-08 09:32:47 +000016OTHER=$(git svn find-rev "$COMMIT")
Renato Golin39dd1632015-05-16 10:23:48 +000017if [ $? -ne 0 ] || [ "$OTHER" = "" ]; then
Renato Golin9e9c2af2014-10-08 09:32:47 +000018 echo "Error! Could not find an svn/git revision for commit $COMMIT!"
Renato Golin39dd1632015-05-16 10:23:48 +000019 echo
20 echo "Possible problems are:"
21 echo " * Your revision number ($COMMIT) is wrong"
22 echo " * This tree is not up to date (before that commit)"
23 echo " * This commit in in another three (llvm, clang, compiler-rt, etc)"
Michael Gottesman19c37352013-04-26 00:58:45 +000024 exit 1
25fi
26
Renato Golin9e9c2af2014-10-08 09:32:47 +000027if [ -n "$(echo $COMMIT | grep '^r[0-9]\+')" ]; then
28 SVN=`echo $COMMIT | sed -e 's/^r//'`
29 GIT=$OTHER
30else
31 SVN=$OTHER
32 GIT=$COMMIT
33fi
34
Michael Gottesman19c37352013-04-26 00:58:45 +000035# Grab the one line message for our revert commit message.
Renato Golin9e9c2af2014-10-08 09:32:47 +000036ONE_LINE_MSG=$(git log --oneline $GIT -1 | cut -f2- -d " ")
Michael Gottesman19c37352013-04-26 00:58:45 +000037
38# Revert the commit.
Renato Golin9e9c2af2014-10-08 09:32:47 +000039git revert --no-commit $GIT 2>/dev/null
Michael Gottesman19c37352013-04-26 00:58:45 +000040if [ $? -ne 0 ]; then
Renato Golin9e9c2af2014-10-08 09:32:47 +000041 echo "Error! Failed to revert commit r$SVN. Resetting to head."
Michael Gottesman19c37352013-04-26 00:58:45 +000042 git reset --hard HEAD
43 exit 1
44fi
45
46# Create a template in our .git directory.
47TEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
48cat > $TEMPLATE <<EOF
49Revert "$ONE_LINE_MSG"
50
Renato Golin9e9c2af2014-10-08 09:32:47 +000051This reverts commit r$SVN.
Michael Gottesman19c37352013-04-26 00:58:45 +000052EOF
53
54# Begin the commit but give our user an opportunity to edit it.
55git commit --file="$TEMPLATE" --edit
56if [ $? -ne 0 ]; then
Renato Golin9e9c2af2014-10-08 09:32:47 +000057 echo "Error! Failed to commit reverting commit for commit r$SVN. Reverting to head."
Michael Gottesman19c37352013-04-26 00:58:45 +000058 git reset --hard HEAD
59 rm -rf $TEMPLATE
60 exit 1
61fi
62
63rm -rf $TEMPLATE
64