Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | if [ $# -ne 1 ]; then |
| 4 | echo "Invalid arguments!" |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 5 | echo "$0 <rNNNNNN | git-hash>" |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 6 | exit 1 |
| 7 | fi |
| 8 | |
| 9 | if [ -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 |
| 13 | fi |
| 14 | |
| 15 | COMMIT=$1 |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 16 | OTHER=$(git svn find-rev "$COMMIT") |
Renato Golin | 39dd163 | 2015-05-16 10:23:48 +0000 | [diff] [blame] | 17 | if [ $? -ne 0 ] || [ "$OTHER" = "" ]; then |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 18 | echo "Error! Could not find an svn/git revision for commit $COMMIT!" |
Renato Golin | 39dd163 | 2015-05-16 10:23:48 +0000 | [diff] [blame] | 19 | 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 Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 24 | exit 1 |
| 25 | fi |
| 26 | |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 27 | if [ -n "$(echo $COMMIT | grep '^r[0-9]\+')" ]; then |
| 28 | SVN=`echo $COMMIT | sed -e 's/^r//'` |
| 29 | GIT=$OTHER |
| 30 | else |
| 31 | SVN=$OTHER |
| 32 | GIT=$COMMIT |
| 33 | fi |
| 34 | |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 35 | # Grab the one line message for our revert commit message. |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 36 | ONE_LINE_MSG=$(git log --oneline $GIT -1 | cut -f2- -d " ") |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 37 | |
| 38 | # Revert the commit. |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 39 | git revert --no-commit $GIT 2>/dev/null |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 40 | if [ $? -ne 0 ]; then |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 41 | echo "Error! Failed to revert commit r$SVN. Resetting to head." |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 42 | git reset --hard HEAD |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | # Create a template in our .git directory. |
| 47 | TEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template" |
| 48 | cat > $TEMPLATE <<EOF |
| 49 | Revert "$ONE_LINE_MSG" |
| 50 | |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 51 | This reverts commit r$SVN. |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 52 | EOF |
| 53 | |
| 54 | # Begin the commit but give our user an opportunity to edit it. |
| 55 | git commit --file="$TEMPLATE" --edit |
| 56 | if [ $? -ne 0 ]; then |
Renato Golin | 9e9c2af | 2014-10-08 09:32:47 +0000 | [diff] [blame] | 57 | echo "Error! Failed to commit reverting commit for commit r$SVN. Reverting to head." |
Michael Gottesman | 19c3735 | 2013-04-26 00:58:45 +0000 | [diff] [blame] | 58 | git reset --hard HEAD |
| 59 | rm -rf $TEMPLATE |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | rm -rf $TEMPLATE |
| 64 | |