Skip to content

fix: resolve shared refs from commonPath in linked worktrees#304787

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/git-revparse-worktree-commonpath
Open

fix: resolve shared refs from commonPath in linked worktrees#304787
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/git-revparse-worktree-commonpath

Conversation

@yogeshwaran-c
Copy link

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

In linked worktrees, revParse() only reads ref files from the worktree-specific gitdir (this.dotGit.path). However, shared refs like refs/heads/*, refs/remotes/*, and refs/tags/* are stored in the common gitdir (this.dotGit.commonPath), not in the per-worktree gitdir.

This causes the fast-path file read to always fail with ENOENT for shared refs in linked worktrees, logging a warning and falling back to spawning git rev-parse via the CLI. While the fallback produces correct results, it is slower and generates unnecessary warning logs.

Closes #297786

What is the new behavior?

When the initial file read from the worktree gitdir fails with ENOENT and this.dotGit.commonPath is available, revParse() now tries reading the ref from the common gitdir before falling back to the git rev-parse CLI.

This matches git's own ref resolution order:

  1. Per-worktree refs (e.g., HEAD, MERGE_HEAD) from $GIT_DIR
  2. Shared refs (e.g., refs/heads/*, refs/remotes/*) from $GIT_COMMON_DIR

For non-worktree repositories, commonPath is undefined, so behavior is unchanged.

Additional context

This is consistent with how commonPath is already used elsewhere in the same file:

  • getWorktreesFS() (line 3004): this.dotGit.commonPath ?? this.dotGit.path
  • getRemotesFS() (line 3096): this.dotGit.commonPath ?? this.dotGit.path

In linked worktrees, revParse() only reads ref files from the
worktree-specific gitdir (dotGit.path), but shared refs like
refs/heads/*, refs/remotes/*, and refs/tags/* live in the common
gitdir (dotGit.commonPath). This causes branch detection and other
ref-based operations to fail silently and fall back to spawning
git rev-parse.

When the initial read from the worktree gitdir fails with ENOENT
and a commonPath is available, try reading the ref from the common
gitdir before falling back to the git CLI. This matches git's own
ref resolution order and is consistent with how commonPath is
already used elsewhere in the file (getWorktreesFS, getRemotesFS).

Fixes microsoft#297786
@vs-code-engineering
Copy link
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@lszomoru

Matched files:

  • extensions/git/src/git.ts

@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Git: revParse reads ref files from worktree gitdir instead of common gitdir, causing ENOENT loop in linked worktrees

2 participants