diff --git a/cmd/bd/doctor/git_test.go b/cmd/bd/doctor/git_test.go index fc706921..ea1d0acd 100644 --- a/cmd/bd/doctor/git_test.go +++ b/cmd/bd/doctor/git_test.go @@ -23,8 +23,8 @@ func setupGitRepo(t *testing.T) string { t.Fatalf("failed to create .beads directory: %v", err) } - // Initialize git repo - cmd := exec.Command("git", "init") + // Initialize git repo with 'main' as default branch (modern git convention) + cmd := exec.Command("git", "init", "--initial-branch=main") cmd.Dir = dir if err := cmd.Run(); err != nil { t.Fatalf("failed to init git repo: %v", err) @@ -278,8 +278,8 @@ func setupGitRepoInDir(t *testing.T, dir string) { t.Fatalf("failed to create .beads directory: %v", err) } - // Initialize git repo - cmd := exec.Command("git", "init") + // Initialize git repo with 'main' as default branch (modern git convention) + cmd := exec.Command("git", "init", "--initial-branch=main") cmd.Dir = dir if err := cmd.Run(); err != nil { t.Fatalf("failed to init git repo: %v", err) diff --git a/cmd/bd/reinit_test.go b/cmd/bd/reinit_test.go index f226dd2b..f536414d 100644 --- a/cmd/bd/reinit_test.go +++ b/cmd/bd/reinit_test.go @@ -9,6 +9,7 @@ import ( "runtime" "testing" + "github.com/steveyegge/beads/internal/git" "github.com/steveyegge/beads/internal/storage/sqlite" "github.com/steveyegge/beads/internal/types" ) @@ -90,6 +91,7 @@ func testFreshCloneAutoImport(t *testing.T) { // Test checkGitForIssues detects issues.jsonl t.Chdir(dir) + git.ResetCaches() // Reset git caches after changing directory count, path, gitRef := checkGitForIssues() if count != 1 { @@ -169,6 +171,7 @@ func testDatabaseRemovalScenario(t *testing.T) { // Change to test directory t.Chdir(dir) + git.ResetCaches() // Reset git caches after changing directory // Test checkGitForIssues finds issues.jsonl (canonical name) count, path, gitRef := checkGitForIssues() @@ -247,6 +250,7 @@ func testLegacyFilenameSupport(t *testing.T) { // Change to test directory t.Chdir(dir) + git.ResetCaches() // Reset git caches after changing directory // Test checkGitForIssues finds issues.jsonl count, path, gitRef := checkGitForIssues() @@ -323,6 +327,7 @@ func testPrecedenceTest(t *testing.T) { // Change to test directory t.Chdir(dir) + git.ResetCaches() // Reset git caches after changing directory // Test checkGitForIssues prefers issues.jsonl count, path, _ := checkGitForIssues() @@ -369,6 +374,7 @@ func testInitSafetyCheck(t *testing.T) { // Change to test directory t.Chdir(dir) + git.ResetCaches() // Reset git caches after changing directory // Create empty database (simulating failed import) dbPath := filepath.Join(beadsDir, "test.db") @@ -409,8 +415,14 @@ func testInitSafetyCheck(t *testing.T) { // Helper functions // runCmd runs a command and fails the test if it returns an error +// If the command is "git init", it automatically adds --initial-branch=main +// for modern git compatibility. func runCmd(t *testing.T, dir string, name string, args ...string) { t.Helper() + // Add --initial-branch=main to git init for modern git compatibility + if name == "git" && len(args) > 0 && args[0] == "init" { + args = append(args, "--initial-branch=main") + } cmd := exec.Command(name, args...) cmd.Dir = dir if output, err := cmd.CombinedOutput(); err != nil { diff --git a/cmd/bd/test_wait_helper.go b/cmd/bd/test_wait_helper.go index 78d965c5..94efa88a 100644 --- a/cmd/bd/test_wait_helper.go +++ b/cmd/bd/test_wait_helper.go @@ -5,6 +5,8 @@ import ( "os/exec" "testing" "time" + + "github.com/steveyegge/beads/internal/git" ) // waitFor repeatedly evaluates pred until it returns true or timeout expires. @@ -37,8 +39,11 @@ func setupGitRepo(t *testing.T) (repoPath string, cleanup func()) { t.Fatalf("failed to change to temp directory: %v", err) } - // Initialize git repo - if err := exec.Command("git", "init").Run(); err != nil { + // Reset git caches after changing directory + git.ResetCaches() + + // Initialize git repo with 'main' as default branch (modern git convention) + if err := exec.Command("git", "init", "--initial-branch=main").Run(); err != nil { _ = os.Chdir(originalWd) t.Fatalf("failed to init git repo: %v", err) } @@ -60,6 +65,7 @@ func setupGitRepo(t *testing.T) (repoPath string, cleanup func()) { cleanup = func() { _ = os.Chdir(originalWd) + git.ResetCaches() } return tmpDir, cleanup @@ -80,6 +86,9 @@ func setupGitRepoWithBranch(t *testing.T, branch string) (repoPath string, clean t.Fatalf("failed to change to temp directory: %v", err) } + // Reset git caches after changing directory + git.ResetCaches() + // Initialize git repo with specific branch if err := exec.Command("git", "init", "-b", branch).Run(); err != nil { _ = os.Chdir(originalWd) @@ -103,6 +112,7 @@ func setupGitRepoWithBranch(t *testing.T, branch string) (repoPath string, clean cleanup = func() { _ = os.Chdir(originalWd) + git.ResetCaches() } return tmpDir, cleanup @@ -123,8 +133,11 @@ func setupMinimalGitRepo(t *testing.T) (repoPath string, cleanup func()) { t.Fatalf("failed to change to temp directory: %v", err) } - // Initialize git repo - if err := exec.Command("git", "init").Run(); err != nil { + // Reset git caches after changing directory + git.ResetCaches() + + // Initialize git repo with 'main' as default branch (modern git convention) + if err := exec.Command("git", "init", "--initial-branch=main").Run(); err != nil { _ = os.Chdir(originalWd) t.Fatalf("failed to init git repo: %v", err) } @@ -135,6 +148,7 @@ func setupMinimalGitRepo(t *testing.T) (repoPath string, cleanup func()) { cleanup = func() { _ = os.Chdir(originalWd) + git.ResetCaches() } return tmpDir, cleanup diff --git a/cmd/bd/worktree_daemon_test.go b/cmd/bd/worktree_daemon_test.go index 6e793499..5d38b3d7 100644 --- a/cmd/bd/worktree_daemon_test.go +++ b/cmd/bd/worktree_daemon_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/steveyegge/beads/internal/config" + "github.com/steveyegge/beads/internal/git" // Import SQLite driver for test database creation _ "github.com/ncruces/go-sqlite3/driver" @@ -70,8 +71,10 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) { // Change to the worktree directory origDir, _ := os.Getwd() - defer func() { + defer func() { _ = os.Chdir(origDir) + // Reset git caches after changing directory + git.ResetCaches() // Reinitialize config to restore original state _ = config.Initialize() }() @@ -79,6 +82,9 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) { t.Fatalf("Failed to change to worktree dir: %v", err) } + // Reset git caches after changing directory (required for IsWorktree to re-detect) + git.ResetCaches() + // No sync-branch configured os.Unsetenv("BEADS_SYNC_BRANCH") @@ -106,14 +112,18 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) { // Change to the worktree directory origDir, _ := os.Getwd() - defer func() { + defer func() { _ = os.Chdir(origDir) + git.ResetCaches() _ = config.Initialize() }() if err := os.Chdir(worktreeDir); err != nil { t.Fatalf("Failed to change to worktree dir: %v", err) } + // Reset git caches after changing directory + git.ResetCaches() + // Reinitialize config to pick up the new directory's config.yaml if err := config.Initialize(); err != nil { t.Fatalf("Failed to reinitialize config: %v", err) @@ -137,14 +147,18 @@ func TestShouldDisableDaemonForWorktree(t *testing.T) { // Change to the worktree directory origDir, _ := os.Getwd() - defer func() { + defer func() { _ = os.Chdir(origDir) + git.ResetCaches() _ = config.Initialize() }() if err := os.Chdir(worktreeDir); err != nil { t.Fatalf("Failed to change to worktree dir: %v", err) } + // Reset git caches after changing directory + git.ResetCaches() + // Reinitialize config to pick up the new directory's config.yaml if err := config.Initialize(); err != nil { t.Fatalf("Failed to reinitialize config: %v", err) @@ -187,14 +201,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) { // Change to the worktree directory origDir, _ := os.Getwd() - defer func() { + defer func() { _ = os.Chdir(origDir) + git.ResetCaches() _ = config.Initialize() }() if err := os.Chdir(worktreeDir); err != nil { t.Fatalf("Failed to change to worktree dir: %v", err) } + // Reset git caches after changing directory + git.ResetCaches() + // Clear all daemon-related env vars os.Unsetenv("BEADS_NO_DAEMON") os.Unsetenv("BEADS_AUTO_START_DAEMON") @@ -220,14 +238,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) { // Change to the worktree directory origDir, _ := os.Getwd() - defer func() { + defer func() { _ = os.Chdir(origDir) + git.ResetCaches() _ = config.Initialize() }() if err := os.Chdir(worktreeDir); err != nil { t.Fatalf("Failed to change to worktree dir: %v", err) } + // Reset git caches after changing directory + git.ResetCaches() + // Reinitialize config to pick up the new directory's config.yaml if err := config.Initialize(); err != nil { t.Fatalf("Failed to reinitialize config: %v", err) @@ -253,14 +275,18 @@ func TestShouldAutoStartDaemonWorktreeIntegration(t *testing.T) { // Change to the worktree directory origDir, _ := os.Getwd() - defer func() { + defer func() { _ = os.Chdir(origDir) + git.ResetCaches() _ = config.Initialize() }() if err := os.Chdir(worktreeDir); err != nil { t.Fatalf("Failed to change to worktree dir: %v", err) } + // Reset git caches after changing directory + git.ResetCaches() + // Reinitialize config to pick up the new directory's config.yaml if err := config.Initialize(); err != nil { t.Fatalf("Failed to reinitialize config: %v", err) @@ -302,8 +328,8 @@ func setupWorktreeTestRepo(t *testing.T) (mainDir, worktreeDir string) { // Create main repo directory mainDir = t.TempDir() - // Initialize git repo - cmd := exec.Command("git", "init") + // Initialize git repo with 'main' as default branch (modern git convention) + cmd := exec.Command("git", "init", "--initial-branch=main") cmd.Dir = mainDir if output, err := cmd.CombinedOutput(); err != nil { t.Fatalf("Failed to init git repo: %v\n%s", err, output) diff --git a/internal/syncbranch/worktree_sync_test.go b/internal/syncbranch/worktree_sync_test.go index 038738b9..148d609d 100644 --- a/internal/syncbranch/worktree_sync_test.go +++ b/internal/syncbranch/worktree_sync_test.go @@ -31,7 +31,7 @@ func TestCommitToSyncBranch(t *testing.T) { writeFile(t, jsonlPath, `{"id":"test-1"}`) runGit(t, repoDir, "add", ".") runGit(t, repoDir, "commit", "-m", "initial sync branch commit") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // Write new content to commit writeFile(t, jsonlPath, `{"id":"test-1"}`+"\n"+`{"id":"test-2"}`) @@ -64,7 +64,7 @@ func TestCommitToSyncBranch(t *testing.T) { writeFile(t, jsonlPath, `{"id":"test-1"}`) runGit(t, repoDir, "add", ".") runGit(t, repoDir, "commit", "-m", "initial") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // Write the same content that's in the sync branch writeFile(t, jsonlPath, `{"id":"test-1"}`) @@ -101,7 +101,7 @@ func TestPullFromSyncBranch(t *testing.T) { writeFile(t, jsonlPath, `{"id":"test-1"}`) runGit(t, repoDir, "add", ".") runGit(t, repoDir, "commit", "-m", "local sync") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // Pull should handle the case where remote doesn't have the branch result, err := PullFromSyncBranch(ctx, repoDir, syncBranch, jsonlPath, false) @@ -131,7 +131,7 @@ func TestPullFromSyncBranch(t *testing.T) { runGit(t, repoDir, "commit", "-m", "sync commit") // Set up a fake remote ref at the same commit runGit(t, repoDir, "update-ref", "refs/remotes/origin/"+syncBranch, "HEAD") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // Pull when already at remote HEAD result, err := PullFromSyncBranch(ctx, repoDir, syncBranch, jsonlPath, false) @@ -158,7 +158,7 @@ func TestPullFromSyncBranch(t *testing.T) { runGit(t, repoDir, "add", ".") runGit(t, repoDir, "commit", "-m", "sync commit") runGit(t, repoDir, "update-ref", "refs/remotes/origin/"+syncBranch, "HEAD") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // Remove local JSONL to verify it gets copied back os.Remove(jsonlPath) @@ -198,7 +198,7 @@ func TestPullFromSyncBranch(t *testing.T) { // Reset back to base (so remote is ahead) runGit(t, repoDir, "reset", "--hard", baseCommit) - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // Pull should fast-forward result, err := PullFromSyncBranch(ctx, repoDir, syncBranch, jsonlPath, false) @@ -233,7 +233,7 @@ func TestResetToRemote(t *testing.T) { writeFile(t, jsonlPath, `{"id":"local-1"}`) runGit(t, repoDir, "add", ".") runGit(t, repoDir, "commit", "-m", "local commit") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // ResetToRemote should fail since remote branch doesn't exist err := ResetToRemote(ctx, repoDir, syncBranch, jsonlPath) @@ -264,7 +264,7 @@ func TestPushSyncBranch(t *testing.T) { writeFile(t, filepath.Join(repoDir, ".beads", "issues.jsonl"), `{"id":"test-1"}`) runGit(t, repoDir, "add", ".") runGit(t, repoDir, "commit", "-m", "initial") - runGit(t, repoDir, "checkout", "master") + runGit(t, repoDir, "checkout", "main") // PushSyncBranch should handle the worktree creation err := PushSyncBranch(ctx, repoDir, syncBranch) @@ -391,8 +391,8 @@ func setupTestRepoWithRemote(t *testing.T) string { t.Fatalf("Failed to create temp dir: %v", err) } - // Initialize git repo - runGit(t, tmpDir, "init") + // Initialize git repo with 'main' as default branch (modern git convention) + runGit(t, tmpDir, "init", "--initial-branch=main") runGit(t, tmpDir, "config", "user.email", "test@test.com") runGit(t, tmpDir, "config", "user.name", "Test User")