Skip to content

Commit cbfbd4a

Browse files
committed
chore: ignore PR merge commits to target branch at tip of history
1 parent 0bb3d59 commit cbfbd4a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

git-branch-linearity.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@
44
TARGET_BRANCH="${1:-main}"
55

66
echo "Target branch: $TARGET_BRANCH"
7-
git fetch origin $TARGET_BRANCH 2> /dev/null
7+
git fetch --no-tags --depth=1 origin $TARGET_BRANCH 2> /dev/null
8+
target_sha=$(git rev-parse origin/${TARGET_BRANCH})
89

9-
out=$(git log origin/${TARGET_BRANCH}..HEAD --merges --oneline)
10+
# If in a github PR, base from tip of branch, not the merge commit
11+
if [ -n "$GITHUB_HEAD_REF" ]; then
12+
git fetch --no-tags --shallow-exclude="$target_sha" origin "$GITHUB_HEAD_REF" 2> /dev/null
13+
tip=$(git rev-parse origin/$GITHUB_HEAD_REF)
14+
else
15+
tip="HEAD"
16+
fi
17+
18+
out=$(git log ${target_sha}..${tip} --merges --oneline)
1019
exit_status=$?
20+
1121
if [ -n "$out" ]
1222
then
1323
echo "Please rebase your branch" >&2
1424
echo "If your branch or its base branch is a release branch then ignore this error" >&2
15-
echo "\nMerge commit(s):" >&2
25+
echo >&2
26+
echo "Merge commit(s):" >&2
1627
echo "$out" >&2
1728
# Disclaimer: current version of the check doesn't work well with release branches
1829
exit_status=1
1930
fi
20-
2131
exit ${exit_status}

0 commit comments

Comments
 (0)