Created
January 31, 2016 23:32
-
-
Save abortz/d464c88923c520b79e3d to your computer and use it in GitHub Desktop.
Find the "branching off" point between two commits, irrespective of how many times they've been merged into each other.
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
#!/bin/bash -e | |
BRANCH_A="$1" | |
BRANCH_B="$2" | |
# Filter out early commits that are not common ancestors | |
# Also helpfully throws an error if there are *no* common ancestors. | |
FRONTIER=$(git rev-list --boundary "$BRANCH_A"..."$BRANCH_B" | grep ^- | cut -d- -f2) | |
# Only happens if branch A == branch B exactly | |
if [ -z "$FRONTIER" ]; then | |
echo "$(git rev-parse "$BRANCH_A")" | |
exit 0 | |
fi | |
# Find topologically-first cut | |
while read rev; do | |
# Check if rev is a cut for both branches | |
[ "$(git rev-list --boundary "$BRANCH_A" "$BRANCH_B" "^$rev" | grep ^-)" == "-$rev" ] || continue | |
# Found it! | |
echo "$rev" | |
exit 0 | |
done < <(git rev-list --topo-order $FRONTIER) | |
# Unreachable | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment