-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-commit.sh
More file actions
executable file
·138 lines (117 loc) · 3.52 KB
/
ai-commit.sh
File metadata and controls
executable file
·138 lines (117 loc) · 3.52 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PS1_SCRIPT="$SCRIPT_DIR/ai-commit.ps1"
GEN_SCRIPT="$SCRIPT_DIR/generate-commit-message.sh"
usage() {
cat >&2 <<EOF
Usage: $(basename "$0") [--amend] [-a AGENT] [-m MAX_DIFF_LENGTH]
Examples:
$(basename "$0")
$(basename "$0") --amend
$(basename "$0") -a copilot
$(basename "$0") -m 8000
EOF
}
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
usage
exit 0
fi
# Detect platform - use native bash on macOS/Linux, PowerShell on Windows
USE_NATIVE=0
case "$(uname -s)" in
Darwin|Linux)
USE_NATIVE=1
;;
CYGWIN*|MINGW*|MSYS*)
USE_NATIVE=0
;;
esac
AMEND=""
AGENT_CMD="${AGENT:-agent}"
MAX_DIFF=""
while [ $# -gt 0 ]; do
case "$1" in
--amend) AMEND="--amend"; shift ;;
-a) AGENT_CMD="$2"; shift 2 ;;
-m) MAX_DIFF="$2"; shift 2 ;;
*) echo "Unknown option: $1" >&2; usage; exit 1 ;;
esac
done
# ============================================================================
# NATIVE BASH IMPLEMENTATION (macOS/Linux)
# ============================================================================
if [ $USE_NATIVE -eq 1 ]; then
# --- Validate prerequisites ---
if ! command -v git >/dev/null 2>&1; then
echo "Error: git is not installed or not in PATH." >&2
exit 127
fi
if [ ! -f "$GEN_SCRIPT" ]; then
echo "Error: generate-commit-message.sh not found in $SCRIPT_DIR" >&2
exit 1
fi
# --- Validate state ---
if [ -n "$AMEND" ]; then
if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
echo "Error: No commits to amend." >&2
exit 1
fi
else
if git diff --cached --quiet 2>&1; then
echo "Error: No staged changes found. Stage files with 'git add' first." >&2
exit 1
fi
fi
# --- Generate commit message ---
TMP_FILE=$(mktemp)
trap 'rm -f "$TMP_FILE"' EXIT
GEN_ARGS=(-o "$TMP_FILE")
[ -n "$AMEND" ] && GEN_ARGS+=("$AMEND")
[ -n "$AGENT_CMD" ] && [ "$AGENT_CMD" != "agent" ] && GEN_ARGS+=(-a "$AGENT_CMD")
[ -n "$MAX_DIFF" ] && GEN_ARGS+=(-m "$MAX_DIFF")
if ! "$GEN_SCRIPT" "${GEN_ARGS[@]}" >/dev/null; then
echo "Error: Message generation failed." >&2
exit 1
fi
# --- Open editor ---
echo "" >&2
echo "Opening editor for review ..." >&2
if [ -n "$AMEND" ]; then
git commit --amend -e -F "$TMP_FILE"
else
git commit -e -F "$TMP_FILE"
fi
exit 0
fi
# ============================================================================
# POWERSHELL FALLBACK (Windows)
# ============================================================================
if [ ! -f "$PS1_SCRIPT" ]; then
echo "Error: $PS1_SCRIPT not found." >&2
exit 1
fi
# Convert Cygwin/MSYS paths to Windows paths for PowerShell
to_win_path() {
if command -v cygpath >/dev/null 2>&1; then
cygpath -w "$1"
else
echo "$1"
fi
}
POWERSHELL=""
for cmd in pwsh powershell.exe powershell; do
if command -v "$cmd" >/dev/null 2>&1; then
POWERSHELL="$cmd"
break
fi
done
if [ -z "$POWERSHELL" ]; then
echo "Error: PowerShell not found (tried pwsh, powershell.exe, powershell)." >&2
exit 1
fi
ARGS=(-ExecutionPolicy Bypass -File "$(to_win_path "$PS1_SCRIPT")")
[ -n "$AMEND" ] && ARGS+=(-Amend)
[ -n "$AGENT_CMD" ] && [ "$AGENT_CMD" != "agent" ] && ARGS+=(-Agent "$AGENT_CMD")
[ -n "$MAX_DIFF" ] && ARGS+=(-MaxDiffLength "$MAX_DIFF")
"$POWERSHELL" "${ARGS[@]}"