-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-commit.ps1
More file actions
executable file
·76 lines (66 loc) · 1.95 KB
/
ai-commit.ps1
File metadata and controls
executable file
·76 lines (66 loc) · 1.95 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
<#
.SYNOPSIS
Generates a commit message via AI and opens the git editor to review before committing.
.USAGE
.\ai-commit.ps1 [-Amend] [-Agent <command>] [-MaxDiffLength <int>]
.EXAMPLE
.\ai-commit.ps1
.\ai-commit.ps1 -Amend
.\ai-commit.ps1 -Agent "cursor-agent"
.NOTES
Requires agent.cmd (or the command specified by -Agent) to be available in PATH.
Must be run from within a git repository with staged changes (or -Amend to rewrite the last commit).
#>
param(
[Parameter(Mandatory=$false)]
[switch]$Amend,
[Parameter(Mandatory=$false)]
[string]$Agent = "agent.cmd",
[Parameter(Mandatory=$false)]
[int]$MaxDiffLength = 12000
)
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$GenScript = Join-Path $ScriptDir "generate-commit-message.ps1"
if (-not (Test-Path $GenScript)) {
Write-Error "generate-commit-message.ps1 not found in $ScriptDir"
exit 1
}
if (-not (Get-Command "git" -ErrorAction SilentlyContinue)) {
Write-Error "git is not installed or not in PATH."
exit 127
}
$genArgs = @{
Agent = $Agent
MaxDiffLength = $MaxDiffLength
}
if ($Amend) {
git rev-parse --verify HEAD 2>$null | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Error "No commits to amend."
exit 1
}
$genArgs["Amend"] = $true
} else {
git diff --cached --quiet 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Error "No staged changes found. Stage files with 'git add' first."
exit 1
}
}
$tmpFile = [System.IO.Path]::GetTempFileName()
try {
& $GenScript @genArgs -OutputFile $tmpFile
if ($LASTEXITCODE -ne 0) {
Write-Error "Message generation failed."
exit 1
}
Write-Host ""
Write-Host "Opening editor for review ..." -ForegroundColor Cyan
if ($Amend) {
git commit --amend -e -F $tmpFile
} else {
git commit -e -F $tmpFile
}
} finally {
Remove-Item -Path $tmpFile -ErrorAction SilentlyContinue
}