Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
chore: ignore PR merge commits to target branch at tip of history
  • Loading branch information
halms committed Aug 15, 2025
commit cbfbd4a6207f840fa4bf2eb7271df6347fb235f3
18 changes: 14 additions & 4 deletions git-branch-linearity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
TARGET_BRANCH="${1:-main}"

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

out=$(git log origin/${TARGET_BRANCH}..HEAD --merges --oneline)
# If in a github PR, base from tip of branch, not the merge commit
if [ -n "$GITHUB_HEAD_REF" ]; then
git fetch --no-tags --shallow-exclude="$target_sha" origin "$GITHUB_HEAD_REF" 2> /dev/null
tip=$(git rev-parse origin/$GITHUB_HEAD_REF)
else
tip="HEAD"
fi

out=$(git log ${target_sha}..${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
fi

exit ${exit_status}