branch.go 1000 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package repo
  2. import (
  3. "gitote/gitote/models/errors"
  4. "gitote/gitote/pkg/context"
  5. "gitote/gitote/routes/api/v1/convert"
  6. api "gitlab.com/gitote/go-gitote-client"
  7. )
  8. func GetBranch(c *context.APIContext) {
  9. branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
  10. if err != nil {
  11. if errors.IsErrBranchNotExist(err) {
  12. c.Error(404, "GetBranch", err)
  13. } else {
  14. c.Error(500, "GetBranch", err)
  15. }
  16. return
  17. }
  18. commit, err := branch.GetCommit()
  19. if err != nil {
  20. c.Error(500, "GetCommit", err)
  21. return
  22. }
  23. c.JSON(200, convert.ToBranch(branch, commit))
  24. }
  25. func ListBranches(c *context.APIContext) {
  26. branches, err := c.Repo.Repository.GetBranches()
  27. if err != nil {
  28. c.Error(500, "GetBranches", err)
  29. return
  30. }
  31. apiBranches := make([]*api.Branch, len(branches))
  32. for i := range branches {
  33. commit, err := branches[i].GetCommit()
  34. if err != nil {
  35. c.Error(500, "GetCommit", err)
  36. return
  37. }
  38. apiBranches[i] = convert.ToBranch(branches[i], commit)
  39. }
  40. c.JSON(200, &apiBranches)
  41. }