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.4KB

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