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