refactor(worktree): route addToGitignore through gitignore.AppendLines (post-merge #5677) (#7006)

#5677 made addToGitignore preserve a CRLF .gitignore by consuming
gitignore.AppendLineEnding, but it re-implemented the rest of the #6343
append policy inline: the final-line completion, including the rule that a
trailing CR is completed with a bare LF. That policy already lives in
gitignore.AppendLines, which the three other project-ignore writers call.
The two copies are byte-equivalent today, and only one of them is
discoverable when the policy changes.

Derive the appended bytes from AppendLines instead. Its contract leaves the
existing bytes unchanged, so everything past len(content) is exactly the
append, and writing only that suffix keeps the O_APPEND handle: the user's
.gitignore is still appended to, never truncated and rewritten. The write
also becomes a single Write call instead of a separator write followed by an
Fprintf.

No behavior change. The call-site tests pass unmodified, byte for byte. The
doc comment on TestAddToGitignoreCompletesUnterminatedCRLFLine is reworded
because it described the removed separator variable; the arm it pins, and
why only that test reaches it, are unchanged.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR

Co-authored-by: Eddie the Engineer <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Julian Knutsen 2026-10-01 05:24:11 -07:00 • committed by GitHub
commit ba2f0afe4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 27 deletions

View file

@ -2141,22 +2141,12 @@ func addToGitignore(ctx context.Context, repoRoot, entry string) error {
}
defer f.Close()
lineEnding := gitignore.AppendLineEnding(content)
// Add newline if file doesn't end with one
if len(content) > 0 && content[len(content)-1] != '\n' {
separator := lineEnding
// Only CRLF needs this guard: the LF separator already completes a trailing CR.
if lineEnding == "\r\n" && content[len(content)-1] == '\r' {
separator = "\n" // Complete the existing CR without rewriting it.
}
if _, err := f.WriteString(separator); err != nil {
return err
}
}
// Add comment and entry
if _, err := fmt.Fprintf(f, "# bd worktree%s%s/%s", lineEnding, entry, lineEnding); err != nil {
// AppendLines owns the line-ending and final-line completion policy and
// leaves existing bytes unchanged, so everything past len(content) is the
// append. Writing only that suffix keeps this an O_APPEND write rather than
// a rewrite of the user's file.
appended := gitignore.AppendLines(content, []string{"# bd worktree", entry + "/"})
if _, err := f.Write(appended[len(content):]); err != nil {
return err
}

View file

@ -31,21 +31,21 @@ func TestAddToGitignoreCompletesTrailingCR(t *testing.T) {
}
// TestAddToGitignoreCompletesUnterminatedCRLFLine covers the one arm where
// addToGitignore actually uses the detected line ending as its separator.
// addToGitignore's append completes the final line with the detected line ending.
//
// The separator assignment is guarded twice over: the block is only entered when
// the file does not end in "\n", and inside it a trailing "\r" is completed with
// a bare "\n" instead. So the detected "\r\n" is used only for a CRLF file whose
// gitignore.AppendLines guards that completion twice over: it only completes a
// file that does not end in "\n", and it completes a trailing "\r" with a bare
// "\n" instead. So the detected "\r\n" is used only for a CRLF file whose
// final byte is neither — an unterminated final line like "build/". The sibling
// fixtures in this file cannot reach it: "a/\r\nb/\r" ends in CR and takes the
// completion branch, and "node_modules/\r\n" ends in LF and never enters the
// block at all. Without this case, reverting the separator to a hardcoded "\n"
// still passes every test here while writing exactly the mixed-terminator
// .gitignore this change exists to prevent.
// bare "\n" completion, and "node_modules/\r\n" ends in LF and needs no
// completion at all. Without this case, completing the final line with a
// hardcoded "\n" still passes every test here while writing exactly the
// mixed-terminator .gitignore this change exists to prevent.
//
// internal/gitignore's own table already covers this input for the detector
// (append_test.go, "CRLF with unterminated final line"); this is the call site
// that consumes it.
// internal/gitignore's own tables already cover this input (append_test.go,
// "CRLF with unterminated final line" for the detector and "CRLF unterminated"
// for AppendLines); this is the call site that consumes them.
func TestAddToGitignoreCompletesUnterminatedCRLFLine(t *testing.T) {
repoRoot := initGitRepoForGitignoreTest(t)
path := filepath.Join(repoRoot, ".gitignore")