Skip to content

Instantly share code, notes, and snippets.

@speier
Created December 4, 2024 10:28
Show Gist options
  • Save speier/85f696a25dff0526bd90c236bec44fc0 to your computer and use it in GitHub Desktop.
Save speier/85f696a25dff0526bd90c236bec44fc0 to your computer and use it in GitHub Desktop.
next semver from git tag
package main
import (
"fmt"
"os"
"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
func main() {
wd, err := os.Getwd()
if err != nil {
fatal(err)
}
tag := latestTag(wd)
v := incMinor(tag, "v")
fmt.Println(v)
}
func incMinor(v string, prefix string) string {
sv, err := semver.NewVersion(v)
if err != nil {
fatal(err)
}
return fmt.Sprintf("%s%s", prefix, sv.IncMinor())
}
func latestTag(wd string) string {
repo, err := git.PlainOpen(wd)
if err != nil {
fatal(err)
}
iter, err := repo.Tags()
if err != nil {
fatal(err)
}
tags := make([]string, 0)
iter.ForEach(func(ref *plumbing.Reference) error {
tags = append(tags, ref.Name().Short())
return nil
})
if len(tags) == 0 {
return "0.0.0"
}
return tags[len(tags)-1]
}
func fatal(err error) {
fmt.Println(err)
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment