Skip to content

Commit 5baa649

Browse files
committed
Use the treePath to optimize commit info search.
Signed-off-by: Filip Navara <[email protected]>
1 parent 8a211df commit 5baa649

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

modules/git/commit_info.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package git
66

77
import (
8-
"path"
9-
108
"gopkg.in/src-d/go-git.v4/plumbing"
119
"gopkg.in/src-d/go-git.v4/plumbing/object"
1210
"gopkg.in/src-d/go-git.v4/plumbing/storer"
@@ -16,15 +14,15 @@ import (
1614
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCommitCache) ([][]interface{}, error) {
1715
entryPaths := make([]string, len(tes))
1816
for i, entry := range tes {
19-
entryPaths[i] = path.Join(treePath, entry.Name())
17+
entryPaths[i] = entry.Name()
2018
}
2119

2220
c, err := commit.repo.gogitRepo.CommitObject(plumbing.Hash(commit.ID))
2321
if err != nil {
2422
return nil, err
2523
}
2624

27-
revs, err := getLastCommitForPaths(c, entryPaths)
25+
revs, err := getLastCommitForPaths(c, treePath, entryPaths)
2826
if err != nil {
2927
return nil, err
3028
}
@@ -45,7 +43,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCom
4543
return commitsInfo, nil
4644
}
4745

48-
func getLastCommitForPaths(c *object.Commit, paths []string) ([]*object.Commit, error) {
46+
func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) ([]*object.Commit, error) {
4947
cIter := object.NewCommitIterCTime(c, nil, nil)
5048
result := make([]*object.Commit, len(paths))
5149
remainingResults := len(paths)
@@ -55,6 +53,14 @@ func getLastCommitForPaths(c *object.Commit, paths []string) ([]*object.Commit,
5553
return nil, err
5654
}
5755

56+
if treePath != "" {
57+
cTree, err = cTree.Tree(treePath)
58+
if err != nil {
59+
return nil, err
60+
}
61+
}
62+
lastTreeHash := cTree.Hash
63+
5864
currentEntryHashes := make([]plumbing.Hash, len(paths))
5965
for i, path := range paths {
6066
cEntry, err := cTree.FindEntry(path)
@@ -73,6 +79,21 @@ func getLastCommitForPaths(c *object.Commit, paths []string) ([]*object.Commit,
7379
return err
7480
}
7581

82+
if treePath != "" {
83+
parentTree, err = parentTree.Tree(treePath)
84+
// the whole tree doesn't exist
85+
if err != nil {
86+
return nil
87+
}
88+
}
89+
90+
// bail-out early if this tree branch was not changed in the commit
91+
if lastTreeHash == parentTree.Hash {
92+
copy(newEntryHashes, currentEntryHashes)
93+
return nil
94+
}
95+
lastTreeHash = parentTree.Hash
96+
7697
for i, path := range paths {
7798
// skip path if we already found it
7899
if currentEntryHashes[i] != plumbing.ZeroHash {

0 commit comments

Comments
 (0)