CodeWiki icon indicating copy to clipboard operation
CodeWiki copied to clipboard

Nested Git checkouts (agent worktrees, clones) are analyzed as part of the repository

Open fabceolin opened this issue 3 weeks ago • 0 comments

Summary

CodeWiki analyzes the files of nested Git checkouts inside the repository: git worktree folders, and clones or other repositories in subfolders. Git does not go into these folders, but they are not "ignored", so the .gitignore filter does not remove them. The result is duplicate components, higher cost, and a wrong graph.

How to reproduce

mkdir demo && cd demo && git init -q
printf 'def f():\n    return 1\n' > app.py
git add app.py && git commit -qm init
git worktree add -q .claude/worktrees/feature   # e.g. an agent worktree
git clone -q . vendor/other                     # any nested repository

RepoAnalyzer().analyze_repository_structure("demo") on main (a1b9f4a) gives three Python files:

['.claude/worktrees/feature/app.py', 'app.py', 'vendor/other/app.py']

git ls-files gives one: app.py.

Cause

GitIgnoreFilter (analysis/repo_analyzer.py) asks Git only for the ignored paths (git ls-files --others --ignored --exclude-standard --directory). A nested checkout is not ignored. Git shows it only as an untracked folder and does not go into it. So the filter keeps the folder, and the walk reads every file below it.

Effect on a real repository

AI coding agents put worktrees inside the repository by default (for example .claude/worktrees/ for Claude Code). On one repository with a single agent worktree, the walk found 4,439 source files, and 2,103 without the worktree. The language counts doubled. A copy also makes each name defined twice, so the analyzers that drop an ambiguous name (Go, Dart, and the Lean analyzer from #154) lose edges.

The work-around is --exclude ".claude/worktrees", but the user must know that the folder is there.

Proposed fix

Do not go into a subfolder that contains a .git entry (a folder, or a file as in a worktree or submodule), as Git does. Two points need your decision:

  1. Submodules. Git also stops at a submodule. By the same cause, the walk probably reads submodule files today (I did not test this). Some users can need that code, so the fix can keep registered submodules (from .gitmodules) and skip only the other nested checkouts.
  2. Plain directories. When the root is not a Git repository, the pathspec fallback can apply the same .git rule.

I can send a PR, with a test that uses git worktree add and a nested clone.

🤖 Generated with Claude Code

fabceolin avatar Nov 06 '26 12:11 fabceolin