From 70f625ffd128f132cc3f92a4465b7e4d73a9ed97 Mon Sep 17 00:00:00 2001 From: enes Date: Tue, 6 Aug 2024 16:01:54 +0200 Subject: [PATCH] feat(ci): add git hooks --- .git-hooks/commit-msg | 18 ++++++++++++++++++ .git-hooks/pre-commit | 11 +++++++++++ package.json | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .git-hooks/commit-msg create mode 100644 .git-hooks/pre-commit diff --git a/.git-hooks/commit-msg b/.git-hooks/commit-msg new file mode 100644 index 0000000..d12ec2f --- /dev/null +++ b/.git-hooks/commit-msg @@ -0,0 +1,18 @@ +#!/bin/sh +RED="\033[1;31m" +GREEN="\033[1;32m" + +# Get the commit message (the parameter we're given is just the path to the +# temporary file which holds the message). +commit_message=$(cat "$1") + +if (echo "$commit_message" | grep -Eq "^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9 \-]+\))?!?: .+$") then + echo "${GREEN} ✔ Commit message meets Conventional Commit standards" + exit 0 +fi + +echo "${RED}❌ Commit message does not meet the Conventional Commit standard!" +echo "An example of a valid message is:" +echo " feat(login): add the 'remember me' button" +echo "ℹ More details at: https://www.conventionalcommits.org/en/v1.0.0/#summary" +exit 1 \ No newline at end of file diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit new file mode 100644 index 0000000..b3dfacf --- /dev/null +++ b/.git-hooks/pre-commit @@ -0,0 +1,11 @@ +#!/bin/sh + +# Avoid commits to the master branch +BRANCH=`git rev-parse --abbrev-ref HEAD` +REGEX="^(master|main|staging|development)$" + +if [[ "$BRANCH" =~ $REGEX ]]; then + echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?" + echo "If so, commit with -n to bypass the pre-commit hook." + exit 1 +fi \ No newline at end of file diff --git a/package.json b/package.json index 0041c32..62771d5 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "lint:fix": "eslint . --fix --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "formatter:check": "prettier --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"", "formatter:fix": "prettier --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"", - "preview": "vite preview" + "preview": "vite preview", + "preinstall": "git config core.hooksPath .git-hooks" }, "dependencies": { "@emotion/react": "11.11.4",