Skip to content

refactor(lib): simplify the last third of hook-utils.sh - #15417

Merged
kyle-sexton merged 1 commit into
mainfrom
refactor/simplify-hook-utils-part-3
Nov 11, 2026
Merged

kyle-sexton merged 1 commit into
mainfrom
refactor/simplify-hook-utils-part-3

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Closes #14333

Summary

Simplifies the last third of lib/hook-utils.sh (lines 3469-5201 at 1feb847, from ansi_c_decode_to on) without changing its behavior, per #14333. Changed: env_s_split, shell_c_operand, wsl_operand, git_resolve_index, git_resolve_subcommand, git_effective_dir_to, bash_parse_segments_shfmt and findings_to. No new functions; exported names, flags and printed text unchanged. emit_telemetry, which starts in part 2's range, is untouched.

Built by hand rather than by the issue lane: the lane's patch would exceed its 262144-byte scan cap because hook-utils.sh has 20 regenerated copies under plugins/. Same route as part 2 (#15399), agreed with the hub session.

session: Simplify Planning

Fix

  • 139 hand-changed lines (+44/-95) in one source file (cap 400 lines, 15 files).
  • Removed four case arms that did what the default arm does or could not be reached, and -[!-]* guards the peel regexes already cover; merged the config arm (its break reaches the same return 1 with the same globals) and the env -S arm (same operand source, slice offset and double strip). Each new A && B && ((i++)) list increments a value already 1 or more, so it is safe under set -e.
  • scripts/sync-shared-copies.sh regenerated the 20 copies; each carrier plugin has one bump: patch fragment.

Verification

  • Before/after pin over 159 cases under C and C.UTF-8, on linux-gnu and on msys with a cygpath stub, dumping every HOOK_GIT_* result global that destructive-guard.sh and the tests read (git_invocation, git_resolve_index, env_s_split, git_effective_dir_to, git_is_bin, shell_c_operand, wsl_operand, bash_parse_segments callbacks, refusals and heredoc modes, findings_to --delta): identical. Two deliberate breaks were caught.
  • Suites: hook-utils 703 pass (1 fail also at base), shfmt reader 170, secret-pattern 312, sync-shared-copies.test.sh 96, block-dangerous-git 1030 (5 fail also at base: strace execve counts and 512 KB-stack timing on this host), noncanonical-commit 292 (2 fail also at base).
  • shellcheck, check-shell-portability, sync-shared-copies.sh --check (767 copies) and --check-bump, fragment --check, typos: clean.

Related

Follow-ups

  • Pre-existing (advisory): lib/hook-utils.sh fails shfmt under the repo .editorconfig (switch_case_indent) at base; not reformatted here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VQb9Q3tnh28AzDYZ8AUjhH

Simplify the functions that start in the last third of lib/hook-utils.sh
(hook::ansi_c_decode_to onward) without changing behavior, per #14333:

- git_resolve_index: merge the two env -S splice arms, drop option arms
  that matched what the default arm already did, drop the redundant
  guard around the env and sudo short-option peels, and fold the
  optional-operand skips into one line each.
- git_resolve_subcommand: one arm for -c/--config/--config-env in both
  the two-word and the = form.
- env_s_split, wsl_operand, shell_c_operand, git_effective_dir_to,
  bash_parse_segments_shfmt and findings_to: shorter control flow.

Regenerated the 20 shared copies and added one patch fragment per
carrying plugin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VQb9Q3tnh28AzDYZ8AUjhH
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 11, 2026 19:05
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T19:07:40.269101Z adaaa52 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

claude-security-review reviewed adaaa521715f829c8ec92b66efb424a2b27741eb: read 41 of 41 changed file(s); 0 finding(s), 0 as inline comments.

Security review: no security issues found

This PR refactors the shared bash library lib/hook-utils.sh and regenerates its 20 copies under plugins/. It also adds 20 identical bump: patch changelog fragments. The library parses commands for the guard hooks, for example how destructive-guard.sh and block-dangerous-git recognise git behind wrappers. A refactor here could let a dangerous command get past those guards. So I checked every changed hunk to see whether it makes the parser recognise less than before. None does.

