@@ -19,8 +19,8 @@ import (
1919 unit_model "code.gitea.io/gitea/models/unit"
2020 user_model "code.gitea.io/gitea/models/user"
2121 "code.gitea.io/gitea/modules/git"
22- giturl "code.gitea.io/gitea/modules/git/url"
2322 "code.gitea.io/gitea/modules/gitrepo"
23+ "code.gitea.io/gitea/modules/htmlutil"
2424 "code.gitea.io/gitea/modules/httplib"
2525 "code.gitea.io/gitea/modules/log"
2626 repo_module "code.gitea.io/gitea/modules/repository"
@@ -258,35 +258,41 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
258258 ctx .Redirect (link )
259259}
260260
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 )
266270 return
267271 }
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 ) {
272278 // don't auto-redirect to external URL, to avoid open redirect or phishing
273- ctx .Data ["NotFoundPrompt" ] = submoduleURL
279+ ctx .Data ["NotFoundPrompt" ] = redirectLink
274280 ctx .NotFound (nil )
281+ } else {
282+ ctx .Redirect (submoduleWebLink .CommitWebLink )
275283 }
276284}
277285
278286func prepareToRenderDirOrFile (entry * git.TreeEntry ) func (ctx * context.Context ) {
279287 return func (ctx * context.Context ) {
280288 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 )
282290 if err != nil {
283- HandleGitError (ctx , "prepareToRenderDirOrFile: GetSubModule " , err )
291+ HandleGitError (ctx , "prepareToRenderDirOrFile: GetCommitInfoSubmoduleFile " , err )
284292 return
285293 }
286- handleRepoViewSubmodule (ctx , submodule )
287- return
288- }
289- if entry .IsDir () {
294+ handleRepoViewSubmodule (ctx , commitSubmoduleFile )
295+ } else if entry .IsDir () {
290296 prepareToRenderDirectory (ctx )
291297 } else {
292298 prepareFileView (ctx , entry )
@@ -441,7 +447,7 @@ func Home(ctx *context.Context) {
441447 }
442448 }
443449
444- if ctx . FormBool ( "only_content" ) {
450+ if isViewHomeOnlyContent ( ctx ) {
445451 ctx .HTML (http .StatusOK , tplRepoViewContent )
446452 } else if len (treeNames ) != 0 {
447453 ctx .HTML (http .StatusOK , tplRepoView )
0 commit comments