gitea源码

repo_branch_test.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestRepository_GetBranches(t *testing.T) {
  11. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  12. bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
  13. assert.NoError(t, err)
  14. defer bareRepo1.Close()
  15. branches, countAll, err := bareRepo1.GetBranchNames(0, 2)
  16. assert.NoError(t, err)
  17. assert.Len(t, branches, 2)
  18. assert.Equal(t, 3, countAll)
  19. assert.ElementsMatch(t, []string{"master", "branch2"}, branches)
  20. branches, countAll, err = bareRepo1.GetBranchNames(0, 0)
  21. assert.NoError(t, err)
  22. assert.Len(t, branches, 3)
  23. assert.Equal(t, 3, countAll)
  24. assert.ElementsMatch(t, []string{"master", "branch2", "branch1"}, branches)
  25. branches, countAll, err = bareRepo1.GetBranchNames(5, 1)
  26. assert.NoError(t, err)
  27. assert.Empty(t, branches)
  28. assert.Equal(t, 3, countAll)
  29. assert.ElementsMatch(t, []string{}, branches)
  30. }
  31. func BenchmarkRepository_GetBranches(b *testing.B) {
  32. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  33. bareRepo1, err := OpenRepository(b.Context(), bareRepo1Path)
  34. if err != nil {
  35. b.Fatal(err)
  36. }
  37. defer bareRepo1.Close()
  38. for b.Loop() {
  39. _, _, err := bareRepo1.GetBranchNames(0, 0)
  40. if err != nil {
  41. b.Fatal(err)
  42. }
  43. }
  44. }
  45. func TestGetRefsBySha(t *testing.T) {
  46. bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls")
  47. bareRepo5, err := OpenRepository(t.Context(), bareRepo5Path)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. defer bareRepo5.Close()
  52. // do not exist
  53. branches, err := bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "")
  54. assert.NoError(t, err)
  55. assert.Empty(t, branches)
  56. // refs/pull/1/head
  57. branches, err = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", PullPrefix)
  58. assert.NoError(t, err)
  59. assert.Equal(t, []string{"refs/pull/1/head"}, branches)
  60. branches, err = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", BranchPrefix)
  61. assert.NoError(t, err)
  62. assert.Equal(t, []string{"refs/heads/master", "refs/heads/master-clone"}, branches)
  63. branches, err = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", BranchPrefix)
  64. assert.NoError(t, err)
  65. assert.Equal(t, []string{"refs/heads/test-patch-1"}, branches)
  66. }
  67. func BenchmarkGetRefsBySha(b *testing.B) {
  68. bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls")
  69. bareRepo5, err := OpenRepository(b.Context(), bareRepo5Path)
  70. if err != nil {
  71. b.Fatal(err)
  72. }
  73. defer bareRepo5.Close()
  74. _, _ = bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "")
  75. _, _ = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", "")
  76. _, _ = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", "")
  77. _, _ = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", "")
  78. }
  79. func TestRepository_IsObjectExist(t *testing.T) {
  80. repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
  81. require.NoError(t, err)
  82. defer repo.Close()
  83. // FIXME: Inconsistent behavior between gogit and nogogit editions
  84. // See the comment of IsObjectExist in gogit edition for more details.
  85. supportShortHash := !isGogit
  86. tests := []struct {
  87. name string
  88. arg string
  89. want bool
  90. }{
  91. {
  92. name: "empty",
  93. arg: "",
  94. want: false,
  95. },
  96. {
  97. name: "branch",
  98. arg: "master",
  99. want: false,
  100. },
  101. {
  102. name: "commit hash",
  103. arg: "ce064814f4a0d337b333e646ece456cd39fab612",
  104. want: true,
  105. },
  106. {
  107. name: "short commit hash",
  108. arg: "ce06481",
  109. want: supportShortHash,
  110. },
  111. {
  112. name: "blob hash",
  113. arg: "153f451b9ee7fa1da317ab17a127e9fd9d384310",
  114. want: true,
  115. },
  116. {
  117. name: "short blob hash",
  118. arg: "153f451",
  119. want: supportShortHash,
  120. },
  121. }
  122. for _, tt := range tests {
  123. t.Run(tt.name, func(t *testing.T) {
  124. assert.Equal(t, tt.want, repo.IsObjectExist(tt.arg))
  125. })
  126. }
  127. }
  128. func TestRepository_IsReferenceExist(t *testing.T) {
  129. repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
  130. require.NoError(t, err)
  131. defer repo.Close()
  132. // FIXME: Inconsistent behavior between gogit and nogogit editions
  133. // See the comment of IsReferenceExist in gogit edition for more details.
  134. supportBlobHash := !isGogit
  135. tests := []struct {
  136. name string
  137. arg string
  138. want bool
  139. }{
  140. {
  141. name: "empty",
  142. arg: "",
  143. want: false,
  144. },
  145. {
  146. name: "branch",
  147. arg: "master",
  148. want: true,
  149. },
  150. {
  151. name: "commit hash",
  152. arg: "ce064814f4a0d337b333e646ece456cd39fab612",
  153. want: true,
  154. },
  155. {
  156. name: "short commit hash",
  157. arg: "ce06481",
  158. want: true,
  159. },
  160. {
  161. name: "blob hash",
  162. arg: "153f451b9ee7fa1da317ab17a127e9fd9d384310",
  163. want: supportBlobHash,
  164. },
  165. {
  166. name: "short blob hash",
  167. arg: "153f451",
  168. want: supportBlobHash,
  169. },
  170. }
  171. for _, tt := range tests {
  172. t.Run(tt.name, func(t *testing.T) {
  173. assert.Equal(t, tt.want, repo.IsReferenceExist(tt.arg))
  174. })
  175. }
  176. }