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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. const testReposDir = "tests/repos/"
  13. const benchmarkReposDir = "benchmark/repos/"
  14. func cloneRepo(url, dir, name string) (string, error) {
  15. repoDir := filepath.Join(dir, name)
  16. if _, err := os.Stat(repoDir); err == nil {
  17. return repoDir, nil
  18. }
  19. return repoDir, Clone(url, repoDir, CloneRepoOptions{
  20. Mirror: false,
  21. Bare: false,
  22. Quiet: true,
  23. Timeout: 5 * time.Minute,
  24. })
  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. assert.NoError(t, err)
  54. tree, err := commit.Tree.SubTree(testCase.Path)
  55. assert.NoError(t, err)
  56. entries, err := tree.ListEntries()
  57. assert.NoError(t, err)
  58. commitsInfo, treeCommit, err := entries.GetCommitsInfo(commit, testCase.Path, nil)
  59. assert.Equal(t, testCase.ExpectedTreeCommit, treeCommit.ID.String())
  60. assert.NoError(t, err)
  61. assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
  62. for _, commitInfo := range commitsInfo {
  63. entry := commitInfo[0].(*TreeEntry)
  64. commit := commitInfo[1].(*Commit)
  65. expectedID, ok := testCase.ExpectedIDs[entry.Name()]
  66. if !assert.True(t, ok) {
  67. continue
  68. }
  69. assert.Equal(t, expectedID, commit.ID.String())
  70. }
  71. }
  72. }
  73. func TestEntries_GetCommitsInfo(t *testing.T) {
  74. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  75. bareRepo1, err := OpenRepository(bareRepo1Path)
  76. assert.NoError(t, err)
  77. defer bareRepo1.Close()
  78. testGetCommitsInfo(t, bareRepo1)
  79. clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo")
  80. assert.NoError(t, err)
  81. defer os.RemoveAll(clonedPath)
  82. clonedRepo1, err := OpenRepository(clonedPath)
  83. assert.NoError(t, err)
  84. defer clonedRepo1.Close()
  85. testGetCommitsInfo(t, clonedRepo1)
  86. }
  87. func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
  88. benchmarks := []struct {
  89. url string
  90. name string
  91. }{
  92. {url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
  93. {url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
  94. {url: "https://github.com/moby/moby.git", name: "moby"},
  95. {url: "https://github.com/golang/go.git", name: "go"},
  96. {url: "https://github.com/torvalds/linux.git", name: "linux"},
  97. }
  98. for _, benchmark := range benchmarks {
  99. var commit *Commit
  100. var entries Entries
  101. var repo *Repository
  102. if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil {
  103. b.Fatal(err)
  104. } else if repo, err = OpenRepository(repoPath); err != nil {
  105. b.Fatal(err)
  106. } else if commit, err = repo.GetBranchCommit("master"); err != nil {
  107. repo.Close()
  108. b.Fatal(err)
  109. } else if entries, err = commit.Tree.ListEntries(); err != nil {
  110. repo.Close()
  111. b.Fatal(err)
  112. }
  113. entries.Sort()
  114. b.ResetTimer()
  115. b.Run(benchmark.name, func(b *testing.B) {
  116. for i := 0; i < b.N; i++ {
  117. _, _, err := entries.GetCommitsInfo(commit, "", nil)
  118. if err != nil {
  119. b.Fatal(err)
  120. }
  121. }
  122. })
  123. repo.Close()
  124. }
  125. }