Skip to content

Commit 7162f0c

Browse files
committed
Use gitrepo.GetTreePathLatestCommit to get file lastest commit instead from cache
1 parent 594edad commit 7162f0c

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

modules/gitrepo/tree_path.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/git"
10+
)
11+
12+
func GetTreePathLatestCommit(ctx context.Context, repo Repository, commitID, treePath string) (*git.Commit, error) {
13+
gitRepo, closer, err := RepositoryFromContextOrOpen(ctx, repo)
14+
if err != nil {
15+
return nil, err
16+
}
17+
defer closer.Close()
18+
19+
stdout, _, err := git.NewCommand(ctx, "rev-list", "-1").
20+
AddDynamicArguments(commitID).AddArguments("--").AddDynamicArguments(treePath).
21+
RunStdString(&git.RunOpts{Dir: repoPath(repo)})
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
return gitRepo.GetCommit(stdout)
27+
}

modules/gitrepo/tree_path_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo_test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
_ "code.gitea.io/gitea/models/actions"
11+
_ "code.gitea.io/gitea/models/activities"
12+
_ "code.gitea.io/gitea/models/perm/access"
13+
repo_model "code.gitea.io/gitea/models/repo"
14+
"code.gitea.io/gitea/models/unittest"
15+
"code.gitea.io/gitea/modules/gitrepo"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func TestMain(m *testing.M) {
21+
unittest.MainTest(m)
22+
}
23+
24+
func Test_GetTreePathLatestCommit(t *testing.T) {
25+
assert.NoError(t, unittest.PrepareTestDatabase())
26+
27+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
28+
commitID, err := gitrepo.GetBranchCommitID(context.Background(), repo, repo.DefaultBranch)
29+
assert.NoError(t, err)
30+
assert.EqualValues(t, "1032bbf17fbc0d9c95bb5418dabe8f8c99278700", commitID)
31+
32+
commit, err := gitrepo.GetTreePathLatestCommit(context.Background(), repo, repo.DefaultBranch, "Home.md")
33+
assert.NoError(t, err)
34+
assert.NotNil(t, commit)
35+
assert.EqualValues(t, "2c54faec6c45d31c1abfaecdab471eac6633738a", commit.ID.String())
36+
}

routers/api/v1/repo/file.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
"io"
1313
"net/http"
14-
"path"
1514
"strings"
1615
"time"
1716

@@ -242,19 +241,14 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn
242241
return nil, nil, nil
243242
}
244243

245-
info, _, err := git.Entries([]*git.TreeEntry{entry}).GetCommitsInfo(ctx, ctx.Repo.Commit, path.Dir("/" + ctx.Repo.TreePath)[1:])
244+
latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath)
246245
if err != nil {
247-
ctx.Error(http.StatusInternalServerError, "GetCommitsInfo", err)
246+
ctx.Error(http.StatusInternalServerError, "GetTreePathLatestCommit", err)
248247
return nil, nil, nil
249248
}
249+
when := &latestCommit.Committer.When
250250

251-
if len(info) == 1 {
252-
// Not Modified
253-
lastModified = &info[0].Commit.Committer.When
254-
}
255-
blob = entry.Blob()
256-
257-
return blob, entry, lastModified
251+
return entry.Blob(), entry, when
258252
}
259253

260254
// GetArchive get archive of a repository

routers/web/repo/download.go

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

77
import (
8-
"path"
98
"time"
109

1110
git_model "code.gitea.io/gitea/models/git"
1211
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/gitrepo"
1313
"code.gitea.io/gitea/modules/httpcache"
1414
"code.gitea.io/gitea/modules/lfs"
1515
"code.gitea.io/gitea/modules/log"
@@ -82,7 +82,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified *time.Tim
8282
return common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified)
8383
}
8484

85-
func getBlobForEntry(ctx *context.Context) (blob *git.Blob, lastModified *time.Time) {
85+
func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) {
8686
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
8787
if err != nil {
8888
if git.IsErrNotExist(err) {
@@ -98,19 +98,14 @@ func getBlobForEntry(ctx *context.Context) (blob *git.Blob, lastModified *time.T
9898
return nil, nil
9999
}
100100

101-
info, _, err := git.Entries([]*git.TreeEntry{entry}).GetCommitsInfo(ctx, ctx.Repo.Commit, path.Dir("/" + ctx.Repo.TreePath)[1:])
101+
latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath)
102102
if err != nil {
103-
ctx.ServerError("GetCommitsInfo", err)
103+
ctx.ServerError("GetTreePathLatestCommit", err)
104104
return nil, nil
105105
}
106+
lastModified := &latestCommit.Committer.When
106107

107-
if len(info) == 1 {
108-
// Not Modified
109-
lastModified = &info[0].Commit.Committer.When
110-
}
111-
blob = entry.Blob()
112-
113-
return blob, lastModified
108+
return entry.Blob(), lastModified
114109
}
115110

116111
// SingleDownload download a file by repos path

0 commit comments

Comments
 (0)