Skip to content

Commit

Permalink
Add prerelease-prefix parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
thorgull authored and choffmeister committed Jan 25, 2022
1 parent d4b1ebe commit faa8a92
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Execute(version FullVersion) error {
fallbackFlag := flag.String("fallback", "", "The first version to fallback to should there be no tag")
dropPrefixFlag := flag.Bool("drop-prefix", false, "Drop prefix from output")
prereleaseSuffixFlag := flag.String("prerelease-suffix", "", "Suffix to add to prereleases")
prereleasePrefixFlag := flag.String("prerelease-prefix", "dev", "Prefix to use as start of prerelease (default to \"dev\"))")
formatFlag := flag.String("format", "", "Format of output")
flag.Parse()

Expand All @@ -41,6 +42,7 @@ func Execute(version FullVersion) error {
FallbackTagName: *fallbackFlag,
DropTagNamePrefix: *dropPrefixFlag,
PrereleaseSuffix: *prereleaseSuffixFlag,
PrereleasePrefix: *prereleasePrefixFlag,
Format: *formatFlag,
}
result, err := run(dir, opts)
Expand Down
8 changes: 4 additions & 4 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ func TestRun(t *testing.T) {
assert := assert.New(t)
dir, _ := ioutil.TempDir("", "example")
author := object.Signature{Name: "Test", Email: "[email protected]"}
_, err := run(dir, internal.GenerateVersionOptions{})
_, err := run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"})
assert.Error(err)

repo, _ := git.PlainInit(dir, false)
worktree, _ := repo.Worktree()
_, err = run(dir, internal.GenerateVersionOptions{})
_, err = run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"})
assert.Error(err)

commit1, _ := worktree.Commit("first", &git.CommitOptions{Author: &author})
repo.CreateTag("invalid", commit1, nil)
_, err = run(dir, internal.GenerateVersionOptions{})
_, err = run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"})
assert.Error(err)

commit2, _ := worktree.Commit("first", &git.CommitOptions{Author: &author})
repo.CreateTag("v1.0.0", commit2, nil)

commit3, _ := worktree.Commit("second", &git.CommitOptions{Author: &author})
result, err := run(dir, internal.GenerateVersionOptions{})
result, err := run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"})
assert.NoError(err)
assert.Equal("v1.0.1-dev.1.g"+commit3.String()[0:7], *result)
}
3 changes: 2 additions & 1 deletion internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ type GenerateVersionOptions struct {
FallbackTagName string
DropTagNamePrefix bool
PrereleaseSuffix string
PrereleasePrefix string
Format string
}

// GenerateVersion ...
func GenerateVersion(tagName string, counter int, headHash string, opts GenerateVersionOptions) (*string, error) {
devPrerelease := []string{"dev", strconv.Itoa(counter), "g" + (headHash)[0:7]}
devPrerelease := []string{opts.PrereleasePrefix, strconv.Itoa(counter), "g" + (headHash)[0:7]}
if opts.PrereleaseSuffix != "" {
devPrerelease[len(devPrerelease)-1] = devPrerelease[len(devPrerelease)-1] + "-" + opts.PrereleaseSuffix
}
Expand Down
33 changes: 18 additions & 15 deletions internal/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@ func TestGenerateVersion(t *testing.T) {
}
}

test("0.0.0", 0, "abc1234", GenerateVersionOptions{}, "0.0.0")
test("0.0.0", 1, "abc1234", GenerateVersionOptions{}, "0.0.1-dev.1.gabc1234")
test("0.0.0-rc1", 1, "abc1234", GenerateVersionOptions{}, "0.0.0-rc1.dev.1.gabc1234")
test("0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{}, "0.0.0-rc.1.dev.1.gabc1234")
test("0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{}, "0.0.0-rc.1.dev.1.gabc1234+foobar")
test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{}, "v0.0.0-rc.1.dev.1.gabc1234+foobar")
test("0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0")
test("0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.1-dev.1.gabc1234")
test("0.0.0-rc1", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc1.dev.1.gabc1234")
test("0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234")
test("0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234+foobar")
test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "v0.0.0-rc.1.dev.1.gabc1234+foobar")

test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "0.0.0"}, "0.0.0-dev.1.gabc1234")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0"}, "v0.0.0-dev.1.gabc1234")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "0.0.0", PrereleasePrefix: "dev"}, "0.0.0-dev.1.gabc1234")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", PrereleasePrefix: "dev"}, "v0.0.0-dev.1.gabc1234")

test("v0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT"}, "v0.0.0")
test("v0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT"}, "v0.0.1-dev.1.gabc1234-SNAPSHOT")
test("v0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT", PrereleasePrefix: "dev"}, "v0.0.0")
test("v0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT", PrereleasePrefix: "dev"}, "v0.0.1-dev.1.gabc1234-SNAPSHOT")

test("v0.0.0", 0, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true}, "0.0.0")
test("v0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true}, "0.0.0-rc.1.dev.1.gabc1234")
test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true}, "0.0.0-rc.1.dev.1.gabc1234+foobar")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", DropTagNamePrefix: true}, "0.0.0-dev.1.gabc1234")
test("v0.0.0", 0, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0")
test("v0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234")
test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234+foobar")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0-dev.1.gabc1234")

_, err := GenerateVersion("", 1, "abc1234", GenerateVersionOptions{})
test("0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleasePrefix: "custom"}, "0.0.0")
test("0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "custom"}, "0.0.1-custom.1.gabc1234")

_, err := GenerateVersion("", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"})
assert.Error(err)
}

0 comments on commit faa8a92

Please sign in to comment.