-
-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathpre-commit
More file actions
27 lines (22 loc) · 943 Bytes
/
Copy pathpre-commit
File metadata and controls
27 lines (22 loc) · 943 Bytes
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
#!/bin/bash
# Formats staged Java sources with the sirix formatter before they are committed.
# Install with: cp pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
# Spotless is ratcheted from origin/main, so spotlessApply only rewrites files
# changed since main — no per-file plumbing is needed.
set -uo pipefail
staged_files=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.java$' || true)
if [ -z "$staged_files" ]; then
exit 0
fi
echo "Running the sirix formatter on staged Java files..."
if ! ./gradlew spotlessApply --daemon; then
echo "Spotless formatting failed." >&2
exit 1
fi
# Restage the files that were staged before formatting (formatting may have
# rewritten them). Note: for a file with both staged and unstaged hunks this
# stages the whole file, as any format-on-commit hook must.
while IFS= read -r file; do
[ -f "$file" ] && git add -- "$file"
done <<< "$staged_files"
exit 0