Skip to content

Commit eb1cb66

Browse files
committed
Extract the latest commit at treePath along with the other commits.
Signed-off-by: Filip Navara <[email protected]>
1 parent 5baa649 commit eb1cb66

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

modules/git/commit_info.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import (
1111
)
1212

1313
// GetCommitsInfo gets information of all commits that are corresponding to these entries
14-
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCommitCache) ([][]interface{}, error) {
14+
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCommitCache) ([][]interface{}, *Commit, error) {
1515
entryPaths := make([]string, len(tes))
1616
for i, entry := range tes {
1717
entryPaths[i] = entry.Name()
1818
}
1919

2020
c, err := commit.repo.gogitRepo.CommitObject(plumbing.Hash(commit.ID))
2121
if err != nil {
22-
return nil, err
22+
return nil, nil, err
2323
}
2424

25-
revs, err := getLastCommitForPaths(c, treePath, entryPaths)
25+
revs, treeCommit, err := getLastCommitForPaths(c, treePath, entryPaths)
2626
if err != nil {
27-
return nil, err
27+
return nil, nil, err
2828
}
2929

3030
commit.repo.gogitStorage.Close()
@@ -40,23 +40,45 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCom
4040
}
4141
commitsInfo[i] = []interface{}{entry, commit}
4242
}
43-
return commitsInfo, nil
43+
return commitsInfo, convertCommit(treeCommit), nil
4444
}
4545

46-
func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) ([]*object.Commit, error) {
46+
func convertCommit(c *object.Commit) *Commit {
47+
commiter := Signature(c.Committer)
48+
author := Signature(c.Author)
49+
50+
var pgpSignaure *CommitGPGSignature
51+
if c.PGPSignature != "" {
52+
pgpSignaure = &CommitGPGSignature{
53+
Signature: c.PGPSignature,
54+
Payload: c.Message, // FIXME: This is not correct
55+
}
56+
}
57+
58+
return &Commit{
59+
ID: SHA1(c.Hash),
60+
CommitMessage: c.Message,
61+
Committer: &commiter,
62+
Author: &author,
63+
Signature: pgpSignaure,
64+
}
65+
}
66+
67+
func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) ([]*object.Commit, *object.Commit, error) {
4768
cIter := object.NewCommitIterCTime(c, nil, nil)
4869
result := make([]*object.Commit, len(paths))
70+
var resultTree *object.Commit
4971
remainingResults := len(paths)
5072

5173
cTree, err := c.Tree()
5274
if err != nil {
53-
return nil, err
75+
return nil, nil, err
5476
}
5577

5678
if treePath != "" {
5779
cTree, err = cTree.Tree(treePath)
5880
if err != nil {
59-
return nil, err
81+
return nil, nil, err
6082
}
6183
}
6284
lastTreeHash := cTree.Hash
@@ -65,7 +87,7 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) ([
6587
for i, path := range paths {
6688
cEntry, err := cTree.FindEntry(path)
6789
if err != nil {
68-
return nil, err
90+
return nil, nil, err
6991
}
7092
currentEntryHashes[i] = cEntry.Hash
7193
}
@@ -91,6 +113,9 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) ([
91113
if lastTreeHash == parentTree.Hash {
92114
copy(newEntryHashes, currentEntryHashes)
93115
return nil
116+
} else if resultTree == nil {
117+
// save the latest commit that updated treePath
118+
resultTree = current
94119
}
95120
lastTreeHash = parentTree.Hash
96121

@@ -136,5 +161,5 @@ func getLastCommitForPaths(c *object.Commit, treePath string, paths []string) ([
136161
return nil
137162
})
138163

139-
return result, nil
164+
return result, resultTree, nil
140165
}

routers/repo/view.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func renderDirectory(ctx *context.Context, treeLink string) {
4949
}
5050
entries.CustomSort(base.NaturalSortLess)
5151

52-
ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath, nil)
52+
var latestCommit *git.Commit
53+
ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath, nil)
5354
if err != nil {
5455
ctx.ServerError("GetCommitsInfo", err)
5556
return
@@ -178,14 +179,6 @@ func renderDirectory(ctx *context.Context, treeLink string) {
178179

179180
// Show latest commit info of repository in table header,
180181
// or of directory if not in root directory.
181-
latestCommit := ctx.Repo.Commit
182-
if len(ctx.Repo.TreePath) > 0 {
183-
latestCommit, err = ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
184-
if err != nil {
185-
ctx.ServerError("GetCommitByPath", err)
186-
return
187-
}
188-
}
189182
ctx.Data["LatestCommit"] = latestCommit
190183
ctx.Data["LatestCommitVerification"] = models.ParseCommitWithSignature(latestCommit)
191184
ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)

0 commit comments

Comments
 (0)