Created
December 4, 2024 10:28
-
-
Save speier/85f696a25dff0526bd90c236bec44fc0 to your computer and use it in GitHub Desktop.
next semver from git tag
This file contains 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
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