Skip to content

Commit 6e2a59e

Browse files
filipnavaralunny
authored andcommitted
Use commit graph files for listing pages (#7314)
* Experimental support for git commit graph files and bloom filter index Signed-off-by: Filip Navara <[email protected]> * Force vendor of commitgraph Signed-off-by: Filip Navara <[email protected]> * Remove bloom filter experiment and debug prints * Remove old code for building commit graphs * Remove unused function * Remove mmap usage * gofmt * sort vendor/modules.txt * Add copyright header and log commit-graph error
1 parent e728b55 commit 6e2a59e

File tree

14 files changed

+1166
-13
lines changed

14 files changed

+1166
-13
lines changed

modules/git/commit_info.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/emirpasic/gods/trees/binaryheap"
99
"gopkg.in/src-d/go-git.v4/plumbing"
1010
"gopkg.in/src-d/go-git.v4/plumbing/object"
11+
cgobject "gopkg.in/src-d/go-git.v4/plumbing/object/commitgraph"
1112
)
1213

1314
// GetCommitsInfo gets information of all commits that are corresponding to these entries
@@ -19,7 +20,12 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCom
1920
entryPaths[i+1] = entry.Name()
2021
}
2122

22-
c, err := commit.repo.gogitRepo.CommitObject(plumbing.Hash(commit.ID))
23+
commitNodeIndex, commitGraphFile := commit.repo.CommitNodeIndex()
24+
if commitGraphFile != nil {
25+
defer commitGraphFile.Close()
26+
}
27+
28+
c, err := commitNodeIndex.Get(plumbing.Hash(commit.ID))
2329
if err != nil {
2430
return nil, nil, err
2531
}
@@ -69,14 +75,14 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCom
6975
}
7076

7177
type commitAndPaths struct {
72-
commit *object.Commit
78+
commit cgobject.CommitNode
7379
// Paths that are still on the branch represented by commit
7480
paths []string
7581
// Set of hashes for the paths
7682
hashes map[string]plumbing.Hash
7783
}
7884

79-
func getCommitTree(c *object.Commit, treePath string) (*object.Tree, error) {
85+
func getCommitTree(c cgobject.CommitNode, treePath string) (*object.Tree, error) {
8086
tree, err := c.Tree()
8187
if err != nil {
8288
return nil, err
@@ -93,7 +99,7 @@ func getCommitTree(c *object.Commit, treePath string) (*object.Tree, error) {
9399
return tree, nil
94100
}
95101

96-
func getFileHashes(c *object.Commit, treePath string, paths []string) (map[string]plumbing.Hash, error) {
102+
func getFileHashes(c cgobject.CommitNode, treePath string, paths []string) (map[string]plumbing.Hash, error) {
97103
tree, err := getCommitTree(c, treePath)
98104
if err == object.ErrDirectoryNotFound {
99105
// The whole tree didn't exist, so return empty map
@@ -118,16 +124,16 @@ func getFileHashes(c *object.Commit, treePath string, paths []string) (map[strin
118124
return hashes, nil
119125
}
120126

121-
func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) (map[string]*object.Commit, error) {
127+
func getLastCommitForPaths(c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) {
122128
// We do a tree traversal with nodes sorted by commit time
123129
heap := binaryheap.NewWith(func(a, b interface{}) int {
124-
if a.(*commitAndPaths).commit.Committer.When.Before(b.(*commitAndPaths).commit.Committer.When) {
130+
if a.(*commitAndPaths).commit.CommitTime().Before(b.(*commitAndPaths).commit.CommitTime()) {
125131
return 1
126132
}
127133
return -1
128134
})
129135

130-
result := make(map[string]*object.Commit)
136+
resultNodes := make(map[string]cgobject.CommitNode)
131137
initialHashes, err := getFileHashes(c, treePath, paths)
132138
if err != nil {
133139
return nil, err
@@ -145,9 +151,9 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) (m
145151

146152
// Load the parent commits for the one we are currently examining
147153
numParents := current.commit.NumParents()
148-
var parents []*object.Commit
154+
var parents []cgobject.CommitNode
149155
for i := 0; i < numParents; i++ {
150-
parent, err := current.commit.Parent(i)
156+
parent, err := current.commit.ParentNode(i)
151157
if err != nil {
152158
break
153159
}
@@ -174,7 +180,7 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) (m
174180
for i, path := range current.paths {
175181
// The results could already contain some newer change for the same path,
176182
// so don't override that and bail out on the file early.
177-
if result[path] == nil {
183+
if resultNodes[path] == nil {
178184
if pathUnchanged[i] {
179185
// The path existed with the same hash in at least one parent so it could
180186
// not have been changed in this commit directly.
@@ -188,7 +194,7 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) (m
188194
// - We are looking at a merge commit and the hash of the file doesn't
189195
// match any of the hashes being merged. This is more common for directories,
190196
// but it can also happen if a file is changed through conflict resolution.
191-
result[path] = current.commit
197+
resultNodes[path] = current.commit
192198
}
193199
}
194200
}
@@ -222,5 +228,15 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) (m
222228
}
223229
}
224230

231+
// Post-processing
232+
result := make(map[string]*object.Commit)
233+
for path, commitNode := range resultNodes {
234+
var err error
235+
result[path], err = commitNode.Commit()
236+
if err != nil {
237+
return nil, err
238+
}
239+
}
240+
225241
return result, nil
226242
}

modules/git/notes.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
5050
return err
5151
}
5252

53-
lastCommits, err := getLastCommitForPaths(commit, "", []string{commitID})
53+
commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
54+
if commitGraphFile != nil {
55+
defer commitGraphFile.Close()
56+
}
57+
58+
commitNode, err := commitNodeIndex.Get(commit.Hash)
59+
if err != nil {
60+
return nil
61+
}
62+
63+
lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID})
5464
if err != nil {
5565
return err
5666
}

modules/git/repo_commitgraph.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2019 The Gitea Authors.
2+
// All rights reserved.
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
package git
7+
8+
import (
9+
"os"
10+
"path"
11+
12+
gitealog "code.gitea.io/gitea/modules/log"
13+
"gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph"
14+
cgobject "gopkg.in/src-d/go-git.v4/plumbing/object/commitgraph"
15+
)
16+
17+
// CommitNodeIndex returns the index for walking commit graph
18+
func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) {
19+
indexPath := path.Join(r.Path, "objects", "info", "commit-graph")
20+
21+
file, err := os.Open(indexPath)
22+
if err == nil {
23+
var index commitgraph.Index
24+
index, err = commitgraph.OpenFileIndex(file)
25+
if err == nil {
26+
return cgobject.NewGraphCommitNodeIndex(index, r.gogitRepo.Storer), file
27+
}
28+
}
29+
30+
if !os.IsNotExist(err) {
31+
gitealog.Warn("Unable to read commit-graph for %s: %v", r.Path, err)
32+
}
33+
34+
return cgobject.NewObjectCommitNodeIndex(r.gogitRepo.Storer), nil
35+
}

vendor/gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph/commitgraph.go

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph/doc.go

+103
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)