branch.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models/errors"
  9. "gitote/gitote/pkg/context"
  10. "gitote/gitote/routes/api/v1/convert"
  11. api "gitlab.com/gitote/go-gitote-client"
  12. )
  13. // GetBranch get a branch of a repository
  14. func GetBranch(c *context.APIContext) {
  15. branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
  16. if err != nil {
  17. if errors.IsErrBranchNotExist(err) {
  18. c.Error(404, "GetBranch", err)
  19. } else {
  20. c.Error(500, "GetBranch", err)
  21. }
  22. return
  23. }
  24. commit, err := branch.GetCommit()
  25. if err != nil {
  26. c.Error(500, "GetCommit", err)
  27. return
  28. }
  29. c.JSON(200, convert.ToBranch(branch, commit))
  30. }
  31. // ListBranches list all the branches of a repository
  32. func ListBranches(c *context.APIContext) {
  33. branches, err := c.Repo.Repository.GetBranches()
  34. if err != nil {
  35. c.Error(500, "GetBranches", err)
  36. return
  37. }
  38. apiBranches := make([]*api.Branch, len(branches))
  39. for i := range branches {
  40. commit, err := branches[i].GetCommit()
  41. if err != nil {
  42. c.Error(500, "GetCommit", err)
  43. return
  44. }
  45. apiBranches[i] = convert.ToBranch(branches[i], commit)
  46. }
  47. c.JSON(200, &apiBranches)
  48. }