diff --git a/.gitignore b/.gitignore index 36e2213..1e8445d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ tmp/ *.DS_Store dist/ + +semvertool diff --git a/cmd/bump.go b/cmd/bump.go index 5228b04..cb2d8c2 100644 --- a/cmd/bump.go +++ b/cmd/bump.go @@ -46,7 +46,7 @@ var bumpCmd = &cobra.Command{ } func init() { - rootCmd.AddCommand(bumpCmd) + bumpCmd.AddCommand(gitCmd) cf := getCommonBumpFlags() bumpCmd.Flags().AddFlagSet(cf) diff --git a/cmd/git.go b/cmd/git.go index a658900..5e30c08 100644 --- a/cmd/git.go +++ b/cmd/git.go @@ -56,15 +56,26 @@ var gitCmd = &cobra.Command{ Run: runGit, } -func init() { - rootCmd.AddCommand(gitCmd) +var deprecatedGitCmd = &cobra.Command{ + Use: gitCmd.Use, + Short: gitCmd.Short, + Long: gitCmd.Long, + Run: gitCmd.Run, + Deprecated: "and will be removed in a future release. Use 'bump git' instead", +} +func init() { cf := getCommonBumpFlags() gitCmd.Flags().AddFlagSet(cf) gitCmd.Flags().BoolP("hash", "s", false, "Append the short hash (sha) to the version as metadata information.") gitCmd.Flags().BoolP("from-commit", "c", false, "Extract the bump type from a commit message") gitCmd.MarkFlagsMutuallyExclusive("major", "minor", "patch", "prerelease", "from-message", "from-commit") + deprecatedGitCmd.Flags().AddFlagSet(cf) + deprecatedGitCmd.Flags().BoolP("hash", "s", false, "Append the short hash (sha) to the version as metadata information.") + deprecatedGitCmd.Flags().BoolP("from-commit", "c", false, "Extract the bump type from a commit message") + deprecatedGitCmd.MarkFlagsMutuallyExclusive("major", "minor", "patch", "prerelease", "from-message", "from-commit") + } func gitBump(repo *goget.Repository) (*semver.Version, error) { diff --git a/cmd/git_test.go b/cmd/git_test.go index c824aa0..192007c 100644 --- a/cmd/git_test.go +++ b/cmd/git_test.go @@ -131,6 +131,39 @@ func TestInvalidVersionAndRepository(t *testing.T) { assert.Equal(t, expected, resultStrings) } +func TestGetTagsStringsValidVersions(t *testing.T) { + repo, err := setupRepo() + assert.NoError(t, err) + + commit, err := commitFile("file1.txt", repo) + assert.NoError(t, err) + + _, err = repo.CreateTag("v1.0.0", commit, nil) + assert.NoError(t, err) + + commit, err = commitFile("file2.txt", repo) + assert.NoError(t, err) + + _, err = repo.CreateTag("1.0.1", commit, nil) + assert.NoError(t, err) + + expected := []string{"v1.0.0", "1.0.1"} + resultStrings, err := getTagsStrings(repo) + + assert.NoError(t, err) + assert.Equal(t, expected, resultStrings) +} + +func TestGetTagsStringsNoTags(t *testing.T) { + repo, err := setupRepo() + assert.NoError(t, err) + + expected := []string{} + resultStrings, err := getTagsStrings(repo) + assert.NoError(t, err) + assert.Equal(t, expected, resultStrings) +} + func TestGitBumpNoTags(t *testing.T) { repo, err := setupRepo() assert.NoError(t, err) diff --git a/cmd/root.go b/cmd/root.go index d3d8588..27a614d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -38,4 +38,9 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. + rootCmd.AddCommand(deprecatedGitCmd) + rootCmd.AddCommand(bumpCmd) + + // Add the sort subcommand to the root command + rootCmd.AddCommand(SortCmd) } diff --git a/cmd/sort.go b/cmd/sort.go index 9b20f9b..2751f56 100644 --- a/cmd/sort.go +++ b/cmd/sort.go @@ -63,12 +63,8 @@ func RunSort(cmd *cobra.Command, args []string) error { versions = filtered } - ascending := true - // Sort versions - if order == "descending" || order == "dsc" { - // Sort in descending order - ascending = false - } + ascending := order == "ascending" || order == "asc" + sort.SortVersions(semver.Collection(versions), ascending) result := VersionsToStrings(versions) diff --git a/cmd/sort_test.go b/cmd/sort_test.go index 7301f72..e551dc0 100644 --- a/cmd/sort_test.go +++ b/cmd/sort_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSort_RunSortSimple(t *testing.T) { +func TestSortRunSortSimple(t *testing.T) { args := []string{"1.0.0", "2.0.0", "1.0.1"} expected := "1.0.0 1.0.1 2.0.0" @@ -34,7 +34,7 @@ func TestSort_RunSortSimple(t *testing.T) { assert.Equal(t, expected, strings.TrimSuffix(buf.String(), "\n")) } -func TestSort_RunSortDescending(t *testing.T) { +func TestSortRunSortDescending(t *testing.T) { args := []string{"1.0.0", "2.0.0", "1.0.1"} expected := "2.0.0 1.0.1 1.0.0" @@ -61,7 +61,7 @@ func TestSort_RunSortDescending(t *testing.T) { assert.Equal(t, expected, strings.TrimSuffix(buf.String(), "\n")) } -func TestSort_RunSortNoPrerelease(t *testing.T) { +func TestSortRunSortNoPrerelease(t *testing.T) { args := []string{"1.0.0", "2.0.0", "1.0.1-alpha.1"} expected := "1.0.0 2.0.0" @@ -88,7 +88,7 @@ func TestSort_RunSortNoPrerelease(t *testing.T) { assert.Equal(t, expected, strings.TrimSuffix(buf.String(), "\n")) } -// func TestSort_RunSortInvalidTag(t *testing.T) { +// func TestSortRunSortInvalidTag(t *testing.T) { // args := []string{"1.0.0", "2.0.0", "1.0.1-alpha.1", "invalid-tag"} // expected := "invalid semver version: invalid-tag"