Skip to content

Commit 2f05073

Browse files
committed
Use the function directly
1 parent 16506ad commit 2f05073

File tree

6 files changed

+35
-68
lines changed

6 files changed

+35
-68
lines changed

modules/git/tree.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error
6363
return filelist, err
6464
}
6565

66-
// GetTreePathLatestCommitID returns the latest commit ID of a tree path
67-
func (repo *Repository) GetTreePathLatestCommitID(refName, treePath string) (string, error) {
66+
// GetTreePathLatestCommitID returns the latest commit of a tree path
67+
func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Commit, error) {
6868
stdout, _, err := NewCommand(repo.Ctx, "rev-list", "-1").
6969
AddDynamicArguments(refName).AddArguments("--").AddDynamicArguments(treePath).
7070
RunStdString(&RunOpts{Dir: repo.Path})
7171
if err != nil {
72-
return "", err
72+
return nil, err
7373
}
74-
return stdout, nil
74+
return repo.GetCommit(stdout)
7575
}

modules/git/tree_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ func TestSubTree_Issue29101(t *testing.T) {
2525
assert.True(t, IsErrNotExist(err))
2626
}
2727
}
28+
29+
func Test_GetTreePathLatestCommit(t *testing.T) {
30+
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo6_blame"))
31+
assert.NoError(t, err)
32+
defer repo.Close()
33+
34+
commitID, err := repo.GetBranchCommitID("master")
35+
assert.NoError(t, err)
36+
assert.EqualValues(t, "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7", commitID)
37+
38+
commit, err := repo.GetTreePathLatestCommit("master", "blame.txt")
39+
assert.NoError(t, err)
40+
assert.NotNil(t, commit)
41+
assert.EqualValues(t, "45fb6cbc12f970b04eacd5cd4165edd11c8d7376", commit.ID.String())
42+
}

modules/gitrepo/tree_path.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

modules/gitrepo/tree_path_test.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

routers/api/v1/repo/file.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,14 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn
241241
return nil, nil, nil
242242
}
243243

244-
latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath)
244+
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository)
245+
if err != nil {
246+
ctx.Error(http.StatusInternalServerError, "RepositoryFromContextOrOpen", err)
247+
return nil, nil, nil
248+
}
249+
defer closer.Close()
250+
251+
latestCommit, err := gitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath)
245252
if err != nil {
246253
ctx.Error(http.StatusInternalServerError, "GetTreePathLatestCommit", err)
247254
return nil, nil, nil

routers/web/repo/download.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) {
9898
return nil, nil
9999
}
100100

101-
latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath)
101+
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository)
102+
if err != nil {
103+
ctx.ServerError("RepositoryFromContextOrOpen", err)
104+
return nil, nil
105+
}
106+
defer closer.Close()
107+
108+
latestCommit, err := gitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath)
102109
if err != nil {
103110
ctx.ServerError("GetTreePathLatestCommit", err)
104111
return nil, nil

0 commit comments

Comments
 (0)