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