From 546ae82c033e23d78cbde1aa124afca1dce07f16 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 7 Feb 2021 19:35:51 +0000 Subject: [PATCH] Add support for ref parameter to get raw file API Fix #14597 Signed-off-by: Andrew Thornton --- routers/api/v1/repo/file.go | 22 +++++++++++++++++++++- templates/swagger/v1_json.tmpl | 6 ++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 2bd57f1460fe3..37e02874b4ffd 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -43,6 +43,11 @@ func GetRawFile(ctx *context.APIContext) { // description: filepath of the file to get // type: string // required: true + // - name: ref + // in: query + // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)" + // type: string + // required: false // responses: // 200: // description: success @@ -54,7 +59,22 @@ func GetRawFile(ctx *context.APIContext) { return } - blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath) + commit := ctx.Repo.Commit + + if ref := ctx.QueryTrim("ref"); len(ref) > 0 { + var err error + commit, err = ctx.Repo.GitRepo.GetCommit(ref) + if err != nil { + if git.IsErrNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err) + } + return + } + } + + blob, err := commit.GetBlobByPath(ctx.Repo.TreePath) if err != nil { if git.IsErrNotExist(err) { ctx.NotFound() diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 45f396f2830f6..28aa617799d21 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -7845,6 +7845,12 @@ "name": "filepath", "in": "path", "required": true + }, + { + "type": "string", + "description": "The name of the commit/branch/tag. Default the repository’s default branch (usually master)", + "name": "ref", + "in": "query" } ], "responses": {