aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorkuba-- <kuba@sourced.tech>2019-01-09 14:45:56 +0100
committerkuba-- <kuba@sourced.tech>2019-01-10 00:35:35 +0100
commitc9609eb875fea54ca9ffe409d1581ef94a38b6bc (patch)
tree19161a599c8f9bdfbfefa88ae02ee9381bf57fcc /repository.go
parentf65d7f4c42cccd3a1daaef3ce3c3a5cfe006d3c4 (diff)
downloadgo-git-c9609eb875fea54ca9ffe409d1581ef94a38b6bc.tar.gz
Refine Log.
Signed-off-by: kuba-- <kuba@sourced.tech>
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go99
1 files changed, 59 insertions, 40 deletions
diff --git a/repository.go b/repository.go
index 6be07c2..de92d64 100644
--- a/repository.go
+++ b/repository.go
@@ -1027,64 +1027,83 @@ func (r *Repository) PushContext(ctx context.Context, o *PushOptions) error {
// Log returns the commit history from the given LogOptions.
func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) {
+ fn := commitIterFunc(o.Order)
+ if fn == nil {
+ return nil, fmt.Errorf("invalid Order=%v", o.Order)
+ }
+
var (
- err error
- commitIterFunc func(*object.Commit) object.CommitIter
- commitIter object.CommitIter
+ it object.CommitIter
+ err error
)
- switch o.Order {
+ if o.All {
+ it, err = r.logAll(fn)
+ } else {
+ it, err = r.log(o.From, fn)
+ }
+
+ if err != nil {
+ return nil, err
+ }
+
+ if o.FileName != nil {
+ // for `git log --all` also check parent (if the next commit comes from the real parent)
+ it = r.logWithFile(*o.FileName, it, o.All)
+ }
+
+ return it, nil
+}
+
+func (r *Repository) log(from plumbing.Hash, commitIterFunc func(*object.Commit) object.CommitIter) (object.CommitIter, error) {
+ h := from
+ if from == plumbing.ZeroHash {
+ head, err := r.Head()
+ if err != nil {
+ return nil, err
+ }
+
+ h = head.Hash()
+ }
+
+ commit, err := r.CommitObject(h)
+ if err != nil {
+ return nil, err
+ }
+ return commitIterFunc(commit), nil
+}
+
+func (r *Repository) logAll(commitIterFunc func(*object.Commit) object.CommitIter) (object.CommitIter, error) {
+ return object.NewCommitAllIter(r.Storer, commitIterFunc)
+}
+
+func (*Repository) logWithFile(fileName string, commitIter object.CommitIter, checkParent bool) object.CommitIter {
+ return object.NewCommitFileIterFromIter(fileName, commitIter, checkParent)
+}
+
+func commitIterFunc(order LogOrder) func(c *object.Commit) object.CommitIter {
+ switch order {
case LogOrderDefault:
- commitIterFunc = func(c *object.Commit) object.CommitIter {
+ return func(c *object.Commit) object.CommitIter {
return object.NewCommitPreorderIter(c, nil, nil)
}
case LogOrderDFS:
- commitIterFunc = func(c *object.Commit) object.CommitIter {
+ return func(c *object.Commit) object.CommitIter {
return object.NewCommitPreorderIter(c, nil, nil)
}
case LogOrderDFSPost:
- commitIterFunc = func(c *object.Commit) object.CommitIter {
+ return func(c *object.Commit) object.CommitIter {
return object.NewCommitPostorderIter(c, nil)
}
case LogOrderBSF:
- commitIterFunc = func(c *object.Commit) object.CommitIter {
+ return func(c *object.Commit) object.CommitIter {
return object.NewCommitIterBSF(c, nil, nil)
}
case LogOrderCommitterTime:
- commitIterFunc = func(c *object.Commit) object.CommitIter {
+ return func(c *object.Commit) object.CommitIter {
return object.NewCommitIterCTime(c, nil, nil)
}
- default:
- return nil, fmt.Errorf("invalid Order=%v", o.Order)
}
-
- if o.All {
- commitIter, err = object.NewCommitAllIter(r.Storer, commitIterFunc)
- if err != nil {
- return nil, err
- }
- } else {
- h := o.From
- if o.From == plumbing.ZeroHash {
- head, err := r.Head()
- if err != nil {
- return nil, err
- }
-
- h = head.Hash()
- }
-
- commit, err := r.CommitObject(h)
- if err != nil {
- return nil, err
- }
- commitIter = commitIterFunc(commit)
- }
-
- if o.FileName != nil {
- commitIter = object.NewCommitFileIterFromIter(*o.FileName, commitIter, o.All)
- }
-
- return commitIter, nil
+ return nil
}
// Tags returns all the tag References in a repository.