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