Files
coder/scripts/lib/timed-shell.sh
T
Mathias Fredriksson e7e2de99ba build(Makefile): capture pre-commit output to log files (#22978)
pre-commit was noisy: every sub-target dumped full stdout/stderr to the
terminal, burying failures in pages of compiler output and lint details.

Teach timed-shell.sh a quiet mode via MAKE_LOGDIR: when set, recipe
output is redirected to per-target log files and a one-line status is
printed instead. When unset, behavior is unchanged (with a refreshed
output format).

Makefile changes:

- pre-commit creates a tmpdir, passes MAKE_LOGDIR to sub-makes
- Drop --output-sync=target (log files eliminate interleaving)
- Add --no-print-directory to suppress Entering/Leaving noise
- Split check-unstaged and check-untracked into separate defines
- Restyle both with colored indicators and clearer instructions
- Clean up tmpdir on success, preserve on failure for debugging
2026-03-12 11:16:31 +02:00

54 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# timed-shell.sh wraps bash with per-target wall-clock timing.
#
# Recipe invocation: timed-shell.sh <target> -ceu <recipe>
# $(shell ...) calls: timed-shell.sh -c <command>
#
# Enable via Makefile:
# SHELL := $(CURDIR)/scripts/lib/timed-shell.sh
# .SHELLFLAGS = $@ -ceu
#
# When MAKE_LOGDIR is set, recipe output is captured to a log file.
# Otherwise output goes to stdout/stderr as normal.
#
# $(shell ...) uses SHELL but passes -c directly, not .SHELLFLAGS.
# Detect this and delegate to bash without timing output.
if [[ $1 == -* ]]; then
exec bash "$@"
fi
set -eu
target=$1
shift
dim=$(tput dim 2>/dev/null) || dim=$(tput setaf 8 2>/dev/null) || true
green=$(tput setaf 2 2>/dev/null) || true
red=$(tput setaf 1 2>/dev/null) || true
reset=$(tput sgr0 2>/dev/null) || true
start=$(date +%s)
set +e
if [[ -n ${MAKE_LOGDIR:-} ]]; then
logfile="${MAKE_LOGDIR}/${target//\//-}.log"
bash "$@" >"$logfile" 2>&1
else
printf '%s○%s %s\n' "$dim" "$reset" "$target"
bash "$@"
fi
rc=$?
set -e
elapsed=$(($(date +%s) - start))
if ((rc == 0)); then
printf '%s✓%s %s (%ds)\n' "$green" "$reset" "$target" "$elapsed"
else
if [[ -n ${MAKE_LOGDIR:-} ]]; then
printf '%s✗%s %s (%ds) → %s\n' "$red" "$reset" "$target" "$elapsed" "$logfile"
else
printf '%s✗%s %s (%ds)\n' "$red" "$reset" "$target" "$elapsed"
fi
exit "$rc"
fi