119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman#!/bin/bash
219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ $# -ne 1 ]; then
419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "Invalid arguments!"
537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    echo "$0 <rNNNNNN | git-hash>"
619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
719c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
919c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ -n "$(git status -uno -s --porcelain)" ]; then
1019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    echo "You have unstashed changes. Please stash and then revert."
1119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    git status -uno
1219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
1319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
1419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
1519c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanCOMMIT=$1
1637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesOTHER=$(git svn find-rev "$COMMIT")
176948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainarif [ $? -ne 0 ] || [ "$OTHER" = "" ]; then
1837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    echo "Error! Could not find an svn/git revision for commit $COMMIT!"
196948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    echo
206948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    echo "Possible problems are:"
216948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    echo "  * Your revision number ($COMMIT) is wrong"
226948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    echo "  * This tree is not up to date (before that commit)"
236948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    echo "  * This commit in in another three (llvm, clang, compiler-rt, etc)"
2419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
2519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
2619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
2737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesif [ -n "$(echo $COMMIT | grep '^r[0-9]\+')" ]; then
2837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  SVN=`echo $COMMIT | sed -e 's/^r//'`
2937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GIT=$OTHER
3037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hineselse
3137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  SVN=$OTHER
3237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GIT=$COMMIT
3337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesfi
3437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
3519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Grab the one line message for our revert commit message.
3637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesONE_LINE_MSG=$(git log --oneline $GIT -1 | cut -f2- -d " ")
3719c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
3819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Revert the commit.
3937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesgit revert --no-commit $GIT 2>/dev/null
4019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ $? -ne 0 ]; then
4137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    echo "Error! Failed to revert commit r$SVN. Resetting to head."
4219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    git reset --hard HEAD
4319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
4419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
4519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
4619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Create a template in our .git directory.
4719c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanTEMPLATE="`git rev-parse --git-dir`/git-svn-revert-template"
4819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmancat > $TEMPLATE <<EOF
4919c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanRevert "$ONE_LINE_MSG"
5019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
5137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesThis reverts commit r$SVN.
5219c37352626531efe1a8128a168804a4bb5adc86Michael GottesmanEOF
5319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
5419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman# Begin the commit but give our user an opportunity to edit it.
5519c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmangit commit --file="$TEMPLATE" --edit
5619c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanif [ $? -ne 0 ]; then
5737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    echo "Error! Failed to commit reverting commit for commit r$SVN. Reverting to head."
5819c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    git reset --hard HEAD
5919c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    rm -rf $TEMPLATE
6019c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman    exit 1
6119c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanfi
6219c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
6319c37352626531efe1a8128a168804a4bb5adc86Michael Gottesmanrm -rf $TEMPLATE
6419c37352626531efe1a8128a168804a4bb5adc86Michael Gottesman
65