@@ -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
}
@@ -899,15 +900,19 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
899
900
// redirect from old URL scheme to new URL scheme
900
901
// * /user2/repo1/commits/master => /user2/repo1/commits/branch/master
901
902
// * /user2/repo1/src/master => /user2/repo1/src/branch/master
902
-
903
+ // * /user2/repo1/src/README.md => /user2/repo1/src/branch/master/README.md (fallback to default branch)
903
904
var redirect string
904
905
refSubPath := "src"
905
906
// remove the "/subpath/owner/repo/" prefix, the names are case-insensitive
906
907
remainingLowerPath , cut := strings .CutPrefix (setting .AppSubURL + strings .ToLower (ctx .Req .URL .Path ), strings .ToLower (ctx .Repo .RepoLink )+ "/" )
907
908
if cut {
908
909
refSubPath , _ , _ = strings .Cut (remainingLowerPath , "/" ) // it could be "src" or "commits"
909
910
}
910
- redirect = fmt .Sprintf ("%s/%s/%s/%s" , ctx .Repo .RepoLink , refSubPath , refType , ctx .PathParamRaw ("*" ))
911
+ if fallbackDefaultBranch {
912
+ redirect = fmt .Sprintf ("%s/%s/%s/%s/%s" , ctx .Repo .RepoLink , refSubPath , refType , refShortName , ctx .PathParamRaw ("*" ))
913
+ } else {
914
+ redirect = fmt .Sprintf ("%s/%s/%s/%s" , ctx .Repo .RepoLink , refSubPath , refType , ctx .PathParamRaw ("*" ))
915
+ }
911
916
if ctx .Req .URL .RawQuery != "" {
912
917
redirect += "?" + ctx .Req .URL .RawQuery
913
918
}
0 commit comments