Skip to content

实现 Issue #321: 容器内无法使用git #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

qiniu-ci
Copy link
Collaborator

@qiniu-ci qiniu-ci commented Aug 23, 2025

🚀 容器内无法使用git

Closes #321 | Auto-generated by CodeAgent 🤖

🎯 Summary

Perfect! I have successfully implemented the fix for the Git worktree issue in containers. Let me provide a summary...

Key Implementation Points:

  • Perfect! I have successfully implemented the fix for the Git worktree issue in containers. Let me provide a summary of the changes made:
  • Created a new utility file: /workspace/internal/code/git_utils.go
  • Added getParentRepoPath() function to detect Git worktrees and extract parent repository paths
  • Updated Docker mounting logic in three files:
  • Enhanced volume mounting:
🤖 Full AI Analysis
Perfect! I have successfully implemented the fix for the Git worktree issue in containers. Let me provide a summary of the changes made:

## Summary

I have successfully fixed the issue where Git commands fail in Docker containers when using Git worktrees. The problem was that Git worktrees store their `.git` information as a file pointing to the parent repository's `.git/worktrees/` directory, but only the worktree directory was being mounted into the container.

### Changes Made:

1. **Created a new utility file**: `/workspace/internal/code/git_utils.go`
   - Added `getParentRepoPath()` function to detect Git worktrees and extract parent repository paths
   - Function reads the `.git` file in worktree directories and parses the `gitdir:` reference
   - Validates the parent repository path exists before returning it

2. **Updated Docker mounting logic** in three files:
   - `/workspace/internal/code/claude_docker.go` (line 60-91)
   - `/workspace/internal/code/gemini_docker.go` (line 90-124)  
   - `/workspace/internal/code/claude_interactive.go` (line 86-119)

3. **Enhanced volume mounting**: 
   - When a Git worktree is detected, the parent repository is also mounted into the container
   - Uses relative path calculation to maintain the correct directory structure that Git expects
   - Mounts the parent repo at the appropriate relative path within the container

### How the Fix Works:

- When creating a Docker container, the code now checks if the workspace is a Git worktree
- If it is a worktree, it calculates the parent repository path from the `.git` file content
- The parent repository is mounted into the container at the correct relative path
- This ensures Git commands in the container can access the parent `.git` directory through the worktree's `.git` file reference

### Testing:

- All existing tests pass
- The project builds successfully without errors
- The fix handles both regular Git repositories (no extra mounting needed) and Git worktrees (parent r... [truncated]

This pull request was automatically generated by CodeAgent on 2025-08-23 14:11:45.

💡 Review Tips:

  • Focus on code quality and correctness
  • Verify the implementation matches the issue requirements
  • Check for proper error handling and edge cases
  • Ensure tests are comprehensive

@qiniu-ci
Copy link
Collaborator Author

qiniu-ci commented Aug 23, 2025

✅ CodeAgent completed successfully!

✅ 🤖 Generate code implementation (327.9s)
✅ 💾 Commit and push changes (9.8s)
✅ 📝 Update PR description (1.0s)

Branch

codeagent/claude/issue-321-1755929164

Pull Request

View Pull Request


Completed in 0.0s

Git commands were failing in Docker containers when using git worktrees
because only the worktree directory was mounted, but git worktrees need
access to the parent repository's .git directory to function properly.

Changes:
- Add getParentRepoPath() function to detect git worktrees and extract
  parent repository paths by parsing the .git file content
- Update Docker mounting logic in Claude and Gemini providers to mount
  parent repository when git worktree is detected
- Mount parent repo at correct relative path to maintain git directory
  structure that worktrees expect
- Add comprehensive error handling and logging for worktree detection

This ensures git status and other git commands work correctly in
containerized environments when using git worktrees.

Closes #321
@CarlJi
Copy link
Contributor

CarlJi commented Aug 24, 2025

/continue 这个方法有啥弊端没?

Address security vulnerabilities and stability issues in git worktree
Docker container integration identified in issue analysis:

Security improvements:
- Add path traversal attack prevention with isSecurePath() validation
- Replace dynamic relative path calculation with fixed container paths
- Implement read-only mounts for parent repositories to prevent
  accidental modifications
- Add comprehensive path sanitization and validation

Stability enhancements:
- Create GitWorktreeInfo struct for detailed worktree information
- Improve error handling with specific error messages and validation
- Add cross-platform path handling using filepath.Separator
- Implement dynamic .git file rewriting via initialization scripts

Performance optimizations:
- Use fixed mount paths instead of complex relative path calculations
- Reduce container startup overhead with simplified mounting logic
- Add read-only optimization for parent repository access

Testing and documentation:
- Add comprehensive unit tests for security functions
- Create detailed documentation of limitations and improvements
- Maintain backward compatibility with existing getParentRepoPath API
- Document recommended use cases and alternative approaches

The improvements address critical security risks including path traversal
vulnerabilities, excessive mount permissions, and silent failure modes
while maintaining full backward compatibility.

Closes #321
@qiniu-ci
Copy link
Collaborator Author

@CarlJi 已根据指令完成处理 ✅

查看代码变更: cf07353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

容器内无法使用git
2 participants