branch.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "github.com/gogs/go-gogs-client"
  7. "github.com/gogs/gogs/models/errors"
  8. "github.com/gogs/gogs/pkg/context"
  9. "github.com/gogs/gogs/routes/api/v1/convert"
  10. )
  11. // https://github.com/gogs/go-gogs-client/wiki/Repositories#get-branch
  12. func GetBranch(c *context.APIContext) {
  13. branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
  14. if err != nil {
  15. if errors.IsErrBranchNotExist(err) {
  16. c.Error(404, "GetBranch", err)
  17. } else {
  18. c.Error(500, "GetBranch", err)
  19. }
  20. return
  21. }
  22. commit, err := branch.GetCommit()
  23. if err != nil {
  24. c.Error(500, "GetCommit", err)
  25. return
  26. }
  27. c.JSON(200, convert.ToBranch(branch, commit))
  28. }
  29. // https://github.com/gogs/go-gogs-client/wiki/Repositories#list-branches
  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. }