Skip to content

Commit

Permalink
Make fallback tag configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Aug 16, 2020
1 parent 1ba5a86 commit 9234f63
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
26 changes: 14 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"log"
"os"
Expand All @@ -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
}
Expand Down Expand Up @@ -57,28 +56,31 @@ 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)
}
return result, nil
}

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)
}
Expand Down
29 changes: 17 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 9234f63

Please sign in to comment.