-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathprecommit.sh
More file actions
executable file
·57 lines (44 loc) · 2.04 KB
/
precommit.sh
File metadata and controls
executable file
·57 lines (44 loc) · 2.04 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
# Precommit hook for formatting staged files.
# Autoformats staged files, unless any are partially staged (committed in chunks).
set -euo pipefail
# we have to unset this env var set by git hooks so that this relative paths work correctly when used inside worktrees
unset GIT_DIR
cd $(dirname $0)
export FORCE_COLOR=true
# Get all staged files (excluding deleted), relative to yarn-project
staged_files=$(git diff-index --diff-filter=d --relative --cached --name-only HEAD)
[ -z "$staged_files" ] && exit 0
# Filter for formattable files
staged_format_files=$(echo "$staged_files" | grep -E '\.(json|js|mjs|cjs|ts)$' || true)
# Drop symlinks; prettier errors when handed a symbolic link (e.g. .codex/settings.json).
if [[ -n "$staged_format_files" ]]; then
staged_format_files=$(echo "$staged_format_files" | while IFS= read -r f; do [ -L "$f" ] || printf '%s\n' "$f"; done)
fi
# Get unstaged changes for formattable files
unstaged_format_files=$(git diff --relative --name-only --diff-filter=d | grep -E '\.(json|js|mjs|cjs|ts)$' || true)
# Detect partially staged files (staged + unstaged changes).
# We don't want to auto-format anything if someone has staged a hunk (partial file)
# as we might corrupt their hunks.
partially_staged=()
for file in $staged_format_files; do
if echo "$unstaged_format_files" | grep -Fxq "$file"; then
partially_staged+=("$file")
fi
done
if (( ${#partially_staged[@]} > 0 )); then
echo -e "\033[33mWarning:\033[0m The following files are partially staged:"
for f in "${partially_staged[@]}"; do
echo " - $f"
done
echo -e "\033[33mSkipping prettier autoformat because of the partial staging. Format manually with 'yarn format'.\033[0m"
exit 0
fi
if [[ -n "$staged_format_files" ]]; then
echo "Formatting staged files..."
echo "$staged_format_files" | parallel -N10 ./node_modules/.bin/prettier --log-level warn --write
# Re-stage formatted files
repo_root=$(git rev-parse --show-toplevel)
echo "$staged_format_files" | xargs -I {} git add "$repo_root/yarn-project/{}"
fi
yarn prepare:check