Skip to content

Commit e1b9d9a

Browse files
committed
Improve git root detection for CI environments
- Fall back to .git directory search if git command not available - Better error handling for git root detection - CI uses GITHUB_WORKSPACE, local dev uses git rev-parse or .git search
1 parent 2514576 commit e1b9d9a

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

mysql/testcontainers_helper.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,34 @@ func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
503503
moduleRoot := os.Getenv("GITHUB_WORKSPACE")
504504
if moduleRoot == "" {
505505
// For local development, find git root using git rev-parse
506-
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
507-
output, err := cmd.Output()
506+
// First try to find git in PATH
507+
gitPath, err := exec.LookPath("git")
508508
if err != nil {
509-
return nil, fmt.Errorf("failed to find git root: %v", err)
509+
// Git not found, try to find repo root by looking for .git directory
510+
cwd, err := os.Getwd()
511+
if err != nil {
512+
return nil, fmt.Errorf("failed to get current working directory: %v", err)
513+
}
514+
dir := cwd
515+
for {
516+
if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
517+
moduleRoot = dir
518+
break
519+
}
520+
parent := filepath.Dir(dir)
521+
if parent == dir {
522+
return nil, fmt.Errorf("could not find git root (.git directory) in parent directories of %s", cwd)
523+
}
524+
dir = parent
525+
}
526+
} else {
527+
cmd := exec.Command(gitPath, "rev-parse", "--show-toplevel")
528+
output, err := cmd.Output()
529+
if err != nil {
530+
return nil, fmt.Errorf("failed to find git root: %v", err)
531+
}
532+
moduleRoot = strings.TrimSpace(string(output))
510533
}
511-
moduleRoot = strings.TrimSpace(string(output))
512534
}
513535

514536
// Verify Dockerfile exists
@@ -528,8 +550,9 @@ func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
528550
Tag: imageTag, // Use consistent tag for caching
529551
},
530552
ExposedPorts: []string{"4000/tcp"},
531-
// TiUP Playground needs to run processes, so we need privileged mode
553+
// TiUP Playground needs to run processes and requires elevated capabilities
532554
HostConfigModifier: func(hostConfig *container.HostConfig) {
555+
// Privileged mode allows TiUP to run multiple processes (PD, TiKV, TiDB)
533556
hostConfig.Privileged = true
534557
// Set ulimit for file descriptors (TiKV inside playground needs this)
535558
hostConfig.Ulimits = []*container.Ulimit{

0 commit comments

Comments
 (0)