-
-
Notifications
You must be signed in to change notification settings - Fork 279
Description
Describe the bug
MegaLinter fails when running in a Git worktree environment with the error:
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
cmdline: git fetch origin HEAD:refs/remotes/origin/HEAD
stderr: 'fatal: not a git repository: /home/[user]/[repo]/.git/worktrees/[worktree-name]'
The issue occurs during the file collection phase when MegaLinter attempts to run git fetch origin HEAD:refs/remotes/origin/HEAD inside a Docker container. The git worktree path stored in the .git file becomes invalid inside the container because it references an absolute path on the host system.
To Reproduce
Steps to reproduce the behavior:
-
Create a git worktree:
git worktree add ../my-worktree my-branch
-
Navigate to the worktree directory:
cd ../my-worktree -
Run MegaLinter with Docker:
mega-linter-runner --nodockerpull
-
See error during the "MegaLinter now collects the files to analyse" phase
Expected behavior
MegaLinter should successfully run in Git worktree environments, properly handling the worktree's .git file (which is a pointer to the main repository) when executing git commands inside the Docker container.
Screenshots
Error output:
MegaLinter now collects the files to analyse
Listing updated files in [/tmp/lint] using git diff.
Traceback (most recent call last):
File "/megalinter/MegaLinter.py", line 811, in list_files_git_diff
repo.git.fetch("origin", f"{remote_ref}:{local_ref}")
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
cmdline: git fetch origin HEAD:refs/remotes/origin/HEAD
stderr: 'fatal: not a git repository: /home/[user]/[repo]/.git/worktrees/[worktree]'
Additional context
- This is a persistent issue when using Git worktrees with MegaLinter
- Worktrees are being adopted by coding agent workflows (ie Cursor and Claude Code).
- The problem occurs because worktrees use a
.gitfile containing a path likegitdir: /path/to/main/.git/worktrees/name, but this absolute path is invalid inside the Docker container - Manually running
git fetch origin HEAD:refs/remotes/origin/HEADon the host before invoking MegaLinter doesn't resolve the issue because the git command is executed inside the container - The workaround of running the fetch command beforehand only works if MegaLinter skips that particular git operation, but doesn't address the root cause
- MegaLinter version: v8.8.0
The root cause appears to be in /megalinter/MegaLinter.py line 811 where repo.git.fetch() is called, and the GitPython library cannot resolve the worktree path correctly in the containerized environment.
A potential solution might involve:
- Detecting when running in a worktree environment
- Adjusting the git directory path resolution for the containerized environment
- Or providing an option to skip the git fetch operation when the user has already ensured the repository is up-to-date