Skip to content

Commit

Permalink
chore: Update git pre-push hook to use correct base branch name (#2626)
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol authored Aug 3, 2024
1 parent aea3b27 commit 3242fa9
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions scripts/pre-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ if [ -f .pants.rc ]; then
fi
CURRENT_COMMIT=$(git rev-parse --short HEAD)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -n "$(echo "$CURRENT_BRANCH" | sed -n '/^[[:digit:]]\{1,\}\.[[:digit:]]\{1,\}/p')" ]; then
# if we are on the release branch, use it as the base branch.
BASE_BRANCH="$CURRENT_BRANCH"

if ! command -v gh &> /dev/null; then
echo "GitHub CLI (gh) is not installed. Since we cannot determine the base branch, running the full check before push."
pants tailor --check update-build-files --check ::
pants lint check ::
else
BASE_BRANCH="main"
# Get the base branch name from GitHub if we are on a pull request.
BASE_BRANCH=$(gh pr view "$CURRENT_BRANCH" --json baseRefName -q '.baseRefName' 2>/dev/null)
if [[ -z "$BASE_BRANCH" ]]; then
BASE_BRANCH="main"
echo "No pull request found for the branch $CURRENT_BRANCH, falling back to main."
else
echo "Detected the pull request's base branch: $BASE_BRANCH"
fi
if [ "$1" != "origin" ]; then
# extract the owner name of the target repo
ORIGIN="$(echo "$1" | grep -o '://[^/]\+/[^/]\+/' | grep -o '/[^/]\+/$' | tr -d '/')"
cleanup_remote() {
git remote remove "$ORIGIN"
}
trap cleanup_remote EXIT
git remote add "$ORIGIN" "$1"
git fetch -q --depth=1 --no-tags "$ORIGIN" "$BASE_BRANCH"
else
ORIGIN="origin"
fi
echo "Performing lint and check on ${ORIGIN}/${BASE_BRANCH}..HEAD@${CURRENT_COMMIT} ..."
pants tailor --check update-build-files --check ::
pants lint check --changed-since="${ORIGIN}/${BASE_BRANCH}"
fi
if [ "$1" != "origin" ]; then
# extract the owner name of the target repo
ORIGIN="$(echo "$1" | grep -o '://[^/]\+/[^/]\+/' | grep -o '/[^/]\+/$' | tr -d '/')"
cleanup_remote() {
git remote remove "$ORIGIN"
}
trap cleanup_remote EXIT
git remote add "$ORIGIN" "$1"
git fetch -q --depth=1 --no-tags "$ORIGIN" "$BASE_BRANCH"
else
ORIGIN="origin"
fi
echo "Performing lint and check on ${ORIGIN}/${BASE_BRANCH}..HEAD@${CURRENT_COMMIT} ..."
pants tailor --check update-build-files --check ::
pants lint check --changed-since="${ORIGIN}/${BASE_BRANCH}"

0 comments on commit 3242fa9

Please sign in to comment.