forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-commit.sh
More file actions
executable file
·368 lines (312 loc) · 10.7 KB
/
Copy pathpre-commit.sh
File metadata and controls
executable file
·368 lines (312 loc) · 10.7 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env bash
# Pre-commit verification script
# Run all mandatory checks before committing changes.
#
# Usage:
# ./contrib/dev-tools/git/hooks/pre-commit.sh
#
# Expected runtime: ~1 minute on a modern developer machine (concise default profile).
# AI agents: set a per-command timeout of at least 3 minutes before invoking this script.
#
# All steps must pass (exit 0) before committing.
#
# TODO: Implement branch-name validation in the Rust git-hooks binary (#1843).
# When the branch uses an issue-number prefix (e.g. "42-some-description"), verify that
# docs/issues/open/ contains a matching spec file or directory starting with that number.
# This prevents committing under a wrong, closed, or non-existent issue number.
# See also: docs/issues/open/1843-migrate-git-hooks-scripts-from-bash-to-rust.md
set -uo pipefail
# ============================================================================
# STEPS
# ============================================================================
# Each step: "description|command"
declare -a STEPS=(
"Checking for unused dependencies (cargo machete --with-metadata)|cargo machete --with-metadata"
"Running all linters|linter all"
"Running documentation tests|cargo test --doc --workspace"
)
FORMAT="text"
VERBOSITY="concise"
FAILURE_TAIL_LINES=10
LOG_DIR="${TORRUST_GIT_HOOKS_LOG_DIR:-/tmp}"
declare -a STEP_NAMES=()
declare -a STEP_COMMANDS=()
declare -a STEP_STATUSES=()
declare -a STEP_ELAPSED_SECONDS=()
declare -a STEP_LOG_PATHS=()
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
format_time() {
local total_seconds=$1
local minutes=$((total_seconds / 60))
local seconds=$((total_seconds % 60))
if [ "$minutes" -gt 0 ]; then
echo "${minutes}m ${seconds}s"
else
echo "${seconds}s"
fi
}
print_usage() {
cat >&2 <<'EOF'
Usage: ./contrib/dev-tools/git/hooks/pre-commit.sh [--format=<text|json>] [--verbosity=<concise|verbose>] [--verbose]
Options:
--format=<text|json> Output format. Default: text
--verbosity=<concise|verbose> Text output verbosity. Default: concise
--verbose Compatibility alias for --verbosity=verbose
-h, --help Show this help
Environment:
TORRUST_GIT_HOOKS_LOG_DIR Directory for per-step log files (shared by all git hooks). Default: /tmp
EOF
}
prepare_log_dir() {
if ! mkdir -p "${LOG_DIR}"; then
echo "Error: cannot create log directory '${LOG_DIR}'." >&2
exit 2
fi
if [[ ! -d "${LOG_DIR}" || ! -w "${LOG_DIR}" ]]; then
echo "Error: log directory '${LOG_DIR}' is not writable." >&2
exit 2
fi
}
json_escape() {
local input=$1
input=${input//\\/\\\\}
input=${input//\"/\\\"}
input=${input//$'\b'/\\b}
input=${input//$'\f'/\\f}
input=${input//$'\n'/\\n}
input=${input//$'\r'/\\r}
input=${input//$'\t'/\\t}
input=$(printf '%s' "${input}" | tr -d '\000-\010\013\016-\037')
printf '%s' "${input}"
}
strip_ansi() {
sed -E 's/\x1B\[[0-9;]*[A-Za-z]//g'
}
sanitize_name_for_log() {
local raw_name=$1
local normalized
normalized=$(printf '%s' "${raw_name}" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9' '-')
normalized=${normalized#-}
normalized=${normalized%-}
if [[ -z "${normalized}" ]]; then
normalized="step"
fi
printf '%s' "${normalized}"
}
print_step_summary() {
local step_number=$1
local total_steps=$2
local description=$3
local status=$4
local elapsed_seconds=$5
local log_path=$6
if [[ "${status}" == "pass" ]]; then
printf '[Step %d/%d] %s ... PASS (%s)\n' "${step_number}" "${total_steps}" "${description}" "$(format_time "${elapsed_seconds}")"
return
fi
printf '[Step %d/%d] %s ... FAIL (%s) log: %s\n' \
"${step_number}" \
"${total_steps}" \
"${description}" \
"$(format_time "${elapsed_seconds}")" \
"${log_path}"
local -a tail_lines=()
while IFS= read -r line; do
tail_lines+=("${line}")
done < <(tail -n "${FAILURE_TAIL_LINES}" "${log_path}" | strip_ansi)
local shown_count=${#tail_lines[@]}
for line in "${tail_lines[@]}"; do
printf ' %s\n' "${line}"
done
printf ' (%d lines shown - full log: %s)\n' "${shown_count}" "${log_path}"
}
run_command() {
local command=$1
local log_path=$2
if [[ "${FORMAT}" == "text" && "${VERBOSITY}" == "verbose" ]]; then
bash -o pipefail -c "${command}" 2>&1 | tee "${log_path}"
local command_exit_code=${PIPESTATUS[0]}
return "${command_exit_code}"
fi
bash -o pipefail -c "${command}" >"${log_path}" 2>&1
}
run_step() {
local step_number=$1
local total_steps=$2
local description=$3
local command=$4
if [[ "${FORMAT}" == "text" && "${VERBOSITY}" == "verbose" ]]; then
printf '[Step %d/%d] %s...\n' "${step_number}" "${total_steps}" "${description}"
fi
local step_start=$SECONDS
local safe_name
safe_name=$(sanitize_name_for_log "${description}")
local _tmp log_path
if ! _tmp=$(mktemp "${LOG_DIR%/}/pre-commit-${safe_name}-XXXXXX"); then
echo "Error: failed to create a temporary log file in '${LOG_DIR}'." >&2
return 2
fi
log_path="${_tmp}.log"
mv "$_tmp" "$log_path"
run_command "${command}" "${log_path}"
local command_exit_code=$?
local step_elapsed=$((SECONDS - step_start))
STEP_NAMES+=("${description}")
STEP_COMMANDS+=("${command}")
STEP_ELAPSED_SECONDS+=("${step_elapsed}")
STEP_LOG_PATHS+=("${log_path}")
if [[ "${command_exit_code}" -eq 0 ]]; then
STEP_STATUSES+=("pass")
else
STEP_STATUSES+=("fail")
fi
local step_status=${STEP_STATUSES[$(( ${#STEP_STATUSES[@]} - 1 ))]}
if [[ "${FORMAT}" == "text" ]]; then
print_step_summary \
"${step_number}" \
"${total_steps}" \
"${description}" \
"${step_status}" \
"${step_elapsed}" \
"${log_path}"
if [[ "${VERBOSITY}" == "verbose" ]]; then
echo
fi
fi
return "${command_exit_code}"
}
emit_json_result() {
local overall_status=$1
local exit_code=$2
local total_elapsed=$3
local failed_step_name=$4
printf '{\n'
printf ' "schema_version": 1,\n'
printf ' "status": "%s",\n' "${overall_status}"
printf ' "exit_code": %d,\n' "${exit_code}"
printf ' "elapsed_seconds": %d' "${total_elapsed}"
if [[ -n "${failed_step_name}" ]]; then
printf ',\n "failed_step": "%s"' "$(json_escape "${failed_step_name}")"
fi
printf ',\n "steps": [\n'
local steps_count=${#STEP_NAMES[@]}
for ((index = 0; index < steps_count; index++)); do
local name=${STEP_NAMES[$index]}
local command=${STEP_COMMANDS[$index]}
local status=${STEP_STATUSES[$index]}
local elapsed=${STEP_ELAPSED_SECONDS[$index]}
local log_path=${STEP_LOG_PATHS[$index]}
printf ' {\n'
printf ' "name": "%s",\n' "$(json_escape "${name}")"
printf ' "command": "%s",\n' "$(json_escape "${command}")"
printf ' "status": "%s",\n' "${status}"
printf ' "elapsed_seconds": %d' "${elapsed}"
if [[ "${status}" == "fail" ]]; then
printf ',\n "log_path": "%s",\n' "$(json_escape "${log_path}")"
printf ' "failure_tail": ['
local -a tail_lines=()
while IFS= read -r line; do
tail_lines+=("${line}")
done < <(tail -n "${FAILURE_TAIL_LINES}" "${log_path}" | strip_ansi)
local tail_count=${#tail_lines[@]}
for ((tail_index = 0; tail_index < tail_count; tail_index++)); do
if [[ "${tail_index}" -gt 0 ]]; then
printf ', '
fi
printf '"%s"' "$(json_escape "${tail_lines[$tail_index]}")"
done
printf ']'
fi
if [[ "${index}" -lt $((steps_count - 1)) ]]; then
printf '\n },\n'
else
printf '\n }\n'
fi
done
printf ' ]\n'
printf '}\n'
}
parse_args() {
for arg in "$@"; do
case "${arg}" in
--format=text)
FORMAT="text"
;;
--format=json)
FORMAT="json"
;;
--verbosity=concise)
VERBOSITY="concise"
;;
--verbosity=verbose)
VERBOSITY="verbose"
;;
--verbose)
VERBOSITY="verbose"
;;
-h|--help)
print_usage
exit 0
;;
--format=*)
echo "Error: invalid --format value in '${arg}'. Expected --format=text or --format=json." >&2
print_usage
exit 2
;;
--verbosity=*)
echo "Error: invalid --verbosity value in '${arg}'. Expected --verbosity=concise or --verbosity=verbose." >&2
print_usage
exit 2
;;
*)
echo "Error: unknown option '${arg}'." >&2
print_usage
exit 2
;;
esac
done
}
parse_args "$@"
prepare_log_dir
# ============================================================================
# MAIN
# ============================================================================
TOTAL_START=$SECONDS
TOTAL_STEPS=${#STEPS[@]}
overall_status="pass"
exit_code=0
failed_step_name=""
if [[ "${FORMAT}" == "text" ]]; then
echo "Running pre-commit checks..."
echo
fi
for i in "${!STEPS[@]}"; do
IFS='|' read -r description command <<< "${STEPS[$i]}"
if ! run_step $((i + 1)) "${TOTAL_STEPS}" "${description}" "${command}"; then
overall_status="fail"
exit_code=1
failed_step_name="${description}"
break
fi
done
TOTAL_ELAPSED=$((SECONDS - TOTAL_START))
if [[ "${FORMAT}" == "json" ]]; then
emit_json_result "${overall_status}" "${exit_code}" "${TOTAL_ELAPSED}" "${failed_step_name}"
exit "${exit_code}"
fi
if [[ "${overall_status}" == "pass" ]]; then
echo "=========================================="
echo "SUCCESS: All pre-commit checks passed! ($(format_time "${TOTAL_ELAPSED}"))"
echo "=========================================="
echo
echo "You can now safely stage and commit your changes."
exit 0
fi
echo
echo "=========================================="
echo "FAILED: Pre-commit checks failed!"
echo "Fix the errors above before committing."
echo "=========================================="
exit 1