@@ -19,8 +19,8 @@ import (
19
19
unit_model "code.gitea.io/gitea/models/unit"
20
20
user_model "code.gitea.io/gitea/models/user"
21
21
"code.gitea.io/gitea/modules/git"
22
- giturl "code.gitea.io/gitea/modules/git/url"
23
22
"code.gitea.io/gitea/modules/gitrepo"
23
+ "code.gitea.io/gitea/modules/htmlutil"
24
24
"code.gitea.io/gitea/modules/httplib"
25
25
"code.gitea.io/gitea/modules/log"
26
26
repo_module "code.gitea.io/gitea/modules/repository"
@@ -258,35 +258,41 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
258
258
ctx .Redirect (link )
259
259
}
260
260
261
- func handleRepoViewSubmodule (ctx * context.Context , submodule * git.SubModule ) {
262
- // TODO: it needs to use git.NewCommitSubmoduleFile and SubmoduleWebLinkTree to correctly handle relative paths
263
- submoduleRepoURL , err := giturl .ParseRepositoryURL (ctx , submodule .URL )
264
- if err != nil {
265
- HandleGitError (ctx , "handleRepoViewSubmodule: ParseRepositoryURL" , err )
261
+ func isViewHomeOnlyContent (ctx * context.Context ) bool {
262
+ return ctx .FormBool ("only_content" )
263
+ }
264
+
265
+ func handleRepoViewSubmodule (ctx * context.Context , commitSubmoduleFile * git.CommitSubmoduleFile ) {
266
+ submoduleWebLink := commitSubmoduleFile .SubmoduleWebLinkTree (ctx )
267
+ if submoduleWebLink == nil {
268
+ ctx .Data ["NotFoundPrompt" ] = ctx .Repo .TreePath
269
+ ctx .NotFound (nil )
266
270
return
267
271
}
268
- submoduleURL := giturl .MakeRepositoryWebLink (submoduleRepoURL )
269
- if httplib .IsCurrentGiteaSiteURL (ctx , submoduleURL ) {
270
- ctx .RedirectToCurrentSite (submoduleURL )
271
- } else {
272
+
273
+ redirectLink := submoduleWebLink .CommitWebLink
274
+ if isViewHomeOnlyContent (ctx ) {
275
+ ctx .Resp .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
276
+ _ , _ = ctx .Resp .Write ([]byte (htmlutil .HTMLFormat (`<a href="%s">%s</a>` , redirectLink , redirectLink )))
277
+ } else if ! httplib .IsCurrentGiteaSiteURL (ctx , redirectLink ) {
272
278
// don't auto-redirect to external URL, to avoid open redirect or phishing
273
- ctx .Data ["NotFoundPrompt" ] = submoduleURL
279
+ ctx .Data ["NotFoundPrompt" ] = redirectLink
274
280
ctx .NotFound (nil )
281
+ } else {
282
+ ctx .Redirect (submoduleWebLink .CommitWebLink )
275
283
}
276
284
}
277
285
278
286
func prepareToRenderDirOrFile (entry * git.TreeEntry ) func (ctx * context.Context ) {
279
287
return func (ctx * context.Context ) {
280
288
if entry .IsSubModule () {
281
- submodule , err := ctx .Repo .Commit . GetSubModule ( entry .Name () )
289
+ commitSubmoduleFile , err := git . GetCommitInfoSubmoduleFile ( ctx .Repo .RepoLink , ctx . Repo . TreePath , ctx . Repo . Commit , entry .ID )
282
290
if err != nil {
283
- HandleGitError (ctx , "prepareToRenderDirOrFile: GetSubModule " , err )
291
+ HandleGitError (ctx , "prepareToRenderDirOrFile: GetCommitInfoSubmoduleFile " , err )
284
292
return
285
293
}
286
- handleRepoViewSubmodule (ctx , submodule )
287
- return
288
- }
289
- if entry .IsDir () {
294
+ handleRepoViewSubmodule (ctx , commitSubmoduleFile )
295
+ } else if entry .IsDir () {
290
296
prepareToRenderDirectory (ctx )
291
297
} else {
292
298
prepareFileView (ctx , entry )
@@ -441,7 +447,7 @@ func Home(ctx *context.Context) {
441
447
}
442
448
}
443
449
444
- if ctx . FormBool ( "only_content" ) {
450
+ if isViewHomeOnlyContent ( ctx ) {
445
451
ctx .HTML (http .StatusOK , tplRepoViewContent )
446
452
} else if len (treeNames ) != 0 {
447
453
ctx .HTML (http .StatusOK , tplRepoView )
0 commit comments