gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/test"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestRepository_GetCommitBranches(t *testing.T) {
  14. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  15. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  16. assert.NoError(t, err)
  17. defer bareRepo1.Close()
  18. // these test case are specific to the repo1_bare test repo
  19. testCases := []struct {
  20. CommitID string
  21. ExpectedBranches []string
  22. }{
  23. {"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}},
  24. {"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}},
  25. {"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
  26. {"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
  27. {"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
  28. {"master", []string{"master"}},
  29. }
  30. for _, testCase := range testCases {
  31. commit, err := bareRepo1.GetCommit(testCase.CommitID)
  32. assert.NoError(t, err)
  33. branches, err := bareRepo1.getBranches(os.Environ(), commit.ID.String(), 2)
  34. assert.NoError(t, err)
  35. assert.Equal(t, testCase.ExpectedBranches, branches)
  36. }
  37. }
  38. func TestGetTagCommitWithSignature(t *testing.T) {
  39. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  40. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  41. assert.NoError(t, err)
  42. defer bareRepo1.Close()
  43. // both the tag and the commit are signed here, this validates only the commit signature
  44. commit, err := bareRepo1.GetCommit("28b55526e7100924d864dd89e35c1ea62e7a5a32")
  45. assert.NoError(t, err)
  46. assert.NotNil(t, commit)
  47. assert.NotNil(t, commit.Signature)
  48. // test that signature is not in message
  49. assert.Equal(t, "signed-commit\n", commit.CommitMessage)
  50. }
  51. func TestGetCommitWithBadCommitID(t *testing.T) {
  52. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  53. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  54. assert.NoError(t, err)
  55. defer bareRepo1.Close()
  56. commit, err := bareRepo1.GetCommit("bad_branch")
  57. assert.Nil(t, commit)
  58. assert.Error(t, err)
  59. assert.True(t, IsErrNotExist(err))
  60. }
  61. func TestIsCommitInBranch(t *testing.T) {
  62. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  63. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  64. assert.NoError(t, err)
  65. defer bareRepo1.Close()
  66. result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1")
  67. assert.NoError(t, err)
  68. assert.True(t, result)
  69. result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2")
  70. assert.NoError(t, err)
  71. assert.False(t, result)
  72. }
  73. func TestRepository_CommitsBetweenIDs(t *testing.T) {
  74. bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween")
  75. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  76. assert.NoError(t, err)
  77. defer bareRepo1.Close()
  78. cases := []struct {
  79. OldID string
  80. NewID string
  81. ExpectedCommits int
  82. }{
  83. {"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, // com1 -> com2
  84. {"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, // reset HEAD~, com2 -> com1
  85. {"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new
  86. }
  87. for i, c := range cases {
  88. commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
  89. assert.NoError(t, err)
  90. assert.Len(t, commits, c.ExpectedCommits, "case %d", i)
  91. }
  92. }
  93. func TestGetRefCommitID(t *testing.T) {
  94. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  95. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  96. assert.NoError(t, err)
  97. defer bareRepo1.Close()
  98. // these test case are specific to the repo1_bare test repo
  99. testCases := []struct {
  100. Ref string
  101. ExpectedCommitID string
  102. }{
  103. {RefNameFromBranch("master").String(), "ce064814f4a0d337b333e646ece456cd39fab612"},
  104. {RefNameFromBranch("branch1").String(), "2839944139e0de9737a044f78b0e4b40d989a9e3"},
  105. {RefNameFromTag("test").String(), "3ad28a9149a2864384548f3d17ed7f38014c9e8a"},
  106. {"ce064814f4a0d337b333e646ece456cd39fab612", "ce064814f4a0d337b333e646ece456cd39fab612"},
  107. }
  108. for _, testCase := range testCases {
  109. commitID, err := bareRepo1.GetRefCommitID(testCase.Ref)
  110. if assert.NoError(t, err) {
  111. assert.Equal(t, testCase.ExpectedCommitID, commitID)
  112. }
  113. }
  114. }
  115. func TestCommitsByFileAndRange(t *testing.T) {
  116. defer test.MockVariableValue(&setting.Git.CommitsRangeSize, 2)()
  117. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  118. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  119. require.NoError(t, err)
  120. defer bareRepo1.Close()
  121. // "foo" has 3 commits in "master" branch
  122. commits, err := bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1})
  123. require.NoError(t, err)
  124. assert.Len(t, commits, 2)
  125. commits, err = bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2})
  126. require.NoError(t, err)
  127. assert.Len(t, commits, 1)
  128. }