@@ -686,24 +686,24 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
686
686
return ""
687
687
}
688
688
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 ) {
690
690
reqRefPath := path .Join (extraRef , reqPath )
691
691
reqRefPathParts := strings .Split (reqRefPath , "/" )
692
692
if refName := getRefName (ctx , repo , reqRefPath , git .RefTypeBranch ); refName != "" {
693
- return refName , git .RefTypeBranch
693
+ return refName , git .RefTypeBranch , false
694
694
}
695
695
if refName := getRefName (ctx , repo , reqRefPath , git .RefTypeTag ); refName != "" {
696
- return refName , git .RefTypeTag
696
+ return refName , git .RefTypeTag , false
697
697
}
698
698
if git .IsStringLikelyCommitID (git .ObjectFormatFromName (repo .Repository .ObjectFormatName ), reqRefPathParts [0 ]) {
699
699
// FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
700
700
repo .TreePath = strings .Join (reqRefPathParts [1 :], "/" )
701
- return reqRefPathParts [0 ], git .RefTypeCommit
701
+ return reqRefPathParts [0 ], git .RefTypeCommit , false
702
702
}
703
703
// FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
704
704
// "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
705
705
repo .TreePath = reqPath
706
- return repo .Repository .DefaultBranch , git .RefTypeBranch
706
+ return repo .Repository .DefaultBranch , git .RefTypeBranch , true
707
707
}
708
708
709
709
func getRefName (ctx * Base , repo * Repository , path string , refType git.RefType ) string {
@@ -838,8 +838,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
838
838
}
839
839
} else { // there is a path in request
840
840
guessLegacyPath := refType == ""
841
+ fallbackDefaultBranch := false
841
842
if guessLegacyPath {
842
- refShortName , refType = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
843
+ refShortName , refType , fallbackDefaultBranch = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
843
844
} else {
844
845
refShortName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
845
846
}
@@ -897,12 +898,24 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
897
898
898
899
if guessLegacyPath {
899
900
// 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
+ }
906
919
ctx .Redirect (redirect )
907
920
return
908
921
}
0 commit comments