Files
Gogs/routes/api/v1/repo/branch.go

56 lines
1.3 KiB
Go
Raw Normal View History

2016-01-28 20:49:05 +01:00
// Copyright 2016 The Gogs Authors. All rights reserved.
2016-01-15 19:24:03 +01:00
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
2016-01-15 19:24:03 +01:00
"gogs.io/gogs/models/errors"
"gogs.io/gogs/pkg/context"
"gogs.io/gogs/routes/api/v1/convert"
2016-01-15 19:24:03 +01:00
)
2018-05-27 08:53:48 +08:00
// https://github.com/gogs/go-gogs-client/wiki/Repositories#get-branch
2017-06-03 07:26:09 -04:00
func GetBranch(c *context.APIContext) {
branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
2016-01-15 19:24:03 +01:00
if err != nil {
if errors.IsErrBranchNotExist(err) {
2017-06-03 07:26:09 -04:00
c.Error(404, "GetBranch", err)
} else {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetBranch", err)
}
2016-01-15 19:24:03 +01:00
return
}
2016-02-02 17:07:40 -05:00
2017-06-03 07:26:09 -04:00
commit, err := branch.GetCommit()
2016-01-15 19:24:03 +01:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetCommit", err)
2016-01-15 19:24:03 +01:00
return
}
2016-02-02 17:07:40 -05:00
2017-06-03 07:26:09 -04:00
c.JSON(200, convert.ToBranch(branch, commit))
2016-01-15 19:24:03 +01:00
}
2018-05-27 08:53:48 +08:00
// https://github.com/gogs/go-gogs-client/wiki/Repositories#list-branches
2017-06-03 07:26:09 -04:00
func ListBranches(c *context.APIContext) {
branches, err := c.Repo.Repository.GetBranches()
2016-01-15 19:24:03 +01:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetBranches", err)
2016-01-15 19:24:03 +01:00
return
}
2016-02-02 17:07:40 -05:00
apiBranches := make([]*api.Branch, len(branches))
for i := range branches {
2017-06-03 07:26:09 -04:00
commit, err := branches[i].GetCommit()
2016-01-15 19:24:03 +01:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetCommit", err)
return
2016-01-15 19:24:03 +01:00
}
2017-06-03 07:26:09 -04:00
apiBranches[i] = convert.ToBranch(branches[i], commit)
2016-01-15 19:24:03 +01:00
}
2016-02-02 17:07:40 -05:00
2017-06-03 07:26:09 -04:00
c.JSON(200, &apiBranches)
2016-01-15 19:24:03 +01:00
}