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
#!/usr/bin/env pwsh
#require 7.5
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
Write-Output "[pre-commit] fix and format changes..."
$rust_files=@( $(git status --short '*.rs') )
$staged = @( $rust_files | Where-Object { $_[0] -ne ' ' } | ForEach-Object { $_ -split " " | tail -1 } )
if ($staged.Count -eq 0) {
Write-Output "[pre-commit] skipped ✓"
# no rust files
exit 0;
}
$dirty = @( $rust_files | Where-Object { $_[1] -ne ' ' } | ForEach-Object { $_ -split " " | tail -1 } )
$partial = @( $staged | Where-Object { $dirty -contains $_ } )
function abort_commit {
Write-Output "[pre-commit] aborted"
exit 1;
}
# check for partially staged rust files
if ($partial.Count -eq 0) {
# format and re-add
cargo clippy --fix --allow-staged --allow-dirty --all-features 2>&1 `
| Select -Last 2 | Select -First 1
if (! $?) { abort_commit }
cargo fmt
if (! $?) { abort_commit }
git update-index --again
if (! $?) { abort_commit }
Write-Output "[pre-commit] fixed up and formatted ✓"
} else {
# otherwise only check formatting
cargo fmt --check
if (! $?) { abort_commit }
Write-Output "[pre-commit] formatted ✓"
}
Write-Output "[pre-commit] testing"
$job = Start-Job { cargo test --all-features 2>&1 && Write-Output "PASS" || Write-Output "FAIL" }
$output = Receive-Job -Job $job -Wait | Out-String
$output = $output.Trim()
if ($output.EndsWith("PASS")) {
Write-Output "[pre-commit] tests PASS ✓"
exit 0
}
Write-Output $output
if ($output.EndsWith("FAIL")) {
Write-Output "[pre-commit] tests FAIL"
abort_commit
}
Write-Output "[pre-commit] unexpected test output!"
abort_commit