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.

branch.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 The Gogs 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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/routers/api/v1/convert"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // GetBranch get a branch of a repository
  12. func GetBranch(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
  14. // ---
  15. // summary: List a repository's branches
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // - name: branch
  30. // in: path
  31. // description: branch to get
  32. // type: string
  33. // required: true
  34. // responses:
  35. // "200":
  36. // "$ref": "#/responses/Branch"
  37. if ctx.Repo.TreePath != "" {
  38. // if TreePath != "", then URL contained extra slashes
  39. // (i.e. "master/subbranch" instead of "master"), so branch does
  40. // not exist
  41. ctx.Status(404)
  42. return
  43. }
  44. branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName)
  45. if err != nil {
  46. if models.IsErrBranchNotExist(err) {
  47. ctx.Error(404, "GetBranch", err)
  48. } else {
  49. ctx.Error(500, "GetBranch", err)
  50. }
  51. return
  52. }
  53. c, err := branch.GetCommit()
  54. if err != nil {
  55. ctx.Error(500, "GetCommit", err)
  56. return
  57. }
  58. ctx.JSON(200, convert.ToBranch(ctx.Repo.Repository, branch, c))
  59. }
  60. // ListBranches list all the branches of a repository
  61. func ListBranches(ctx *context.APIContext) {
  62. // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
  63. // ---
  64. // summary: List a repository's branches
  65. // produces:
  66. // - application/json
  67. // parameters:
  68. // - name: owner
  69. // in: path
  70. // description: owner of the repo
  71. // type: string
  72. // required: true
  73. // - name: repo
  74. // in: path
  75. // description: name of the repo
  76. // type: string
  77. // required: true
  78. // responses:
  79. // "200":
  80. // "$ref": "#/responses/BranchList"
  81. branches, err := ctx.Repo.Repository.GetBranches()
  82. if err != nil {
  83. ctx.Error(500, "GetBranches", err)
  84. return
  85. }
  86. apiBranches := make([]*api.Branch, len(branches))
  87. for i := range branches {
  88. c, err := branches[i].GetCommit()
  89. if err != nil {
  90. ctx.Error(500, "GetCommit", err)
  91. return
  92. }
  93. apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c)
  94. }
  95. ctx.JSON(200, &apiBranches)
  96. }