branch.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package repo
  2. import (
  3. api "github.com/gogs/go-gogs-client"
  4. "gogs.io/gogs/internal/context"
  5. "gogs.io/gogs/internal/route/api/v1/convert"
  6. )
  7. // https://github.com/gogs/go-gogs-client/wiki/Repositories#get-branch
  8. func GetBranch(c *context.APIContext) {
  9. branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
  10. if err != nil {
  11. c.NotFoundOrError(err, "get branch")
  12. return
  13. }
  14. commit, err := branch.GetCommit()
  15. if err != nil {
  16. c.Error(err, "get commit")
  17. return
  18. }
  19. c.JSONSuccess(convert.ToBranch(branch, commit))
  20. }
  21. // https://github.com/gogs/go-gogs-client/wiki/Repositories#list-branches
  22. func ListBranches(c *context.APIContext) {
  23. branches, err := c.Repo.Repository.GetBranches()
  24. if err != nil {
  25. c.Error(err, "get branches")
  26. return
  27. }
  28. apiBranches := make([]*api.Branch, len(branches))
  29. for i := range branches {
  30. commit, err := branches[i].GetCommit()
  31. if err != nil {
  32. c.Error(err, "get commit")
  33. return
  34. }
  35. apiBranches[i] = convert.ToBranch(branches[i], commit)
  36. }
  37. c.JSONSuccess(&apiBranches)
  38. }