Skip to content

Commit ddc9f61

Browse files
authored
Add githook for auto formatting (#62)
Add githook for auto formatting (#62)
1 parent 3c77b77 commit ddc9f61

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

githooks/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
To reuse these githooks, run the following command after cloning the repository:
2+
```
3+
git config core.hooksPath githooks
4+
```

githooks/pre-commit

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# Get list of Python files that are staged for commit
4+
files=$(git diff --cached --name-only --diff-filter=d | grep '\.py$' | grep -v '^ir/lib/')
5+
6+
if [ -z "$files" ]; then
7+
# No Python files to format
8+
exit 0
9+
fi
10+
11+
echo "Running code formatting on staged files..."
12+
13+
# Create a temporary file to store the file list
14+
temp_file=$(mktemp)
15+
echo "$files" > "$temp_file"
16+
17+
# Format only the staged files
18+
poetry run black --quiet $(cat "$temp_file")
19+
poetry run isort --quiet $(cat "$temp_file")
20+
21+
# Clean up
22+
rm "$temp_file"
23+
24+
# Add the formatted files back to staging
25+
if ! git diff --quiet; then
26+
echo "Code formatting created changes. Adding them to the commit..."
27+
git add $files
28+
fi
29+
30+
exit 0

0 commit comments

Comments
 (0)