@@ -765,35 +765,30 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
765765 return ""
766766}
767767
768- func getRefNameLegacy (ctx * Base , repo * Repository , optionalExtraRef ... string ) (string , RepoRefType ) {
769- extraRef := util .OptionalArg (optionalExtraRef )
770- reqPath := ctx .PathParam ("*" )
771- reqPath = path .Join (extraRef , reqPath )
772-
773- if refName := getRefName (ctx , repo , RepoRefBranch ); refName != "" {
768+ func getRefNameLegacy (ctx * Base , repo * Repository , reqPath , extraRef string ) (string , RepoRefType ) {
769+ reqRefPath := path .Join (extraRef , reqPath )
770+ reqRefPathParts := strings .Split (reqRefPath , "/" )
771+ if refName := getRefName (ctx , repo , reqRefPath , RepoRefBranch ); refName != "" {
774772 return refName , RepoRefBranch
775773 }
776- if refName := getRefName (ctx , repo , RepoRefTag ); refName != "" {
774+ if refName := getRefName (ctx , repo , reqRefPath , RepoRefTag ); refName != "" {
777775 return refName , RepoRefTag
778776 }
779-
780- // For legacy support only full commit sha
781- parts := strings .Split (reqPath , "/" )
782- if git .IsStringLikelyCommitID (git .ObjectFormatFromName (repo .Repository .ObjectFormatName ), parts [0 ]) {
777+ if git .IsStringLikelyCommitID (git .ObjectFormatFromName (repo .Repository .ObjectFormatName ), reqRefPathParts [0 ]) {
783778 // FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
784- repo .TreePath = strings .Join (parts [1 :], "/" )
785- return parts [0 ], RepoRefCommit
779+ repo .TreePath = strings .Join (reqRefPathParts [1 :], "/" )
780+ return reqRefPathParts [0 ], RepoRefCommit
786781 }
787-
788- if refName := getRefName (ctx , repo , RepoRefBlob ); len (refName ) > 0 {
782+ if refName := getRefName (ctx , repo , reqPath , RepoRefBlob ); refName != "" {
789783 return refName , RepoRefBlob
790784 }
785+ // FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
786+ // "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
791787 repo .TreePath = reqPath
792788 return repo .Repository .DefaultBranch , RepoRefBranch
793789}
794790
795- func getRefName (ctx * Base , repo * Repository , pathType RepoRefType ) string {
796- path := ctx .PathParam ("*" )
791+ func getRefName (ctx * Base , repo * Repository , path string , pathType RepoRefType ) string {
797792 switch pathType {
798793 case RepoRefBranch :
799794 ref := getRefNameFromPath (repo , path , repo .GitRepo .IsBranchExist )
@@ -889,7 +884,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
889884 }
890885
891886 // Get default branch.
892- if len (ctx .PathParam ("*" )) == 0 {
887+ reqPath := ctx .PathParam ("*" )
888+ if reqPath == "" {
893889 refName = ctx .Repo .Repository .DefaultBranch
894890 if ! ctx .Repo .GitRepo .IsBranchExist (refName ) {
895891 brs , _ , err := ctx .Repo .GitRepo .GetBranches (0 , 1 )
@@ -914,12 +910,12 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
914910 return
915911 }
916912 ctx .Repo .IsViewBranch = true
917- } else {
913+ } else { // there is a path in request
918914 guessLegacyPath := refType == RepoRefUnknown
919915 if guessLegacyPath {
920- refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo )
916+ refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
921917 } else {
922- refName = getRefName (ctx .Base , ctx .Repo , refType )
918+ refName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
923919 }
924920 ctx .Repo .RefName = refName
925921 isRenamedBranch , has := ctx .Data ["IsRenamedBranch" ].(bool )
0 commit comments