Skip to content

Commit 3f0f22b

Browse files
johnml1135Copilotpapeh
authored
Improve whitespace guidance and tooling (#767)
* Improve whitespace guidance and tooling * Apply AI suggestions from code review Co-authored-by: Copilot Autofix <175728472+Copilot@users.noreply.github.com> Co-authored-by: papeh <hasso_pape@sil.org>
1 parent 564ccbb commit 3f0f22b

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

.github/instructions/repo.instructions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ Provide clear, concise, and enforceable rules that help AI coding agents and aut
1212
## Rules (high-impact, short)
1313
- Prefer the repository top-level build (`.\build.ps1`) and solution (`FieldWorks.sln`) for full builds.
1414
- Keep localization consistent: use `.resx` and follow `crowdin.json` for crowdin integration.
15+
- Before commit/push, run the existing VS Code task `CI: Full local check`.
16+
- After rebase/merge/cherry-pick conflict resolution, run `CI: Whitespace check`; if it fixes files, restage them and rerun.
17+
- When rewriting history or adding commits, run `CI: Commit messages` before pushing.

.github/instructions/terminal.instructions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Placement policy for wrappers:
1313

1414
**MCP-first:** When the `ps-tools` MCP server is running, prefer MCP tools (`Git-Search`, `Read-FileContent`, `Invoke-AgentTask`, `build`, `test`, agent tools) instead of direct terminal commands. Use wrappers only when MCP is unavailable.
1515

16+
**Task-first for CI parity:** Prefer the existing VS Code tasks `CI: Whitespace check`, `CI: Commit messages`, and `CI: Full local check` over ad-hoc shell commands. These tasks are already allowed for agent use and match the repository CI checks.
17+
1618
## Transformations
1719

1820
| ❌ Blocked | ✅ Use Instead |

AGENTS.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Minimal, high-signal guidance for coding agents in this repository.
88
- Build with `.\build.ps1`.
99
- Test with `.\test.ps1`.
1010
- Do not bypass repository scripts for normal build/test work.
11+
- Before committing or pushing, run the existing VS Code task `CI: Full local check`.
12+
- After any rebase, merge, cherry-pick, or manual conflict resolution, run `CI: Whitespace check` before committing.
13+
- If `CI: Whitespace check` rewrites files, review and restage those files, then rerun the task until it passes cleanly.
14+
- When commit history changes, run `CI: Commit messages` before pushing.
1115

1216
## Critical constraints
1317

@@ -36,5 +40,6 @@ Minimal, high-signal guidance for coding agents in this repository.
3640
## Validation checklist
3741

3842
1. Run the relevant build/test scripts for touched areas.
39-
2. Keep edits scoped and avoid unrelated refactors.
40-
3. Update docs only when behavior/contracts/process changed.
43+
2. Run `CI: Full local check` before commit/push; use `CI: Whitespace check` immediately after conflict resolution.
44+
3. Keep edits scoped and avoid unrelated refactors.
45+
4. Update docs only when behavior/contracts/process changed.

Build/Agent/check-whitespace.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Get-Content check-results.log | ForEach-Object {
8585
if ($problems.Count -gt 0) {
8686
Write-Host "`u26A0`uFE0F Please review the output for further information."
8787
Write-Host '### A whitespace issue was found in one or more of the commits.'
88+
Write-Host 'This check validates commit history from origin/main..HEAD, not just the current working tree.'
89+
Write-Host 'If the report names an older commit, fix the file and then amend, squash, or rebase so that commit no longer appears in the branch history.'
8890
Write-Host ''
8991
Write-Host 'Errors:'
9092
$problems | ForEach-Object { Write-Host $_ }

Build/Agent/fix-whitespace.ps1

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,39 @@ function Get-BaseRef {
1010
return (Get-DefaultBranchRef)
1111
}
1212

13+
function Test-HasUtf8Bom {
14+
param([Parameter(Mandatory)][string]$Path)
15+
16+
# Read only the first three bytes to check for a UTF-8 BOM to avoid loading the entire file.
17+
$buffer = [byte[]]::new(3)
18+
$stream = [System.IO.File]::OpenRead($Path)
19+
try {
20+
$bytesRead = $stream.Read($buffer, 0, 3)
21+
}
22+
finally {
23+
$stream.Dispose()
24+
}
25+
26+
if ($bytesRead -lt 3) { return $false }
27+
return $buffer[0] -eq 0xEF -and $buffer[1] -eq 0xBB -and $buffer[2] -eq 0xBF
28+
}
29+
30+
function Write-Utf8Text {
31+
param(
32+
[Parameter(Mandatory)][string]$Path,
33+
[Parameter(Mandatory)][string]$Content,
34+
[Parameter(Mandatory)][bool]$EmitBom
35+
)
36+
37+
$encoding = New-Object System.Text.UTF8Encoding($EmitBom)
38+
[System.IO.File]::WriteAllText($Path, $Content, $encoding)
39+
}
40+
1341
function Format-FileWhitespace {
1442
param([Parameter(Mandatory)][string]$Path)
1543
if (-not (Test-Path -LiteralPath $Path)) { return }
1644
try {
45+
$hasUtf8Bom = Test-HasUtf8Bom -Path $Path
1746
$raw = Get-Content -LiteralPath $Path -Raw -Encoding utf8
1847
}
1948
catch {
@@ -33,7 +62,7 @@ function Format-FileWhitespace {
3362
# Join back and ensure exactly one trailing newline
3463
$new = ($lines -join "`n") + "`n"
3564
if ($new -ne $orig) {
36-
Set-Content -LiteralPath $Path -Value $new -Encoding utf8 -NoNewline
65+
Write-Utf8Text -Path $Path -Content $new -EmitBom $hasUtf8Bom
3766
Write-Host "Fixed whitespace: $Path"
3867
}
3968
}
@@ -56,5 +85,6 @@ $files = $fixFiles | Where-Object { $_ -and (Test-Path $_) }
5685

5786
foreach ($f in $files) { Format-FileWhitespace -Path $f }
5887

59-
Write-Host "Whitespace fix completed. Review changes, commit, and rebase as needed."
88+
Write-Host "Whitespace fix completed. Review and stage the updated files before committing."
89+
Write-Host "If check-whitespace reported an older commit in origin/main..HEAD, rewrite history with amend, squash, or rebase so that offending commit is no longer part of the branch."
6090
exit 0

0 commit comments

Comments
 (0)