Created
December 30, 2020 13:05
-
-
Save percybolmer/d1b370ea240bd1888c4b3c47ed174bb0 to your computer and use it in GitHub Desktop.
Embedded getlatest into repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GetLatestTag is used to check the latestTag | |
// it will return a reference to the LatestTag and the PreviousTag | |
func (r *Repository) GetLatestTag() (*plumbing.Reference, *plumbing.Reference, error) { | |
tagRefs, err := r.repo.Tags() | |
if err != nil { | |
return nil, nil, err | |
} | |
var latestTagCommit *object.Commit | |
var latestTagName *plumbing.Reference | |
var previousTag *plumbing.Reference | |
var previousTagReturn *plumbing.Reference | |
err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error { | |
revision := plumbing.Revision(tagRef.Name().String()) | |
tagCommitHash, err := r.repo.ResolveRevision(revision) | |
if err != nil { | |
return err | |
} | |
commit, err := r.repo.CommitObject(*tagCommitHash) | |
if err != nil { | |
return err | |
} | |
if latestTagCommit == nil { | |
latestTagCommit = commit | |
latestTagName = tagRef | |
previousTagReturn = previousTag | |
} | |
if commit.Committer.When.After(latestTagCommit.Committer.When) { | |
latestTagCommit = commit | |
latestTagName = tagRef | |
previousTagReturn = previousTag | |
} | |
previousTag = tagRef | |
return nil | |
}) | |
if err != nil { | |
return nil, nil, err | |
} | |
return latestTagName, previousTagReturn, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment