Last active
April 9, 2020 08:38
-
-
Save CMCDragonkai/bfb4529f2384948c45f8b1f97b237f69 to your computer and use it in GitHub Desktop.
Acquire the earliest common descendant for 2 Git commit revision hashes #git
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
#!/usr/bin/env bash | |
commit_1="$1" | |
commit_2="$2" | |
branch="$(git rev-parse --abbrev-ref HEAD)" | |
if git merge-base --is-ancestor "$commit_1" "$commit_2"; then | |
echo "$commit_2" | |
exit 0 | |
fi | |
if git merge-base --is-ancestor "$commit_2" "$commit_1"; then | |
echo "$commit_1" | |
exit 0 | |
fi | |
while read -r merge_commit; do | |
if | |
git merge-base --is-ancestor "$commit_1" "$merge_commit" \ | |
&& \ | |
git merge-base --is-ancestor "$commit_2" "$merge_commit"; | |
then | |
echo "$merge_commit" | |
exit 0 | |
fi | |
done < <( | |
git rev-list \ | |
--topo-order \ | |
--reverse \ | |
--merges \ | |
--ancestry-path \ | |
"^$commit_1" \ | |
"^$commit_2" \ | |
"$branch" | |
) | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on: https://stackoverflow.com/a/55885818/582917