diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index 10a1cdd..1b66cee 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -83,3 +83,27 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, return nil } } + +func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor) func(c *cli.Context) error { + return func(c *cli.Context) error { + describe := git.Describe() + + currentVer, err := sv.ToVersion(describe) + if err != nil { + return fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err) + } + + commits, err := git.Log(describe) + if err != nil { + return fmt.Errorf("error getting git log, message: %v", err) + } + + nextVer := semverProcessor.NextVersion(currentVer, commits) + fmt.Printf("%d.%d.%d\n", nextVer.Major(), nextVer.Minor(), nextVer.Patch()) + + if err := git.Tag(nextVer); err != nil { + return fmt.Errorf("error generate tag version: %s, message: %v", nextVer.String(), err) + } + return nil + } +} diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index f60a3ab..cb1e264 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -43,6 +43,12 @@ func main() { Usage: "generate release notes", Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor), }, + { + Name: "tag", + Aliases: []string{"tg"}, + Usage: "generate tag with version based on git commit messages", + Action: tagHandler(git, semverProcessor, releasenotesProcessor), + }, } apperr := app.Run(os.Args) diff --git a/sv/git.go b/sv/git.go index 0af4296..28e7f4e 100644 --- a/sv/git.go +++ b/sv/git.go @@ -73,9 +73,16 @@ func (g GitImpl) Log(lastTag string) ([]GitCommitLog, error) { // Tag create a git tag func (g GitImpl) Tag(version semver.Version) error { + tag := fmt.Sprintf(g.tagPattern, version.Major(), version.Minor(), version.Patch()) tagMsg := fmt.Sprintf("-v \"Version %d.%d.%d\"", version.Major(), version.Minor(), version.Patch()) - cmd := exec.Command("git", "tag", "-a "+fmt.Sprintf(g.tagPattern, version.Major(), version.Minor(), version.Patch()), tagMsg) - return cmd.Run() + + tagCommand := exec.Command("git", "tag", "-a "+tag, tagMsg) + if err := tagCommand.Run(); err != nil { + return err + } + + pushCommand := exec.Command("git", "push", "origin", tag) + return pushCommand.Run() } func parseLogOutput(messageMetadata map[string]string, log string) []GitCommitLog {