From 2743257f106296331ec3ab8b9024d29748b84833 Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Mon, 30 Dec 2024 18:49:25 +0000 Subject: [PATCH] chore - add pre-commit hooks for branch name validation --- .githooks/pre-commit | 16 +++++++++++++++ .githooks/prepare-commit-msg | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 .githooks/pre-commit create mode 100644 .githooks/prepare-commit-msg diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 0000000..91cbd3c --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +LC_ALL=C + +local_branch="$(git rev-parse --abbrev-ref HEAD)" + +valid_branch_regex="^(penify|gitauto|dependabot|feature|fix|docs|style|refactor|perf|hotfix|test|chore|create)(\/[a-zA-Z0-9#._-]+)+$" + +message="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. You should rename your branch to a valid name and try again." + +if [[ ! $local_branch =~ $valid_branch_regex ]] +then + echo "$message" + exit 1 +fi + +#npm run lint \ No newline at end of file diff --git a/.githooks/prepare-commit-msg b/.githooks/prepare-commit-msg new file mode 100644 index 0000000..fc9f2e7 --- /dev/null +++ b/.githooks/prepare-commit-msg @@ -0,0 +1,39 @@ +#!/bin/sh + +# This script generates an AI-powered commit message using dotnet-aicommitmessage. +# It can be bypassed by setting the GIT_AICOMMIT_SKIP environment variable. + +# Exit immediately if GIT_AICOMMIT_SKIP is set +if [ -n "$GIT_AICOMMIT_SKIP" ]; then + exit 0 +fi + +if ! command -v dotnet-aicommitmessage &> /dev/null; then + echo "Error: dotnet-aicommitmessage is not installed or not in PATH" >&2 + echo "Please install it by running 'dotnet tool install -g aicommitmessage'" >&2 + exit 1 +fi + +COMMIT_MSG_FILE=$1 +CURRENT_MESSAGE=$(cat "$COMMIT_MSG_FILE") + +# From version 0.6.1, this is not needed anymore. +# GIT_DIFF=$(git diff --staged) +# GIT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) + +# Run dotnet-aicommitmessage with error handling +# From version 0.6.1 branch and diff are now retrieved by the tool and don't need to be passed manually. +# Version 0.6.1 and higher: dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" +# Version 0.6.0 and lower: dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" -b "$GIT_BRANCH_NAME" -d "$GIT_DIFF" + +if ! AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE"); then + echo "Error: Failed to generate AI commit message. Using original message." >&2 + exit 0 +fi + +if [[ -z "$AI_MESSAGE" || "$AI_MESSAGE" =~ ^[[:space:]]*$ ]]; then + echo "Error: Generated commit message is empty." >&2 + exit 1 +fi +echo "$AI_MESSAGE" > "$COMMIT_MSG_FILE" +exit 0