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.6KB

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