aboutsummaryrefslogtreecommitdiffstats
path: root/references.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-04-11 21:28:19 +0200
committerGitHub <noreply@github.com>2017-04-11 21:28:19 +0200
commit9b45f468c61a0756dd19d09b64c2b1a88cc99ec5 (patch)
tree0e513815f1ef3765538c19b893ab4990264ae46d /references.go
parent3daede53835e8572b2957a016f068781db646567 (diff)
parentefef1479b7948e4f9f310446cfdf360f2cb09596 (diff)
downloadgo-git-9b45f468c61a0756dd19d09b64c2b1a88cc99ec5.tar.gz
Merge pull request #337 from ajnavarro/repository-log
Add Repository.Log() method (fix #298)
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 {