Skip to content

Instantly share code, notes, and snippets.

@abortz
Created January 31, 2016 23:32
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 abortz/d464c88923c520b79e3d to your computer and use it in GitHub Desktop.
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.
#!/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