From 8b8e306f949fe8ddfebc92241d0a7df461d3bef1 Mon Sep 17 00:00:00 2001 From: James Evans Date: Tue, 6 May 2025 10:14:02 -0500 Subject: [PATCH 1/4] feat: Move git command to a subcommand of bump. --- .gitignore | 2 ++ cmd/bump.go | 2 +- cmd/git.go | 23 +++++++++++++++++++++-- cmd/git_test.go | 33 +++++++++++++++++++++++++++++++++ cmd/root.go | 5 +++++ 5 files changed, 62 insertions(+), 3 deletions(-) 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..3fca123 100644 --- a/cmd/git.go +++ b/cmd/git.go @@ -5,6 +5,7 @@ package cmd import ( "fmt" + "os" "github.com/Masterminds/semver/v3" "github.com/spf13/cobra" @@ -56,15 +57,33 @@ var gitCmd = &cobra.Command{ Run: runGit, } -func init() { - rootCmd.AddCommand(gitCmd) +var deprecatedGitCmd = &cobra.Command{ + Use: "git", + Short: "DEPRECATED: " + gitCmd.Short, + Long: ` + DEPRECATED: Use 'bump git' instead. This command will be removed in a future version + + Bump a semver version to the next major, minor, patch, or prerelease version. + + See 'bump git' for more information.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintf(os.Stderr, "DEPRECATED: Use 'bump git' instead. This command will be removed in a future version\n") + runGit(cmd, args) + }, +} +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) } From c18add308ece3b02bd83cb516fd956f0c149b6d7 Mon Sep 17 00:00:00 2001 From: James Evans Date: Tue, 6 May 2025 11:11:57 -0500 Subject: [PATCH 2/4] Fix more test names --- cmd/sort_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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" From 9c8c1e1bacfe8cede80c456849997b3c12b4f644 Mon Sep 17 00:00:00 2001 From: James Evans Date: Tue, 6 May 2025 11:31:12 -0500 Subject: [PATCH 3/4] Use cobra tools to mark the old command as deprecated --- cmd/git.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/cmd/git.go b/cmd/git.go index 3fca123..5e30c08 100644 --- a/cmd/git.go +++ b/cmd/git.go @@ -5,7 +5,6 @@ package cmd import ( "fmt" - "os" "github.com/Masterminds/semver/v3" "github.com/spf13/cobra" @@ -58,18 +57,11 @@ var gitCmd = &cobra.Command{ } var deprecatedGitCmd = &cobra.Command{ - Use: "git", - Short: "DEPRECATED: " + gitCmd.Short, - Long: ` - DEPRECATED: Use 'bump git' instead. This command will be removed in a future version - - Bump a semver version to the next major, minor, patch, or prerelease version. - - See 'bump git' for more information.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Fprintf(os.Stderr, "DEPRECATED: Use 'bump git' instead. This command will be removed in a future version\n") - runGit(cmd, args) - }, + 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() { From 72e7e28f08e930498a92bcd252cc545f8dcc64a0 Mon Sep 17 00:00:00 2001 From: James Evans Date: Tue, 6 May 2025 11:43:50 -0500 Subject: [PATCH 4/4] Update assignment to fix linting error --- cmd/sort.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) 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)