diff --git a/src/segments/git.go b/src/segments/git.go index f68c02332455..30520fb013b0 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -186,13 +186,13 @@ func (g *Git) Enabled() bool { } fetchUser := g.options.Bool(FetchUser, false) - if fetchUser { - g.setUser() - } - g.RepoName = g.repoName() if g.IsBare { + if fetchUser { + g.setUser() + } + g.getBareRepoInfo() return true } @@ -207,19 +207,36 @@ func (g *Git) Enabled() bool { displayStatus = false } + // Phase 1: setUser is independent, run it alongside setStatus + var wg sync.WaitGroup + + if fetchUser { + wg.Go(g.setUser) + } + if displayStatus { g.setStatus() - g.setHEADStatus() + + // Phase 2: fan out work that depends only on setStatus results + wg.Go(g.setHEADStatus) + wg.Go(g.setPushStatus) + + if g.options.Bool(FetchUpstreamIcon, false) { + wg.Go(func() { + g.UpstreamIcon = g.getUpstreamIcon() + }) + } + g.setBranchStatus() - g.setPushStatus() } else { g.updateHEADReference() - } - if g.options.Bool(FetchUpstreamIcon, false) { - g.UpstreamIcon = g.getUpstreamIcon() + if g.options.Bool(FetchUpstreamIcon, false) { + g.UpstreamIcon = g.getUpstreamIcon() + } } + wg.Wait() return true } @@ -382,8 +399,20 @@ func (g *Git) isRepo(gitdir *runtime.FileInfo) bool { } func (g *Git) setUser() { - g.User.Name = g.getGitCommandOutput("config", "user.name") - g.User.Email = g.getGitCommandOutput("config", "user.email") + output := g.getGitCommandOutput("config", "--get-regexp", "^user\\.") + for line := range strings.SplitSeq(output, "\n") { + key, val, ok := strings.Cut(line, " ") + if !ok { + continue + } + + switch key { + case "user.name": + g.User.Name = val + case "user.email": + g.User.Email = val + } + } } func (g *Git) isBareRepo(gitDir *runtime.FileInfo) bool { @@ -554,15 +583,21 @@ func (g *Git) setPushStatus() { return } - ahead := g.getGitCommandOutput("rev-list", "--count", pushRemote+"..HEAD") - if ahead != "" { - g.PushAhead, _ = strconv.Atoi(strings.TrimSpace(ahead)) - } + var wg sync.WaitGroup - behind := g.getGitCommandOutput("rev-list", "--count", "HEAD.."+pushRemote) - if behind != "" { - g.PushBehind, _ = strconv.Atoi(strings.TrimSpace(behind)) - } + wg.Go(func() { + if v := g.getGitCommandOutput("rev-list", "--count", pushRemote+"..HEAD"); v != "" { + g.PushAhead, _ = strconv.Atoi(strings.TrimSpace(v)) + } + }) + + wg.Go(func() { + if v := g.getGitCommandOutput("rev-list", "--count", "HEAD.."+pushRemote); v != "" { + g.PushBehind, _ = strconv.Atoi(strings.TrimSpace(v)) + } + }) + + wg.Wait() } func (g *Git) getPushRemote() string {