repo_branch.go 983 B

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