Skip to content

megalinter fails to run in worktree repo. #6455

@philkuz

Description

@philkuz

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:

  1. Create a git worktree:

    git worktree add ../my-worktree my-branch
  2. Navigate to the worktree directory:

    cd ../my-worktree
  3. Run MegaLinter with Docker:

    mega-linter-runner --nodockerpull
  4. 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 .git file containing a path like gitdir: /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/HEAD on 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:

  1. Detecting when running in a worktree environment
  2. Adjusting the git directory path resolution for the containerized environment
  3. Or providing an option to skip the git fetch operation when the user has already ensured the repository is up-to-date

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions