Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created December 30, 2020 20:07
Show Gist options
  • Save percybolmer/ff909f1c1b0758d3713fa91f6a73b48f to your computer and use it in GitHub Desktop.
Save percybolmer/ff909f1c1b0758d3713fa91f6a73b48f to your computer and use it in GitHub Desktop.
Check if a git commit is Nearest
// IsCommitNearest will check if a commit tag Hash is equal to the current repository HEAD tag
// If the Hashes matches, it will return true
func (r *Repository) IsCommitNearest(commit *object.Commit) (bool, error) {
latestTag, previousTag, err := r.GetLatestTag()
if err != nil {
return false, fmt.Errorf("%v:%w", "Couldn't get latest tag...", err)
}
if latestTag != nil {
// Compare latest tag Hash with the repository HEAD hash
// Hash() returns a Slice which can be compared without converting to string
// Noticed by /OfficialTomCruise on reddit comments
if latestTag.Hash() == r.ref.Hash() {
return true, nil
}
return previousTag.Hash() == commit.Hash, nil
}
return false, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment