Skip to content

Commit

Permalink
chore: ignore merge commits to target branch at tip of history
Browse files Browse the repository at this point in the history
  • Loading branch information
halms committed Mar 19, 2024
1 parent 7659672 commit efbabe3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions git-branch-linearity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ TARGET_BRANCH="${1:-main}"
echo "Target branch: $TARGET_BRANCH"
git fetch origin $TARGET_BRANCH 2> /dev/null

out=$(git log origin/${TARGET_BRANCH}..HEAD --merges --oneline)

# Detection of (PR) merge commits at tip of HEAD:
target_sha=$(git rev-parse origin/${TARGET_BRANCH})
# Get SHA of parent commit
merge_target=$(git log --merges --oneline --parents --no-abbrev-commit HEAD^..HEAD | cut -d" " -f2)
# If parent is the target branch, start from the commit before the merge commit
if [ "$target_sha" = "$merge_target" ] ; then
tip="HEAD^"
else
tip="HEAD"
fi

out=$(git log origin/${TARGET_BRANCH}..${tip} --merges --oneline)
exit_status=$?
if [ -n "$out" ]
then
echo "Please rebase your branch" >&2
echo "If your branch or its base branch is a release branch then ignore this error" >&2
echo "\nMerge commit(s):" >&2
echo >&2
echo "Merge commit(s):" >&2
echo "$out" >&2
# Disclaimer: current version of the check doesn't work well with release branches
exit_status=1
Expand Down

0 comments on commit efbabe3

Please sign in to comment.