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