Created
November 4, 2021 09:54
-
-
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.
This file contains 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
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