From 2c4ea26154ac4f92886e0b179c98d24d0ccc0d0e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 21 Apr 2023 01:27:19 +0200 Subject: [PATCH 1/3] nit --- modules/context/api.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/context/api.go b/modules/context/api.go index f7a3384691246..64e99ef8924e0 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -342,6 +342,7 @@ func RepoRefForAPI(next http.Handler) http.Handler { return } ctx.Repo.Commit = commit + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() ctx.Repo.TreePath = ctx.Params("*") return } From ec4b7e9610c5e19413e06580801ca427e8d2a39e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 21 Apr 2023 01:50:52 +0200 Subject: [PATCH 2/3] RepoRefForAPI: use func(ctx *APIContext) instead of http.Handler --- modules/context/api.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 64e99ef8924e0..8b99a9c5d6f2d 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -322,10 +322,8 @@ func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) (cancel context } // RepoRefForAPI handles repository reference names when the ref name is not explicitly given -func RepoRefForAPI(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - ctx := GetAPIContext(req) - +func RepoRefForAPI(ctx *APIContext) { + { if ctx.Repo.GitRepo == nil { ctx.InternalServerError(fmt.Errorf("no open git repo")) return @@ -375,7 +373,5 @@ func RepoRefForAPI(next http.Handler) http.Handler { ctx.NotFound(fmt.Errorf("not exist: '%s'", ctx.Params("*"))) return } - - next.ServeHTTP(w, req) - }) + } } From 889e645864042f67af9f62580cbb554e8ed9cb64 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 21 Apr 2023 01:51:19 +0200 Subject: [PATCH 3/3] format --- modules/context/api.go | 82 +++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 8b99a9c5d6f2d..7196143606933 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -323,55 +323,53 @@ func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) (cancel context // RepoRefForAPI handles repository reference names when the ref name is not explicitly given func RepoRefForAPI(ctx *APIContext) { - { - if ctx.Repo.GitRepo == nil { - ctx.InternalServerError(fmt.Errorf("no open git repo")) - return - } + if ctx.Repo.GitRepo == nil { + ctx.InternalServerError(fmt.Errorf("no open git repo")) + return + } - if ref := ctx.FormTrim("ref"); len(ref) > 0 { - commit, err := ctx.Repo.GitRepo.GetCommit(ref) - if err != nil { - if git.IsErrNotExist(err) { - ctx.NotFound() - } else { - ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err) - } - return + if ref := ctx.FormTrim("ref"); len(ref) > 0 { + commit, err := ctx.Repo.GitRepo.GetCommit(ref) + if err != nil { + if git.IsErrNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err) } - ctx.Repo.Commit = commit - ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - ctx.Repo.TreePath = ctx.Params("*") return } + ctx.Repo.Commit = commit + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() + ctx.Repo.TreePath = ctx.Params("*") + return + } - var err error - refName := getRefName(ctx.Context, RepoRefAny) + var err error + refName := getRefName(ctx.Context, RepoRefAny) - if ctx.Repo.GitRepo.IsBranchExist(refName) { - ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName) - if err != nil { - ctx.InternalServerError(err) - return - } - ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - } else if ctx.Repo.GitRepo.IsTagExist(refName) { - ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName) - if err != nil { - ctx.InternalServerError(err) - return - } - ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - } else if len(refName) == git.SHAFullLength { - ctx.Repo.CommitID = refName - ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName) - if err != nil { - ctx.NotFound("GetCommit", err) - return - } - } else { - ctx.NotFound(fmt.Errorf("not exist: '%s'", ctx.Params("*"))) + if ctx.Repo.GitRepo.IsBranchExist(refName) { + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName) + if err != nil { + ctx.InternalServerError(err) return } + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() + } else if ctx.Repo.GitRepo.IsTagExist(refName) { + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() + } else if len(refName) == git.SHAFullLength { + ctx.Repo.CommitID = refName + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName) + if err != nil { + ctx.NotFound("GetCommit", err) + return + } + } else { + ctx.NotFound(fmt.Errorf("not exist: '%s'", ctx.Params("*"))) + return } }