feat: only use --to to replace --exclude-to, --include-to

main
hau 2021-01-05 14:16:00 +07:00
parent ed8e4704e3
commit 2dd6ffd1eb
2 changed files with 5 additions and 75 deletions

28
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
@ -32,8 +31,7 @@ const (
defaultVersion = "0.1.0"
fromFlag = "from"
excludeToFlag = "exclude-to"
includeToFlag = "include-to"
toFlag = "to"
versionFlag = "version"
repositoryFlag = "repository"
outputDirFlag = "output-dir"
@ -57,11 +55,7 @@ func main() {
Usage: "generate from `COMMIT`",
},
&cli.StringFlag{
Name: excludeToFlag,
Usage: "generate to `COMMIT` (exclude that commit)",
},
&cli.StringFlag{
Name: includeToFlag,
Name: toFlag,
Usage: "generate to `COMMIT` (include that commit)",
},
&cli.StringFlag{
@ -140,8 +134,7 @@ func (a *action) Run(c *cli.Context) error {
func (a *action) getFlags(c *cli.Context) {
a.debug = c.Bool(debugFlag)
a.flags[fromFlag] = c.String(fromFlag)
a.flags[excludeToFlag] = c.String(excludeToFlag)
a.flags[includeToFlag] = c.String(includeToFlag)
a.flags[toFlag] = c.String(toFlag)
a.flags[versionFlag] = a.getFlagValue(c, versionFlag, defaultVersion)
a.flags[repositoryFlag] = a.getFlagValue(c, repositoryFlag, defaultRepositry)
a.flags[outputDirFlag] = a.getFlagValue(c, outputDirFlag, defaultOutputDir)
@ -170,19 +163,8 @@ func (a *action) getCommits() ([]git.Commit, error) {
fromRev := a.flags[fromFlag]
a.logDebug("from revision %s", fromRev)
excludeToRev := a.flags[excludeToFlag]
a.logDebug("exclude to revision %s", excludeToRev)
includeToRev := a.flags[includeToFlag]
a.logDebug("include to revision %s", includeToRev)
if excludeToRev != "" && includeToRev != "" {
return nil, errors.New("excludeToFlag and includeToFlag can not appear same time")
}
if excludeToRev != "" {
return r.LogExcludeTo(fromRev, excludeToRev)
}
includeToRev := a.flags[toFlag]
a.logDebug("to revision %s", includeToRev)
if includeToRev != "" {
return r.LogIncludeTo(fromRev, includeToRev)

View File

@ -17,7 +17,6 @@ const (
type Repository interface {
Log(fromRev string) ([]Commit, error)
LogExcludeTo(fromRev, toRev string) ([]Commit, error)
LogIncludeTo(fromRev, toRev string) ([]Commit, error)
}
@ -54,29 +53,6 @@ func (r *repo) Log(fromRev string) ([]Commit, error) {
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 == "" {
fromRev = head
}
fromHash, err := r.r.ResolveRevision(plumbing.Revision(fromRev))
if err != nil {
return nil, fmt.Errorf("failed to resolve %s: %w", fromRev, err)
}
if toRev == "" {
return r.log(fromHash)
}
toHash, err := r.r.ResolveRevision(plumbing.Revision(toRev))
if err != nil {
return nil, fmt.Errorf("failed to resolve %s: %w", toRev, err)
}
return r.logWithStopFnFirst(fromHash, stopAtHash(toHash))
}
// Get all commits between <from revision> and <to revision> (include <to revision>)
func (r *repo) LogIncludeTo(fromRev, toRev string) ([]Commit, error) {
if fromRev == "" {
@ -122,34 +98,6 @@ func (r *repo) log(fromHash *plumbing.Hash) ([]Commit, error) {
return commits, nil
}
func (r *repo) logWithStopFnFirst(fromHash *plumbing.Hash, fn stopFn) ([]Commit, error) {
cIter, err := r.r.Log(&git.LogOptions{
From: *fromHash,
})
if err != nil {
return nil, fmt.Errorf("failed to git log: %w", err)
}
commits := make([]Commit, 0, defaultCommitCount)
if err := cIter.ForEach(func(c *object.Commit) error {
if fn != nil {
if err := fn(c); err != nil {
return err
}
}
commit := newCommit(c)
commits = append(commits, commit)
return nil
}); err != nil {
return nil, fmt.Errorf("failed to iterate each git log: %w", err)
}
return commits, nil
}
func (r *repo) logWithStopFnLast(fromHash *plumbing.Hash, fn stopFn) ([]Commit, error) {
cIter, err := r.r.Log(&git.LogOptions{
From: *fromHash,