diff --git a/.gitignore b/.gitignore index c4300bc28a417b16f22ee24fa6134754e796e36c..800110200bb88481179c1b3017bc34ea77f37357 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ devenv.local.nix # pre-commit .pre-commit-config.yaml +.attach_pid* diff --git a/commandline.go b/commandline.go index c9baea3b1a0151c7db4a23db5d95923d2d5146d8..2d115df4f61943fe71a68ce439d3f4e56ccf1d72 100644 --- a/commandline.go +++ b/commandline.go @@ -10,9 +10,8 @@ import ( "github.com/go-git/go-git/v5" ) -var ( - arguments *commandLineOptions -) +var arguments *commandLineOptions +var gitRepo *git.Repository type commandLineOptions struct { Path string `short:"p" long:"path" description:"path to the file"` @@ -67,8 +66,6 @@ func increaseMajor() (string, error) { return next.String(), nil } -var gitRepo *git.Repository - func executeCommand() { arguments = new(commandLineOptions) @@ -93,15 +90,21 @@ func executeCommand() { if arguments.Git || command == "auto" { gitRepo, err = git.PlainOpen(".") if err != nil { - fmt.Println(err) + _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) + if err != nil { + fmt.Printf("Error: %s\n", err) + } os.Exit(-1) } } if command == "auto" { - updateType, err := GetCommitType(gitRepo) + updateType, err := GetCommitType() if err != nil { - fmt.Println(err) + _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) + if err != nil { + fmt.Printf("Error: %s\n", err) + } os.Exit(-1) } @@ -122,16 +125,25 @@ func executeCommand() { switch command { case "print": - if arguments.Git { - version, err := getLatestSemanticTag(gitRepo) + + version, err := getVersion() + if err != nil { + _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) if err != nil { - fmt.Println(err) - os.Exit(-1) + fmt.Printf("Error: %s\n", err) } + os.Exit(-1) + } + + if arguments.Null { + // terminate with null byte fmt.Printf("%s", version.String()) - os.Exit(0) + } else { + fmt.Printf("%s\n", version.String()) } + os.Exit(0) + case "date": currentTime := time.Now() build = currentTime.Format("20060102150405") @@ -141,6 +153,7 @@ func executeCommand() { } fmt.Printf("%s", build) + os.Exit(0) case "init": newVersion = "0.1.0" @@ -154,13 +167,19 @@ func executeCommand() { } if err != nil { - fmt.Println(err) + _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) + if err != nil { + fmt.Printf("Error: %s\n", err) + } os.Exit(-1) } err = writeVersion(newVersion) if err != nil { - fmt.Println(err) + _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) + if err != nil { + fmt.Printf("Error: %s\n", err) + } os.Exit(-1) } diff --git a/errors.go b/errors.go index 81938f1738c0e1a17bfb4f870e9562cab5800668..9a9b0640ccdef7352fb724860c5af53001a980a2 100644 --- a/errors.go +++ b/errors.go @@ -3,11 +3,10 @@ package main import "fmt" var ( - timeoutError = fmt.Errorf("timeout") - noActiveCommandError = fmt.Errorf("no command active command") - notImplementedError = fmt.Errorf("not implemented") - missingSelector = fmt.Errorf("missing selector") - noSemverError = fmt.Errorf("no semver") - notFoundError = fmt.Errorf("not found") - multipleFoundError = fmt.Errorf("multiple found") + timeoutError = fmt.Errorf("timeout") + notImplementedError = fmt.Errorf("not implemented") + missingSelector = fmt.Errorf("missing selector") + noSemverError = fmt.Errorf("no semver") + notFoundError = fmt.Errorf("not found") + multipleFoundError = fmt.Errorf("multiple found") ) diff --git a/git.go b/git.go index d6a19c313346de76b2bce7073a49100933a9ca48..11b9fd9ff6a3e29d21157d0b7f0007235e27413d 100644 --- a/git.go +++ b/git.go @@ -56,19 +56,19 @@ const ( FixCommit ) -func GetCommitType(r *git.Repository) (CommitType, error) { +func GetCommitType() (CommitType, error) { - latestTag, err := getLatestSemanticTag(r) + latestTag, err := getLatestSemanticTag() if err != nil { return OtherCommit, err } - tagCommit, err := getTagCommit(r, latestTag.Tag) + tagCommit, err := getTagCommit(latestTag.Tag) if err != nil { return OtherCommit, err } - commitType, err := getCommitTypeSinceTag(r, tagCommit) + commitType, err := getCommitTypeSinceTag(tagCommit) if err != nil { return OtherCommit, err } @@ -76,8 +76,8 @@ func GetCommitType(r *git.Repository) (CommitType, error) { return commitType, nil } -func getLatestSemanticTag(r *git.Repository) (SemanticVersion, error) { - tagList, err := getSemanticTags(r) +func getLatestSemanticTag() (SemanticVersion, error) { + tagList, err := getSemanticTags() if err != nil { return SemanticVersion{}, err } @@ -89,7 +89,10 @@ func getLatestSemanticTag(r *git.Repository) (SemanticVersion, error) { return tagList[len(tagList)-1], nil } -func getTagCommit(r *git.Repository, tag string) (*object.Commit, error) { +func getTagCommit(tag string) (*object.Commit, error) { + + var r = gitRepo + tags, err := r.Tags() if err != nil { return nil, fmt.Errorf("failed to get tags: %v", err) @@ -120,7 +123,10 @@ func getTagCommit(r *git.Repository, tag string) (*object.Commit, error) { return tagCommit, nil } -func getSemanticTags(r *git.Repository) ([]SemanticVersion, error) { +func getSemanticTags() ([]SemanticVersion, error) { + + r := gitRepo + tags, err := r.Tags() if err != nil { return nil, fmt.Errorf("failed to get tags: %v", err) @@ -146,7 +152,10 @@ func getSemanticTags(r *git.Repository) ([]SemanticVersion, error) { return tagList, nil } -func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitType, error) { +func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) { + + r := gitRepo + cIter, err := r.Log(&git.LogOptions{}) if err != nil { return OtherCommit, fmt.Errorf("failed to get commit log: %v", err) diff --git a/version.go b/version.go index 2ba259736a9b0cde3fcf33d6f8a1207c0f7f3465..b30922c46f3b3ab8b717494816c09d54ce5ed6c1 100644 --- a/version.go +++ b/version.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" "os" "path/filepath" "strings" @@ -89,46 +88,11 @@ func getFromYaml() (string, error) { } -func getVersionFromGit() (string, error) { - - path, err := os.Getwd() - if err != nil { - return "", err - } - - repo, err := git.PlainOpen(path) - if err != nil { - return "", err - } - - tags, err := repo.Tags() - if err != nil { - return "", err - } - - currentVersion, _ := semver.NewVersion("0.1.0") - - err = tags.ForEach(func(t *plumbing.Reference) error { - - version, err := semver.NewVersion(t.Name().Short()) - if err != nil { - return nil - } - - if version.GreaterThan(currentVersion) { - currentVersion = version - } - - return nil - }) - - return currentVersion.String(), nil -} - func readVersion() (string, error) { if arguments.Git { - return getVersionFromGit() + version, err := getLatestSemanticTag() + return version.String(), err } else if arguments.Path != "" {