Skip to content

Commit

Permalink
fix(cache): invalidate Git cache for worktrees
Browse files Browse the repository at this point in the history
Fixes #6085
  • Loading branch information
heaths committed Jan 10, 2025
1 parent 41bb368 commit 9b2443b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/segments/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9b2443b

Please sign in to comment.