feat: implement formatting for staged Rust files in pre-commit hook

This commit is contained in:
t8y2 2026-05-26 16:14:58 +08:00
parent 3959629672
commit 29462937d3
1 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,22 @@
npx lint-staged
cd src-tauri && cargo fmt && git add -u .
cd "$(git rev-parse --show-toplevel)/crates/dbx-core" && cargo fmt --all && git add -u .
cd "$(git rev-parse --show-toplevel)/crates/dbx-web" && cargo fmt && git add -u .
# Only format and re-add Rust files that are already staged
TOPLEVEL=$(git rev-parse --show-toplevel)
format_staged_rust() {
local dir="$1"
local staged=""
staged=$(git diff --staged --name-only --diff-filter=ACM -- ":(top,literal)$dir/*.rs" ":(top,literal)$dir/**/*.rs" 2>/dev/null)
if [ -n "$staged" ]; then
echo "$staged" | while IFS= read -r f; do
rustfmt "$TOPLEVEL/$f" 2>/dev/null || true
done
echo "$staged" | while IFS= read -r f; do
git add "$TOPLEVEL/$f" 2>/dev/null || true
done
fi
}
format_staged_rust "src-tauri"
format_staged_rust "crates/dbx-core"
format_staged_rust "crates/dbx-web"