You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree.go 2.6 kB

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