From 9b2443b60ddf96c6633bf61723cb2c439059c627 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Thu, 9 Jan 2025 19:04:17 -0800 Subject: [PATCH] fix(cache): invalidate Git cache for worktrees Fixes #6085 --- src/segments/git.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/segments/git.go b/src/segments/git.go index 0d80abd32199..9071f9fc054f 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -224,8 +224,16 @@ func (g *Git) CacheKey() (string, bool) { return "", false } - ref := g.FileContents(dir.Path, "HEAD") + if !g.isRepo(dir) { + return "", false + } + + // Read the HEAD from the real repo directory. + ref := g.FileContents(g.workingDir, "HEAD") ref = strings.Replace(ref, "ref: refs/heads/", "", 1) + + // Use the repo clone in the cache key so the mapped path is consistent + // for primary and worktree repos. return fmt.Sprintf("%s@%s", dir.Path, ref), true } @@ -349,10 +357,15 @@ func (g *Git) shouldDisplay() bool { return false } + return g.isRepo(gitdir) +} + +func (g *Git) isRepo(gitdir *runtime.FileInfo) bool { g.setDir(gitdir.Path) if !gitdir.IsDir { if g.hasWorktree(gitdir) { + // g.workingDir will contain the worktree repo dir. g.realDir = g.convertToWindowsPath(g.realDir) return true }