remove bashisms
parent
d0c9d9c985
commit
0cde236937
40
git-force
40
git-force
|
@ -1,5 +1,5 @@
|
|||
#!/bin/bash
|
||||
if [[ "$1" == "--help" || "$1" == "-h" ]]
|
||||
#!/bin/sh
|
||||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]
|
||||
then
|
||||
echo "Usage:"
|
||||
echo " git force"
|
||||
|
@ -8,7 +8,7 @@ then
|
|||
exit 0
|
||||
fi
|
||||
git rev-parse --is-inside-work-tree > /dev/null 2>&1
|
||||
if [[ "$?" != "0" ]]
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "error: not a git repository"
|
||||
exit 10
|
||||
|
@ -16,78 +16,78 @@ fi
|
|||
REMOTE=""
|
||||
BRANCH=""
|
||||
PRETTY="--pretty=format:%h, %an <%ae>, %cr: %s"
|
||||
if [[ "$#" == "0" ]]
|
||||
if [ "$#" = "0" ]
|
||||
then
|
||||
BRANCH="$(git branch --show-current)"
|
||||
if [[ "$BRANCH" == "" ]]
|
||||
if [ "$BRANCH" = "" ]
|
||||
then
|
||||
echo "error: currently in detached HEAD state" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
if [[ "$#" -gt "0" ]]
|
||||
if [ "$#" -gt "0" ]
|
||||
then
|
||||
REMOTE="$1"
|
||||
git config "remote.$REMOTE.url" > /dev/null 2>&1
|
||||
if [[ "$?" != "0" ]]
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "error: specified remote is invalid" >&2
|
||||
exit 2
|
||||
fi
|
||||
fi
|
||||
if [[ "$#" -gt "1" ]]
|
||||
if [ "$#" -gt "1" ]
|
||||
then
|
||||
BRANCH="$2"
|
||||
git rev-parse --verify "$BRANCH" > /dev/null 2>&1
|
||||
if [[ "$?" != "0" ]]
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "error: specified branch is invalid" >&2
|
||||
exit 3
|
||||
fi
|
||||
fi
|
||||
if [[ "$REMOTE" == "" ]]
|
||||
if [ "$REMOTE" = "" ]
|
||||
then
|
||||
REMOTE=$(git config "branch.$BRANCH.remote")
|
||||
if [[ "$?" != "0" ]]
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "error: current branch does not have a remote and no remote was specified" >&2
|
||||
echo "see: git force --help"
|
||||
exit 4
|
||||
fi
|
||||
fi
|
||||
if [[ "$REMOTE" == "" || "$BRANCH" == "" ]]
|
||||
if [ "$REMOTE" = "" ] || [ "$BRANCH" = "" ]
|
||||
then
|
||||
echo "assertion failed: remote or branch is blank" >&2
|
||||
exit 5
|
||||
fi
|
||||
REMOTE_BRANCH="$REMOTE/$BRANCH"
|
||||
REMOTE_HASH="$(git rev-parse "$REMOTE_BRANCH")" # later used to throw errors if something changes remote or local
|
||||
if [[ "$?" != "0" || "$REMOTE_HASH" == "" ]]
|
||||
if [ "$?" != "0" ] || [ "$REMOTE_HASH" = "" ]
|
||||
then
|
||||
echo "internal error: failed to fetch remote hash" >&2
|
||||
exit 5
|
||||
fi
|
||||
LOCAL_HASH="$(git rev-parse "$BRANCH")" # as that may cause the user unintentionally pushing/overwriting unwanted things
|
||||
if [[ "$?" != "0" || "$LOCAL_HASH" == "" ]]
|
||||
if [ "$?" != "0" ] || [ "$LOCAL_HASH" = "" ]
|
||||
then
|
||||
echo "internal error: failed to fetch local hash" >&2
|
||||
exit 5
|
||||
fi
|
||||
|
||||
if [[ "$REMOTE_HASH" == "$LOCAL_HASH" ]]
|
||||
if [ "$REMOTE_HASH" = "$LOCAL_HASH" ]
|
||||
then
|
||||
echo "error: the branches are already the same" >&2
|
||||
exit 8
|
||||
fi
|
||||
|
||||
if [[ "$REMOTE_HASH" == "$FORK_POINT" ]]
|
||||
if [ "$REMOTE_HASH" = "$FORK_POINT" ]
|
||||
then
|
||||
echo "error: the branches have not diverged" >&2
|
||||
exit 9
|
||||
fi
|
||||
|
||||
FORK_POINT="$(git merge-base "$BRANCH" "$REMOTE_BRANCH")"
|
||||
if [[ "$?" != "0" ]]
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "The branches do not have a common ancestor, the latest commits for each are listed below:"
|
||||
echo ""
|
||||
|
@ -105,14 +105,14 @@ git log "$PRETTY" "$FORK_POINT..$LOCAL_HASH"
|
|||
fi
|
||||
echo ""
|
||||
read -p "Continue? [y/N] " SHOULD_CONTINUE
|
||||
if [[ "$SHOULD_CONTINUE" != "y" ]]
|
||||
if [ "$SHOULD_CONTINUE" != "y" ]
|
||||
then
|
||||
echo "Abort." >&2
|
||||
exit 6
|
||||
fi
|
||||
REMOTE_HASH_2="$(git rev-parse "$REMOTE_BRANCH")"
|
||||
LOCAL_HASH_2="$(git rev-parse "$BRANCH")"
|
||||
if [[ "$REMOTE_HASH_2" != "$REMOTE_HASH" ]]
|
||||
if [ "$REMOTE_HASH_2" != "$REMOTE_HASH" ]
|
||||
then
|
||||
echo "@@@@@@@@@@@@@@@@@@@@@@@@@" >&2
|
||||
echo "@ REMOTE HAS UPDATED! @" >&2
|
||||
|
@ -123,7 +123,7 @@ echo "to $REMOTE_HASH_2 while you were confirming."
|
|||
echo "Please re-run this command and check again."
|
||||
exit 7
|
||||
fi
|
||||
if [[ "$LOCAL_HASH_2" != "$LOCAL_HASH" ]]
|
||||
if [ "$LOCAL_HASH_2" != "$LOCAL_HASH" ]
|
||||
then
|
||||
echo "@@@@@@@@@@@@@@@@@@@@@@@@" >&2
|
||||
echo "@ LOCAL HAS UPDATED! @" >&2
|
||||
|
|
Loading…
Reference in New Issue