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/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/modules/context"
  8. "github.com/gogits/gogs/routers/api/v1/convert"
  9. )
  10. // https://github.com/gogits/go-gogs-client/wiki/Repositories#get-branch
  11. func GetBranch(ctx *context.APIContext) {
  12. branch, err := ctx.Repo.Repository.GetBranch(ctx.Params(":branchname"))
  13. if err != nil {
  14. ctx.Error(500, "GetBranch", err)
  15. return
  16. }
  17. c, err := branch.GetCommit()
  18. if err != nil {
  19. ctx.Error(500, "GetCommit", err)
  20. return
  21. }
  22. ctx.JSON(200, convert.ToBranch(branch, c))
  23. }
  24. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-branches
  25. func ListBranches(ctx *context.APIContext) {
  26. branches, err := ctx.Repo.Repository.GetBranches()
  27. if err != nil {
  28. ctx.Error(500, "GetBranches", err)
  29. return
  30. }
  31. apiBranches := make([]*api.Branch, len(branches))
  32. for i := range branches {
  33. c, err := branches[i].GetCommit()
  34. if err != nil {
  35. ctx.Error(500, "GetCommit", err)
  36. return
  37. }
  38. apiBranches[i] = convert.ToBranch(branches[i], c)
  39. }
  40. ctx.JSON(200, &apiBranches)
  41. }