* fix(doctor): report stealth exclude read failures (saa-cgh.118) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(doctor): retain leak detail when exclude cannot be read (saa-cgh.120) Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell * fix(doctor): run the stealth privacy repair even when the exclude is unreadable Maintainer review fixes for PR #6427 (adopt-pr iteration 1). This PR makes addProjectPatternsToGitExclude go from always-nil to sometimes-error, which newly activates the `err == nil` gate in applyFixList's stealth branch and skips removeBeadsProjectGitignoreSection — the privacy half of the repair, which never needed the exclude file at all. On an unreadable .git/info/exclude, `bd doctor --fix` therefore left Dolt and credential patterns in a committed .gitignore. The two halves are independent, so join their errors instead of gating one on the other, the way `bd init --stealth` already runs both (init.go:1098-1109). With --fix able to complete the privacy half again, restore the leak headline and `Fix: "Run: bd doctor --fix"` on checkProjectExcludeStealth's new read-error branch — but only when the tracked .gitignore is actually leaked. A read error with a clean .gitignore keeps the bare "Unable to read" message and no Fix, because --fix genuinely cannot repair that. Also, for AppendLines' no-lines fast path, return bytes.Clone(content) rather than aliasing the caller's slice. The branch cannot simply be dropped: the unterminated-final-line completion runs before the loop, so dropping it turns "last\r" into "last\r\n" and reds the package's own "no lines" case. Follow-up filed as bd-ltf9k for the three same-class appenders still on the raw-LF idiom (worktree_cmd.go addToGitignore, doctor EnsureGitignoreForBeadsDir, fs.WriteBeadsGitignore) — minor, no behavior change for LF files. Validation: go test ./internal/gitignore/ and a blast-radius ./cmd/bd/ run (20 top-level tests, 41 with subtests) green; CGO=0 and GOOS=windows go vet clean on both touched packages. Each half of the fix is mutation-proven: restoring the `err == nil` gate reds the new applyFixList test while leaving the diagnostic boundaries green, and dropping the headline/Fix reds only the `directory` case, not `directory_clean`. Iteration 2 — report the newly reachable path instead of going silent. Running both halves means --fix can now delete the leaked section while the exclude write fails, leaving the project-root patterns ignored by neither plane, and applyFixList said nothing about either outcome: it discarded the `removed` bool that its init.go sibling prints and reported only the exclude error. Print that same confirmation when the section was removed, and, when the exclude half failed and the remove half did not, name the patterns that are now ignored by neither .git/info/exclude nor the tracked .gitignore. The warning is gated on removeErr == nil because that is the only state in which its claim is true — if the remove also failed, the leaked section may still be covering them. Pin the reporting half too. The regression test asserted only .gitignore bytes, so dropping excludeErr from the join passed it while --fix printed "✓ Fixed" and "Fix summary: 1 fixed, 0 errors" for a repair whose exclude half had failed. It now captures applyFixList's stdout and pins the surfaced failure alongside both new lines; each of the three is mutation-proven to red its own assertion and no other. Declined, and tracked as bd-x0vk1: giving checkProjectExcludeStealth's read-error branch a standing Fix. Measured rather than argued — it reds `directory_clean` alone, which is the leak-only scoping both review lanes ratified, and since fixable checks are selected by `Status == warning && Fix != ""` (doctor_fix.go:24,61) it would also pull the no-leak population into --fix, which would then attempt a repair that is a guaranteed no-op on the tracked .gitignore and a guaranteed failure on the unreadable exclude. The read fault itself stays diagnosable through Detail. Validation (iteration 2): the same blast-radius ./cmd/bd/ run and ./internal/gitignore/ green, CGO=0 and GOOS=windows go vet clean on both touched packages. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
// Package gitignore provides formatting helpers for user-owned gitignore files.
|
|
package gitignore
|
|
|
|
import "bytes"
|
|
|
|
// AppendLineEnding preserves an unambiguous CRLF convention, following the
|
|
// append policy in #6343. Empty, delimiter-free, LF and mixed files
|
|
// default to LF; callers must leave existing bytes unchanged.
|
|
func AppendLineEnding(content []byte) string {
|
|
lineFeeds := bytes.Count(content, []byte{'\n'})
|
|
if lineFeeds > 0 && lineFeeds == bytes.Count(content, []byte("\r\n")) {
|
|
return "\r\n"
|
|
}
|
|
return "\n"
|
|
}
|
|
|
|
// AppendLines appends logical lines using the existing file's line ending,
|
|
// preserving existing bytes and completing any unterminated final line.
|
|
// A trailing CR is completed with a bare LF.
|
|
// Callers choose their own blank lines, headers and patterns.
|
|
// With no lines, content is returned unchanged as a copy: the branch cannot be
|
|
// dropped, because an unterminated final line would otherwise be completed.
|
|
func AppendLines(content []byte, lines []string) []byte {
|
|
if len(lines) == 0 {
|
|
return bytes.Clone(content)
|
|
}
|
|
lineEnding := AppendLineEnding(content)
|
|
var buf bytes.Buffer
|
|
buf.Write(content)
|
|
if len(content) > 0 && content[len(content)-1] != '\n' {
|
|
if content[len(content)-1] == '\r' {
|
|
buf.WriteByte('\n') // Complete the existing CR without doubling it.
|
|
} else {
|
|
buf.WriteString(lineEnding)
|
|
}
|
|
}
|
|
for _, line := range lines {
|
|
buf.WriteString(line)
|
|
buf.WriteString(lineEnding)
|
|
}
|
|
return buf.Bytes()
|
|
}
|