You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commit_info_test.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "context"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const (
  15. testReposDir = "tests/repos/"
  16. )
  17. func cloneRepo(url, name string) (string, error) {
  18. repoDir, err := os.MkdirTemp("", name)
  19. if err != nil {
  20. return "", err
  21. }
  22. if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{
  23. Mirror: false,
  24. Bare: false,
  25. Quiet: true,
  26. Timeout: 5 * time.Minute,
  27. }); err != nil {
  28. _ = util.RemoveAll(repoDir)
  29. return "", err
  30. }
  31. return repoDir, nil
  32. }
  33. func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
  34. // these test case are specific to the repo1 test repo
  35. testCases := []struct {
  36. CommitID string
  37. Path string
  38. ExpectedIDs map[string]string
  39. ExpectedTreeCommit string
  40. }{
  41. {"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]string{
  42. "file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
  43. "file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  44. }, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2"},
  45. {"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]string{
  46. "file1.txt": "2839944139e0de9737a044f78b0e4b40d989a9e3",
  47. "branch1.txt": "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
  48. }, "2839944139e0de9737a044f78b0e4b40d989a9e3"},
  49. {"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]string{
  50. "branch2.txt": "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
  51. }, "5c80b0245c1c6f8343fa418ec374b13b5d4ee658"},
  52. {"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", "", map[string]string{
  53. "file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
  54. "file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  55. "foo": "37991dec2c8e592043f47155ce4808d4580f9123",
  56. }, "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"},
  57. }
  58. for _, testCase := range testCases {
  59. commit, err := repo1.GetCommit(testCase.CommitID)
  60. if err != nil {
  61. assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err)
  62. // no point trying to do anything else for this test.
  63. continue
  64. }
  65. assert.NotNil(t, commit)
  66. assert.NotNil(t, commit.Tree)
  67. assert.NotNil(t, commit.Tree.repo)
  68. tree, err := commit.Tree.SubTree(testCase.Path)
  69. if err != nil {
  70. assert.NoError(t, err, "Unable to get subtree: %s of commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
  71. // no point trying to do anything else for this test.
  72. continue
  73. }
  74. assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
  75. assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
  76. entries, err := tree.ListEntries()
  77. if err != nil {
  78. assert.NoError(t, err, "Unable to get entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
  79. // no point trying to do anything else for this test.
  80. continue
  81. }
  82. // FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
  83. commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path)
  84. assert.NoError(t, err, "Unable to get commit information for entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
  85. if err != nil {
  86. t.FailNow()
  87. }
  88. assert.Equal(t, testCase.ExpectedTreeCommit, treeCommit.ID.String())
  89. assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
  90. for _, commitInfo := range commitsInfo {
  91. entry := commitInfo.Entry
  92. commit := commitInfo.Commit
  93. expectedID, ok := testCase.ExpectedIDs[entry.Name()]
  94. if !assert.True(t, ok) {
  95. continue
  96. }
  97. assert.Equal(t, expectedID, commit.ID.String())
  98. }
  99. }
  100. }
  101. func TestEntries_GetCommitsInfo(t *testing.T) {
  102. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  103. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  104. assert.NoError(t, err)
  105. defer bareRepo1.Close()
  106. testGetCommitsInfo(t, bareRepo1)
  107. clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo")
  108. if err != nil {
  109. assert.NoError(t, err)
  110. }
  111. defer util.RemoveAll(clonedPath)
  112. clonedRepo1, err := openRepositoryWithDefaultContext(clonedPath)
  113. if err != nil {
  114. assert.NoError(t, err)
  115. }
  116. defer clonedRepo1.Close()
  117. testGetCommitsInfo(t, clonedRepo1)
  118. }
  119. func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
  120. type benchmarkType struct {
  121. url string
  122. name string
  123. }
  124. benchmarks := []benchmarkType{
  125. {url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
  126. {url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
  127. {url: "https://github.com/moby/moby.git", name: "moby"},
  128. {url: "https://github.com/golang/go.git", name: "go"},
  129. {url: "https://github.com/torvalds/linux.git", name: "linux"},
  130. }
  131. doBenchmark := func(benchmark benchmarkType) {
  132. var commit *Commit
  133. var entries Entries
  134. var repo *Repository
  135. repoPath, err := cloneRepo(benchmark.url, benchmark.name)
  136. if err != nil {
  137. b.Fatal(err)
  138. }
  139. defer util.RemoveAll(repoPath)
  140. if repo, err = openRepositoryWithDefaultContext(repoPath); err != nil {
  141. b.Fatal(err)
  142. }
  143. defer repo.Close()
  144. if commit, err = repo.GetBranchCommit("master"); err != nil {
  145. b.Fatal(err)
  146. } else if entries, err = commit.Tree.ListEntries(); err != nil {
  147. b.Fatal(err)
  148. }
  149. entries.Sort()
  150. b.ResetTimer()
  151. b.Run(benchmark.name, func(b *testing.B) {
  152. for i := 0; i < b.N; i++ {
  153. _, _, err := entries.GetCommitsInfo(context.Background(), commit, "")
  154. if err != nil {
  155. b.Fatal(err)
  156. }
  157. }
  158. })
  159. }
  160. for _, benchmark := range benchmarks {
  161. doBenchmark(benchmark)
  162. }
  163. }