@@ -25,7 +25,9 @@ import (
2525 "code.gitea.io/gitea/modules/setting"
2626 "code.gitea.io/gitea/modules/storage"
2727 api "code.gitea.io/gitea/modules/structs"
28+ "code.gitea.io/gitea/modules/util"
2829 "code.gitea.io/gitea/modules/web"
30+ "code.gitea.io/gitea/routers/api/v1/utils"
2931 "code.gitea.io/gitea/routers/common"
3032 "code.gitea.io/gitea/services/context"
3133 pull_service "code.gitea.io/gitea/services/pull"
@@ -894,6 +896,17 @@ func DeleteFile(ctx *context.APIContext) {
894896 }
895897}
896898
899+ func resolveRefCommit (ctx * context.APIContext , ref string ) * utils.RefCommit {
900+ ref = util .IfZero (ref , ctx .Repo .Repository .DefaultBranch )
901+ refCommit , err := utils .ResolveRefCommit (ctx , ref )
902+ if errors .Is (err , util .ErrNotExist ) {
903+ ctx .APIErrorNotFound (err )
904+ } else if err != nil {
905+ ctx .APIErrorInternal (err )
906+ }
907+ return refCommit
908+ }
909+
897910// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
898911func GetContents (ctx * context.APIContext ) {
899912 // swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
@@ -928,18 +941,13 @@ func GetContents(ctx *context.APIContext) {
928941 // "404":
929942 // "$ref": "#/responses/notFound"
930943
931- if ! canReadFiles (ctx .Repo ) {
932- ctx .APIErrorInternal (repo_model.ErrUserDoesNotHaveAccessToRepo {
933- UserID : ctx .Doer .ID ,
934- RepoName : ctx .Repo .Repository .LowerName ,
935- })
944+ treePath := ctx .PathParam ("*" )
945+ refCommit := resolveRefCommit (ctx , ctx .FormTrim ("ref" ))
946+ if ctx .Written () {
936947 return
937948 }
938949
939- treePath := ctx .PathParam ("*" )
940- ref := ctx .FormTrim ("ref" )
941-
942- if fileList , err := files_service .GetContentsOrList (ctx , ctx .Repo .Repository , treePath , ref ); err != nil {
950+ if fileList , err := files_service .GetContentsOrList (ctx , ctx .Repo .Repository , refCommit , treePath ); err != nil {
943951 if git .IsErrNotExist (err ) {
944952 ctx .APIErrorNotFound ("GetContentsOrList" , err )
945953 return
@@ -1023,19 +1031,10 @@ func GetFiles(ctx *context.APIContext) {
10231031 // "$ref": "#/responses/notFound"
10241032
10251033 apiOpts := web .GetForm (ctx ).(* api.GetFilesOptions )
1026-
1027- ref := ctx .FormTrim ("ref" )
1028- if ref == "" {
1029- ref = ctx .Repo .Repository .DefaultBranch
1030- }
1031-
1032- if ! ctx .Repo .GitRepo .IsReferenceExist (ref ) {
1033- ctx .APIErrorNotFound ("GetFiles" , "ref does not exist" )
1034+ refCommit := resolveRefCommit (ctx , ctx .FormTrim ("ref" ))
1035+ if ctx .Written () {
10341036 return
10351037 }
1036-
1037- files := apiOpts .Files
1038- filesResponse := files_service .GetContentsListFromTrees (ctx , ctx .Repo .Repository , ref , files )
1039-
1040- ctx .JSON (http .StatusOK , filesResponse )
1038+ filesResponse := files_service .GetContentsListFromTrees (ctx , ctx .Repo .Repository , refCommit , apiOpts .Files )
1039+ ctx .JSON (http .StatusOK , util .SliceNilAsEmpty (filesResponse ))
10411040}
0 commit comments