From 4d5c428caa1d3e80c8294666d7a4d08868ddaff6 Mon Sep 17 00:00:00 2001 From: Brendan Bergen Date: Thu, 21 Mar 2024 16:16:01 -0600 Subject: [PATCH] Add git hooks for branch name validation --- Makefile | 3 +++ README.md | 7 +++++++ docs/prepare-your-dev-environment.md | 6 ++++++ hack/git/hooks/pre-commit | 23 +++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 hack/git/hooks/pre-commit diff --git a/Makefile b/Makefile index 2c31d5cdeaa..d6cb636a5d7 100644 --- a/Makefile +++ b/Makefile @@ -113,6 +113,9 @@ generate-kiota: go run ./hack/validate-imports pkg/util/graph/graphsdk go run ./hack/licenses -dirs ./pkg/util/graph/graphsdk +init-contrib: + cp -R hack/git/hooks/* .git/hooks/ + image-aro-multistage: docker build --platform=linux/amd64 --network=host --no-cache -f Dockerfile.aro-multistage -t $(ARO_IMAGE) --build-arg REGISTRY=$(REGISTRY) . diff --git a/README.md b/README.md index 5b0a959e9d0..901ac516a5e 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,13 @@ you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com. +Before you start development, please set up your local git hooks to conform to our +development standards: + +```bash +make init-contrib +``` + When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need diff --git a/docs/prepare-your-dev-environment.md b/docs/prepare-your-dev-environment.md index 68b3ef9d2bb..cba63ee6daa 100644 --- a/docs/prepare-your-dev-environment.md +++ b/docs/prepare-your-dev-environment.md @@ -138,3 +138,9 @@ Make sure that `PKG_CONFIG_PATH` contains the pkgconfig files of the above packa ```bash cd ${GOPATH:-$HOME/go}/src/github.com/Azure/ARO-RP ``` + +1. Add standard git hooks + + ```bash + make init-contrib + ``` diff --git a/hack/git/hooks/pre-commit b/hack/git/hooks/pre-commit new file mode 100644 index 00000000000..0fca9ed8ca7 --- /dev/null +++ b/hack/git/hooks/pre-commit @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -e +LC_ALL=C + +git_username_lower="$(git config github.user | tr '[:upper:]' '[:lower:]')" +if [[ -z "${git_username_lower}" ]] +then + echo "Please set github.user (git config github.user) locally or globally before issuing commits to this repo." + exit 1 +fi + +# e.g. "USERNAME/ARO-1234", "USERNAME/hotfix-v20240321.00", or "USERNAME/gh-issue-123" +valid_branch_regex="^${git_username_lower}\/(ARO-[0-9]{4}|hotfix-[a-z0-9._-]+|gh-issue-[0-9]+)$" + +local_branch="$(git rev-parse --abbrev-ref HEAD)" + +if [[ ! $local_branch =~ $valid_branch_regex ]] +then + echo "There is something wrong with your branch name. Branch names in this project must adhere to this contract: $valid_branch_regex. Your commit will be rejected. Please rename your branch (git branch --move) to a valid name and try again." + exit 1 +fi + +exit 0 \ No newline at end of file