Files
UnityAI/hooks/commit-check.ps1

110 lines
3.3 KiB
PowerShell
Raw Normal View History

param(
[Parameter(Mandatory = $true)]
2026-07-10 17:50:20 +08:00
[string]$Message,
[string]$RepositoryRoot = ''
)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = 'Stop'
2026-07-10 17:50:20 +08:00
if ([string]::IsNullOrWhiteSpace($RepositoryRoot)) {
$RepositoryRoot = (Get-Location).Path
}
$RepositoryRoot = [System.IO.Path]::GetFullPath($RepositoryRoot)
$gitRoot = & git -C $RepositoryRoot rev-parse --show-toplevel 2>$null | Select-Object -First 1
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($gitRoot)) {
Write-Error "Git repository was not found: $RepositoryRoot"
exit 1
}
2026-07-10 17:50:20 +08:00
$RepositoryRoot = [System.IO.Path]::GetFullPath($gitRoot)
2026-07-10 17:50:20 +08:00
$mappingFile = Join-Path $PSScriptRoot 'commit-module-mappings.json'
$mapping = Get-Content -LiteralPath $mappingFile -Raw -Encoding UTF8 | ConvertFrom-Json
2026-07-10 17:50:20 +08:00
function Get-CommitFiles {
param([string]$Root)
2026-07-10 17:50:20 +08:00
$staged = @(& git -C $Root -c core.quotepath=false diff --cached --name-only --diff-filter=ACMRD)
$staged = @($staged | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
if ($staged.Count -gt 0) {
return $staged
}
2026-07-10 17:50:20 +08:00
$files = @()
$statusLines = & git -C $Root -c core.quotepath=false status --porcelain=v1 --untracked-files=all
foreach ($line in $statusLines) {
if ([string]::IsNullOrWhiteSpace($line) -or $line.Length -lt 4) {
continue
}
$path = $line.Substring(3).Trim()
if ($path.Contains(' -> ')) {
$path = ($path -split ' -> ')[-1]
}
2026-07-10 17:50:20 +08:00
$files += $path.Replace('\', '/')
}
2026-07-10 17:50:20 +08:00
return @($files | Select-Object -Unique)
}
2026-07-10 17:50:20 +08:00
function Get-Modules {
param([string[]]$Files)
$modules = @()
2026-07-10 17:50:20 +08:00
foreach ($file in $Files) {
foreach ($entry in $mapping.mappings) {
if ($file -match $entry.pattern) {
$modules += $entry.module
break
}
}
}
2026-07-10 17:50:20 +08:00
return @($modules | Select-Object -Unique)
}
2026-07-10 17:50:20 +08:00
if ($Message -notmatch '^\[([^\]]+)\]\[([^\]]+)\]:\s+(.+)$') {
Write-Error 'Commit message format must be: [module][type]: description'
exit 1
}
2026-07-10 17:50:20 +08:00
$messageModule = $Matches[1]
$messageType = $Matches[2]
$description = $Matches[3]
if ([string]::IsNullOrWhiteSpace($description)) {
Write-Error 'Commit message description is empty.'
exit 1
}
if ($mapping.validTypes -notcontains $messageType) {
Write-Error ("Invalid commit type '{0}'. Allowed types: {1}" -f
$messageType, ($mapping.validTypes -join ', '))
exit 1
}
2026-07-10 17:50:20 +08:00
$files = @(Get-CommitFiles $RepositoryRoot)
$modules = @(Get-Modules $files)
2026-07-10 17:50:20 +08:00
Write-Output ("Repository: {0}" -f $RepositoryRoot)
if ($files.Count -eq 0) {
Write-Warning 'No staged, unstaged, or untracked files were found. Only the message was validated.'
}
else {
2026-07-10 17:50:20 +08:00
Write-Output 'Commit scope files:'
foreach ($file in $files) {
Write-Output (" - {0}" -f $file)
}
}
2026-07-10 17:50:20 +08:00
if ($modules.Count -gt 0) {
Write-Output ("Detected modules: {0}" -f ($modules -join ', '))
if ($modules -notcontains $messageModule) {
Write-Warning ("Message module [{0}] does not match detected modules [{1}]. Review the commit scope." -f
$messageModule, ($modules -join ', '))
}
}
else {
Write-Warning 'No path mapping matched. Review the commit module manually.'
}
2026-07-10 17:50:20 +08:00
Write-Output 'Commit message is valid.'
exit 0