branch.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. func GetBranch(c *context.APIContext) {
  14. branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
  15. if err != nil {
  16. if errors.IsErrBranchNotExist(err) {
  17. c.Error(404, "GetBranch", err)
  18. } else {
  19. c.Error(500, "GetBranch", err)
  20. }
  21. return
  22. }
  23. commit, err := branch.GetCommit()
  24. if err != nil {
  25. c.Error(500, "GetCommit", err)
  26. return
  27. }
  28. c.JSON(200, convert.ToBranch(branch, commit))
  29. }
  30. func ListBranches(c *context.APIContext) {
  31. branches, err := c.Repo.Repository.GetBranches()
  32. if err != nil {
  33. c.Error(500, "GetBranches", err)
  34. return
  35. }
  36. apiBranches := make([]*api.Branch, len(branches))
  37. for i := range branches {
  38. commit, err := branches[i].GetCommit()
  39. if err != nil {
  40. c.Error(500, "GetCommit", err)
  41. return
  42. }
  43. apiBranches[i] = convert.ToBranch(branches[i], commit)
  44. }
  45. c.JSON(200, &apiBranches)
  46. }