Add check-rebased Github action (#2002)

It will prevent merging in a branch that's not based on its base branch HEAD, leading to streamlined history.

Note it will not prevent squash commits, nor commits directly to base branch.
This commit is contained in:
Dima Lazerka 2021-12-24 10:38:06 +02:00 committed by GitHub
parent 681a800086
commit 2104330d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

26
.github/workflows/check-rebased.sh vendored Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -x -e -u
git fetch origin "$GITHUB_BASE_REF"
BASE_ID=$(git rev-parse --verify "origin/$GITHUB_BASE_REF")
CURRENT_ID=$(git rev-parse --verify "HEAD")
# Oldest commit that's not in the base branch
FIRST_BRANCH_ID=$(git rev-list "origin/$GITHUB_BASE_REF..HEAD" | tail -n 1)
# Common ancestor (usually just parent of $FIRST_BRANCH_ID)
# Command will return error 1 if not found anything. So we || true to proceed.
FORK_POINT_ID=$(git merge-base "$FIRST_BRANCH_ID" "origin/$GITHUB_BASE_REF") || true
if [[ -z "$FORK_POINT_ID" ]]
then
echo "Current branch is not forked from its base branch origin/$GITHUB_BASE_REF"
exit 1
fi
if [[ "$BASE_ID" != "$FORK_POINT_ID" ]]
then
echo "Current branch (at $CURRENT_ID) forked at $FORK_POINT_ID is not $BASE_ID (which is the latest \"$GITHUB_BASE_REF\")"
exit 1
fi

17
.github/workflows/check-rebased.yml vendored Normal file
View File

@ -0,0 +1,17 @@
name: check-rebased
on:
pull_request:
types: [ synchronize, opened, reopened, edited ] # +edited (for triggering on PR base change)
push:
branches: [ main ]
jobs:
hook:
name: Check whether current branch is based on its base branch
runs-on: ubuntu-latest
steps:
- name: Check out repo @ current branch
uses: actions/checkout@v2
with:
fetch-depth: 1000 # Hopefully current branch is less than 1000 commits from main
- name: Run check-rebased.sh
run: .github/workflows/check-rebased.sh