@@ -20,6 +20,7 @@ import (
2020 "code.gitea.io/gitea/models/unittest"
2121 user_model "code.gitea.io/gitea/models/user"
2222 "code.gitea.io/gitea/modules/cache"
23+ git2 "code.gitea.io/gitea/modules/git"
2324 "code.gitea.io/gitea/modules/gitrepo"
2425 "code.gitea.io/gitea/modules/reqctx"
2526 "code.gitea.io/gitea/modules/session"
@@ -30,6 +31,7 @@ import (
3031
3132 "github.com/go-chi/chi/v5"
3233 "github.com/stretchr/testify/assert"
34+ "github.com/stretchr/testify/require"
3335)
3436
3537func mockRequest (t * testing.T , reqPath string ) * http.Request {
@@ -106,13 +108,13 @@ func MockPrivateContext(t *testing.T, reqPath string) (*context.PrivateContext,
106108// LoadRepo load a repo into a test context.
107109func LoadRepo (t * testing.T , ctx gocontext.Context , repoID int64 ) {
108110 var doer * user_model.User
109- repo := & context.Repository {}
111+ var repo * context.Repository
110112 switch ctx := ctx .(type ) {
111113 case * context.Context :
112- ctx . Repo = repo
114+ repo = ctx . Repo
113115 doer = ctx .Doer
114116 case * context.APIContext :
115- ctx . Repo = repo
117+ repo = ctx . Repo
116118 doer = ctx .Doer
117119 default :
118120 assert .FailNow (t , "context is not *context.Context or *context.APIContext" )
@@ -140,15 +142,17 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) {
140142 }
141143
142144 gitRepo , err := gitrepo .OpenRepository (ctx , repo .Repository )
143- assert .NoError (t , err )
145+ require .NoError (t , err )
144146 defer gitRepo .Close ()
145- branch , err := gitRepo . GetHEADBranch ()
146- assert . NoError ( t , err )
147- assert . NotNil ( t , branch )
148- if branch != nil {
149- repo .Commit , err = gitRepo . GetBranchCommit ( branch . Name )
150- assert . NoError ( t , err )
147+
148+ if repo . RefFullName == "" {
149+ repo . RefFullName = git2 . RefNameFromBranch ( repo . Repository . DefaultBranch )
150+ }
151+ if repo .RefFullName . IsPull () {
152+ repo . BranchName = repo . RefFullName . ShortName ( )
151153 }
154+ repo .Commit , err = gitRepo .GetCommit (repo .RefFullName .String ())
155+ require .NoError (t , err )
152156}
153157
154158// LoadUser load a user into a test context
0 commit comments