aboutsummaryrefslogtreecommitdiffstats
path: root/references.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 /references.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 'references.go')
-rw-r--r--references.go28
1 files changed, 26 insertions, 2 deletions
diff --git a/references.go b/references.go
index 957d741..fc81103 100644
--- a/references.go
+++ b/references.go
@@ -2,6 +2,7 @@ package git
import (
"io"
+ "sort"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
@@ -23,19 +24,42 @@ import (
// - Cherry-picks are not detected unless there are no commits between them and
// therefore can appear repeated in the list. (see git path-id for hints on how
// to fix this).
-func References(c *object.Commit, path string) ([]*object.Commit, error) {
+func references(c *object.Commit, path string) ([]*object.Commit, error) {
var result []*object.Commit
seen := make(map[plumbing.Hash]struct{}, 0)
if err := walkGraph(&result, &seen, c, path); err != nil {
return nil, err
}
- object.SortCommits(result)
+ // TODO result should be returned without ordering
+ sortCommits(result)
// for merges of identical cherry-picks
return removeComp(path, result, equivalent)
}
+type commitSorterer struct {
+ l []*object.Commit
+}
+
+func (s commitSorterer) Len() int {
+ return len(s.l)
+}
+
+func (s commitSorterer) Less(i, j int) bool {
+ return s.l[i].Committer.When.Before(s.l[j].Committer.When)
+}
+
+func (s commitSorterer) Swap(i, j int) {
+ s.l[i], s.l[j] = s.l[j], s.l[i]
+}
+
+// SortCommits sorts a commit list by commit date, from older to newer.
+func sortCommits(l []*object.Commit) {
+ s := &commitSorterer{l}
+ sort.Sort(s)
+}
+
// Recursive traversal of the commit graph, generating a linear history of the
// path.
func walkGraph(result *[]*object.Commit, seen *map[plumbing.Hash]struct{}, current *object.Commit, path string) error {