Skip to content

Commit

Permalink
chore - add pre-commit hooks for branch name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco committed Dec 30, 2024
1 parent adcbe46 commit 2743257
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions .githooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2743257

Please sign in to comment.