Skip to content
Snippets Groups Projects
Verified Commit aa4874d1 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: add verbose mode to detect git transactions

parent c6f1adc9
Branches
Tags
No related merge requests found
...@@ -16,8 +16,9 @@ var gitRepo *git.Repository ...@@ -16,8 +16,9 @@ var gitRepo *git.Repository
type commandLineOptions struct { type commandLineOptions struct {
Path string `short:"p" long:"path" description:"path to the file"` Path string `short:"p" long:"path" description:"path to the file"`
Selector string `short:"s" long:"selector" description:"selector of data"` Selector string `short:"s" long:"selector" description:"selector of data"`
Null bool `short:"0" long:"null" description:"terminate with null byte"`
Git bool `short:"g" long:"git" description:"read from git"` Git bool `short:"g" long:"git" description:"read from git"`
Verbose bool `short:"v" long:"verbose" description:"verbose output"`
Null bool `short:"0" long:"null" description:"terminate with null byte"`
Major struct { Major struct {
} `command:"major" description:"increase the major version"` } `command:"major" description:"increase the major version"`
...@@ -66,6 +67,9 @@ func increaseMajor() (string, error) { ...@@ -66,6 +67,9 @@ func increaseMajor() (string, error) {
return next.String(), nil return next.String(), nil
} }
var verbosity = false
var nullTerminated = false
func executeCommand() { func executeCommand() {
arguments = new(commandLineOptions) arguments = new(commandLineOptions)
...@@ -75,7 +79,7 @@ func executeCommand() { ...@@ -75,7 +79,7 @@ func executeCommand() {
_, err = p.Parse() _, err = p.Parse()
if err != nil { if err != nil {
os.Exit(-1) os.Exit(1)
} }
activeCommand := p.Command.Active activeCommand := p.Command.Active
...@@ -87,28 +91,55 @@ func executeCommand() { ...@@ -87,28 +91,55 @@ func executeCommand() {
var newVersion string var newVersion string
command := activeCommand.Name command := activeCommand.Name
if arguments.Verbose {
verbosity = true
}
if arguments.Null {
nullTerminated = true
}
if arguments.Git || command == "auto" { if arguments.Git || command == "auto" {
gitRepo, err = git.PlainOpen(".")
path, err := os.Getwd()
if err != nil {
_, err := fmt.Fprintf(os.Stderr, "Could not get current working directory: %s\n", err)
if err != nil {
fmt.Printf("Could not get current working directory: %s\n", err)
}
os.Exit(1)
}
if verbosity {
fmt.Println("git path:", path)
}
gitRepo, err = git.PlainOpen(path)
if err != nil { if err != nil {
_, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
os.Exit(-1) os.Exit(1)
} }
} }
if command == "auto" { if command == "auto" {
updateType, err := GetCommitType() commitType, err := GetCommitType()
if arguments.Verbose {
fmt.Printf("commit type: %s\n", commitType)
}
if err != nil { if err != nil {
_, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
os.Exit(-1) os.Exit(1)
} }
switch updateType { switch commitType {
case BreakingCommit: case BreakingCommit:
command = "major" command = "major"
case FeatCommit: case FeatCommit:
...@@ -133,12 +164,12 @@ func executeCommand() { ...@@ -133,12 +164,12 @@ func executeCommand() {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
os.Exit(-1) os.Exit(1)
} }
if arguments.Null { if nullTerminated {
// terminate with null byte // terminate with null byte
fmt.Printf("%s", version.String()) fmt.Printf("%s%s", version.String(), string(rune(0)))
} else { } else {
fmt.Printf("%s\n", version.String()) fmt.Printf("%s\n", version.String())
} }
...@@ -148,7 +179,7 @@ func executeCommand() { ...@@ -148,7 +179,7 @@ func executeCommand() {
currentTime := time.Now() currentTime := time.Now()
build = currentTime.Format("20060102150405") build = currentTime.Format("20060102150405")
if arguments.Null { if nullTerminated {
build += string(rune(0)) build += string(rune(0))
} }
...@@ -163,7 +194,6 @@ func executeCommand() { ...@@ -163,7 +194,6 @@ func executeCommand() {
newVersion, err = increaseMinor() newVersion, err = increaseMinor()
case "patch": case "patch":
newVersion, err = increasePatch() newVersion, err = increasePatch()
} }
if err != nil { if err != nil {
...@@ -171,7 +201,11 @@ func executeCommand() { ...@@ -171,7 +201,11 @@ func executeCommand() {
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
os.Exit(-1) os.Exit(1)
}
if verbosity {
fmt.Printf("new version: %s\n", newVersion)
} }
err = writeVersion(newVersion) err = writeVersion(newVersion)
...@@ -180,7 +214,11 @@ func executeCommand() { ...@@ -180,7 +214,11 @@ func executeCommand() {
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
os.Exit(-1) os.Exit(1)
}
if verbosity {
fmt.Printf("wrote version: %s\n", newVersion)
} }
} }
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
"devenv": { "devenv": {
"locked": { "locked": {
"dir": "src/modules", "dir": "src/modules",
"lastModified": 1689504341, "lastModified": 1689667485,
"narHash": "sha256-btK/nUaxB6HBCiuhnebiCTnohatHElVwWrBOWP+oWpI=", "narHash": "sha256-tLNoMRSPLlW1D4wgNpSIPUUHd6x1GdjAhETLpRTKnfo=",
"owner": "cachix", "owner": "cachix",
"repo": "devenv", "repo": "devenv",
"rev": "b9c9d83e89c0405ab02880132a0601911c477250", "rev": "6f8add968bc12bf81d845eb7dc684a0733bb1518",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -74,11 +74,11 @@ ...@@ -74,11 +74,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1689503327, "lastModified": 1689605451,
"narHash": "sha256-qVwzYLA8oT2oWNDXO0A3bZHOhoPOihIB9T677+Hor1E=", "narHash": "sha256-u2qp2k9V1smCfk6rdUcgMKvBj3G9jVvaPHyeXinjN9E=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "f64b9738da8e86195766147e9752c67fccee006c", "rev": "53657afe29748b3e462f1f892287b7e254c26d77",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -106,11 +106,11 @@ ...@@ -106,11 +106,11 @@
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1689503327, "lastModified": 1689605451,
"narHash": "sha256-qVwzYLA8oT2oWNDXO0A3bZHOhoPOihIB9T677+Hor1E=", "narHash": "sha256-u2qp2k9V1smCfk6rdUcgMKvBj3G9jVvaPHyeXinjN9E=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "f64b9738da8e86195766147e9752c67fccee006c", "rev": "53657afe29748b3e462f1f892287b7e254c26d77",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -130,11 +130,11 @@ ...@@ -130,11 +130,11 @@
"nixpkgs-stable": "nixpkgs-stable" "nixpkgs-stable": "nixpkgs-stable"
}, },
"locked": { "locked": {
"lastModified": 1689553106, "lastModified": 1689668210,
"narHash": "sha256-RFFf6BbpqQB0l1ehAbgri9g9MGZkAY9UdiNotD9fG8Y=", "narHash": "sha256-XAATwDkaUxH958yXLs1lcEOmU6pSEIkatY3qjqk8X0E=",
"owner": "cachix", "owner": "cachix",
"repo": "pre-commit-hooks.nix", "repo": "pre-commit-hooks.nix",
"rev": "87589fa438dd6d5b8c7c1c6ab2ad69e4663bb51f", "rev": "eb433bff05b285258be76513add6f6c57b441775",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -171,11 +171,11 @@ ...@@ -171,11 +171,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1689584837, "lastModified": 1689585498,
"narHash": "sha256-fthvNvvzQlXBP2is6UanGY06nKmmBOU1m+NJoOtptpA=", "narHash": "sha256-oNgRghsOrPN7W+fcNL22LsetUvJS21v4Hm/Z1Hbcon4=",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"rev": "226cf5dc731ce42d842466257382e983a0bb2c4c", "rev": "c6f1adc90800afdf3f6a42998fe602228615edae",
"revCount": 25, "revCount": 27,
"type": "git", "type": "git",
"url": "https://gitlab.schukai.com/oss/utilities/version.git" "url": "https://gitlab.schukai.com/oss/utilities/version.git"
}, },
......
...@@ -56,6 +56,19 @@ const ( ...@@ -56,6 +56,19 @@ const (
FixCommit FixCommit
) )
func (c CommitType) String() string {
switch c {
case FeatCommit:
return "feat"
case BreakingCommit:
return "breaking"
case FixCommit:
return "fix"
default:
return "other"
}
}
func GetCommitType() (CommitType, error) { func GetCommitType() (CommitType, error) {
latestTag, err := getLatestSemanticTag() latestTag, err := getLatestSemanticTag()
...@@ -63,16 +76,28 @@ func GetCommitType() (CommitType, error) { ...@@ -63,16 +76,28 @@ func GetCommitType() (CommitType, error) {
return OtherCommit, err return OtherCommit, err
} }
if verbosity {
fmt.Println("latest found tag:", latestTag)
}
tagCommit, err := getTagCommit(latestTag.Tag) tagCommit, err := getTagCommit(latestTag.Tag)
if err != nil { if err != nil {
return OtherCommit, err return OtherCommit, err
} }
if verbosity {
fmt.Println("tag commit:", tagCommit)
}
commitType, err := getCommitTypeSinceTag(tagCommit) commitType, err := getCommitTypeSinceTag(tagCommit)
if err != nil { if err != nil {
return OtherCommit, err return OtherCommit, err
} }
if verbosity {
fmt.Println("commit type:", commitType)
}
return commitType, nil return commitType, nil
} }
...@@ -135,9 +160,15 @@ func getSemanticTags() ([]SemanticVersion, error) { ...@@ -135,9 +160,15 @@ func getSemanticTags() ([]SemanticVersion, error) {
var tagList []SemanticVersion var tagList []SemanticVersion
err = tags.ForEach(func(tag *plumbing.Reference) error { err = tags.ForEach(func(tag *plumbing.Reference) error {
tagName := tag.Name().Short() tagName := tag.Name().Short()
if verbosity {
fmt.Println("found tag: ", tagName)
}
if versionRegex.MatchString(tagName) { if versionRegex.MatchString(tagName) {
tagList = append(tagList, ParseSemanticVersion(tagName)) tagList = append(tagList, ParseSemanticVersion(tagName))
} }
return nil return nil
}) })
...@@ -149,6 +180,10 @@ func getSemanticTags() ([]SemanticVersion, error) { ...@@ -149,6 +180,10 @@ func getSemanticTags() ([]SemanticVersion, error) {
return tagList[i].IsLessThan(tagList[j]) return tagList[i].IsLessThan(tagList[j])
}) })
if verbosity {
fmt.Println("tag list: ", tagList)
}
return tagList, nil return tagList, nil
} }
...@@ -164,21 +199,54 @@ func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) { ...@@ -164,21 +199,54 @@ func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) {
var commitType CommitType var commitType CommitType
found := false found := false
counter := 0
err = cIter.ForEach(func(commit *object.Commit) error { err = cIter.ForEach(func(commit *object.Commit) error {
counter++
if commit.Hash == tagCommit.Hash { if commit.Hash == tagCommit.Hash {
found = true found = true
if verbosity {
fmt.Println("found tag commit after", counter, "commits")
}
return storer.ErrStop // stop iteration return storer.ErrStop // stop iteration
} }
if strings.HasPrefix(commit.Message, "feat:") { message := strings.TrimSpace(commit.Message)
if strings.HasPrefix(message, "feat") {
commitType = FeatCommit commitType = FeatCommit
found = true found = true
return storer.ErrStop // stop iteration
} else if strings.HasPrefix(commit.Message, "fix:") { if verbosity {
fmt.Println("found feat commit after", counter, "commits")
}
} else if strings.HasPrefix(message, "fix") {
// if we already found a feat or breaking commit, we don't care about fix commits
if commitType < FixCommit && commitType > OtherCommit {
return nil
}
commitType = FixCommit commitType = FixCommit
} else if containsBreakingChangeFooter(commit.Message) {
if verbosity {
fmt.Println("found fix commit after", counter, "commits")
}
} else if containsBreakingChangeFooter(message) {
commitType = BreakingCommit commitType = BreakingCommit
found = true found = true
if verbosity {
fmt.Println("found breaking commit after", counter, "commits")
}
return storer.ErrStop // stop iteration return storer.ErrStop // stop iteration
} }
......
...@@ -212,25 +212,25 @@ func writeVersionToYaml(version string) error { ...@@ -212,25 +212,25 @@ func writeVersionToYaml(version string) error {
func writeVersionToGit(version string) error { func writeVersionToGit(version string) error {
path, err := os.Getwd() repo := gitRepo
if err != nil {
return err
}
repo, err := git.PlainOpen(path) h, err := repo.Head()
if err != nil { if err != nil {
return err return err
} }
h, err := repo.Head() if verbosity {
if err != nil { fmt.Println("Writing to git HEAD:", h.Hash())
return err
} }
_, err = repo.CreateTag(version, h.Hash(), &git.CreateTagOptions{ _, err = repo.CreateTag(version, h.Hash(), &git.CreateTagOptions{
Message: version, Message: "chore: bump version to " + version,
}) })
if err != nil {
return err
}
return nil return nil
} }
...@@ -245,7 +245,7 @@ func writeVersion(version string) error { ...@@ -245,7 +245,7 @@ func writeVersion(version string) error {
} }
if arguments.Path == "" { if arguments.Path == "" {
if arguments.Null { if nullTerminated {
version += string(rune(0)) version += string(rune(0))
fmt.Printf("%s", version) fmt.Printf("%s", version)
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment