From efbabe3ccb0a75622c42493f8a67f38b700ff403 Mon Sep 17 00:00:00 2001 From: Daniel Halmschlager <7513146+halms@users.noreply.github.com> Date: Mon, 18 Mar 2024 20:43:11 +0100 Subject: [PATCH] chore: ignore merge commits to target branch at tip of history --- git-branch-linearity.sh | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/git-branch-linearity.sh b/git-branch-linearity.sh index cd310d5..4560f71 100755 --- a/git-branch-linearity.sh +++ b/git-branch-linearity.sh @@ -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