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