2018-11-27 21:52:20 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2019-12-20 17:07:12 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-11-27 21:52:20 +00:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 09:33:00 +00:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-05-11 10:21:34 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2018-11-27 21:52:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetGitAllRefs get ref or an list all the refs of a repository
|
|
|
|
func GetGitAllRefs(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/git/refs repository repoListAllGitRefs
|
|
|
|
// ---
|
|
|
|
// summary: Get specified ref or filtered repository's refs
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Reference"
|
|
|
|
// "$ref": "#/responses/ReferenceList"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
getGitRefsInternal(ctx, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGitRefs get ref or an filteresd list of refs of a repository
|
|
|
|
func GetGitRefs(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/git/refs/{ref} repository repoListGitRefs
|
|
|
|
// ---
|
|
|
|
// summary: Get specified ref or filtered repository's refs
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: ref
|
|
|
|
// in: path
|
|
|
|
// description: part or full name of the ref
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Reference"
|
|
|
|
// "$ref": "#/responses/ReferenceList"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
getGitRefsInternal(ctx, ctx.Params("*"))
|
|
|
|
}
|
|
|
|
|
2019-08-09 02:13:03 +00:00
|
|
|
func getGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
|
2018-11-27 21:52:20 +00:00
|
|
|
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
|
|
|
|
if err != nil {
|
2019-08-09 02:13:03 +00:00
|
|
|
return nil, "OpenRepository", err
|
2018-11-27 21:52:20 +00:00
|
|
|
}
|
2019-11-13 07:01:19 +00:00
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2018-11-27 21:52:20 +00:00
|
|
|
if len(filter) > 0 {
|
|
|
|
filter = "refs/" + filter
|
|
|
|
}
|
|
|
|
refs, err := gitRepo.GetRefsFiltered(filter)
|
2019-08-09 02:13:03 +00:00
|
|
|
return refs, "GetRefsFiltered", err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGitRefsInternal(ctx *context.APIContext, filter string) {
|
|
|
|
refs, lastMethodName, err := getGitRefs(ctx, filter)
|
2018-11-27 21:52:20 +00:00
|
|
|
if err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
|
2018-11-27 21:52:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(refs) == 0 {
|
2019-03-19 02:29:43 +00:00
|
|
|
ctx.NotFound()
|
2018-11-27 21:52:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiRefs := make([]*api.Reference, len(refs))
|
|
|
|
for i := range refs {
|
|
|
|
apiRefs[i] = &api.Reference{
|
|
|
|
Ref: refs[i].Name,
|
|
|
|
URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Name,
|
|
|
|
Object: &api.GitObject{
|
|
|
|
SHA: refs[i].Object.String(),
|
|
|
|
Type: refs[i].Type,
|
2019-06-08 14:31:11 +00:00
|
|
|
URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Type + "s/" + refs[i].Object.String(),
|
2018-11-27 21:52:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If single reference is found and it matches filter exactly return it as object
|
|
|
|
if len(apiRefs) == 1 && apiRefs[0].Ref == filter {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.JSON(http.StatusOK, &apiRefs[0])
|
2018-11-27 21:52:20 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.JSON(http.StatusOK, &apiRefs)
|
2018-11-27 21:52:20 +00:00
|
|
|
}
|