Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ USER codeagent
# 设置工作目录
WORKDIR /workspace

# 配置 Git safe directory 以解决跨用户挂载的权限问题
RUN git config --global safe.directory /workspace

# 默认命令
CMD ["tail", "-f", "/dev/null"]
7 changes: 7 additions & 0 deletions internal/code/claude_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func NewClaudeDocker(workspace *models.Workspace, cfg *config.Config) (Code, err

log.Infof("docker container started successfully")

// Configure Git safe directory inside the container to fix ownership issues
if err := configureGitSafeDirectoryInContainer(containerName); err != nil {
log.Warnf("Failed to configure Git safe directory in container: %v", err)
// Don't fail the container creation, just warn
}

return &claudeCode{
containerName: containerName,
}, nil
Expand Down Expand Up @@ -241,3 +247,4 @@ func copyHostClaudeConfig(isolatedConfigDir string) error {
log.Infof("Successfully copied host Claude config to isolated directory")
return nil
}

7 changes: 7 additions & 0 deletions internal/code/gemini_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ func NewGeminiDocker(workspace *models.Workspace, cfg *config.Config) (Code, err

log.Infof("docker container started successfully")

// Configure Git safe directory inside the container to fix ownership issues
if err := configureGitSafeDirectoryInContainer(containerName); err != nil {
log.Warnf("Failed to configure Git safe directory in container: %v", err)
// Don't fail the container creation, just warn
}

return &geminiDocker{
containerName: containerName,
}, nil
Expand Down Expand Up @@ -178,3 +184,4 @@ func (g *geminiDocker) Close() error {
stopCmd := exec.Command("docker", "rm", "-f", g.containerName)
return stopCmd.Run()
}

19 changes: 19 additions & 0 deletions internal/code/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package code

import (
"bytes"
"context"
"fmt"
"os/exec"
Expand Down Expand Up @@ -212,3 +213,21 @@ func generateConfigDirName(provider, org, repoName string, workspace *models.Wor
return fmt.Sprintf(".%s-%s-%s-workspace-%d", provider, org, repoName, timestamp)
}
}

// configureGitSafeDirectoryInContainer configures Git safe directory inside the Docker container
// to prevent "fatal: unsafe repository" errors when Git operations are performed
func configureGitSafeDirectoryInContainer(containerName string) error {
// Configure Git to treat /workspace as a safe directory
cmd := exec.Command("docker", "exec", containerName, "git", "config", "--global", "safe.directory", "/workspace")

var stderr bytes.Buffer
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
log.Errorf("Failed to configure Git safe directory: %v, stderr: %s", err, stderr.String())
return fmt.Errorf("failed to configure Git safe directory: %w", err)
}

log.Infof("Successfully configured Git safe directory in container %s", containerName)
return nil
}
Loading