Skip to content

Instantly share code, notes, and snippets.

@arthurcgusmao
Created November 4, 2021 09:54
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 arthurcgusmao/c3a076f94bc549207694c53c07256888 to your computer and use it in GitHub Desktop.
Save arthurcgusmao/c3a076f94bc549207694c53c07256888 to your computer and use it in GitHub Desktop.
Return the SHA of a parent commit that is equivalent to the current git commit. Useful for CI and DevOps purposes.
TARGET_COMMIT=${1:-HEAD}
if [ $TARGET_COMMIT == "-h" ]; then
echo "
Usage: ./get_equal_parent.sh <commit-hexsha>
Return the SHA of a parent commit that is equivalent (no diff) to the current
commit. If no equivalent parent commit exists, then return \"\".
"
exit
fi
PARENTS=$(git rev-list --parents -n 1 $TARGET_COMMIT | cut -d' ' -f2-)
EQUAL_COMMIT=""
for PARENT in $PARENTS
do
DIFF=$(git diff $TARGET_COMMIT..$PARENT)
if [ -z "${DIFF}" ]; then
EQUAL_COMMIT=$PARENT
fi
done
echo $EQUAL_COMMIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment