feat: add exclude-to, include-to flag

main
hau 2020-11-10 23:28:58 +07:00
parent 64c90b6d9a
commit b06387dff2
2 changed files with 67 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"log"
"os"
@ -22,10 +23,11 @@ const (
currentPath = "."
changelogFile = "CHANGELOG.md"
fromFlag = "from"
toFlag = "to"
versionFlag = "version"
verboseFlag = "verbose"
fromFlag = "from"
excludeToFlag = "exclude-to"
includeToFlag = "include-to"
versionFlag = "version"
verboseFlag = "verbose"
)
func main() {
@ -42,8 +44,12 @@ func main() {
Usage: "from commit revision",
},
&cli.StringFlag{
Name: toFlag,
Usage: "to commit revision",
Name: excludeToFlag,
Usage: "to commit revision (exclude)",
},
&cli.StringFlag{
Name: includeToFlag,
Usage: "to commit revision (include)",
},
&cli.StringFlag{
Name: versionFlag,
@ -101,14 +107,25 @@ func (a *action) getCommits(c *cli.Context, path string) ([]git.Commit, error) {
fromRev := c.String(fromFlag)
a.log("from revision %s", fromRev)
toRev := c.String(toFlag)
a.log("to revision %s", toRev)
excludeToRev := c.String(excludeToFlag)
a.log("exclude to revision %s", excludeToFlag)
commits, err := r.LogExcludeTo(fromRev, toRev)
if err != nil {
return nil, err
includeToRev := c.String(includeToFlag)
a.log("include to revision %s", includeToFlag)
if excludeToRev != "" && includeToRev != "" {
return nil, errors.New("excludeToFlag and includeToFlag can not appear same time")
}
return commits, nil
if excludeToFlag != "" {
return r.LogExcludeTo(fromRev, excludeToRev)
}
if includeToFlag != "" {
return r.LogIncludeTo(fromRev, includeToFlag)
}
return r.Log(fromRev)
}
func (a *action) getConventionalCommits(c *cli.Context, commits []git.Commit) []convention.Commit {

View File

@ -14,6 +14,7 @@ const (
)
type Repository interface {
Log(fromRev string) ([]Commit, error)
LogExcludeTo(fromRev, toRev string) ([]Commit, error)
LogIncludeTo(fromRev, toRev string) ([]Commit, error)
}
@ -37,6 +38,19 @@ func NewRepository(path string) (Repository, error) {
}, nil
}
func (r *repo) Log(fromRev string) ([]Commit, error) {
if fromRev == "" {
fromRev = head
}
fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev))
if err != nil {
return nil, err
}
return r.log(fromHash)
}
// Get all commits between <from revision> and <to revision> (exclude <to revision>)
func (r *repo) LogExcludeTo(fromRev, toRev string) ([]Commit, error) {
if fromRev == "" {
@ -49,7 +63,7 @@ func (r *repo) LogExcludeTo(fromRev, toRev string) ([]Commit, error) {
}
if toRev == "" {
return r.logWithStopFnFirst(fromHash, nil)
return r.log(fromHash)
}
toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev))
@ -76,7 +90,7 @@ func (r *repo) LogIncludeTo(fromRev, toRev string) ([]Commit, error) {
}
if toRev == "" {
return r.logWithStopFnLast(fromHash, nil)
return r.log(fromHash)
}
toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev))
@ -92,6 +106,28 @@ func (r *repo) LogIncludeTo(fromRev, toRev string) ([]Commit, error) {
return commits, nil
}
func (r *repo) log(fromHash *plumbing.Hash) ([]Commit, error) {
cIter, err := r.r.Log(&git.LogOptions{
From: *fromHash,
})
if err != nil {
return nil, err
}
commits := make([]Commit, 0, defaultCommitCount)
if err := cIter.ForEach(func(c *object.Commit) error {
commit := newCommit(c)
commits = append(commits, commit)
return nil
}); err != nil {
return nil, err
}
return commits, nil
}
func (r *repo) logWithStopFnFirst(fromHash *plumbing.Hash, fn stopFn) ([]Commit, error) {
cIter, err := r.r.Log(&git.LogOptions{
From: *fromHash,