|
| 1 | +package gitutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetRepoRoot(t *testing.T) { |
| 13 | + // Create a temp directory for the test |
| 14 | + tmpDir := t.TempDir() |
| 15 | + tmpDir, err := filepath.EvalSymlinks(tmpDir) |
| 16 | + require.NoError(t, err) |
| 17 | + |
| 18 | + t.Chdir(tmpDir) |
| 19 | + _, err = GetRepoRoot() |
| 20 | + require.Error(t, err) |
| 21 | + require.ErrorIs(t, err, ErrNotGitRepo) |
| 22 | + |
| 23 | + // In a regular git repo |
| 24 | + mainRepoDir := filepath.Join(tmpDir, "main-repo") |
| 25 | + require.NoError(t, os.MkdirAll(mainRepoDir, 0o755)) |
| 26 | + testGitInit(t, mainRepoDir) |
| 27 | + t.Chdir(mainRepoDir) |
| 28 | + |
| 29 | + root, err := GetRepoRoot() |
| 30 | + require.NoError(t, err) |
| 31 | + require.Equal(t, mainRepoDir, root) |
| 32 | + |
| 33 | + // In a subdirectory of a git repo |
| 34 | + subDir := filepath.Join(mainRepoDir, "subdir") |
| 35 | + require.NoError(t, os.MkdirAll(subDir, 0o755)) |
| 36 | + t.Chdir(subDir) |
| 37 | + root, err = GetRepoRoot() |
| 38 | + require.NoError(t, err) |
| 39 | + require.Equal(t, mainRepoDir, root) |
| 40 | + |
| 41 | + // In a worktree |
| 42 | + // We need a commit to create a worktree |
| 43 | + runGit(t, mainRepoDir, "config", "user.email", "test@example.com") |
| 44 | + runGit(t, mainRepoDir, "config", "user.name", "Test User") |
| 45 | + require.NoError(t, os.WriteFile(filepath.Join(mainRepoDir, "file.txt"), []byte("hello"), 0o644)) |
| 46 | + runGit(t, mainRepoDir, "add", "file.txt") |
| 47 | + runGit(t, mainRepoDir, "commit", "-m", "initial commit") |
| 48 | + |
| 49 | + worktreeDir := filepath.Join(tmpDir, "worktree") |
| 50 | + runGit(t, mainRepoDir, "worktree", "add", worktreeDir) |
| 51 | + |
| 52 | + t.Chdir(worktreeDir) |
| 53 | + root, err = GetRepoRoot() |
| 54 | + require.NoError(t, err) |
| 55 | + require.Equal(t, worktreeDir, root) |
| 56 | +} |
| 57 | + |
| 58 | +func TestGetRepoRoot_HookContext(t *testing.T) { |
| 59 | + // Create a temp directory for the test |
| 60 | + tmpDir := t.TempDir() |
| 61 | + tmpDir, err := filepath.EvalSymlinks(tmpDir) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + // In a regular git repo |
| 65 | + mainRepoDir := filepath.Join(tmpDir, "main-repo") |
| 66 | + require.NoError(t, os.MkdirAll(mainRepoDir, 0o755)) |
| 67 | + testGitInit(t, mainRepoDir) |
| 68 | + |
| 69 | + // In a subdirectory of a git repo |
| 70 | + subDir := filepath.Join(mainRepoDir, "subdir") |
| 71 | + require.NoError(t, os.MkdirAll(subDir, 0o755)) |
| 72 | + |
| 73 | + // Simulate a hook context where GIT_DIR and GIT_WORK_TREE are set. |
| 74 | + // This usually happens when git runs a hook. |
| 75 | + // If we're in 'subdir' and GIT_DIR is '../.git', we want to make sure |
| 76 | + // GetRepoRoot still returns the correct root. |
| 77 | + |
| 78 | + // First, let's see what happens without environment variables |
| 79 | + t.Chdir(subDir) |
| 80 | + root, err := GetRepoRoot() |
| 81 | + require.NoError(t, err) |
| 82 | + require.Equal(t, mainRepoDir, root) |
| 83 | + |
| 84 | + // Now set environment variables as if we're in a hook |
| 85 | + // We'll set GIT_DIR to point to the .git directory relative to the current dir |
| 86 | + t.Setenv("GIT_DIR", filepath.Join("..", ".git")) |
| 87 | + t.Setenv("GIT_WORK_TREE", "..") |
| 88 | + |
| 89 | + // If we are in 'subdir', but we set GIT_WORK_TREE to '.', we want to see if |
| 90 | + // rev-parse returns the current directory instead of the main repo root. |
| 91 | + t.Setenv("GIT_WORK_TREE", ".") |
| 92 | + |
| 93 | + root, err = GetRepoRoot() |
| 94 | + require.NoError(t, err) |
| 95 | + // If the fix is NOT applied, GetRepoRoot() might return 'subDir' |
| 96 | + // because git rev-parse --show-toplevel respects GIT_WORK_TREE. |
| 97 | + require.Equal(t, mainRepoDir, root) |
| 98 | +} |
| 99 | + |
| 100 | +func testGitInit(t *testing.T, dir string) { |
| 101 | + t.Helper() |
| 102 | + cmd := exec.Command("git", "init", "--template=") |
| 103 | + cmd.Dir = dir |
| 104 | + cmd.Env = append(os.Environ(), |
| 105 | + "GIT_CONFIG_GLOBAL="+os.DevNull, |
| 106 | + "GIT_CONFIG_SYSTEM="+os.DevNull, |
| 107 | + ) |
| 108 | + if err := cmd.Run(); err != nil { |
| 109 | + t.Fatalf("git init failed: %v", err) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func runGit(t *testing.T, dir string, args ...string) { |
| 114 | + t.Helper() |
| 115 | + cmd := exec.Command("git", args...) |
| 116 | + cmd.Dir = dir |
| 117 | + cmd.Env = append(os.Environ(), |
| 118 | + "GIT_CONFIG_GLOBAL="+os.DevNull, |
| 119 | + "GIT_CONFIG_SYSTEM="+os.DevNull, |
| 120 | + ) |
| 121 | + out, err := cmd.CombinedOutput() |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("git %v failed: %v\nOutput: %s", args, err, string(out)) |
| 124 | + } |
| 125 | +} |
0 commit comments