aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorAntonio Jesus Navarro Perez <antonio@sourced.tech>2017-04-10 16:48:40 +0200
committerAntonio Jesus Navarro Perez <antonio@sourced.tech>2017-04-11 11:22:45 +0200
commitbe8d19438ada078a8598e366ab74aa09e4c521cd (patch)
treed9edb36326016ea35d38f68810e9ce70e538e7a0 /repository.go
parent3daede53835e8572b2957a016f068781db646567 (diff)
downloadgo-git-be8d19438ada078a8598e366ab74aa09e4c521cd.tar.gz
Add Repository.Log() method (fix #298)
- CommitIter is now an interface - The old CommitIter implementation is now called StorerCommitIter - CommitWalker and CommitWalkerPost are now iterators (CommitPreIterator and CommitPostIterator). - Remove Commit.History() method. There are so many ways to iterate a commit history, depending of the use case. Now, instead of use the History() method, you must use CommitPreIterator or CommitPostIterator. - Move commitSorterer to references.go because is the only place that it is used, and it must not be used into another place. - Make References method private, it must only be used into blame logic. - Added a TODO into references method, where the sortCommits is used to remove it in a near future.
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go71
1 files changed, 45 insertions, 26 deletions
diff --git a/repository.go b/repository.go
index 9e325e4..d9a1d7e 100644
--- a/repository.go
+++ b/repository.go
@@ -608,6 +608,26 @@ func (r *Repository) Push(o *PushOptions) error {
return remote.Push(o)
}
+// Log returns the commit history from the given LogOptions.
+func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) {
+ 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
+ }
+
+ return object.NewCommitPreIterator(commit), nil
+}
+
// Tags returns all the References from Tags. This method returns all the tag
// types, lightweight, and annotated ones.
func (r *Repository) Tags() (storer.ReferenceIter, error) {
@@ -671,7 +691,7 @@ func (r *Repository) CommitObject(h plumbing.Hash) (*object.Commit, error) {
}
// CommitObjects returns an unsorted CommitIter with all the commits in the repository.
-func (r *Repository) CommitObjects() (*object.CommitIter, error) {
+func (r *Repository) CommitObjects() (object.CommitIter, error) {
iter, err := r.Storer.IterEncodedObjects(plumbing.CommitObject)
if err != nil {
return nil, err
@@ -838,29 +858,28 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err
commit = c
}
case revision.CaretReg:
- history, err := commit.History()
-
- if err != nil {
- return &plumbing.ZeroHash, err
- }
+ history := object.NewCommitPreIterator(commit)
re := item.(revision.CaretReg).Regexp
negate := item.(revision.CaretReg).Negate
var c *object.Commit
- for i := 0; i < len(history); i++ {
- if !negate && re.MatchString(history[i].Message) {
- c = history[i]
-
- break
+ err := history.ForEach(func(hc *object.Commit) error {
+ if !negate && re.MatchString(hc.Message) {
+ c = hc
+ return storer.ErrStop
}
- if negate && !re.MatchString(history[i].Message) {
- c = history[i]
-
- break
+ if negate && !re.MatchString(hc.Message) {
+ c = hc
+ return storer.ErrStop
}
+
+ return nil
+ })
+ if err != nil {
+ return &plumbing.ZeroHash, err
}
if c == nil {
@@ -869,21 +888,21 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err
commit = c
case revision.AtDate:
- history, err := commit.History()
-
- if err != nil {
- return &plumbing.ZeroHash, err
- }
+ history := object.NewCommitPreIterator(commit)
date := item.(revision.AtDate).Date
- var c *object.Commit
- for i := 0; i < len(history); i++ {
- if date.Equal(history[i].Committer.When.UTC()) || history[i].Committer.When.UTC().Before(date) {
- c = history[i]
-
- break
+ var c *object.Commit
+ err := history.ForEach(func(hc *object.Commit) error {
+ if date.Equal(hc.Committer.When.UTC()) || hc.Committer.When.UTC().Before(date) {
+ c = hc
+ return storer.ErrStop
}
+
+ return nil
+ })
+ if err != nil {
+ return &plumbing.ZeroHash, err
}
if c == nil {