Skip to content

Commit efbabe3

Browse files
committed
chore: ignore merge commits to target branch at tip of history
1 parent 7659672 commit efbabe3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

git-branch-linearity.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ TARGET_BRANCH="${1:-main}"
66
echo "Target branch: $TARGET_BRANCH"
77
git fetch origin $TARGET_BRANCH 2> /dev/null
88

9-
out=$(git log origin/${TARGET_BRANCH}..HEAD --merges --oneline)
9+
10+
# Detection of (PR) merge commits at tip of HEAD:
11+
target_sha=$(git rev-parse origin/${TARGET_BRANCH})
12+
# Get SHA of parent commit
13+
merge_target=$(git log --merges --oneline --parents --no-abbrev-commit HEAD^..HEAD | cut -d" " -f2)
14+
# If parent is the target branch, start from the commit before the merge commit
15+
if [ "$target_sha" = "$merge_target" ] ; then
16+
tip="HEAD^"
17+
else
18+
tip="HEAD"
19+
fi
20+
21+
out=$(git log origin/${TARGET_BRANCH}..${tip} --merges --oneline)
1022
exit_status=$?
1123
if [ -n "$out" ]
1224
then
1325
echo "Please rebase your branch" >&2
1426
echo "If your branch or its base branch is a release branch then ignore this error" >&2
15-
echo "\nMerge commit(s):" >&2
27+
echo >&2
28+
echo "Merge commit(s):" >&2
1629
echo "$out" >&2
1730
# Disclaimer: current version of the check doesn't work well with release branches
1831
exit_status=1

0 commit comments

Comments
 (0)