Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var allowDirty bool
var currentTag string
var latestTag bool
var dryRun bool
var onlyAncestors bool

var rootCmd = &cobra.Command{
Use: "bump",
Expand All @@ -52,6 +53,7 @@ func init() {
rootCmd.PersistentFlags().BoolVar(&allowDirty, "allow-dirty", false, "allow usage of bump on dirty git")
rootCmd.PersistentFlags().BoolVar(&latestTag, "latest-tag", true, "use latest tag, prompt tags if false")
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Don't touch git repository")
rootCmd.PersistentFlags().BoolVar(&onlyAncestors, "only-ancestors", true, "Ignore tags which are not ancestors of HEAD")

}

Expand All @@ -68,7 +70,7 @@ func preRun(cmd *cobra.Command, args []string) {
}

if !latestTag {
tags, err := g.Tags()
tags, err := g.Tags(onlyAncestors)
if err != nil {
log.Fatalf("error tags: %s", err)
}
Expand All @@ -85,7 +87,7 @@ func preRun(cmd *cobra.Command, args []string) {
}
fmt.Printf("You choose %q\n", currentTag)
} else {
currentTag, err = g.LatestTag()
currentTag, err = g.LatestTag(onlyAncestors)
if err != nil {
log.Fatalf("Can't get latest tag: %s", err)
}
Expand Down
62 changes: 49 additions & 13 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,34 @@ func (g *Git) CurrentCommit() (string, error) {
return headSha, nil
}

func (g *Git) Tags() ([]string, error) {
tags, _, err := g.tags()
func (g *Git) currentCommitObject() (*object.Commit, error) {
var headCommit *object.Commit

headRef, err := g.repository.Head()
if err != nil {
return headCommit, err
}
headHash := headRef.Hash()
headCommit, err = g.repository.CommitObject(headHash)

if err != nil {
return headCommit, err
}

return headCommit, nil
}

func (g *Git) Tags(onlyAncestors bool) ([]string, error) {
tags, _, err := g.tags(onlyAncestors)
return tags, err
}

func (g *Git) LatestTag() (string, error) {
_, tag, err := g.tags()
func (g *Git) LatestTag(onlyAncestors bool) (string, error) {
_, tag, err := g.tags(onlyAncestors)
return tag, err
}

func (g *Git) tags() ([]string, string, error) {
func (g *Git) tags(onlyAncestors bool) ([]string, string, error) {
var latestTagName string
var tags []string
var latestTagCommit *object.Commit
Expand All @@ -122,6 +139,11 @@ func (g *Git) tags() ([]string, string, error) {
return tags, latestTagName, err
}

headCommit, err := g.currentCommitObject()
if err != nil {
return tags, latestTagName, err
}

err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
revision := plumbing.Revision(tagRef.Name().String())
tagCommitHash, err := g.repository.ResolveRevision(revision)
Expand All @@ -133,16 +155,30 @@ func (g *Git) tags() ([]string, string, error) {
if err != nil {
return err
}
tags = append(tags, tagRef.Name().Short())

if latestTagCommit == nil {
latestTagCommit = commit
latestTagName = tagRef.Name().Short()
}
useTag := true

if commit.Committer.When.After(latestTagCommit.Committer.When) {
latestTagCommit = commit
latestTagName = tagRef.Name().Short()
if onlyAncestors {
isAncestor, err := commit.IsAncestor(headCommit)
if err != nil {
return err
}
if !isAncestor {
useTag = false
}
}
if useTag {
tags = append(tags, tagRef.Name().Short())

if latestTagCommit == nil {
latestTagCommit = commit
latestTagName = tagRef.Name().Short()
}

if commit.Committer.When.After(latestTagCommit.Committer.When) {
latestTagCommit = commit
latestTagName = tagRef.Name().Short()
}
}

return nil
Expand Down