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

View File

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