From 2dd6ffd1eb4f68d242ce5fc1b0087140f3756bcd Mon Sep 17 00:00:00 2001 From: hau Date: Tue, 5 Jan 2021 14:16:00 +0700 Subject: [PATCH] feat: only use --to to replace --exclude-to, --include-to --- main.go | 28 +++++------------------ pkg/git/repository.go | 52 ------------------------------------------- 2 files changed, 5 insertions(+), 75 deletions(-) diff --git a/main.go b/main.go index f370b33..b3c95db 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/pkg/git/repository.go b/pkg/git/repository.go index 7a6a8f8..5b8d29e 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -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 and (exclude ) -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 and (include ) 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,