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.

210 lines
5.9 KiB

Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
  1. // Copyright 2017 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 repo
  5. import (
  6. "io/ioutil"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/test"
  13. wiki_service "code.gitea.io/gitea/services/wiki"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. const content = "Wiki contents for unit tests"
  17. const message = "Wiki commit message for unit tests"
  18. func wikiEntry(t *testing.T, repo *models.Repository, wikiName string) *git.TreeEntry {
  19. wikiRepo, err := git.OpenRepository(repo.WikiPath())
  20. assert.NoError(t, err)
  21. defer wikiRepo.Close()
  22. commit, err := wikiRepo.GetBranchCommit("master")
  23. assert.NoError(t, err)
  24. entries, err := commit.ListEntries()
  25. assert.NoError(t, err)
  26. for _, entry := range entries {
  27. if entry.Name() == wiki_service.NameToFilename(wikiName) {
  28. return entry
  29. }
  30. }
  31. return nil
  32. }
  33. func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string {
  34. entry := wikiEntry(t, repo, wikiName)
  35. if !assert.NotNil(t, entry) {
  36. return ""
  37. }
  38. reader, err := entry.Blob().DataAsync()
  39. assert.NoError(t, err)
  40. defer reader.Close()
  41. bytes, err := ioutil.ReadAll(reader)
  42. assert.NoError(t, err)
  43. return string(bytes)
  44. }
  45. func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) {
  46. assert.NotNil(t, wikiEntry(t, repo, wikiName))
  47. }
  48. func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) {
  49. assert.Nil(t, wikiEntry(t, repo, wikiName))
  50. }
  51. func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) {
  52. pageMetas, ok := metas.([]PageMeta)
  53. if !assert.True(t, ok) {
  54. return
  55. }
  56. if !assert.EqualValues(t, len(expectedNames), len(pageMetas)) {
  57. return
  58. }
  59. for i, pageMeta := range pageMetas {
  60. assert.EqualValues(t, expectedNames[i], pageMeta.Name)
  61. }
  62. }
  63. func TestWiki(t *testing.T) {
  64. models.PrepareTestEnv(t)
  65. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  66. ctx.SetParams(":page", "Home")
  67. test.LoadRepo(t, ctx, 1)
  68. Wiki(ctx)
  69. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  70. assert.EqualValues(t, "Home", ctx.Data["Title"])
  71. assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
  72. }
  73. func TestWikiPages(t *testing.T) {
  74. models.PrepareTestEnv(t)
  75. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  76. test.LoadRepo(t, ctx, 1)
  77. WikiPages(ctx)
  78. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  79. assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
  80. }
  81. func TestNewWiki(t *testing.T) {
  82. models.PrepareTestEnv(t)
  83. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  84. test.LoadUser(t, ctx, 2)
  85. test.LoadRepo(t, ctx, 1)
  86. NewWiki(ctx)
  87. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  88. assert.EqualValues(t, ctx.Tr("repo.wiki.new_page"), ctx.Data["Title"])
  89. }
  90. func TestNewWikiPost(t *testing.T) {
  91. for _, title := range []string{
  92. "New page",
  93. "&&&&",
  94. } {
  95. models.PrepareTestEnv(t)
  96. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  97. test.LoadUser(t, ctx, 2)
  98. test.LoadRepo(t, ctx, 1)
  99. NewWikiPost(ctx, auth.NewWikiForm{
  100. Title: title,
  101. Content: content,
  102. Message: message,
  103. })
  104. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  105. assertWikiExists(t, ctx.Repo.Repository, title)
  106. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  107. }
  108. }
  109. func TestNewWikiPost_ReservedName(t *testing.T) {
  110. models.PrepareTestEnv(t)
  111. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  112. test.LoadUser(t, ctx, 2)
  113. test.LoadRepo(t, ctx, 1)
  114. NewWikiPost(ctx, auth.NewWikiForm{
  115. Title: "_edit",
  116. Content: content,
  117. Message: message,
  118. })
  119. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  120. assert.EqualValues(t, ctx.Tr("repo.wiki.reserved_page"), ctx.Flash.ErrorMsg)
  121. assertWikiNotExists(t, ctx.Repo.Repository, "_edit")
  122. }
  123. func TestEditWiki(t *testing.T) {
  124. models.PrepareTestEnv(t)
  125. ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home")
  126. ctx.SetParams(":page", "Home")
  127. test.LoadUser(t, ctx, 2)
  128. test.LoadRepo(t, ctx, 1)
  129. EditWiki(ctx)
  130. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  131. assert.EqualValues(t, "Home", ctx.Data["Title"])
  132. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, "Home"), ctx.Data["content"])
  133. }
  134. func TestEditWikiPost(t *testing.T) {
  135. for _, title := range []string{
  136. "Home",
  137. "New/<page>",
  138. } {
  139. models.PrepareTestEnv(t)
  140. ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home")
  141. ctx.SetParams(":page", "Home")
  142. test.LoadUser(t, ctx, 2)
  143. test.LoadRepo(t, ctx, 1)
  144. EditWikiPost(ctx, auth.NewWikiForm{
  145. Title: title,
  146. Content: content,
  147. Message: message,
  148. })
  149. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  150. assertWikiExists(t, ctx.Repo.Repository, title)
  151. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  152. if title != "Home" {
  153. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  154. }
  155. }
  156. }
  157. func TestDeleteWikiPagePost(t *testing.T) {
  158. models.PrepareTestEnv(t)
  159. ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete")
  160. test.LoadUser(t, ctx, 2)
  161. test.LoadRepo(t, ctx, 1)
  162. DeleteWikiPagePost(ctx)
  163. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  164. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  165. }
  166. func TestWikiRaw(t *testing.T) {
  167. for filepath, filetype := range map[string]string{
  168. "jpeg.jpg": "image/jpeg",
  169. "Page With Spaced Name": "text/plain; charset=utf-8",
  170. "Page-With-Spaced-Name": "text/plain; charset=utf-8",
  171. "Page With Spaced Name.md": "text/plain; charset=utf-8",
  172. "Page-With-Spaced-Name.md": "text/plain; charset=utf-8",
  173. } {
  174. models.PrepareTestEnv(t)
  175. ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+filepath)
  176. ctx.SetParams("*", filepath)
  177. test.LoadUser(t, ctx, 2)
  178. test.LoadRepo(t, ctx, 1)
  179. WikiRaw(ctx)
  180. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  181. assert.EqualValues(t, filetype, ctx.Resp.Header().Get("Content-Type"))
  182. }
  183. }