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