Skip to content

Commit

Permalink
✨ Timeout long commands, default 10s
Browse files Browse the repository at this point in the history
Fix: #54
  • Loading branch information
MicahElliott committed Jan 28, 2025
1 parent ecbb3a6 commit ba03f64
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ Some things to notice in that file:
- Some triggers are a line with a `somename:` "name" prefix, then the eval'd command
- After a `name` is an optional "filter": `cljfmt` will only look at `.clj` and `.cljc` files
- The `lint` and `format` are run in parallel by being backgrounded (`&`)
- The `commitlint` is prefixed with a `-`: this instructs Captain to let itxs proceed even if it fails
- The `commitlint` is prefixed with a `-`: this instructs Captain to let it proceed even if it fails
- You generally should use single-quote commands, even with env vars
- The `$CAPT_CHANGES` is the convenient list of files that are part of the commit
- The `$GITARG1` is the first available param passed from git to a hook script
Expand Down Expand Up @@ -472,6 +472,7 @@ You can fine-tune Captain’s behavior with several environment variables.
- `CAPT_BLACK_TRIGGERS` :: Set to CSV string of individual triggers you wish to disable
- `CAPT_BLACK_HOOKS` :: Set to CSV string of individual hooks you wish to
disable
- `CAPT_TIMEOUT` :: limit the duration of all triggers
- `CAPT_FILES_OVERRIDE` :: Set to list of files to run on (instead of git-staged)
- `CAPT_MAIN_BRANCH` :: Useful for running in CI since default will be feature branch
- `CAPT_FILE` :: Team-shared control file containing global hooks/triggers
Expand Down
9 changes: 7 additions & 2 deletions bin/capt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hooksdir=${CAPT_HOOKSDIR-$captdir/hooks}
scriptsdir=${CAPT_SCRIPTSDIR-$captdir/scripts}
captfile=${CAPT_FILE-$captdir/share.sh}
captfilelocal=${CAPT_LOCALFILE-.capt/local.sh}
timeout=${CAPT_TIMEOUT-10}
path+=$scriptsdir
hookfiles=( # Supported hooks
pre-commit prepare-commit-msg commit-msg post-commit post-checkout
Expand Down Expand Up @@ -120,6 +121,7 @@ typeset -A envars=(
CAPT_CAVALIER 'Set to ‘1’ to progress past all errors with no remorse'
CAPT_BLACK_TRIGGERS 'Set to CSV string of individual triggers you wish to disable'
CAPT_BLACK_HOOKS 'Set to CSV string of individual hooks you wish to disable'
CAPT_TIMEOUT 'Set to number of seconds allowed for each trigger completion (default 10s)'
CAPT_FILES_OVERRIDE 'Set to list of files to run on (instead of git-staged)'
CAPT_MAIN_BRANCH 'Useful for running in CI since default will be feature branch'
CAPT_FILE 'Team-shared control file containing global hooks/triggers'
Expand Down Expand Up @@ -605,13 +607,16 @@ run_scripts () {
dbg "script: $scriptsdir/$script"
# Try to find script or command
# FIXME Some commands print to stderr; need to capture it too; but look out for &
if [[ -f $scriptsdir/$script ]]; then output=$(eval $scriptsdir/$cmd)
elif type $cmd0 >/dev/null; then output=$(eval $cmd)
if [[ -f $scriptsdir/$script ]]; then output=$(eval timeout $timeout $scriptsdir/$cmd)
elif type $cmd0 >/dev/null; then output=$(eval timeout $timeout $cmd)
# Just skip this one hook if command not found
else aye "$fg[red]WARNING: Skippin on yer $print_name; $cmd0 and $script be lost at sea.$reset_color"
fi
es=$?
[[ -n $CAPT_VERBOSE && -n $output ]] && print -- "$output"
if [[ $es == 124 ]] # special timeout exit code; seems universal
then print "${fg[red]}Timeout (${timeout}s) occurred (Set it via CAPT_TIMEOUT, 0 to disable)$reset_color"
fi
wait # don't process until background scripts are finished
tfinal=$(( $(sdate +%s%3N) - $t0 )) units=ms units_color=white
if (( 1000 < $tfinal )); then tfinal=$(( $tfinal / 1000 )); units=s; units_color=red; fi
Expand Down

0 comments on commit ba03f64

Please sign in to comment.