2018-11-28 05:52:20 +08: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-21 01:07:12 +08:00
|
|
|
"net/http"
|
2021-11-17 02:18:25 +08:00
|
|
|
"net/url"
|
2019-12-21 01:07:12 +08:00
|
|
|
|
2018-11-28 05:52:20 +08:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-05-11 18:21:34 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-11-17 02:18:25 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-07-13 15:14:14 +08:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2018-11-28 05:52:20 +08: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":
|
2022-08-31 10:15:45 +08:00
|
|
|
// # "$ref": "#/responses/Reference" TODO: swagger doesnt support different output formats by ref
|
2018-11-28 05:52:20 +08:00
|
|
|
// "$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":
|
2022-08-31 10:15:45 +08:00
|
|
|
// # "$ref": "#/responses/Reference" TODO: swagger doesnt support different output formats by ref
|
2018-11-28 05:52:20 +08:00
|
|
|
// "$ref": "#/responses/ReferenceList"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
getGitRefsInternal(ctx, ctx.Params("*"))
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:13:03 +08:00
|
|
|
func getGitRefsInternal(ctx *context.APIContext, filter string) {
|
2021-07-13 15:14:14 +08:00
|
|
|
refs, lastMethodName, err := utils.GetGitRefs(ctx, filter)
|
2018-11-28 05:52:20 +08:00
|
|
|
if err != nil {
|
2019-12-21 01:07:12 +08:00
|
|
|
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
|
2018-11-28 05:52:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(refs) == 0 {
|
2019-03-19 10:29:43 +08:00
|
|
|
ctx.NotFound()
|
2018-11-28 05:52:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiRefs := make([]*api.Reference, len(refs))
|
|
|
|
for i := range refs {
|
|
|
|
apiRefs[i] = &api.Reference{
|
|
|
|
Ref: refs[i].Name,
|
2021-11-17 02:18:25 +08:00
|
|
|
URL: ctx.Repo.Repository.APIURL() + "/git/" + util.PathEscapeSegments(refs[i].Name),
|
2018-11-28 05:52:20 +08:00
|
|
|
Object: &api.GitObject{
|
|
|
|
SHA: refs[i].Object.String(),
|
|
|
|
Type: refs[i].Type,
|
2021-11-17 02:18:25 +08:00
|
|
|
URL: ctx.Repo.Repository.APIURL() + "/git/" + url.PathEscape(refs[i].Type) + "s/" + url.PathEscape(refs[i].Object.String()),
|
2018-11-28 05:52:20 +08: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-21 01:07:12 +08:00
|
|
|
ctx.JSON(http.StatusOK, &apiRefs[0])
|
2018-11-28 05:52:20 +08:00
|
|
|
return
|
|
|
|
}
|
2019-12-21 01:07:12 +08:00
|
|
|
ctx.JSON(http.StatusOK, &apiRefs)
|
2018-11-28 05:52:20 +08:00
|
|
|
}
|