Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .husky/hooks/install-hooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ echo
HOOKS=(
"commit-msg:Validates commit message format (conventional commits)"
"pre-commit:Runs pre-commit checks (syntax, large files, etc.)"
"prepare-commit-msg:Auto-inserts emoji based on commit type"
)
Comment on lines 35 to 39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Ensure auto-install detects the new hook.
check-hooks.sh currently considers only commit-msg and pre-commit as β€œinstalled”. Existing clones with those two hooks won’t auto-install the new prepare-commit-msg. Update check_hooks_installed to include prepare-commit-msg.

Apply in .husky/check-hooks.sh:

 check_hooks_installed() {
     local git_hooks_dir="$(git rev-parse --git-dir)/hooks"

-    if [[ -f "$git_hooks_dir/commit-msg" ]] && [[ -f "$git_hooks_dir/pre-commit" ]]; then
+    if [[ -f "$git_hooks_dir/commit-msg" ]] && [[ -f "$git_hooks_dir/pre-commit" ]] && [[ -f "$git_hooks_dir/prepare-commit-msg" ]]; then
         return 0  # Hooks are installed
     else
         return 1  # Hooks are missing
     fi
 }
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
HOOKS=(
"commit-msg:Validates commit message format (conventional commits)"
"pre-commit:Runs pre-commit checks (syntax, large files, etc.)"
"prepare-commit-msg:Auto-inserts emoji based on commit type"
)
check_hooks_installed() {
local git_hooks_dir="$(git rev-parse --git-dir)/hooks"
if [[ -f "$git_hooks_dir/commit-msg" ]] && [[ -f "$git_hooks_dir/pre-commit" ]] && [[ -f "$git_hooks_dir/prepare-commit-msg" ]]; then
return 0 # Hooks are installed
else
return 1 # Hooks are missing
fi
}
πŸ€– Prompt for AI Agents
In .husky/hooks/install-hooks.sh around lines 35 to 39, the HOOKS array includes
"prepare-commit-msg" but check-hooks.sh only checks for commit-msg and
pre-commit; update .husky/check-hooks.sh to treat prepare-commit-msg as an
installed hook by adding "prepare-commit-msg" to the list/array of recognized
installed hooks (or include it in the conditional checks) so existing clones
that have the other two hooks will detect the new prepare-commit-msg and
auto-install it.


# Function to install a hook
Expand Down
67 changes: 67 additions & 0 deletions .husky/hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# TiLoKit Prepare-commit-msg Hook
# Auto-inserts the correct emoji based on conventional commit type

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

COMMIT_MSG_FILE="$1"
COMMIT_SOURCE="$2" # message, commit, merge, template, squash

# Do not modify merge, squash, or template generated messages
case "$COMMIT_SOURCE" in
merge|squash|template)
exit 0
;;
esac

# Read first line only
FIRST_LINE=$(head -n1 "$COMMIT_MSG_FILE")

# Trim leading/trailing whitespace
TRIMMED=$(echo "$FIRST_LINE" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')

# If already starts with an emoji then do nothing
if echo "$TRIMMED" | grep -qE '^[^[:space:]]+ [a-z]+: '; then
exit 0
fi

# If doesn't match type: description, try to detect and prepend
if echo "$TRIMMED" | grep -qE '^[a-z]+: .+'; then
TYPE=$(echo "$TRIMMED" | sed -E 's/^([a-z]+): .*/\1/')

Comment on lines +30 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Support scoped and breaking-change commits.
Current regexes miss β€œtype(scope): …” and β€œtype(scope)!: …”, so the hook won’t add emojis for common Conventional Commits (e.g., chore(release): …). This will cause CI failures given your mandatory-emoji policy.

-# If already starts with an emoji then do nothing
-if echo "$TRIMMED" | grep -qE '^[^[:space:]]+ [a-z]+: '; then
+# If already starts with an emoji then do nothing (allow optional scope and !)
+if echo "$TRIMMED" | grep -qE '^[^[:space:]]+ [a-z]+(\([^)]+\))?(!)?: '; then
   exit 0
 fi

-# If doesn't match type: description, try to detect and prepend
-if echo "$TRIMMED" | grep -qE '^[a-z]+: .+'; then
-  TYPE=$(echo "$TRIMMED" | sed -E 's/^([a-z]+): .*/\1/')
+# If matches conventional commit without emoji, detect and prepend (allow scope and !)
+if echo "$TRIMMED" | grep -qE '^[a-z]+(\([^)]+\))?(!)?: .+'; then
+  TYPE=$(echo "$TRIMMED" | sed -E 's/^([a-z]+)(\([^)]+\))?(!)?: .*/\1/')
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if echo "$TRIMMED" | grep -qE '^[^[:space:]]+ [a-z]+: '; then
exit 0
fi
# If doesn't match type: description, try to detect and prepend
if echo "$TRIMMED" | grep -qE '^[a-z]+: .+'; then
TYPE=$(echo "$TRIMMED" | sed -E 's/^([a-z]+): .*/\1/')
if echo "$TRIMMED" | grep -qE '^[^[:space:]]+ [a-z]+(\([^)]+\))?(!)?: '; then
exit 0
fi
# If matches conventional commit without emoji, detect and prepend (allow scope and !)
if echo "$TRIMMED" | grep -qE '^[a-z]+(\([^)]+\))?(!)?: .+'; then
TYPE=$(echo "$TRIMMED" | sed -E 's/^([a-z]+)(\([^)]+\))?(!)?: .*/\1/')
πŸ€– Prompt for AI Agents
.husky/hooks/prepare-commit-msg around lines 30-37: the current grep and sed
only match plain "type: description" and miss Conventional Commits with scopes
and breaking-change bangs like "type(scope): ..." or "type(scope)!: ...". Update
the first and second grep -qE regexes to allow an optional scope group and
optional "!" before the colon (e.g. use a pattern that accepts
^[a-z]+(\([^)]+\))?(!)?: ), and update the sed used to extract TYPE to strip
optional scope and "!" (e.g. change the sed substitution to capture only the
leading type name while tolerating the optional "(...)" and "!" before the
colon).

