Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 54 additions & 19 deletions src/segments/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

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