Skip to content

Instantly share code, notes, and snippets.

@mastermatt
Created March 31, 2019 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastermatt/35bba05fd3019bc077ac9e0387f444ac to your computer and use it in GitHub Desktop.
Save mastermatt/35bba05fd3019bc077ac9e0387f444ac to your computer and use it in GitHub Desktop.
Checks if the current branch is already contained within the provided target ref.
#!/usr/bin/env bash
#
# Checks if the current branch is already contained within the provided target ref.
#
# This solution varies from others with git diff or git log because commits themselves are ignored
# to allow for consistent results if the current branch is rebased/squashed/etc.
# Instead, the change-set as a whole is compared to the target by attempting a merge in a detached
# state and inspecting to see if any resulting merge commit was "empty".
# https://stackoverflow.com/a/55433607/823942
if [[ $# -ne 1 ]]; then
echo "A single argument is required; the target ref to compare against."
exit 1
fi
initial_ref=$(git rev-parse --abbrev-ref --verify HEAD)
target_ref="$1"
git checkout --detach --quiet ${target_ref}
if git merge --no-edit --no-ff ${initial_ref} &> /dev/null ; then
test $(git rev-parse HEAD^{tree}) = $(git rev-parse HEAD^^{tree})
result=$?
else
git merge --abort
result=1
fi
git checkout --force --quiet ${initial_ref}
exit ${result}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment