@@ -20,8 +20,8 @@ import (
2020 unit_model "code.gitea.io/gitea/models/unit"
2121 user_model "code.gitea.io/gitea/models/user"
2222 "code.gitea.io/gitea/modules/git"
23- giturl "code.gitea.io/gitea/modules/git/url"
2423 "code.gitea.io/gitea/modules/gitrepo"
24+ "code.gitea.io/gitea/modules/htmlutil"
2525 "code.gitea.io/gitea/modules/httplib"
2626 "code.gitea.io/gitea/modules/log"
2727 repo_module "code.gitea.io/gitea/modules/repository"
@@ -309,35 +309,41 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
309309 ctx .Redirect (link )
310310}
311311
312- func handleRepoViewSubmodule (ctx * context.Context , submodule * git.SubModule ) {
313- // TODO: it needs to use git.NewCommitSubmoduleFile and SubmoduleWebLinkTree to correctly handle relative paths
314- submoduleRepoURL , err := giturl .ParseRepositoryURL (ctx , submodule .URL )
315- if err != nil {
316- HandleGitError (ctx , "handleRepoViewSubmodule: ParseRepositoryURL" , err )
312+ func isViewHomeOnlyContent (ctx * context.Context ) bool {
313+ return ctx .FormBool ("only_content" )
314+ }
315+
316+ func handleRepoViewSubmodule (ctx * context.Context , commitSubmoduleFile * git.CommitSubmoduleFile ) {
317+ submoduleWebLink := commitSubmoduleFile .SubmoduleWebLinkTree (ctx )
318+ if submoduleWebLink == nil {
319+ ctx .Data ["NotFoundPrompt" ] = ctx .Repo .TreePath
320+ ctx .NotFound (nil )
317321 return
318322 }
319- submoduleURL := giturl .MakeRepositoryWebLink (submoduleRepoURL )
320- if httplib .IsCurrentGiteaSiteURL (ctx , submoduleURL ) {
321- ctx .RedirectToCurrentSite (submoduleURL )
322- } else {
323+
324+ redirectLink := submoduleWebLink .CommitWebLink
325+ if isViewHomeOnlyContent (ctx ) {
326+ ctx .Resp .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
327+ _ , _ = ctx .Resp .Write ([]byte (htmlutil .HTMLFormat (`<a href="%s">%s</a>` , redirectLink , redirectLink )))
328+ } else if ! httplib .IsCurrentGiteaSiteURL (ctx , redirectLink ) {
323329 // don't auto-redirect to external URL, to avoid open redirect or phishing
324- ctx .Data ["NotFoundPrompt" ] = submoduleURL
330+ ctx .Data ["NotFoundPrompt" ] = redirectLink
325331 ctx .NotFound (nil )
332+ } else {
333+ ctx .Redirect (submoduleWebLink .CommitWebLink )
326334 }
327335}
328336
329337func prepareToRenderDirOrFile (entry * git.TreeEntry ) func (ctx * context.Context ) {
330338 return func (ctx * context.Context ) {
331339 if entry .IsSubModule () {
332- submodule , err := ctx .Repo .Commit . GetSubModule ( entry .Name () )
340+ commitSubmoduleFile , err := git . GetCommitInfoSubmoduleFile ( ctx .Repo .RepoLink , ctx . Repo . TreePath , ctx . Repo . Commit , entry .ID )
333341 if err != nil {
334- HandleGitError (ctx , "prepareToRenderDirOrFile: GetSubModule " , err )
342+ HandleGitError (ctx , "prepareToRenderDirOrFile: GetCommitInfoSubmoduleFile " , err )
335343 return
336344 }
337- handleRepoViewSubmodule (ctx , submodule )
338- return
339- }
340- if entry .IsDir () {
345+ handleRepoViewSubmodule (ctx , commitSubmoduleFile )
346+ } else if entry .IsDir () {
341347 prepareToRenderDirectory (ctx )
342348 } else {
343349 prepareToRenderFile (ctx , entry )
@@ -473,7 +479,7 @@ func Home(ctx *context.Context) {
473479 }
474480 }
475481
476- if ctx . FormBool ( "only_content" ) {
482+ if isViewHomeOnlyContent ( ctx ) {
477483 ctx .HTML (http .StatusOK , tplRepoViewContent )
478484 } else if len (treeNames ) != 0 {
479485 ctx .HTML (http .StatusOK , tplRepoView )
0 commit comments