tree.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package repo
  2. import (
  3. "fmt"
  4. "github.com/gogs/git-module"
  5. "gogs.io/gogs/internal/context"
  6. )
  7. type repoGitTree struct {
  8. Sha string `json:"sha"`
  9. URL string `json:"url"`
  10. Tree []*repoGitTreeEntry `json:"tree"`
  11. }
  12. type repoGitTreeEntry struct {
  13. Path string `json:"path"`
  14. Mode string `json:"mode"`
  15. Type string `json:"type"`
  16. Size int64 `json:"size"`
  17. Sha string `json:"sha"`
  18. URL string `json:"url"`
  19. }
  20. func GetRepoGitTree(c *context.APIContext) {
  21. gitTree, err := c.Repo.GitRepo.GetTree(c.Params(":sha"))
  22. if err != nil {
  23. c.NotFoundOrServerError("GetRepoGitTree", git.IsErrNotExist, err)
  24. return
  25. }
  26. entries, err := gitTree.ListEntries()
  27. if err != nil {
  28. c.ServerError("GetRepoGitTree", err)
  29. return
  30. }
  31. templateURL := fmt.Sprintf("%s/repos/%s/%s/git/trees", c.BaseURL, c.Params(":username"), c.Params(":reponame"))
  32. if len(entries) == 0 {
  33. c.JSONSuccess(&repoGitTree{
  34. Sha: c.Params(":sha"),
  35. URL: fmt.Sprintf(templateURL+"/%s", c.Params(":sha")),
  36. })
  37. return
  38. }
  39. children := make([]*repoGitTreeEntry, 0, len(entries))
  40. for _, entry := range entries {
  41. var mode string
  42. switch entry.Type {
  43. case git.ObjectCommit:
  44. mode = "160000"
  45. case git.ObjectTree:
  46. mode = "040000"
  47. case git.ObjectBlob:
  48. mode = "120000"
  49. case git.ObjectTag:
  50. mode = "100644"
  51. default:
  52. mode = ""
  53. }
  54. children = append(children, &repoGitTreeEntry{
  55. Path: entry.Name(),
  56. Mode: mode,
  57. Type: string(entry.Type),
  58. Size: entry.Size(),
  59. Sha: entry.ID.String(),
  60. URL: fmt.Sprintf(templateURL+"/%s", entry.ID.String()),
  61. })
  62. }
  63. c.JSONSuccess(&repoGitTree{
  64. Sha: c.Params(":sha"),
  65. URL: fmt.Sprintf(templateURL+"/%s", c.Params(":sha")),
  66. Tree: children,
  67. })
  68. }