You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

branch.go 1.4 kB

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