Skip to content

Commit

Permalink
add verify of repository state
Browse files Browse the repository at this point in the history
  • Loading branch information
MrVinkel committed Oct 17, 2024
1 parent 08f78f9 commit 5fced04
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/bump/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ var (
DebugFlag *bool
QuietFlag *bool
DryRun *bool
NoVerify *bool
NoFetch *bool
Prefix *string
)
30 changes: 30 additions & 0 deletions cmd/bump/helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bump

import (
"errors"
"sort"
)

Expand Down Expand Up @@ -37,3 +38,32 @@ func GetLatestVersion(repo *Repo) (*Version, error) {
}
return version, nil
}

func CheckRepositoryStatus(repo *Repo) error {
if *NoVerify {
return nil
}

hasChanages, err := repo.HasChanges()
if err != nil {
return err
}
if hasChanages {
return errors.New("uncommitted changes")
}

if !*NoFetch {
if err = repo.Fetch(); err != nil {
return err
}
}

synced, err := repo.IsSynced()
if err != nil {
return err
}
if !synced {
return errors.New("unpushed changes")
}
return nil
}
10 changes: 8 additions & 2 deletions cmd/bump/major.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func MajorCmd() *cobra.Command {
return &cobra.Command{
Use: "major",
Aliases: []string{"a"},
Aliases: []string{"ma"},
Short: "Bump the major version",
RunE: major,
}
Expand All @@ -20,10 +20,17 @@ func major(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

repo, err := NewRepo(cwd)
if err != nil {
return err
}

err = CheckRepositoryStatus(repo)
if err != nil {
return err
}

version, err := GetLatestVersion(repo)
if err != nil {
return err
Expand All @@ -41,5 +48,4 @@ func major(cmd *cobra.Command, args []string) error {
}

return repo.CreateAndPushTag(newVersion.String())

}
10 changes: 8 additions & 2 deletions cmd/bump/minor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func MinorCmd() *cobra.Command {
return &cobra.Command{
Use: "minor",
Aliases: []string{"m"},
Aliases: []string{"m", "mi"},
Short: "Bump the minor version",
RunE: minor,
}
Expand All @@ -20,10 +20,17 @@ func minor(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

repo, err := NewRepo(cwd)
if err != nil {
return err
}

err = CheckRepositoryStatus(repo)
if err != nil {
return err
}

version, err := GetLatestVersion(repo)
if err != nil {
return err
Expand All @@ -41,5 +48,4 @@ func minor(cmd *cobra.Command, args []string) error {
}

return repo.CreateAndPushTag(newVersion.String())

}
12 changes: 9 additions & 3 deletions cmd/bump/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func PatchCmd() *cobra.Command {
return &cobra.Command{
Use: "patch",
Aliases: []string{"p"},
Aliases: []string{"p", "pa"},
Short: "Bump the patch version",
RunE: Patch,
}
Expand All @@ -20,10 +20,17 @@ func Patch(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

repo, err := NewRepo(cwd)
if err != nil {
return err
}

err = CheckRepositoryStatus(repo)
if err != nil {
return err
}

version, err := GetLatestVersion(repo)
if err != nil {
return err
Expand All @@ -36,10 +43,9 @@ func Patch(cmd *cobra.Command, args []string) error {
Info("%s -> %s\n", version.String(), newVersion.String())

if *DryRun {
Info("dry run, not creating tags\n")
Info("dry run, will not create tag\n")
return nil
}

return repo.CreateAndPushTag(newVersion.String())

}
54 changes: 54 additions & 0 deletions cmd/bump/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bump

import (
"fmt"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
Expand Down Expand Up @@ -57,3 +58,56 @@ func (r *Repo) CreateAndPushTag(tag string) error {
}
return r.PushTag(tag)
}

func (r *Repo) Fetch() error {
err := r.repo.Fetch(&git.FetchOptions{})
if err != nil && err != git.NoErrAlreadyUpToDate {
return err
}
return nil
}

func (r *Repo) HasChanges() (bool, error) {
w, err := r.repo.Worktree()
if err != nil {
return false, err
}
status, err := w.Status()
if err != nil {
return false, err
}
return !status.IsClean(), nil
}

func (r *Repo) IsSynced() (bool, error) {
head, err := r.repo.Head()
if err != nil {
return false, err
}
name := strings.TrimPrefix(head.Name().String(), "refs/heads/")

refs, err := r.repo.References()
if err != nil {
return false, err
}
// find remote branch
var remote *plumbing.Reference
err = refs.ForEach(func(ref *plumbing.Reference) error {
if !ref.Name().IsRemote() {
return nil
}
if strings.TrimPrefix(ref.Name().String(), "refs/remotes/origin/") == name {
remote = ref
}
return nil
})

if remote == nil {
return false, fmt.Errorf("remote branch for %s not found", name)
}
if remote.Hash() != head.Hash() {
return false, nil
}

return true, err
}
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func main() {

bump.DebugFlag = root.PersistentFlags().BoolP("debug", "d", false, "Debug mode")
bump.QuietFlag = root.PersistentFlags().BoolP("quiet", "q", false, "Quiet - only output errors")
bump.DryRun = root.PersistentFlags().BoolP("dry-run", "n", false, "Dry run mode")
bump.DryRun = root.PersistentFlags().BoolP("dry-run", "x", false, "Do not create tags, only print what would be done")
bump.NoVerify = root.PersistentFlags().BoolP("no-verify", "n", false, "Do not check repository status before creating tags")
bump.NoFetch = root.PersistentFlags().BoolP("no-fetch", "f", false, "Do not fetch before verifying repository status")
bump.Prefix = root.PersistentFlags().StringP("prefix", "p", "", "Prefix for the version tag")

root.AddCommand(bump.BumpVersionCmd())
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ Available Commands:
Flags:
-d, --debug Debug mode
-n, --dry-run Dry run mode
-x, --dry-run Do not create tags, only print what would be done
-h, --help help for bump
-f, --no-fetch Do not fetch before verifying repository status
-n, --no-verify Do not check repository status before creating tags
-p, --prefix string Prefix for the version tag
-q, --quiet Quiet - only output errors
Expand Down

0 comments on commit 5fced04

Please sign in to comment.