fix(windows): avoid subprocess git calls that hang in pip .exe wrappers#1
Open
hamb3r wants to merge 3 commits intoMikeRecognex:mainfrom
Open
fix(windows): avoid subprocess git calls that hang in pip .exe wrappers#1hamb3r wants to merge 3 commits intoMikeRecognex:mainfrom
hamb3r wants to merge 3 commits intoMikeRecognex:mainfrom
Conversation
046c498 to
80e0fc7
Compare
On Windows, pip-installed .exe console_scripts inherit a reduced PATH that does not include git. This causes subprocess.run(["git", ...]) calls to hang indefinitely. Changes: - Add _resolve_git_dir() that walks up parents and supports both .git directory (regular repos) and .git file (worktrees/submodules with gitdir: pointer) - Rewrite is_git_repo() to use _resolve_git_dir() instead of calling git rev-parse --is-inside-work-tree via subprocess - Rewrite get_head_commit() to use _resolve_git_dir() and read .git/HEAD directly; supports symbolic refs, detached HEAD, packed-refs, and both SHA-1 (40 hex) and SHA-256 (64 hex) object IDs - Add _find_git() helper that resolves the git binary path once at import time via shutil.which() with Windows fallback paths - Use resolved _GIT_CMD for all remaining subprocess-based git calls (diff, ls-files) instead of bare "git" - Add skip_committed parameter to get_changed_files() so callers can skip the expensive since_ref..HEAD diff when HEAD hasn't moved while still checking the working tree (unstaged, staged, untracked) - Update _maybe_incremental_update() to use skip_committed instead of early return, so working tree changes are always detected - Rewrite tests for filesystem-based behavior with 43 test cases covering worktrees, submodules, subdirectory roots, SHA-256, and skip_committed
80e0fc7 to
418b095
Compare
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows, pip-installed console_scripts (
.exewrappers) inherit a reducedPATHthat often does not includegit. Every call tosubprocess.run(["git", ...])either raisesFileNotFoundErroror hangs until the 10-second timeout.Since
_ensure_index()and_maybe_incremental_update()make multiple git subprocess calls (rev-parse,diff,ls-files), a single tool invocation takes 20+ seconds before returning a result.Reproduction
pip install "mcp-codebase-index[mcp]"on Windows 10/11Root cause
is_git_repo()callsgit rev-parse --is-inside-work-treeandget_head_commit()callsgit rev-parse HEADviasubprocess.run. Inside the.exewrapper process,gitis not onPATH, so these calls hang until timeout.Solution
Two source files changed, zero new dependencies.
git_tracker.py_resolve_git_dir().gitdir (regular repos) and.gitfile withgitdir:pointer (worktrees/submodules)is_git_repo()subprocess.run(["git", "rev-parse", ...])_resolve_git_dir()— pure filesystem, no subprocessget_head_commit()subprocess.run(["git", "rev-parse", "HEAD"])_resolve_git_dir(), then reads.git/HEADdirectly. Supports symbolic refs, detached HEAD, packed-refs, SHA-1 (40 hex) and SHA-256 (64 hex)_find_git()gitbinary path once at import time viashutil.which()+ Windows fallback pathsget_changed_files()["git", "diff", ...][_GIT_CMD, "diff", ...]using resolved path; newskip_committedkwarg to skipsince_ref..HEADdiff while still checking working treeserver.py_maybe_incremental_update()skip_committed=Truewhen HEAD hasn't moved instead of early return — working tree changes (unstaged, staged, untracked) are always detectedtest_git_tracker.pyTests fully rewritten for the new filesystem-based implementations:
TestResolveGitDir.gitdir, parent walk,.gitfile with absolute/relativegitdir:, non-git, invalid.gitfile, missing target dirTestIsGitRepo.gitfile, non-git, empty pathTestGetHeadCommit.gitfile, subdirectory root, no.gitdir, missing ref, invalid hashTestCommitHashReTestFindGitshutil.whichsuccess, fallback, bare"git"TestGetChangedFilesskip_committedskips committed diff,skip_committedstill checks working tree,FileNotFoundErrorTestGitChangeSetCross-platform safety
shutil.which("git")resolves immediately; Windows-specific fallback paths are skipped (files don't exist)._resolve_git_dir()filesystem walk is the same logic on all platforms. No behavior change.is_git_repo()andget_head_commit()eliminate the subprocess dependency on hot path._find_git()resolvesgitfor diff operations._resolve_git_dir()follows.gitfiles withgitdir:pointers (both absolute and relative paths).Result
Tested on Windows 10 Pro, Python 3.14, pip editable install, Claude Code MCP integration.