|
1 | 1 | #!/bin/sh |
2 | | -file=$(jq -r '.tool_input.file_path') |
| 2 | +# claude's PostToolUse hook on Edit and Write (settings.json): formats the file |
| 3 | +# the tool just wrote. |
| 4 | +# |
| 5 | +# In the session's own repository, when its lefthook config defines formatters |
| 6 | +# (the pre-commit jobs that set stage_fixed), those run instead of the built-in |
| 7 | +# ones below, and only those: each on this one file, through its own glob, so |
| 8 | +# the file comes out the way a commit would leave it, and a file type the |
| 9 | +# repository doesn't format stays as it is. Everywhere else, and in a |
| 10 | +# repository whose config defines no formatter, the built-in ones run. |
| 11 | +# |
| 12 | +# Only the session's own repository, whose lefthook config the sandbox keeps |
| 13 | +# read-only (claude-sandbox pins each config file that exists; a local config a |
| 14 | +# session adds where there was none runs here, as it would at the next commit). |
| 15 | +# Elsewhere the config could be one the session wrote, which this hook would |
| 16 | +# then run for it, unreviewed. --no-stage-fixed keeps lefthook from staging the |
| 17 | +# fix, and from setting aside the unstaged changes of partially staged files |
| 18 | +# while the formatter runs, which it only does to stage a fix. |
| 19 | +file=$(jq -r '.tool_input.file_path // empty') |
3 | 20 | [ -f "$file" ] || exit 0 |
| 21 | +dir=$(dirname -- "$file") |
| 22 | +selectors= |
| 23 | + |
| 24 | +# The formatters in the config lefthook merges: a first line saying there are |
| 25 | +# some, then a selector for each one lefthook can pick out by name. |
| 26 | +formatters='."pre-commit" // {} |
| 27 | + | [(.jobs // [] | .. | objects | select(.stage_fixed == true and (has("run") or has("script"))) |
| 28 | + | if (.name // "") != "" then "--job=" + .name else "" end), |
| 29 | + (.commands // {} | to_entries[] | select(.value.stage_fixed == true) | "--command=" + .key), |
| 30 | + (.scripts // {} | to_entries[] | select(.value.stage_fixed == true) | "")] |
| 31 | + | if length > 0 then "formatters", (.[] | select(. != "")) else empty end' |
| 32 | + |
| 33 | +top=$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null) |
| 34 | +if [ -n "$top" ] && [ "$top" = "$(git -C "${CLAUDE_PROJECT_DIR:-.}" rev-parse --show-toplevel 2>/dev/null)" ]; then |
| 35 | + selectors=$(cd "$top" && lefthook dump --format json 2>/dev/null | jq -r "$formatters" 2>/dev/null) |
| 36 | +fi |
| 37 | + |
| 38 | +if [ -n "$selectors" ]; then |
| 39 | + set -- |
| 40 | + while IFS= read -r selector; do |
| 41 | + [ "$selector" = formatters ] || set -- "$@" "$selector" |
| 42 | + done <<EOF |
| 43 | +$selectors |
| 44 | +EOF |
| 45 | + # Formatters, but none lefthook can pick out: the file stays as it is. |
| 46 | + [ $# -gt 0 ] || exit 0 |
| 47 | + relpath=$(git -C "$dir" rev-parse --show-prefix)${file##*/} |
| 48 | + out=$(cd "$top" && LEFTHOOK_OUTPUT=execution_out,failure lefthook run pre-commit \ |
| 49 | + --no-stage-fixed --no-auto-install --no-tty --colors off "$@" --file "$relpath" 2>&1 </dev/null) && exit 0 |
| 50 | + rc=$? |
| 51 | + case $out in |
| 52 | + # No formatter's glob takes this file. |
| 53 | + *"no job matching"*) exit 0 ;; |
| 54 | + esac |
| 55 | + printf '%s\n' "$out" >&2 |
| 56 | + exit "$rc" |
| 57 | +fi |
| 58 | + |
4 | 59 | case "$file" in |
5 | 60 | *.go) gofmt -w "$file" ;; |
6 | 61 | *.nix) nixfmt "$file" ;; |
|
0 commit comments