branch.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "gogs.io/gogs/internal/context"
  8. "gogs.io/gogs/internal/route/api/v1/convert"
  9. )
  10. // https://github.com/gogs/go-gogs-client/wiki/Repositories#get-branch
  11. func GetBranch(c *context.APIContext) {
  12. branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
  13. if err != nil {
  14. c.NotFoundOrError(err, "get branch")
  15. return
  16. }
  17. commit, err := branch.GetCommit()
  18. if err != nil {
  19. c.Error(err, "get commit")
  20. return
  21. }
  22. c.JSONSuccess( convert.ToBranch(branch, commit))
  23. }
  24. // https://github.com/gogs/go-gogs-client/wiki/Repositories#list-branches
  25. func ListBranches(c *context.APIContext) {
  26. branches, err := c.Repo.Repository.GetBranches()
  27. if err != nil {
  28. c.Error(err, "get branches")
  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(err, "get commit")
  36. return
  37. }
  38. apiBranches[i] = convert.ToBranch(branches[i], commit)
  39. }
  40. c.JSONSuccess( &apiBranches)
  41. }