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
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
"github.com/gogs/gogs/models/errors"
|
|
|
|
|
"github.com/gogs/gogs/pkg/context"
|
|
|
|
|
"github.com/gogs/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 {
|
2018-03-08 07:15:55 -05:00
|
|
|
if errors.IsErrBranchNotExist(err) {
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Error(404, "GetBranch", err)
|
2017-03-10 23:37:25 -05:00
|
|
|
} else {
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Error(500, "GetBranch", err)
|
2017-03-10 23:37:25 -05:00
|
|
|
}
|
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)
|
2016-01-16 10:36:16 +01:00
|
|
|
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
|
|
|
}
|