Skip to content

Commit

Permalink
fix(gitutils): Fix for how we cache head info when searching for tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Feb 2, 2022
1 parent 1711f68 commit 934b5ad
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/gitutils/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type caching struct {
ahead int
behind int

headInfo *HeadInfo
state *RepositoryState
stats *GitStats
headInfoTagsSearched int
headInfo *HeadInfo
state *RepositoryState
stats *GitStats
}

// NewCaching returns a new caching instance of Git. The returned instance
Expand Down Expand Up @@ -79,7 +80,9 @@ func (c *caching) GetAheadBehind(localRef string, remoteRef string) (ahead int,

// Head returns information about the current head.
func (c *caching) Head(maxTagsToSearch int) (head HeadInfo, err error) {
if c.headInfo == nil {
haveHeadInfo := c.headInfo != nil && (!c.headInfo.Detached || c.headInfo.IsTag || maxTagsToSearch < c.headInfoTagsSearched)
if !haveHeadInfo {
c.headInfoTagsSearched = maxTagsToSearch
headInfo, err := c.underlying.Head(maxTagsToSearch)
if err != nil {
return HeadInfo{}, err
Expand Down
3 changes: 3 additions & 0 deletions internal/gitutils/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type DemoGit struct {
HeadDescription string `yaml:"headDescription"`
// IsDetached is true if HEAD is detached.
IsDetached bool `yaml:"isDetached"`
// IsTag is true if HeadDescription is for a tag.
IsTag bool `yaml:"isTag"`
// CurrentBranchUpstream is the current upstream branch, or "" if none.
CurrentBranchUpstream string `yaml:"currentBranchUpstream"`

Expand Down Expand Up @@ -83,6 +85,7 @@ func (git DemoGit) Head(maxTagsToSearch int) (head HeadInfo, err error) {
Description: headDescription,
Detached: git.IsDetached,
Hash: git.HeadDescription,
IsTag: git.IsTag,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/gitutils/gitUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type HeadInfo struct {
Detached bool
// Hash is the current hash of the head.
Hash string
// IsTag is true if the current head matches a tag.
IsTag bool
}

// Git is an interface for interacting with a git repository.
Expand Down
3 changes: 3 additions & 0 deletions internal/gitutils/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ func (g *gitUtils) Head(maxTagsToSearch int) (HeadInfo, error) {
}

// If we don't have a description, try to get a tag name
isTag := false
if description == "" {
tag, err := g.GetTagNameForHash(headHash, maxTagsToSearch)
if err == nil && tag != "" {
isTag = true
description = "(" + strings.TrimSpace(tag) + ")"
}
}
Expand All @@ -53,6 +55,7 @@ func (g *gitUtils) Head(maxTagsToSearch int) (HeadInfo, error) {
Description: description,
Detached: isDetached,
Hash: headHash,
IsTag: isTag,
}, nil
}

Expand Down

0 comments on commit 934b5ad

Please sign in to comment.