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