case "$TYPE" in
feat) EMOJI="✨" ;;
fix) EMOJI="πŸ›" ;;
docs) EMOJI="πŸ“š" ;;
refactor) EMOJI="♻️" ;;
perf) EMOJI="⚑" ;;
test) EMOJI="πŸ§ͺ" ;;
build) EMOJI="πŸ› οΈ" ;;
ci) EMOJI="πŸ”„" ;;
chore) EMOJI="🧹" ;;
style) EMOJI="🎨" ;;
revert) EMOJI="βͺ" ;;
release) EMOJI="πŸš€" ;;
*) EMOJI="" ;;
esac

if [ -n "$EMOJI" ]; then
NEW_LINE="$EMOJI $TRIMMED"
# Replace first line, keep the rest
{
echo "$NEW_LINE"
tail -n +2 "$COMMIT_MSG_FILE"
} > "$COMMIT_MSG_FILE.tmp"
mv "$COMMIT_MSG_FILE.tmp" "$COMMIT_MSG_FILE"
echo -e "${GREEN}βœ… Auto-inserted emoji:${NC} $NEW_LINE"
exit 0
fi
fi
Comment on lines +35 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Keep CI in sync with scope/! support.
After updating this hook, adjust ci-check-commits.sh to accept scope and optional β€œ!” and to extract type accordingly, or CI will still fail scoped commits.

Apply in .husky/ci-check-commits.sh:

-    local emoji_pattern='^[^[:space:]]+ [a-z]+: .+'
-    local no_emoji_pattern='^[a-z]+: .+'
+    local emoji_pattern='^[^[:space:]]+ [a-z]+(\([^)]+\))?(!)?: .+'
+    local no_emoji_pattern='^[a-z]+(\([^)]+\))?(!)?: .+'
@@
-        local commit_type=$(echo "$commit_msg" | sed -E 's/^[^[:space:]]+ ([a-z]+): .*/\1/')
-        local commit_emoji=$(echo "$commit_msg" | sed -E 's/^([^[:space:]]+) [a-z]+: .*/\1/')
+        local commit_type=$(echo "$commit_msg" | sed -E 's/^[^[:space:]]+ ([a-z]+)(\([^)]+\))?(!)?: .*/\1/')
+        local commit_emoji=$(echo "$commit_msg" | sed -E 's/^([^[:space:]]+) [a-z]+(\([^)]+\))?(!)?: .*/\1/')

Committable suggestion skipped: line range outside the PR's diff.

πŸ€– Prompt for AI Agents
In .husky/hooks/prepare-commit-msg around lines 35 to 65, the hook now supports
scoped commit messages and optional "!" but CI still fails because
.husky/ci-check-commits.sh wasn't updated; modify ci-check-commits.sh to parse
commit type by allowing an optional scope and optional breaking-change "!" (e.g.
type(scope)!: message or type!: message), adjust the regex to capture the
leading type token before an optional "(" or "!" and ignore scope/! when
validating and mapping to emojis, and ensure tests/CI validation use the
extracted type rather than assuming no scope or "!" present.


exit 0