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 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018 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. "fmt"
  7. "strings"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/sdk/gitea"
  12. )
  13. // GetTree get the tree of a repository.
  14. func GetTree(ctx *context.APIContext) {
  15. // swagger:operation GET /repos/{owner}/{repo}/git/trees/{sha} repository GetTree
  16. // ---
  17. // summary: Gets the tree of a repository.
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: owner
  22. // in: path
  23. // description: owner of the repo
  24. // type: string
  25. // required: true
  26. // - name: repo
  27. // in: path
  28. // description: name of the repo
  29. // type: string
  30. // required: true
  31. // - name: sha
  32. // in: path
  33. // description: sha of the commit
  34. // type: string
  35. // required: true
  36. // responses:
  37. // "200":
  38. // "$ref": "#/responses/GitTreeResponse"
  39. sha := ctx.Params("sha")
  40. if len(sha) == 0 {
  41. ctx.Error(400, "sha not provided", nil)
  42. return
  43. }
  44. tree := GetTreeBySHA(ctx, sha)
  45. if tree != nil {
  46. ctx.JSON(200, tree)
  47. } else {
  48. ctx.Error(400, "sha invalid", nil)
  49. }
  50. }
  51. // GetTreeBySHA get the GitTreeResponse of a repository using a sha hash.
  52. func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse {
  53. gitTree, err := ctx.Repo.GitRepo.GetTree(sha)
  54. if err != nil || gitTree == nil {
  55. return nil
  56. }
  57. tree := new(gitea.GitTreeResponse)
  58. repoID := strings.TrimRight(setting.AppURL, "/") + "/api/v1/repos/" + ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  59. tree.SHA = gitTree.ID.String()
  60. tree.URL = repoID + "/git/trees/" + tree.SHA
  61. var entries git.Entries
  62. if ctx.QueryBool("recursive") {
  63. entries, err = gitTree.ListEntriesRecursive()
  64. } else {
  65. entries, err = gitTree.ListEntries()
  66. }
  67. if err != nil {
  68. return tree
  69. }
  70. repoIDLen := len(repoID)
  71. // 51 is len(sha1) + len("/git/blobs/"). 40 + 11.
  72. blobURL := make([]byte, repoIDLen+51)
  73. copy(blobURL[:], repoID)
  74. copy(blobURL[repoIDLen:], "/git/blobs/")
  75. // 51 is len(sha1) + len("/git/trees/"). 40 + 11.
  76. treeURL := make([]byte, repoIDLen+51)
  77. copy(treeURL[:], repoID)
  78. copy(treeURL[repoIDLen:], "/git/trees/")
  79. // 40 is the size of the sha1 hash in hexadecimal format.
  80. copyPos := len(treeURL) - 40
  81. if len(entries) > 1000 {
  82. tree.Entries = make([]gitea.GitEntry, 1000)
  83. } else {
  84. tree.Entries = make([]gitea.GitEntry, len(entries))
  85. }
  86. for e := range entries {
  87. if e > 1000 {
  88. tree.Truncated = true
  89. break
  90. }
  91. tree.Entries[e].Path = entries[e].Name()
  92. tree.Entries[e].Mode = fmt.Sprintf("%06x", entries[e].Mode())
  93. tree.Entries[e].Type = string(entries[e].Type)
  94. tree.Entries[e].Size = entries[e].Size()
  95. tree.Entries[e].SHA = entries[e].ID.String()
  96. if entries[e].IsDir() {
  97. copy(treeURL[copyPos:], entries[e].ID.String())
  98. tree.Entries[e].URL = string(treeURL[:])
  99. } else {
  100. copy(blobURL[copyPos:], entries[e].ID.String())
  101. tree.Entries[e].URL = string(blobURL[:])
  102. }
  103. }
  104. return tree
  105. }