diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index f3a7837e7..c47697a15 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -1164,6 +1164,7 @@ go_test( "workspace_gate_test.go", "workspace_identity_test.go", "worktree_cmd_test.go", + "worktree_gitignore_eol_test.go", "worktree_permissions_test.go", "worktree_remove_cmd_test.go", "worktree_remove_git_test.go", diff --git a/cmd/bd/worktree_cmd.go b/cmd/bd/worktree_cmd.go index 37a3b6b12..51b6ad5aa 100644 --- a/cmd/bd/worktree_cmd.go +++ b/cmd/bd/worktree_cmd.go @@ -23,6 +23,7 @@ import ( "github.com/steveyegge/beads/internal/execenv" "github.com/steveyegge/beads/internal/git" "github.com/steveyegge/beads/internal/gitenv" + "github.com/steveyegge/beads/internal/gitignore" "github.com/steveyegge/beads/internal/metrics" "github.com/steveyegge/beads/internal/ui" "github.com/steveyegge/beads/internal/worktreeremove" @@ -2140,15 +2141,22 @@ 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' { - if _, err := f.WriteString("\n"); err != nil { + 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 := f.WriteString(fmt.Sprintf("# bd worktree\n%s/\n", entry)); err != nil { + if _, err := fmt.Fprintf(f, "# bd worktree%s%s/%s", lineEnding, entry, lineEnding); err != nil { return err } diff --git a/cmd/bd/worktree_cmd_test.go b/cmd/bd/worktree_cmd_test.go index 91ec16bf1..ae2e6d4e1 100644 --- a/cmd/bd/worktree_cmd_test.go +++ b/cmd/bd/worktree_cmd_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "fmt" "os" @@ -271,13 +272,9 @@ func TestAddToGitignore(t *testing.T) { if err != nil { t.Fatalf("failed to read .gitignore: %v", err) } - content := string(updated) - - if count := strings.Count(content, "# bd worktree"); count != 1 { - t.Fatalf("expected one worktree marker, got %d:\n%s", count, content) - } - if count := strings.Count(content, entry+"/"); count != 1 { - t.Fatalf("expected one worktree entry, got %d:\n%s", count, content) + want := []byte("node_modules/\n# bd worktree\nworktree-feature/\n") + if !bytes.Equal(updated, want) { + t.Fatalf(".gitignore bytes after two appends:\nwant: %q\ngot: %q", want, updated) } }) } diff --git a/cmd/bd/worktree_gitignore_eol_test.go b/cmd/bd/worktree_gitignore_eol_test.go new file mode 100644 index 000000000..f330f8965 --- /dev/null +++ b/cmd/bd/worktree_gitignore_eol_test.go @@ -0,0 +1,104 @@ +package main + +import ( + "bytes" + "context" + "os" + "path/filepath" + "testing" +) + +func TestAddToGitignoreCompletesTrailingCR(t *testing.T) { + repoRoot := initGitRepoForGitignoreTest(t) + path := filepath.Join(repoRoot, ".gitignore") + initial := []byte("a/\r\nb/\r") + if err := os.WriteFile(path, initial, 0644); err != nil { + t.Fatal(err) + } + want := []byte("a/\r\nb/\r\n# bd worktree\r\nworktree-feature/\r\n") + for i := 0; i < 2; i++ { + if err := addToGitignore(context.Background(), repoRoot, "worktree-feature"); err != nil { + t.Fatal(err) + } + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Fatalf("append %d bytes = %q, want %q", i+1, got, want) + } + } +} + +// TestAddToGitignoreCompletesUnterminatedCRLFLine covers the one arm where +// addToGitignore actually uses the detected line ending as its separator. +// +// 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 +// 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. +// +// 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. +func TestAddToGitignoreCompletesUnterminatedCRLFLine(t *testing.T) { + repoRoot := initGitRepoForGitignoreTest(t) + path := filepath.Join(repoRoot, ".gitignore") + initial := []byte("node_modules/\r\nbuild/") + if err := os.WriteFile(path, initial, 0644); err != nil { + t.Fatal(err) + } + want := []byte("node_modules/\r\nbuild/\r\n# bd worktree\r\nworktree-feature/\r\n") + // Twice: the append must be idempotent as well as CRLF-correct. + for i := 0; i < 2; i++ { + if err := addToGitignore(context.Background(), repoRoot, "worktree-feature"); err != nil { + t.Fatal(err) + } + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Fatalf("append %d bytes = %q, want %q", i+1, got, want) + } + } +} + +func TestAddToGitignorePreservesCRLFWhenAppending(t *testing.T) { + repoRoot := initGitRepoForGitignoreTest(t) + gitignorePath := filepath.Join(repoRoot, ".gitignore") + initial := []byte("node_modules/\r\n") + if err := os.WriteFile(gitignorePath, initial, 0644); err != nil { + t.Fatalf("failed to write .gitignore: %v", err) + } + + entry := "worktree-feature" + if err := addToGitignore(context.Background(), repoRoot, entry); err != nil { + t.Fatalf("first addToGitignore failed: %v", err) + } + + want := []byte("node_modules/\r\n# bd worktree\r\nworktree-feature/\r\n") + updated, err := os.ReadFile(gitignorePath) + if err != nil { + t.Fatalf("failed to read .gitignore: %v", err) + } + if !bytes.Equal(updated, want) { + t.Fatalf(".gitignore bytes after append:\nwant: %q\ngot: %q", want, updated) + } + + if err := addToGitignore(context.Background(), repoRoot, entry); err != nil { + t.Fatalf("second addToGitignore failed: %v", err) + } + unchanged, err := os.ReadFile(gitignorePath) + if err != nil { + t.Fatalf("failed to reread .gitignore: %v", err) + } + if !bytes.Equal(unchanged, want) { + t.Fatalf("second append changed .gitignore:\nwant: %q\ngot: %q", want, unchanged) + } +} diff --git a/internal/gitignore/append_test.go b/internal/gitignore/append_test.go index 5c51fc85b..250c39ee7 100644 --- a/internal/gitignore/append_test.go +++ b/internal/gitignore/append_test.go @@ -8,6 +8,7 @@ func TestAppendLineEnding(t *testing.T) { {"delimiter-free", "local", "\n"}, {"LF", "a\n", "\n"}, {"CRLF", "a\r\n", "\r\n"}, + {"CRLF with unterminated final line", "node_modules/\r\nbuild/", "\r\n"}, {"mixed", "a\r\nb\r\nc\n", "\n"}, {"BOM LF", "\xef\xbb\xbfa\n", "\n"}, {"BOM CRLF", "\xef\xbb\xbfa\r\n", "\r\n"},