2
0

git-svnrevert 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. if [ $# -ne 1 ]; then
  3. echo "Invalid arguments!"
  4. echo "$0 <rNNNNNN | git-hash>"
  5. exit 1
  6. fi
  7. if [ -n "$(git status -uno -s --porcelain)" ]; then
  8. echo "You have unstashed changes. Please stash and then revert."
  9. git status -uno
  10. exit 1
  11. fi
  12. COMMIT=$1
  13. OTHER=$(git svn find-rev "$COMMIT")
  14. if [ $? -ne 0 ] || [ "$OTHER" = "" ]; then
  15. echo "Error! Could not find an svn/git revision for commit $COMMIT!"
  16. echo
  17. echo "Possible problems are:"
  18. echo " * Your revision number ($COMMIT) is wrong"
  19. echo " * This tree is not up to date (before that commit)"
  20. echo " * This commit in in another three (llvm, clang, compiler-rt, etc)"
  21. exit 1
  22. fi
  23. if [ -n "$(echo $COMMIT | grep '^r[0-9]\+')" ]; then
  24. SVN=`echo $COMMIT | sed -e 's/^r//'`
  25. GIT=$OTHER
  26. else
  27. SVN=$OTHER
  28. GIT=$COMMIT
  29. fi
  30. # Grab the one line message for our revert commit message.
  31. ONE_LINE_MSG=$(git log --oneline $GIT -1 | cut -f2- -d " ")
  32. # Revert the commit.
  33. git revert --no-commit $GIT 2>/dev/null
  34. if [ $? -ne 0 ]; then
  35. echo "Error! Failed to revert commit r$SVN. Resetting to head."
  36. git reset --hard HEAD
  37. exit 1
  38. fi
  39. # Create a template in our .git directory.
  40. TEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
  41. cat > $TEMPLATE <<EOF
  42. Revert "$ONE_LINE_MSG"
  43. This reverts commit r$SVN.
  44. EOF
  45. # Begin the commit but give our user an opportunity to edit it.
  46. git commit --file="$TEMPLATE" --edit
  47. if [ $? -ne 0 ]; then
  48. echo "Error! Failed to commit reverting commit for commit r$SVN. Reverting to head."
  49. git reset --hard HEAD
  50. rm -rf $TEMPLATE
  51. exit 1
  52. fi
  53. rm -rf $TEMPLATE