Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active April 9, 2020 08:38
Show Gist options
  • Save CMCDragonkai/bfb4529f2384948c45f8b1f97b237f69 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/bfb4529f2384948c45f8b1f97b237f69 to your computer and use it in GitHub Desktop.
Acquire the earliest common descendant for 2 Git commit revision hashes #git
#!/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
@CMCDragonkai
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment