Skip to content

Commit

Permalink
Merge pull request #18 from smlx/no-tags
Browse files Browse the repository at this point in the history
fix: handle a repository with no tags
  • Loading branch information
smlx authored Feb 12, 2021
2 parents 16c0e63 + 58595b7 commit 21d3c7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 21d3c7f

Please sign in to comment.