From 9234f639027f61eaae857876ece95888d7718ab1 Mon Sep 17 00:00:00 2001 From: Christian Hoffmeister Date: Sun, 16 Aug 2020 12:21:14 +0200 Subject: [PATCH] Make fallback tag configurable --- Makefile | 3 --- README.md | 2 +- main.go | 26 ++++++++++++++------------ main_test.go | 29 +++++++++++++++++------------ 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index fa98f88..a0348b1 100644 --- a/Makefile +++ b/Makefile @@ -14,9 +14,6 @@ VERSION := $(shell git describe --tags --always --dirty) run: go run . -run-watch: - watch -n1 go run . - build: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o git-describe-semver-linux-amd64 . CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o git-describe-semver-darwin-amd64 . diff --git a/README.md b/README.md index a23fec9..ef28791 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Corresponding git tag | git describe --tags | git-describe-semver ```bash cd my-git-directory -wget -q https://github.com/choffmeister/git-describe-semver/releases/download/v0.0.2/git-describe-semver-linux-amd64 +wget -q https://github.com/choffmeister/git-describe-semver/releases/download/v0.1.0/git-describe-semver-linux-amd64 chmod +x git-describe-semver-linux-amd64 ./git-describe-semver-linux-amd64 ``` diff --git a/main.go b/main.go index d510f3c..4e08ff7 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "log" "os" @@ -10,18 +11,16 @@ import ( ) // GenerateVersion ... -func GenerateVersion(tagName string, counter int, headHash string) (*string, error) { +func GenerateVersion(tagName string, counter int, headHash string, fallbackTagName string) (*string, error) { devPreRelease := []string{"dev", strconv.Itoa(counter)} buildMetadata := []string{"g" + (headHash)[0:7]} if tagName == "" { - version := SemVer{ - Prefix: "v", - Major: 0, - Minor: 0, - Patch: 0, - PreRelease: devPreRelease, - BuildMetadata: buildMetadata, + version := SemVerParse(fallbackTagName) + if version == nil { + return nil, fmt.Errorf("unable to parse fallback tag") } + version.PreRelease = devPreRelease + version.BuildMetadata = buildMetadata result := version.String() return &result, nil } @@ -57,16 +56,16 @@ func GenerateVersion(tagName string, counter int, headHash string) (*string, err } // Run ... -func Run(dir string) (*string, error) { +func Run(dir string, fallback string) (*string, error) { repo, err := git.PlainOpen(dir) if err != nil { return nil, fmt.Errorf("unable to open git repository: %v", err) } tagName, counter, headHash, err := GitDescribe(*repo) if err != nil { - return nil, fmt.Errorf("unable to find head: %v", err) + return nil, fmt.Errorf("unable to describe commit: %v", err) } - result, err := GenerateVersion(*tagName, *counter, *headHash) + result, err := GenerateVersion(*tagName, *counter, *headHash, fallback) if err != nil { return nil, fmt.Errorf("unable to generate version: %v", err) } @@ -74,11 +73,14 @@ func Run(dir string) (*string, error) { } func main() { + fallback := flag.String("fallback", "", "The first version to fallback to should there be no tag") + flag.Parse() + dir, err := os.Getwd() if err != nil { log.Fatalf("unable to determine current directory: %v\n", err) } - result, err := Run(dir) + result, err := Run(dir, *fallback) if err != nil { log.Fatalf("unable to generate version: %v\n", err) } diff --git a/main_test.go b/main_test.go index 9103078..0a3290d 100644 --- a/main_test.go +++ b/main_test.go @@ -10,43 +10,48 @@ import ( func TestGenerateVersion(t *testing.T) { assert := assert.New(t) - test := func(inputTagName string, inputCounter int, inputHeadHash string, expected string) { - actual, err := GenerateVersion(inputTagName, inputCounter, inputHeadHash) + test := func(inputTagName string, inputCounter int, inputHeadHash string, inputFallback string, expected string) { + actual, err := GenerateVersion(inputTagName, inputCounter, inputHeadHash, inputFallback) if assert.NoError(err) { assert.Equal(expected, *actual) } } - test("0.0.0", 0, "abc1234", "0.0.0") - test("0.0.0", 1, "abc1234", "0.0.1-dev.1+gabc1234") - test("0.0.0-rc1", 1, "abc1234", "0.0.0-rc1.dev.1+gabc1234") - test("0.0.0-rc.1", 1, "abc1234", "0.0.0-rc.1.dev.1+gabc1234") - test("0.0.0-rc.1+foobar", 1, "abc1234", "0.0.0-rc.1.dev.1+gabc1234") - test("", 1, "abc1234", "v0.0.0-dev.1+gabc1234") + test("0.0.0", 0, "abc1234", "", "0.0.0") + test("0.0.0", 1, "abc1234", "", "0.0.1-dev.1+gabc1234") + test("0.0.0-rc1", 1, "abc1234", "", "0.0.0-rc1.dev.1+gabc1234") + test("0.0.0-rc.1", 1, "abc1234", "", "0.0.0-rc.1.dev.1+gabc1234") + test("0.0.0-rc.1+foobar", 1, "abc1234", "", "0.0.0-rc.1.dev.1+gabc1234") + test("v0.0.0-rc.1+foobar", 1, "abc1234", "", "v0.0.0-rc.1.dev.1+gabc1234") + + test("", 1, "abc1234", "0.0.0", "0.0.0-dev.1+gabc1234") + test("", 1, "abc1234", "v0.0.0", "v0.0.0-dev.1+gabc1234") + _, err := GenerateVersion("", 1, "abc1234", "") + assert.Error(err) } func TestRun(t *testing.T) { assert := assert.New(t) dir, _ := ioutil.TempDir("", "example") - _, err := Run(dir) + _, err := Run(dir, "") assert.Error(err) repo, _ := git.PlainInit(dir, false) worktree, _ := repo.Worktree() - _, err = Run(dir) + _, err = Run(dir, "") assert.Error(err) commit1, _ := worktree.Commit("first", &git.CommitOptions{}) repo.CreateTag("invalid", commit1, nil) - _, err = Run(dir) + _, err = Run(dir, "") assert.Error(err) commit2, _ := worktree.Commit("first", &git.CommitOptions{}) repo.CreateTag("v1.0.0", commit2, nil) commit3, _ := worktree.Commit("second", &git.CommitOptions{}) - result, err := Run(dir) + result, err := Run(dir, "") assert.NoError(err) assert.Equal("v1.0.1-dev.1+g"+commit3.String()[0:7], *result) }