-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathpre-commit
More file actions
38 lines (28 loc) · 1.08 KB
/
pre-commit
File metadata and controls
38 lines (28 loc) · 1.08 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
#!/usr/bin/env sh
set -eu
repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"
staged_files=$(git diff --cached --name-only --diff-filter=ACMR -- src | grep -E '\.(cs|csproj|props|targets|editorconfig|json|sln|slnx)$' || true)
if [ -z "$staged_files" ]; then
exit 0
fi
unstaged_files=$(git diff --name-only -- $staged_files || true)
if [ -n "$unstaged_files" ]; then
echo "pre-commit: relevant files have unstaged changes."
echo "Stage or stash them before committing so dotnet format does not rewrite mixed content."
printf '%s\n' "$unstaged_files"
exit 1
fi
if ! command -v dotnet >/dev/null 2>&1; then
echo "pre-commit: dotnet CLI not found; skipping whitespace formatting."
exit 0
fi
echo "pre-commit: running dotnet format whitespace on staged src files"
# shellcheck disable=SC2086
dotnet format whitespace src --folder --verbosity minimal --include $staged_files
if ! git diff --quiet -- $staged_files; then
git add -- $staged_files
echo "pre-commit: formatting updates were staged. Review them and run git commit again."
exit 1
fi
exit 0