Skip to content

Commit 16506ad

Browse files
committed
Move NewCommand to git module
1 parent e0f7b89 commit 16506ad

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

modules/git/tree.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error
6262

6363
return filelist, err
6464
}
65+
66+
// GetTreePathLatestCommitID returns the latest commit ID of a tree path
67+
func (repo *Repository) GetTreePathLatestCommitID(refName, treePath string) (string, error) {
68+
stdout, _, err := NewCommand(repo.Ctx, "rev-list", "-1").
69+
AddDynamicArguments(refName).AddArguments("--").AddDynamicArguments(treePath).
70+
RunStdString(&RunOpts{Dir: repo.Path})
71+
if err != nil {
72+
return "", err
73+
}
74+
return stdout, nil
75+
}

modules/gitrepo/tree_path.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ import (
99
"code.gitea.io/gitea/modules/git"
1010
)
1111

12-
func GetTreePathLatestCommit(ctx context.Context, repo Repository, commitID, treePath string) (*git.Commit, error) {
12+
func GetTreePathLatestCommit(ctx context.Context, repo Repository, refName, treePath string) (*git.Commit, error) {
1313
gitRepo, closer, err := RepositoryFromContextOrOpen(ctx, repo)
1414
if err != nil {
1515
return nil, err
1616
}
1717
defer closer.Close()
1818

19-
stdout, _, err := git.NewCommand(ctx, "rev-list", "-1").
20-
AddDynamicArguments(commitID).AddArguments("--").AddDynamicArguments(treePath).
21-
RunStdString(&git.RunOpts{Dir: repoPath(repo)})
19+
latestCommitID, err := gitRepo.GetTreePathLatestCommitID(refName, treePath)
2220
if err != nil {
2321
return nil, err
2422
}
2523

26-
return gitRepo.GetCommit(stdout)
24+
return gitRepo.GetCommit(latestCommitID)
2725
}

0 commit comments

Comments
 (0)