aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/git/tree.go11
-rw-r--r--modules/git/tree_test.go15
2 files changed, 26 insertions, 0 deletions
diff --git a/modules/git/tree.go b/modules/git/tree.go
index 1da4a9fa5d..d35dc58d8d 100644
--- a/modules/git/tree.go
+++ b/modules/git/tree.go
@@ -62,3 +62,14 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error
return filelist, err
}
+
+// GetTreePathLatestCommitID returns the latest commit of a tree path
+func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Commit, error) {
+ stdout, _, err := NewCommand(repo.Ctx, "rev-list", "-1").
+ AddDynamicArguments(refName).AddDashesAndList(treePath).
+ RunStdString(&RunOpts{Dir: repo.Path})
+ if err != nil {
+ return nil, err
+ }
+ return repo.GetCommit(strings.TrimSpace(stdout))
+}
diff --git a/modules/git/tree_test.go b/modules/git/tree_test.go
index 6d2b5c84d5..5fee64b038 100644
--- a/modules/git/tree_test.go
+++ b/modules/git/tree_test.go
@@ -25,3 +25,18 @@ func TestSubTree_Issue29101(t *testing.T) {
assert.True(t, IsErrNotExist(err))
}
}
+
+func Test_GetTreePathLatestCommit(t *testing.T) {
+ repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo6_blame"))
+ assert.NoError(t, err)
+ defer repo.Close()
+
+ commitID, err := repo.GetBranchCommitID("master")
+ assert.NoError(t, err)
+ assert.EqualValues(t, "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7", commitID)
+
+ commit, err := repo.GetTreePathLatestCommit("master", "blame.txt")
+ assert.NoError(t, err)
+ assert.NotNil(t, commit)
+ assert.EqualValues(t, "45fb6cbc12f970b04eacd5cd4165edd11c8d7376", commit.ID.String())
+}