What I checked, hunk by hunk (lib/hook-utils.sh):

  • env_s_split, double-quote escape (L3522): [[ \ ]] && ((i+1<n)) && ((i++)), followed by the shared append, still adds s[i+1] and moves on by 2. A backslash at the end of the string is still added as-is. i is at least 1 there, since the opening " was already consumed, so the trailing ((i++)) cannot trip set -e.
  • shell_c_operand (L3610): ((has_c && i < n)) || return 1 behaves the same as the two separate checks it replaces.
  • wsl_operand backslash count (L3692): it still writes 2nb+1 backslashes before a " and nb otherwise. nb is reset right after.
  • env and sudo option peeling (L3800, L3900): the dropped -[!-]* guard did nothing. The peel patterns ^-[i0v](.+)$ and ^-[ABbeEHKklnPsSvV](.+)$ already cannot match a --long option or a bare -.
  • env -S / --split-string arms merged (L3832):
    • The two old arms sat next to each other, so the merged arm catches nothing an earlier arm used to catch.
    • The bare form still takes w[i+1], or an empty string when nothing follows, and skips 2.
    • The attached form still strips -S or --split-string= and skips 1.
    • The self-referential -S '-S -S' case still runs out, because the argv still gets shorter.
  • nice and timeout option loops (L3881, L3924): the removed arms either did the same thing as the default arm or could not be reached. The *) break arm was unreachable because the loop only runs while the word starts with -.
  • command/exec/builtin/eval/! (L3942): the inline ${tok##*/} == command test matches the old is_command flag, because tok is not changed inside the loop. The bail-out on command -v/-V is kept.
  • git_resolve_subcommand config arms (L4007):
    • All four forms still record the same value and the same kind. The --config-env forms are tagged env, and --config=x cannot match --config-env*.
    • When the option is the last word, break replaces j += 2. Both leave the loop with HOOK_GIT_SUB empty, and the HOOK_GIT_CONFIG_* arrays get nothing new in either case.
  • git_effective_dir_to (L4259): -C path handling is unchanged. An absolute POSIX or X:\ path replaces the base, and anything else is appended to it. A -C as the last word was already ignored.
  • Heredoc mode choice (L4978) and findings_to context reset (L5095): same logic, just rewritten.

Copies: the 20 plugins/**/hook-utils.sh copies carry hunks identical to the source, only shifted by two lines. Each sits in a hooks or scripts folder where it is used as the same shared library, so its location changes nothing about what it does. The .changes/* fragments only hold release notes and run nothing.

Not applicable here:

  • No trusted path, plugin manifest, workflow, token or kill-switch code is touched.
  • No standing instruction is deleted. The guard library's checks are restated in equivalent code, not removed.
  • Nothing in this PR can bill anyone.
  • The PR context contains no embedded instructions aimed at this lane.

critical-findings: 0

@github-actions

Copy link
Copy Markdown
Contributor

claude-review reviewed adaaa521715f829c8ec92b66efb424a2b27741eb: read 41 of 41 changed file(s); 0 finding(s), 0 as inline comments.

Code review: PR #15417, refactor(lib): simplify the last third of hook-utils.sh

No findings. I read every hunk of lib/hook-utils.sh, the first changelog fragment and the first regenerated copy (plugins/animation/hooks/hook-utils.sh). The other 19 fragments and 19 copies are byte-identical to those per identical.txt, and each copy keeps the same role as a sourced hook library. The copy's hunks match the source's, shifted by 2 lines. I checked each change against its unchanged surrounding code and found it equivalent:

  • env_s_split, backslash inside "…": the escaped character is still the one appended, and the index advances the same distance. i ≥ 1 at that point, so the trailing ((i++)) cannot return 1 under set -e.
  • shell_c_operand: ((has_c && i < n)) || return 1 behaves the same as the two separate guards.
  • wsl_operand: after the loop nb is still 0 at every reset point, and the backslash count is the same: 2*nb+1 before a ", else nb.
  • git_resolve_index:
    • The -[!-]* guards removed in env and sudo were redundant: ^-[i0v](.+)$ and ^-[ABb…V](.+)$ can never match a --… word.
    • The -*) arms just above *) did the same thing as the default.
    • The *) break arms in nice and timeout could not be reached because the while condition requires -*.
    • The merged -S* | --split-string | --split-string=* arm keeps the same operand source and slice offset: ${w[i + 1]-} gives "" past the end, as ((i + 1 < n)) && sval=… did. The double strip is unchanged.
    • The inlined command test reads tok, which the inner loop never changes.
    • The new A && B && ((i++)) lists all run with i ≥ 1.
  • git_resolve_subcommand: the merged config arm gives the same values and kinds. --config=* cannot match --config-env=…. When the operand is missing, break reaches the same return 1 with the globals untouched, just as j += 2 used to end the loop.
  • git_effective_dir_to: a trailing -C was skipped before and is still skipped. arg is not read after the loop.
  • bash_parse_segments_shfmt and findings_to: __hu_hm is set after the recursive call it follows, and ((__hu_ft_n)) || … is the same as == 0.

Closing issue #14333 criteria

  • Behavior unchanged: I found no divergence by reading the code. lib/hook-utils.test.sh exercises these functions (40 references), as do the guardrails guard tests. The 159-case before/after comparison and the suite results come from the PR body; I did not run them.
  • Only the listed file, its regenerated copies and changelog fragments are changed: met.
  • Size: about 139 hand-changed lines in one source file, within the 400-line, 15-file cap.
  • Only functions starting in range 3 are changed: every changed function starts after ansi_c_decode_to.
  • sync-shared-copies.sh was run: the 20 copies carry the same hunks.
  • ci-status green: not verified by this lane.

Changelog: each carrier plugin gets one bump: patch fragment. That matches an internal change with no behavior change. No plugin.json or marketplace.json is changed.

Spend: none. The change doesn't touch lane runs, models or schedules.

@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit a3a31af Nov 11, 2026
88 of 89 checks passed
@kyle-sexton
kyle-sexton deleted the refactor/simplify-hook-utils-part-3 branch November 11, 2026 19:25
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify the hook-utils.sh shared lib source (part 3 of 3)

1 participant