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.

branch.go 1.6 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. )
  11. // GetBranch get a branch of a repository
  12. // see https://github.com/gogits/go-gogs-client/wiki/Repositories#get-branch
  13. func GetBranch(ctx *context.APIContext) {
  14. if ctx.Repo.TreePath != "" {
  15. // if TreePath != "", then URL contained extra slashes
  16. // (i.e. "master/subbranch" instead of "master"), so branch does
  17. // not exist
  18. ctx.Status(404)
  19. return
  20. }
  21. branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName)
  22. if err != nil {
  23. if models.IsErrBranchNotExist(err) {
  24. ctx.Error(404, "GetBranch", err)
  25. } else {
  26. ctx.Error(500, "GetBranch", err)
  27. }
  28. return
  29. }
  30. c, err := branch.GetCommit()
  31. if err != nil {
  32. ctx.Error(500, "GetCommit", err)
  33. return
  34. }
  35. ctx.JSON(200, convert.ToBranch(branch, c))
  36. }
  37. // ListBranches list all the branches of a repository
  38. // see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-branches
  39. func ListBranches(ctx *context.APIContext) {
  40. branches, err := ctx.Repo.Repository.GetBranches()
  41. if err != nil {
  42. ctx.Error(500, "GetBranches", err)
  43. return
  44. }
  45. apiBranches := make([]*api.Branch, len(branches))
  46. for i := range branches {
  47. c, err := branches[i].GetCommit()
  48. if err != nil {
  49. ctx.Error(500, "GetCommit", err)
  50. return
  51. }
  52. apiBranches[i] = convert.ToBranch(branches[i], c)
  53. }
  54. ctx.JSON(200, &apiBranches)
  55. }