-
-
Notifications
You must be signed in to change notification settings - Fork 278
fix: Add git worktree support for Docker environments #6456
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
base: main
Are you sure you want to change the base?
Conversation
- Add _is_git_worktree() method to detect worktree environments - Wrap git fetch in try-catch to handle GitCommandError gracefully - Add informative warning messages for worktree scenarios - Update exception handling to catch both InvalidGitRepositoryError and GitCommandError - Add comprehensive unit tests for worktree detection and error handling Fixes oxsecurity#6455
|
I'm surprised the git library doesn't have a way to check it for us. Is the bugbot review comment relevant? I don't really mind that this seems like a AI report, it is very clear, we just need us humans to think about if the way it is done is correct ;) |
I'll remove that, it must have snuck in while I was pushing this PR. I poked around the git python library a month or so back and didn't find a worktree feature, but can do a quick check. Separately, what do you think about try/except around git fetch? Are you ok with this change of functionality in the non worktree (default) case? |
|
The other work-around I've used is to call the docker command directly and mount the absolute path of the original repo at the same absolute path in the container. However, that's kinda bug prone if a container happens to rely on a path that matches the absolute path. Let me see if gitpython lets you override the worktree pointer with an env var, that way we can mount the main repo in a well known location and point to it. |
I looked in the code what they had, and something was close. But I saw in their issues a comment that shows a small change that makes it check for work trees. gitpython-developers/GitPython#719 (comment) Vs
I reread this and it's very fine, either way explanations in the logs. |
Fixes #6455
Proposed Changes
This PR adds support for git worktrees when running MegaLinter in Docker containers. Previously, MegaLinter would crash when running in a worktree because the git fetch operation would fail due to invalid paths inside the container.
_is_git_worktree()method to detect when running in a git worktree environmentGitCommandErrorgracefullyInvalidGitRepositoryErrorandGitCommandErrorTechnical Details
The Problem:
In a git worktree, the
.gitdirectory is replaced with a.gitfile containing a path like:When the worktree is mounted in Docker, this absolute host path becomes invalid inside the container, causing
git fetchto fail with:The Solution:
.gitis a file (not a directory) or if the git directory path contains 'worktrees'GitCommandErrorduring fetch operations and log appropriate warningsTesting
Added comprehensive unit tests in
megalinter/tests/test_megalinter/worktree_test.py:Impact
Readiness Checklist
Author/Contributor
Reviewing Maintainer
breakingif this is a large fundamental changeautomation,bug,documentation,enhancement,infrastructure, orperformanceNote
Adds git worktree detection and handles GitCommandError during fetch to keep diff-based file listing working, plus unit tests.
megalinter/MegaLinter.py):_is_git_worktreeto detect worktree environments.list_files_git_diff:git fetchin try/exceptgit.exc.GitCommandErrorand continue without fetch.collect_filesto catchgit.exc.GitCommandErrorwhen diffing.megalinter/tests/test_megalinter/worktree_test.pywith unit tests for worktree detection, fetch error handling, and exception safety.Written by Cursor Bugbot for commit 4d5935a. This will update automatically on new commits. Configure here.