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.

97 lines
2.6 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
  1. // Copyright 2019 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 repofiles
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. // GetTreeBySHA get the GitTreeResponse of a repository using a sha hash.
  13. func GetTreeBySHA(repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) {
  14. gitRepo, err := git.OpenRepository(repo.RepoPath())
  15. if err != nil {
  16. return nil, err
  17. }
  18. defer gitRepo.Close()
  19. gitTree, err := gitRepo.GetTree(sha)
  20. if err != nil || gitTree == nil {
  21. return nil, models.ErrSHANotFound{
  22. SHA: sha,
  23. }
  24. }
  25. tree := new(api.GitTreeResponse)
  26. tree.SHA = gitTree.ResolvedID.String()
  27. tree.URL = repo.APIURL() + "/git/trees/" + tree.SHA
  28. var entries git.Entries
  29. if recursive {
  30. entries, err = gitTree.ListEntriesRecursive()
  31. } else {
  32. entries, err = gitTree.ListEntries()
  33. }
  34. if err != nil {
  35. return nil, err
  36. }
  37. apiURL := repo.APIURL()
  38. apiURLLen := len(apiURL)
  39. // 51 is len(sha1) + len("/git/blobs/"). 40 + 11.
  40. blobURL := make([]byte, apiURLLen+51)
  41. copy(blobURL, apiURL)
  42. copy(blobURL[apiURLLen:], "/git/blobs/")
  43. // 51 is len(sha1) + len("/git/trees/"). 40 + 11.
  44. treeURL := make([]byte, apiURLLen+51)
  45. copy(treeURL, apiURL)
  46. copy(treeURL[apiURLLen:], "/git/trees/")
  47. // 40 is the size of the sha1 hash in hexadecimal format.
  48. copyPos := len(treeURL) - 40
  49. if perPage <= 0 || perPage > setting.API.DefaultGitTreesPerPage {
  50. perPage = setting.API.DefaultGitTreesPerPage
  51. }
  52. if page <= 0 {
  53. page = 1
  54. }
  55. tree.Page = page
  56. tree.TotalCount = len(entries)
  57. rangeStart := perPage * (page - 1)
  58. if rangeStart >= len(entries) {
  59. return tree, nil
  60. }
  61. var rangeEnd int
  62. if len(entries) > perPage {
  63. tree.Truncated = true
  64. }
  65. if rangeStart+perPage < len(entries) {
  66. rangeEnd = rangeStart + perPage
  67. } else {
  68. rangeEnd = len(entries)
  69. }
  70. tree.Entries = make([]api.GitEntry, rangeEnd-rangeStart)
  71. for e := rangeStart; e < rangeEnd; e++ {
  72. i := e - rangeStart
  73. tree.Entries[i].Path = entries[e].Name()
  74. tree.Entries[i].Mode = fmt.Sprintf("%06o", entries[e].Mode())
  75. tree.Entries[i].Type = entries[e].Type()
  76. tree.Entries[i].Size = entries[e].Size()
  77. tree.Entries[i].SHA = entries[e].ID.String()
  78. if entries[e].IsDir() {
  79. copy(treeURL[copyPos:], entries[e].ID.String())
  80. tree.Entries[i].URL = string(treeURL)
  81. } else {
  82. copy(blobURL[copyPos:], entries[e].ID.String())
  83. tree.Entries[i].URL = string(blobURL)
  84. }
  85. }
  86. return tree, nil
  87. }