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