-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-commit-msg
More file actions
60 lines (52 loc) · 1.78 KB
/
prepare-commit-msg
File metadata and controls
60 lines (52 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
#
# Git hook for automatic commit message generation
# Installation: cp prepare-commit-msg /path/to/your/project/.git/hooks/
#
# Determine script path - there are two options:
# 1. In current CommitPilot repository
# 2. In another repository where hook is installed
if [ -f "$(dirname "$0")/../auto_commit.py" ]; then
# For CommitPilot repository
SCRIPT_PATH="$(dirname "$0")/../auto_commit.py"
elif [ -f "$(dirname "$0")/../../CommitPilot/auto_commit.py" ]; then
# For case when CommitPilot is installed next to project
SCRIPT_PATH="$(dirname "$0")/../../CommitPilot/auto_commit.py"
else
# Check path based on environment variables
if [ -n "$COMMITPILOT_PATH" ]; then
SCRIPT_PATH="$COMMITPILOT_PATH/auto_commit.py"
else
echo "⚠️ Failed to find auto_commit.py script. Set COMMITPILOT_PATH variable."
exit 0
fi
fi
# Check if script exists
if [ ! -f "$SCRIPT_PATH" ]; then
echo "⚠️ Script auto_commit.py not found at path: $SCRIPT_PATH"
exit 0
fi
# Determine Python command
if command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
else
PYTHON_CMD="python"
fi
# Check if commit message already exists
if [ -z "$(cat "$1" | grep -v "^#")" ]; then
# Get diff of changes
DIFF=$(git diff --cached)
STATUS=$(git status --porcelain)
# If there are changes, generate message
if [ -n "$DIFF" ] || [ -n "$STATUS" ]; then
echo "🤖 Generating commit message with AI..."
# Run script to generate message
COMMIT_MSG=$($PYTHON_CMD "$SCRIPT_PATH" --get-message 2>/dev/null)
# Write message to file
if [ -n "$COMMIT_MSG" ]; then
echo "$COMMIT_MSG" > "$1"
echo "✅ Generated commit message: $COMMIT_MSG"
fi
fi
fi
exit 0