Add optional pre-commit hook for code formatting #5178
3 issues
code-review: Found 3 issues (2 high, 1 medium)
High
`set -e` with `grep` causes hook to fail when no C# files are staged - `.githooks/pre-commit:6-11`
With set -e enabled, the pipeline git diff ... | grep '\.cs will terminate the script when grepfinds no matches (exit code 1), before the[ -z "$STAGED" ] check runs. As a result, commits containing no C# files will fail the pre-commit hook instead of exiting cleanly with the intended "No C# files staged" message, blocking unrelated commits. | tr ... will terminate the script when �CODE2� finds no matches (exit code 1), before the �CODE3� check runs. As a result, commits containing no C# files will fail the pre-commit hook instead of exiting cleanly with the intended "No C# files staged" message, blocking unrelated commits.
Also found at:
.githooks/pre-commit:17-26
Hook auto-modifies files, contradicting documented "check-only" design - `.githooks/pre-commit:13-18`
The PR description states "Hook verifies formatting but doesn't auto-fix (explicit, matches CI behavior)", but the command invokes dotnet format without --verify-no-changes, which actively rewrites files on disk. The hook then detects the modifications via git diff --quiet. This causes unintended side effects: developer's working tree is mutated on every commit attempt, and any pre-existing unstaged edits will be conflated with formatter output, making it hard to recover the original state.
Medium
Unquoted `$STAGED` breaks on filenames containing spaces - `.githooks/pre-commit:6-14`
$STAGED is built by joining filenames with spaces and passed unquoted to --include $STAGED. Any C# file path containing whitespace will be split into multiple arguments, causing dotnet format to receive incorrect (likely nonexistent) paths and either skip files silently or error out. While uncommon in C# projects, this is a latent correctness issue.
Duration: 54.0s · Tokens: 62.6k in / 2.7k out · Cost: $0.64 (+extraction: $0.01, +merge: $0.00)
Annotations
Check failure on line 11 in .githooks/pre-commit
sentry-warden / warden: code-review
`set -e` with `grep` causes hook to fail when no C# files are staged
With `set -e` enabled, the pipeline `git diff ... | grep '\.cs will terminate the script when `grep` finds no matches (exit code 1), before the `[ -z "$STAGED" ]` check runs. As a result, commits containing no C# files will fail the pre-commit hook instead of exiting cleanly with the intended "No C# files staged" message, blocking unrelated commits. | tr ...` will terminate the script when CODE2 finds no matches (exit code 1), before the CODE3 check runs. As a result, commits containing no C# files will fail the pre-commit hook instead of exiting cleanly with the intended "No C# files staged" message, blocking unrelated commits.
Check failure on line 26 in .githooks/pre-commit
sentry-warden / warden: code-review
[CVK-JG6] `set -e` with `grep` causes hook to fail when no C# files are staged (additional location)
With `set -e` enabled, the pipeline `git diff ... | grep '\.cs will terminate the script when `grep` finds no matches (exit code 1), before the `[ -z "$STAGED" ]` check runs. As a result, commits containing no C# files will fail the pre-commit hook instead of exiting cleanly with the intended "No C# files staged" message, blocking unrelated commits. | tr ...` will terminate the script when CODE2 finds no matches (exit code 1), before the CODE3 check runs. As a result, commits containing no C# files will fail the pre-commit hook instead of exiting cleanly with the intended "No C# files staged" message, blocking unrelated commits.
Check failure on line 18 in .githooks/pre-commit
sentry-warden / warden: code-review
Hook auto-modifies files, contradicting documented "check-only" design
The PR description states "Hook verifies formatting but doesn't auto-fix (explicit, matches CI behavior)", but the command invokes `dotnet format` without `--verify-no-changes`, which actively rewrites files on disk. The hook then detects the modifications via `git diff --quiet`. This causes unintended side effects: developer's working tree is mutated on every commit attempt, and any pre-existing unstaged edits will be conflated with formatter output, making it hard to recover the original state.
Check warning on line 14 in .githooks/pre-commit
sentry-warden / warden: code-review
Unquoted `$STAGED` breaks on filenames containing spaces
`$STAGED` is built by joining filenames with spaces and passed unquoted to `--include $STAGED`. Any C# file path containing whitespace will be split into multiple arguments, causing `dotnet format` to receive incorrect (likely nonexistent) paths and either skip files silently or error out. While uncommon in C# projects, this is a latent correctness issue.