@@ -686,24 +686,24 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
686686 return ""
687687}
688688
689- func getRefNameLegacy (ctx * Base , repo * Repository , reqPath , extraRef string ) (string , git.RefType ) {
689+ func getRefNameLegacy (ctx * Base , repo * Repository , reqPath , extraRef string ) (refName string , refType git.RefType , fallbackDefaultBranch bool ) {
690690 reqRefPath := path .Join (extraRef , reqPath )
691691 reqRefPathParts := strings .Split (reqRefPath , "/" )
692692 if refName := getRefName (ctx , repo , reqRefPath , git .RefTypeBranch ); refName != "" {
693- return refName , git .RefTypeBranch
693+ return refName , git .RefTypeBranch , false
694694 }
695695 if refName := getRefName (ctx , repo , reqRefPath , git .RefTypeTag ); refName != "" {
696- return refName , git .RefTypeTag
696+ return refName , git .RefTypeTag , false
697697 }
698698 if git .IsStringLikelyCommitID (git .ObjectFormatFromName (repo .Repository .ObjectFormatName ), reqRefPathParts [0 ]) {
699699 // FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
700700 repo .TreePath = strings .Join (reqRefPathParts [1 :], "/" )
701- return reqRefPathParts [0 ], git .RefTypeCommit
701+ return reqRefPathParts [0 ], git .RefTypeCommit , false
702702 }
703703 // FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
704704 // "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
705705 repo .TreePath = reqPath
706- return repo .Repository .DefaultBranch , git .RefTypeBranch
706+ return repo .Repository .DefaultBranch , git .RefTypeBranch , true
707707}
708708
709709func getRefName (ctx * Base , repo * Repository , path string , refType git.RefType ) string {
@@ -838,8 +838,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
838838 }
839839 } else { // there is a path in request
840840 guessLegacyPath := refType == ""
841+ fallbackDefaultBranch := false
841842 if guessLegacyPath {
842- refShortName , refType = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
843+ refShortName , refType , fallbackDefaultBranch = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
843844 } else {
844845 refShortName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
845846 }
@@ -897,12 +898,24 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
897898
898899 if guessLegacyPath {
899900 // redirect from old URL scheme to new URL scheme
900- prefix := strings .TrimPrefix (setting .AppSubURL + strings .ToLower (strings .TrimSuffix (ctx .Req .URL .Path , ctx .PathParam ("*" ))), strings .ToLower (ctx .Repo .RepoLink ))
901- redirect := path .Join (
902- ctx .Repo .RepoLink ,
903- util .PathEscapeSegments (prefix ),
904- ctx .Repo .RefTypeNameSubURL (),
905- util .PathEscapeSegments (ctx .Repo .TreePath ))
901+ // * /user2/repo1/commits/master => /user2/repo1/commits/branch/master
902+ // * /user2/repo1/src/master => /user2/repo1/src/branch/master
903+ // * /user2/repo1/src/README.md => /user2/repo1/src/branch/master/README.md (fallback to default branch)
904+ var redirect string
905+ refSubPath := "src"
906+ // remove the "/subpath/owner/repo/" prefix, the names are case-insensitive
907+ remainingLowerPath , cut := strings .CutPrefix (setting .AppSubURL + strings .ToLower (ctx .Req .URL .Path ), strings .ToLower (ctx .Repo .RepoLink )+ "/" )
908+ if cut {
909+ refSubPath , _ , _ = strings .Cut (remainingLowerPath , "/" ) // it could be "src" or "commits"
910+ }
911+ if fallbackDefaultBranch {
912+ redirect = fmt .Sprintf ("%s/%s/%s/%s/%s" , ctx .Repo .RepoLink , refSubPath , refType , util .PathEscapeSegments (refShortName ), ctx .PathParamRaw ("*" ))
913+ } else {
914+ redirect = fmt .Sprintf ("%s/%s/%s/%s" , ctx .Repo .RepoLink , refSubPath , refType , ctx .PathParamRaw ("*" ))
915+ }
916+ if ctx .Req .URL .RawQuery != "" {
917+ redirect += "?" + ctx .Req .URL .RawQuery
918+ }
906919 ctx .Redirect (redirect )
907920 return
908921 }
0 commit comments