本站源代码
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 line
2.6KB

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