Skip to content

Commit

Permalink
fix: handle a repository with no tags
Browse files Browse the repository at this point in the history
If no tags exist yet, return the initial v0.1.0 suggested in the semver
FAQ.
  • Loading branch information
smlx committed Feb 12, 2021
1 parent 53d1df8 commit 58595b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion main.go
Expand Up @@ -32,6 +32,10 @@ func nextVersion(path string) (string, error) {
if err != nil {
return "", fmt.Errorf("couldn't iterate tags: %w", err)
}
if len(tagRefs) == 0 {
// no existing tags
return "v0.1.0", nil
}
// walk commit hashes back from HEAD
commits, err := r.Log(&git.LogOptions{Order: git.LogOrderDFSPost})
if err != nil {
Expand Down Expand Up @@ -59,9 +63,13 @@ func nextVersion(path string) (string, error) {
}
return nil
})
if (err != nil && err != stopIter) || latestTag == "" {
if err != nil && err != stopIter {
return "", fmt.Errorf("couldn't determine latest tag: %w", err)
}
// not tagged yet,
if latestTag == "" {
return "", fmt.Errorf("couldn't determine latest tag")
}
// found a tag: parse, increment, and return.
latestVersion, err := semver.NewVersion(latestTag)
if err != nil {
Expand Down
18 changes: 17 additions & 1 deletion main_test.go
Expand Up @@ -14,38 +14,55 @@ func TestNextVersion(t *testing.T) {
expect string
}{
"none": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "chore: not much"},
}, expect: "v0.1.0"},
"patch": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "fix: minor bug"},
}, expect: "v0.1.1"},
"minor": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat: cool new feature"},
}, expect: "v0.2.0"},
"major": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat: major refactor\nBREAKING CHANGE: new stuff"},
}, expect: "v1.0.0"},
"major fix bang": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "fix!: major bug"},
}, expect: "v1.0.0"},
"major feat bang": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat!: major change"},
}, expect: "v1.0.0"},
"patch with scope": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "fix(lasers): minor bug"},
}, expect: "v0.1.1"},
"minor with scope": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat(phasers): cool new feature"},
}, expect: "v0.2.0"},
"major with scope": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat(blasters): major refactor\nBREAKING CHANGE: new stuff"},
}, expect: "v1.0.0"},
"major fix bang with scope": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "fix(lightsaber)!: major bug"},
}, expect: "v1.0.0"},
"major feat bang with scope": {gitCmds: [][]string{
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat(bowcaster)!: major change"},
}, expect: "v1.0.0"},
"no existing tags feat": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: new change"},
}, expect: "v0.1.0"},
"no existing tags chore": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "chore: boring change"},
}, expect: "v0.1.0"},
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
Expand All @@ -58,7 +75,6 @@ func TestNextVersion(t *testing.T) {
initCmds := [][]string{
{"init"},
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
}
for _, c := range initCmds {
cmd := exec.Command("git", c...)
Expand Down

0 comments on commit 58595b7

Please sign in to comment.