Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Add git describe options flag #95

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ linters-settings:
- whyNoLint
- wrapperFunc
gocyclo:
min-complexity: 18
min-complexity: 20
goimports:
local-prefixes: github.com/golangci/golangci-lint
golint:
Expand Down
12 changes: 7 additions & 5 deletions cmd/sver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ var (
flagReleaseOnly = false
flagPrefix = false

flagTagsServerURL = ""
flagTagsUsername = ""
flagTagsPassword = ""
flagTagsServerURL = ""
flagTagsUsername = ""
flagTagsPassword = ""
flagGitDescribeOptions = []string{}
)

var rootCmd = &cobra.Command{
Expand All @@ -32,7 +33,7 @@ var rootCmd = &cobra.Command{
return errors.New("Asked for a pre-release version, but the --release flag is on.")
}

version, err := sver.CurrentVersion(flagReleaseOnly, flagForce)
version, err := sver.CurrentVersion(flagReleaseOnly, flagForce, flagGitDescribeOptions...)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,7 +109,7 @@ it's the latest one, it returns the appropriate tags to be pushed.`,
return errors.New("Asked for a pre-release version, but the --release flag is on.")
}

version, err := sver.CurrentVersion(flagReleaseOnly, flagForce)
version, err := sver.CurrentVersion(flagReleaseOnly, flagForce, flagGitDescribeOptions...)
if err != nil {
return err
}
Expand Down Expand Up @@ -159,6 +160,7 @@ func main() {
rootCmd.Flags().BoolVarP(&flagReleaseOnly, "release", "", false, "Fail if this is a dev, pre-release or dirty version.")
rootCmd.Flags().BoolVarP(&flagForce, "force", "f", false, "Ignore a dirty repository.")
rootCmd.Flags().BoolVarP(&flagPrefix, "prefix", "p", false, "Add the 'v' prefix to the output version.")
rootCmd.Flags().StringArrayVarP(&flagGitDescribeOptions, "git-describe-options", "g", []string{}, "Add extra options to git describe used to determine the correct version.(ex. --match='v*', --exclude='prefix/*')")

tagsCmd.Flags().StringVarP(&flagTagsServerURL, "server", "s", "https://registry-1.docker.io/", "Registry server to connect to.")
tagsCmd.Flags().StringVarP(&flagTagsUsername, "user", "u", "", "Username for the registry.")
Expand Down
8 changes: 6 additions & 2 deletions pkg/sver/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ var (
regexTail = regexp.MustCompile(`^\d+\.\d+\.\d+(.*)`)
)

func CurrentVersion(releaseOnly, force bool) (string, error) {
func CurrentVersion(releaseOnly, force bool, options ...string) (string, error) {
err := verifyGit()
if err != nil {
return "", errors.Wrap(err, "git error")
}

hasTag := true
tag, err := git("describe", "--tags", "--abbrev=0")
params := []string{"describe", "--tags", "--abbrev=0"}
if len(options) > 0 {
params = append(params, options...)
}
tag, err := git(params...)
if err != nil {
if !strings.Contains(err.Error(), "cannot describe anything") {
return "", errors.Wrap(err, "exec error")
Expand Down