forked from microsoft/WinObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit.ps1
More file actions
74 lines (58 loc) · 2.31 KB
/
pre-commit.ps1
File metadata and controls
74 lines (58 loc) · 2.31 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
#
# Performs style checks on our codebase before committing.
#
."$PSScriptRoot/common.ps1"
# Ensures all changes are formatted correctly.
function Format-SourceFiles {
$repoRootDirectory = Get-RepoRoot
$unstagedFiles = Get-UnstagedFiles
$stagedFiles = Get-StagedFiles
# Check to make sure a file isn't both staged and unstaged
$intersection = $stagedFiles | ?{$unstagedFiles -contains $_}
if ($intersection) {
$intersectionString = $intersection -join [environment]::NewLine
$Host.UI.WriteErrorLine("Error: files cannot be both staged and unstaged: " + [environment]::NewLine +
[environment]::NewLine +
$intersectionString + [environment]::NewLine)
exit 1
}
# Run clang-format over each file
foreach ($file in $stagedFiles) {
$file = "$file".Trim()
if ($file) {
ClangFormatFile $repoRootDirectory $file $False
}
}
# Was anything changed? Compare staged files to unstaged files.
$unstagedFiles = Get-UnstagedFiles
$formatChangedFiles = @()
foreach ($file in $stagedFiles) {
$file = "$file".Trim()
if ($file -and ($unstagedFiles -contains $file)) {
$formatChangedFiles += $file
}
}
if ($formatChangedFiles) {
$formatChangedFilesString = $formatChangedFiles -join [environment]::NewLine
$errorMessage =
"The following files required style changes and have been modified in your working directory: " + [environment]::NewLine +
[environment]::NewLine +
$formatChangedFilesString + [environment]::NewLine +
[environment]::NewLine +
"Please review the changes and re-stage the modified files before committing." + [environment]::NewLine
$Host.UI.WriteErrorLine($errorMessage)
exit 1
}
}
# Perform validation.
try {
$ErrorActionPreference = "Stop"
$Host.UI.WriteLine("Performing pre-commit validation...")
# Make sure the source conforms to our coding standards
Format-SourceFiles
$Host.UI.WriteLine("Commit approved.")
} catch [Exception] {
$Host.UI.WriteErrorLine($_.Exception.Message)
$Host.UI.WriteErrorLine("If you *must* force this commit through, use 'git commit --no-verify'.")
exit 1
}