fix(hooks): preserve user content when migrating v0.62.x → v1.0.x (GH#3536) (#3557)
* fix(hooks): preserve user content when migrating v0.62.x → v1.0.x (GH#3536) preservePreexistingHooks treated any file containing the BEADS INTEGRATION begin marker as wholly bd-managed and skipped it during preservation. But the v0.62.x inline-injection model only injected a *block* into otherwise-user-owned files (custom dispatchers, framework integrations, husky setups). On migration to v1.0.x's relocate-to-.beads/hooks/ model, that user content was silently discarded — only the bare bd shim ended up in .beads/hooks/<name>. Fix: when the begin marker is present, strip the bd section using the existing removeHookSection helper, then preserve any remaining user content. Only skip if stripping leaves nothing meaningful (an isOnlyShebangOrEmpty check). The inlineHookMarker case (GH#1120 one-liners) is unchanged — those files genuinely were wholly bd-owned. Tests cover: stripping a v0.62.x file (user content survives), skipping a wholly-managed file (v1.0.x bare shim and bare-marker-block), and leaving non-marker hooks alone. Plus a table-driven test for the new isOnlyShebangOrEmpty helper. --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#3536 * test(hooks): extract shouldPreserveHookContent for direct unit-test coverage Codecov flagged 53% patch coverage on PR #3557. The strip-and-preserve logic was inlined inside preservePreexistingHooks (which does file I/O and depends on git context), so unit tests could only exercise it via cumbersome integration tests that proved test-isolation-fragile in this package. Extract shouldPreserveHookContent(content, fromHusky) (string, bool) — a pure function with no I/O, no git context, no globals — that holds the per-file decision logic. preservePreexistingHooks now calls it inline and acts on the returned (transformedContent, keep) tuple. Add table-driven tests covering every branch: inline marker (skip), v0.62.x marker block with user content (strip + keep), marker block with only shebang (strip leaves nothing → skip), no markers (preserve verbatim), husky v8 sanitisation (with and without prior marker strip). Plus a focused regression test that proves the dispatcher above the marker block survives the strip. Coverage on the new pure function: 100%. preservePreexistingHooks coverage unchanged from baseline (still exercised by the existing TestInstallHooksBeads_* integration tests). * fixup: spelling — sanitised → sanitized (lint) * address review nits: CRLF normalisation + godoc + edge tests Three nits from automated review on PR #3557: 1. CRLF normalisation gap. The new strip-then-preserve path inherited a Windows / autocrlf=true edge case where '\\r\\n' could end up in .beads/hooks/<name>. Mirror what injectHookSection does on its inject path — normalize after the strip with a single ReplaceAll. 2. isOnlyShebangOrEmpty godoc. Make the design choice explicit: non-shebang comment lines are intentionally treated as non-content; a file that's just shebang + comment is classified empty and skipped. 3. Edge-case tests. Add TestShouldPreserveHookContent_TwoBlankLinesBeforeMarker (locks in the boundary where removeHookSection collapses '\\n\\n\\n' → '\\n\\n') and TestShouldPreserveHookContent_CRLFNormalised (locks in the new normalisation).
This commit is contained in:
parent
b9e16ac9d0
commit
0db2a66b5b
2 changed files with 264 additions and 11 deletions
|
|
@ -267,6 +267,67 @@ func removeHookSection(content string) (string, bool) {
|
|||
return result, true
|
||||
}
|
||||
|
||||
// isOnlyShebangOrEmpty reports whether the given hook content consists of
|
||||
// nothing meaningful — only an optional shebang line plus blank lines and
|
||||
// comments. Used by shouldPreserveHookContent to decide, after stripping a
|
||||
// BEADS INTEGRATION block, whether anything user-owned remains worth
|
||||
// preserving.
|
||||
//
|
||||
// Note: non-shebang comment lines (e.g. `# preamble`) are intentionally
|
||||
// treated as non-content. A file that's only a shebang plus a comment is
|
||||
// classified empty and skipped — comments alone aren't user logic worth
|
||||
// carrying forward to .beads/hooks/<name>. (GH#3536)
|
||||
func isOnlyShebangOrEmpty(content string) bool {
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" || strings.HasPrefix(trimmed, "#!") || strings.HasPrefix(trimmed, "#") {
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// shouldPreserveHookContent decides what preservePreexistingHooks should do
|
||||
// with one hook file's content. Returns (transformedContent, true) when the
|
||||
// file should be preserved into the target directory (possibly with the bd
|
||||
// section stripped or the husky helper-layout sanitized); returns
|
||||
// ("", false) when preservation should skip this file because it's wholly
|
||||
// bd-managed and contains nothing user-owned worth keeping.
|
||||
//
|
||||
// Decision rules (GH#3536):
|
||||
// - inlineHookMarker (the "# bd (beads)" tag from GH#1120) marks files
|
||||
// that were always wholly bd-owned one-liners — skip.
|
||||
// - hookSectionBeginPrefix marks files that were *user-owned* with bd's
|
||||
// block injected into them (the v0.49+ section-marker model). Strip
|
||||
// the bd block and preserve the remaining user content. If only a
|
||||
// shebang/blank/comments remain, treat as wholly bd-owned and skip.
|
||||
// - When fromHusky is true, sanitize the (possibly stripped) content so
|
||||
// it doesn't depend on husky's helper-layout being mirrored into the
|
||||
// target directory (GH#3132).
|
||||
//
|
||||
// The function is pure: no I/O, no global state.
|
||||
func shouldPreserveHookContent(content string, fromHusky bool) (string, bool) {
|
||||
if strings.Contains(content, inlineHookMarker) {
|
||||
return "", false
|
||||
}
|
||||
if strings.Contains(content, hookSectionBeginPrefix) {
|
||||
stripped, _ := removeHookSection(content)
|
||||
if isOnlyShebangOrEmpty(stripped) {
|
||||
return "", false
|
||||
}
|
||||
// Normalize CRLF → LF on the preserved-and-stripped content so
|
||||
// Windows / autocrlf=true repos don't end up with `\r\n` line
|
||||
// endings in .beads/hooks/<name>. Mirrors the normalization that
|
||||
// injectHookSection does on its output (`hooks.go` ~line 622).
|
||||
content = strings.ReplaceAll(stripped, "\r\n", "\n")
|
||||
}
|
||||
if fromHusky {
|
||||
content = sanitizeHuskyHook(content)
|
||||
}
|
||||
return content, true
|
||||
}
|
||||
|
||||
// HookStatus represents the status of a single git hook
|
||||
type HookStatus struct {
|
||||
Name string
|
||||
|
|
@ -720,19 +781,11 @@ func preservePreexistingHooks(targetDir string) {
|
|||
continue
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
// Skip if it's already a beads hook
|
||||
if strings.Contains(contentStr, hookSectionBeginPrefix) ||
|
||||
strings.Contains(contentStr, inlineHookMarker) {
|
||||
newContent, keep := shouldPreserveHookContent(string(content), fromHusky)
|
||||
if !keep {
|
||||
continue
|
||||
}
|
||||
|
||||
// If this came from a husky directory, rewrite the hook so it no
|
||||
// longer depends on husky's helper layout being mirrored into the
|
||||
// target directory. See sanitizeHuskyHook below for details.
|
||||
if fromHusky {
|
||||
contentStr = sanitizeHuskyHook(contentStr)
|
||||
}
|
||||
contentStr := newContent
|
||||
|
||||
// Don't overwrite existing files in target
|
||||
dstPath := filepath.Join(targetDir, entry.Name())
|
||||
|
|
|
|||
200
cmd/bd/hooks_marker_skip_test.go
Normal file
200
cmd/bd/hooks_marker_skip_test.go
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestIsOnlyShebangOrEmpty_TableDriven exercises the helper used by
|
||||
// shouldPreserveHookContent to decide, after stripping a BEADS INTEGRATION
|
||||
// block, whether anything user-owned remains worth preserving. (GH#3536)
|
||||
func TestIsOnlyShebangOrEmpty_TableDriven(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
want bool
|
||||
}{
|
||||
{name: "empty", content: "", want: true},
|
||||
{name: "only blanks", content: "\n\n\n", want: true},
|
||||
{name: "only shebang", content: "#!/usr/bin/env sh\n", want: true},
|
||||
{name: "shebang + blank lines", content: "#!/bin/sh\n\n\n", want: true},
|
||||
{name: "shebang + comments", content: "#!/bin/sh\n# a comment\n# another\n", want: true},
|
||||
{name: "shebang + one command", content: "#!/bin/sh\necho hi\n", want: false},
|
||||
{name: "no shebang, has command", content: "echo hi\n", want: false},
|
||||
{name: "user dispatcher", content: "#!/bin/sh\nset -e\nrun_precommit pre-commit\n", want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isOnlyShebangOrEmpty(tt.content); got != tt.want {
|
||||
t.Errorf("isOnlyShebangOrEmpty(%q) = %v, want %v", tt.content, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestShouldPreserveHookContent_TableDriven covers every branch of the new
|
||||
// pure-function decision used by preservePreexistingHooks. Each case mirrors
|
||||
// a real situation the migration path encounters. (GH#3536)
|
||||
func TestShouldPreserveHookContent_TableDriven(t *testing.T) {
|
||||
const v062Marker = "# --- BEGIN BEADS INTEGRATION v0.62.0 ---\n" +
|
||||
"# This section is managed by beads. Do not remove these markers.\n" +
|
||||
"if command -v bd >/dev/null 2>&1; then\n" +
|
||||
" bd hook pre-commit \"$@\"\n" +
|
||||
"fi\n" +
|
||||
"# --- END BEADS INTEGRATION v0.62.0 ---\n"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
fromHusky bool
|
||||
wantKeep bool
|
||||
wantBody string // exact wantBody when wantKeep is true; ignored otherwise
|
||||
wantNot string // substring that must NOT be present in the kept body
|
||||
}{
|
||||
{
|
||||
name: "inline marker — wholly bd-managed, skip",
|
||||
content: "#!/bin/sh\n# bd (beads)\nbd hook pre-commit \"$@\"\n",
|
||||
wantKeep: false,
|
||||
},
|
||||
{
|
||||
name: "v0.62.x style: dispatcher above + bd marker block — strip and preserve",
|
||||
content: "#!/bin/sh\nset -e\n" +
|
||||
"# user dispatcher\n" +
|
||||
"if [ -f .pre-commit-config.yaml ] && command -v pre-commit >/dev/null 2>&1; then\n" +
|
||||
" pre-commit run --hook-stage pre-commit \"$@\"\n" +
|
||||
"fi\n\n" +
|
||||
v062Marker,
|
||||
wantKeep: true,
|
||||
wantNot: "BEADS INTEGRATION",
|
||||
},
|
||||
{
|
||||
name: "marker block + only shebang — strip leaves nothing, skip",
|
||||
content: "#!/bin/sh\n\n" + v062Marker,
|
||||
wantKeep: false,
|
||||
},
|
||||
{
|
||||
name: "no markers — preserve verbatim",
|
||||
content: "#!/bin/sh\nset -e\necho 'plain user hook'\nmake lint\n",
|
||||
wantKeep: true,
|
||||
wantBody: "#!/bin/sh\nset -e\necho 'plain user hook'\nmake lint\n",
|
||||
},
|
||||
{
|
||||
name: "husky v8: source line stripped, PATH injected",
|
||||
content: "#!/usr/bin/env sh\n. \"$(dirname -- \"$0\")/_/husky.sh\"\n\nnpx lint-staged\n",
|
||||
fromHusky: true,
|
||||
wantKeep: true,
|
||||
wantNot: "/_/husky.sh",
|
||||
},
|
||||
{
|
||||
name: "husky after marker-strip: still gets sanitized",
|
||||
content: "#!/usr/bin/env sh\n. \"$(dirname -- \"$0\")/_/husky.sh\"\n\nnpx lint-staged\n\n" + v062Marker,
|
||||
fromHusky: true,
|
||||
wantKeep: true,
|
||||
wantNot: "BEADS INTEGRATION",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
body, keep := shouldPreserveHookContent(tt.content, tt.fromHusky)
|
||||
if keep != tt.wantKeep {
|
||||
t.Fatalf("keep = %v, want %v (body=%q)", keep, tt.wantKeep, body)
|
||||
}
|
||||
if !tt.wantKeep {
|
||||
return
|
||||
}
|
||||
if tt.wantBody != "" && body != tt.wantBody {
|
||||
t.Errorf("body mismatch:\nwant=%q\ngot =%q", tt.wantBody, body)
|
||||
}
|
||||
if tt.wantNot != "" && strings.Contains(body, tt.wantNot) {
|
||||
t.Errorf("kept body unexpectedly contains %q:\n%s", tt.wantNot, body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestShouldPreserveHookContent_TwoBlankLinesBeforeMarker exercises the
|
||||
// boundary where user content ends in two blank lines before the bd marker
|
||||
// block. removeHookSection collapses runs of `\n\n\n` to `\n\n`, so this
|
||||
// fixture confirms the strip doesn't over-trim user content.
|
||||
func TestShouldPreserveHookContent_TwoBlankLinesBeforeMarker(t *testing.T) {
|
||||
in := "#!/bin/sh\n" +
|
||||
"set -e\n" +
|
||||
"echo 'user line 1'\n" +
|
||||
"echo 'user line 2'\n" +
|
||||
"\n" +
|
||||
"\n" +
|
||||
"# --- BEGIN BEADS INTEGRATION v0.62.0 ---\n" +
|
||||
"if command -v bd >/dev/null 2>&1; then bd hook pre-commit \"$@\"; fi\n" +
|
||||
"# --- END BEADS INTEGRATION v0.62.0 ---\n"
|
||||
|
||||
body, keep := shouldPreserveHookContent(in, false)
|
||||
if !keep {
|
||||
t.Fatalf("two-blank-line boundary: keep=false, body=%q", body)
|
||||
}
|
||||
if !strings.Contains(body, "echo 'user line 1'") || !strings.Contains(body, "echo 'user line 2'") {
|
||||
t.Errorf("user content lost across two-blank-line boundary:\n%q", body)
|
||||
}
|
||||
if strings.Contains(body, "BEADS INTEGRATION") {
|
||||
t.Errorf("bd markers leaked through:\n%q", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShouldPreserveHookContent_CRLFNormalised verifies that a v0.62.x hook
|
||||
// stored with Windows-style `\r\n` line endings is normalised to LF on the
|
||||
// preserved-and-stripped output. Mirrors the normalisation that
|
||||
// injectHookSection applies on its inject path. (GH#3536 nit)
|
||||
func TestShouldPreserveHookContent_CRLFNormalised(t *testing.T) {
|
||||
in := "#!/bin/sh\r\n" +
|
||||
"set -e\r\n" +
|
||||
"echo 'user'\r\n" +
|
||||
"\r\n" +
|
||||
"# --- BEGIN BEADS INTEGRATION v0.62.0 ---\r\n" +
|
||||
"if command -v bd >/dev/null 2>&1; then bd hook pre-commit \"$@\"; fi\r\n" +
|
||||
"# --- END BEADS INTEGRATION v0.62.0 ---\r\n"
|
||||
|
||||
body, keep := shouldPreserveHookContent(in, false)
|
||||
if !keep {
|
||||
t.Fatalf("CRLF input: keep=false, body=%q", body)
|
||||
}
|
||||
if strings.Contains(body, "\r\n") {
|
||||
t.Errorf("CRLF survived into preserved output (should be normalised to LF):\n%q", body)
|
||||
}
|
||||
if !strings.Contains(body, "echo 'user'") {
|
||||
t.Errorf("user content lost:\n%q", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShouldPreserveHookContent_StrippedDispatcherSurvives is the explicit
|
||||
// GH#3536 regression: the user dispatcher above the bd marker block must
|
||||
// remain in the returned body so preservePreexistingHooks writes it into
|
||||
// .beads/hooks/<name>.
|
||||
func TestShouldPreserveHookContent_StrippedDispatcherSurvives(t *testing.T) {
|
||||
in := `#!/bin/sh
|
||||
set -e
|
||||
if [ -f .pre-commit-config.yaml ] && command -v pre-commit >/dev/null 2>&1; then
|
||||
pre-commit run --hook-stage pre-commit "$@"
|
||||
fi
|
||||
|
||||
# --- BEGIN BEADS INTEGRATION v0.62.0 ---
|
||||
# This section is managed by beads. Do not remove these markers.
|
||||
if command -v bd >/dev/null 2>&1; then
|
||||
bd hook pre-commit "$@"
|
||||
fi
|
||||
# --- END BEADS INTEGRATION v0.62.0 ---
|
||||
`
|
||||
body, keep := shouldPreserveHookContent(in, false)
|
||||
if !keep {
|
||||
t.Fatalf("dispatcher with marker block: keep=false, body=%q", body)
|
||||
}
|
||||
if !strings.Contains(body, "pre-commit run --hook-stage") {
|
||||
t.Errorf("dispatcher line lost:\n%s", body)
|
||||
}
|
||||
if strings.Contains(body, "BEGIN BEADS INTEGRATION") ||
|
||||
strings.Contains(body, "END BEADS INTEGRATION") {
|
||||
t.Errorf("bd markers leaked through:\n%s", body)
|
||||
}
|
||||
if strings.Contains(body, "bd hook pre-commit") {
|
||||
t.Errorf("legacy 'bd hook' invocation should have been stripped with the marker block:\n%s", body)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue