-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustfile
More file actions
178 lines (155 loc) · 5.76 KB
/
justfile
File metadata and controls
178 lines (155 loc) · 5.76 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Algorithms repo task runner
set shell := ["bash", "-euo", "pipefail", "-c"]
# List available recipes
default:
@just --list
# Emit files to operate on. CHECK_MODE=staged (default) lists git-staged files;
# CHECK_MODE=working lists tracked-changed plus untracked working-tree files.
_files:
#!/usr/bin/env bash
set -euo pipefail
if [[ "${CHECK_MODE:-staged}" == "working" ]]; then
{ git diff --name-only --diff-filter d HEAD; \
git ls-files --others --exclude-standard; } | sort -u
else
git diff --cached --name-only --diff-filter d
fi
# Run all pre-commit checks on staged files (re-stages fixes on success)
pre-commit:
#!/usr/bin/env bash
set -uo pipefail
files=$(just _files)
[[ -z "$files" ]] && echo "No staged files." && exit 0
has() { echo "$files" | grep -qE "$1"; }
pids=()
labels=()
run() {
just "$1" > "/tmp/pre-commit-$1.log" 2>&1 &
pids+=($!)
labels+=("$1")
}
if has '\.py$'; then run pre-commit-python; fi
if has '\.py$'; then run pre-commit-typecheck; fi
if has '\.nix$'; then run pre-commit-nix; fi
if has '\.(c|cpp|cc|cxx|h|hpp|hh|hxx)$'; then run pre-commit-c; fi
if has '\.go$'; then run pre-commit-go; fi
run pre-commit-whitespace
failed=0
for i in "${!pids[@]}"; do
if wait "${pids[$i]}"; then
printf '\033[1;32m✓ %s\033[0m\n' "${labels[$i]}"
else
printf '\n\033[1;31m✗ %s\033[0m\n' "${labels[$i]}"
cat "/tmp/pre-commit-${labels[$i]}.log"
failed=1
fi
done
if [[ $failed -gt 0 ]]; then
echo ""
echo "$failed recipe(s) failed — re-stage and retry."
exit 1
fi
git diff --cached --name-only --diff-filter d | xargs -r git add
# Run all checks on working-tree files (changed + untracked); does not stage
check:
#!/usr/bin/env bash
set -uo pipefail
export CHECK_MODE=working
files=$(just _files)
[[ -z "$files" ]] && exit 0
has() { echo "$files" | grep -qE "$1"; }
pids=()
labels=()
run() {
CHECK_MODE=working just "$1" > "/tmp/check-$1.log" 2>&1 &
pids+=($!)
labels+=("$1")
}
if has '\.py$'; then run pre-commit-python; fi
if has '\.py$'; then run pre-commit-typecheck; fi
if has '\.nix$'; then run pre-commit-nix; fi
if has '\.(c|cpp|cc|cxx|h|hpp|hh|hxx)$'; then run pre-commit-c; fi
if has '\.go$'; then run pre-commit-go; fi
run pre-commit-whitespace
failed=0
for i in "${!pids[@]}"; do
if wait "${pids[$i]}"; then
printf '\033[1;32m✓ %s\033[0m\n' "${labels[$i]}"
else
printf '\n\033[1;31m✗ %s\033[0m\n' "${labels[$i]}"
cat "/tmp/check-${labels[$i]}.log"
failed=1
fi
done
exit $failed
# Lint and format Python files
pre-commit-python:
#!/usr/bin/env bash
set -euo pipefail
mapfile -t files < <(just _files | grep '\.py$' || true)
[[ ${#files[@]} -eq 0 ]] && exit 0
ruff check --fix "${files[@]}"
ruff format "${files[@]}"
# Typecheck Python files via pyright
pre-commit-typecheck:
#!/usr/bin/env bash
set -euo pipefail
mapfile -t files < <(just _files | grep '\.py$' || true)
[[ ${#files[@]} -eq 0 ]] && exit 0
./bin/typecheck.sh "${files[@]}"
# Format Nix files
pre-commit-nix:
#!/usr/bin/env bash
set -euo pipefail
mapfile -t files < <(just _files | grep '\.nix$' || true)
[[ ${#files[@]} -eq 0 ]] && exit 0
nixfmt "${files[@]}"
# Format C/C++ files
pre-commit-c:
#!/usr/bin/env bash
set -euo pipefail
mapfile -t files < <(just _files | grep -E '\.(c|cpp|cc|cxx|h|hpp|hh|hxx)$' || true)
[[ ${#files[@]} -eq 0 ]] && exit 0
astyle --style=linux --suffix=none "${files[@]}"
# Format Go files
pre-commit-go:
#!/usr/bin/env bash
set -euo pipefail
mapfile -t files < <(just _files | grep '\.go$' || true)
[[ ${#files[@]} -eq 0 ]] && exit 0
gofmt -s -w "${files[@]}"
# Strip trailing whitespace
pre-commit-whitespace:
#!/usr/bin/env bash
set -euo pipefail
# In working mode, restrict to tracked-changed files; do not silently
# rewrite untracked scratch files (logs, drafts, build noise).
if [[ "${CHECK_MODE:-staged}" == "working" ]]; then
mapfile -t files < <(git diff --name-only --diff-filter d HEAD | grep -vE '\.(png|jpg|jpeg|gif|ico|bmp|pdf|woff2?|ttf|otf|eot|pyc)$' || true)
else
mapfile -t files < <(just _files | grep -vE '\.(png|jpg|jpeg|gif|ico|bmp|pdf|woff2?|ttf|otf|eot|pyc)$' || true)
fi
[[ ${#files[@]} -eq 0 ]] && exit 0
sed -i'' -e 's/[[:space:]]*$//' "${files[@]}"
# Check WIP markers require WIP: commit message prefix (commit-msg hook)
pre-commit-wip *ARGS:
#!/usr/bin/env bash
set -euo pipefail
marker="@""WIP"
commit_msg_file="${@: -1}"
if git diff --cached 2>/dev/null | grep '^+' | grep -q "$marker" 2>/dev/null; then
commit_msg=$(head -n1 "$commit_msg_file")
if ! echo "$commit_msg" | grep -q "^WIP:"; then
echo "ERROR: Found $marker in staged changes."
echo "Either remove $marker markers or prefix your commit message with WIP:"
exit 1
fi
fi
# Clean up merged branches (post-checkout hook)
post-checkout:
#!/usr/bin/env bash
set -euo pipefail
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main")
git branch --merged "$default_branch" \
| grep -v -E "^\*|^\s*(main|master|develop)$" \
| xargs -r git branch -d