Commit graph

1 commit

Author SHA1 Message Date
Paul Gledhill
ad8d6de417
fix(hooks): inject BEADS INTEGRATION above trailing exec block (GH#3537) (#3556)
* fix(hooks): inject BEADS INTEGRATION above trailing exec block (GH#3537)

When bd preserves an existing hook (e.g. .git/hooks/<name> copied into .beads/hooks/<name>) whose tail is an exec-replacing chain, the unconditional bottom-append in injectHookSection landed the BEADS INTEGRATION block below the exec — making it unreachable. The most common case is the hook produced by 'pre-commit init-templatedir', which ends with two exec calls inside an if/elif/else/fi ladder.

Add findExecBlockInjectionPoint that detects the pattern: a final exec line whose only trailing siblings are control-flow closers (fi, else, elif, then), exit, echo, comments, or blank lines. When detected, walk backward past elif/else/then continuations and indented body lines to find the start of the enclosing if-block, then inject the bd section just above it. Otherwise fall back to the existing append-at-end behaviour.

Tests cover: pre-commit's actual hook output, bare trailing exec, exec in middle (must NOT trigger), and a non-exec hook (must keep existing behaviour). New helpers are pure functions with no I/O.

--no-verify used: local pre-commit hook runs golangci-lint without the gms_pure_go build tag CONTRIBUTING.md mandates (ICU headers not installed locally — not a documented requirement). Verified clean with: golangci-lint run --new-from-rev=main --build-tags=gms_pure_go ./cmd/bd/ → 0 issues.

Refs: GH#3537

* test(hooks): cover remaining branches of findExecBlockInjectionPoint helpers

Codecov flagged 91.66% patch coverage on PR #3556. The two uncovered branches in my new code were:

- findExecBlockInjectionPoint:206 — the comment/blank-line skip in the backward-walk loop (no fixture had a comment between the if-opener and the exec line).

- isAllowedAfterExec:252 — the 'elif <args>' / 'else <args>' branch (existing fixtures only hit the bare 'else'/'fi' switch case).

Adds three focused tests: TestInjectHookSection_IfBlockWithCommentInBody (the integration fixture), and table-driven TestIsAllowedAfterExec_TableDriven + TestIsExecLine_TableDriven covering every branch of both helpers. All four functions touched by this PR now report 100% coverage.

* address review nits: doc edge cases + TrimRight + idempotency/double-block tests

Five nits from automated review on PR #3556. The two functional changes:

1. strings.Split off-by-one. Replaced strings.Split(content, '\\n') with strings.Split(strings.TrimRight(content, '\\n'), '\\n') to eliminate the spurious empty trailing element from files that end in newline. The behaviour was correct in practice but the trim makes the intent explicit and removes the off-by-one ambiguity.

2. findExecBlockInjectionPoint godoc. Documented three line-based-heuristic limitations: heredoc bodies containing exec, trailing-comment exec lines (e.g. 'exec /bin/foo  # disabled'), and disjoint exec blocks separated by real code. Each is covered or harmless in practice but worth flagging to future maintainers.

3. New table-driven case 'two disjoint exec blocks, real code after both' — confirms the safe -1 fallback behaviour when the file isn't strictly exec-terminating.

4. New TestInjectHookSection_Idempotent_PrecommitTemplate — runs injectHookSection twice on a pre-commit-templated hook and asserts the output is identical. Locks in the idempotency that the marker logic already provides for free, in case a future refactor moves the strip-and-re-inject path.
2026-04-28 06:40:38 -07:00