Last active
January 29, 2016 05:29
-
-
Save spazm/02b62a48d2d2771d8311 to your computer and use it in GitHub Desktop.
Check if git branch could be successfully merged via fast-forward.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
# Check if HEAD and REF can be related by a fast-forward merge. | |
# Requires that either HEAD or REF be the merge-base between the two branches | |
usage () { | |
cat << EOM | |
$0 -v -h REF | |
Checks if HEAD and REF can be fast-forwared to each other. | |
exits with non-zero status (2) if a --ff-only merge would fail. | |
-q: be quiet | |
-v: be verbose | |
-h: show this help | |
EOM | |
exit 0 | |
} | |
while getopts ":qvh" opt; do | |
case $opt in | |
q) | |
QUIET=1 | |
;; | |
v) | |
VERBOSE=1 | |
;; | |
h) | |
usage | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
if [ $# -ne 1 ]; then | |
usage | |
fi | |
verbose () { | |
if [ ${VERBOSE:0} > 0 ]; then | |
echo $1 | |
fi | |
} | |
quiet () { | |
if [ -z "$QUIET" ]; then | |
echo $1 | |
fi | |
} | |
OTHER_REF=$1 | |
OTHER_SHA=$(git rev-parse --verify $OTHER_REF^{object}) | |
HEAD_SHA=$(git rev-parse HEAD) | |
MERGE_BASE=$(git merge-base HEAD $OTHER_REF) | |
verbose "HEAD_SHA: $HEAD_SHA" | |
verbose "OTHER_SHA: $OTHER_SHA" | |
verbose "MERGE_BASE: $MERGE_BASE" | |
if [ "$HEAD_SHA" = "$OTHER_SHA" ]; then | |
verbose "Already up-to-date." | |
elif [ "$HEAD_SHA" = "$MERGE_BASE" -o "$OTHER_SHA" = "$MERGE_BASE" ]; then | |
verbose "Clean to fast-forward merge" | |
else | |
quiet "Needs to be rebased" | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment