Skip to content

Commit

Permalink
make bit release bump gracefully handle invalid tags and no prior tags.
Browse files Browse the repository at this point in the history
closes #101
  • Loading branch information
chriswalz committed Mar 1, 2021
1 parent 5bcea9c commit 2a6b683
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
15 changes: 15 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func TestAllBitAndGitSubCommands(t *testing.T) {
}
}

func TestGenBumpedSemVersion(t *testing.T) {
reality, err := GenBumpedSemVersion(`v1.0.1`)
assert.Equal(t, "v1.0.2", reality, "expected minor version to increment by 1")

reality, err = GenBumpedSemVersion(`1.0.1`)
assert.Equal(t, "1.0.2", reality, "expected minor version to increment by 1")

reality, err = GenBumpedSemVersion(`nonincrementaltag`)
assert.Error(t, err, "expected an error since the raw tag is invalid")

_, err = GenBumpedSemVersion("\n")
assert.Error(t, err, "expected an error since there was no tag")

}

func TestParseManPage(t *testing.T) {
reality := parseManPage("rebase")
assert.NotContains(t, reality, "GIT-REBASE(1)")
Expand Down
19 changes: 15 additions & 4 deletions cmd/release.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"log"
"fmt"
"os/exec"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -12,14 +14,23 @@ var releaseCmd = &cobra.Command{
Short: "Commit unstaged changes, bump minor tag, push",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
var err error
version := args[0]
if version == "bump" {
version = GenBumpedSemVersion()
rawGitTagOutput, err := exec.Command("/bin/sh", "-c", `git for-each-ref --format="%(refname:short)" --sort=-authordate --count=1 refs/tags`).CombinedOutput()
if err != nil {
log.Debug().Err(err).Send()
}
version, err = GenBumpedSemVersion(string(rawGitTagOutput))
if err != nil {
fmt.Println(err)
return
}
}
save("")
err := tagCurrentBranch(version)
err = tagCurrentBranch(version)
if err != nil {
log.Println(err)
fmt.Println(err)
return
}
RunInTerminalWithColor("git", []string{"push", "--force-with-lease"})
Expand Down
18 changes: 11 additions & 7 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"github.com/chriswalz/complete/v3"
"os"
Expand Down Expand Up @@ -109,18 +110,21 @@ func toStructuredBranchList(rawBranchData string) []*Branch {
}).([]*Branch)
}

func GenBumpedSemVersion() string {
msg, err := exec.Command("/bin/sh", "-c", `git for-each-ref --format="%(refname:short)" --sort=-authordate --count=1 refs/tags`).CombinedOutput()
if err != nil {
log.Debug().Err(err).Send()
}
out := string(msg)
func GenBumpedSemVersion(out string) (string, error) {
out = strings.TrimSpace(out)

// 0.0.1
if len(out) <= 0 {
return "", errors.New("no tags exists. Consider running `bit release 0.0.1`")
}
i := strings.LastIndex(out, ".")
minor, err := strconv.Atoi(out[i+1:])
if err != nil {
return "", err
}
minor++
out = out[:i] + "." + strconv.Itoa(minor)
return out
return out, nil
}

func AddCommandToShellHistory(cmd string, args []string) {
Expand Down

0 comments on commit 2a6b683

Please sign in to comment.