@@ -769,35 +769,30 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
769769 return ""
770770}
771771
772- func getRefNameLegacy (ctx * Base , repo * Repository , optionalExtraRef ... string ) (string , RepoRefType ) {
773- extraRef := util .OptionalArg (optionalExtraRef )
774- reqPath := ctx .PathParam ("*" )
775- reqPath = path .Join (extraRef , reqPath )
776-
777- if refName := getRefName (ctx , repo , RepoRefBranch ); refName != "" {
772+ func getRefNameLegacy (ctx * Base , repo * Repository , reqPath , extraRef string ) (string , RepoRefType ) {
773+ reqRefPath := path .Join (extraRef , reqPath )
774+ reqRefPathParts := strings .Split (reqRefPath , "/" )
775+ if refName := getRefName (ctx , repo , reqRefPath , RepoRefBranch ); refName != "" {
778776 return refName , RepoRefBranch
779777 }
780- if refName := getRefName (ctx , repo , RepoRefTag ); refName != "" {
778+ if refName := getRefName (ctx , repo , reqRefPath , RepoRefTag ); refName != "" {
781779 return refName , RepoRefTag
782780 }
783-
784- // For legacy support only full commit sha
785- parts := strings .Split (reqPath , "/" )
786- if git .IsStringLikelyCommitID (git .ObjectFormatFromName (repo .Repository .ObjectFormatName ), parts [0 ]) {
781+ if git .IsStringLikelyCommitID (git .ObjectFormatFromName (repo .Repository .ObjectFormatName ), reqRefPathParts [0 ]) {
787782 // FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
788- repo .TreePath = strings .Join (parts [1 :], "/" )
789- return parts [0 ], RepoRefCommit
783+ repo .TreePath = strings .Join (reqRefPathParts [1 :], "/" )
784+ return reqRefPathParts [0 ], RepoRefCommit
790785 }
791-
792- if refName := getRefName (ctx , repo , RepoRefBlob ); len (refName ) > 0 {
786+ if refName := getRefName (ctx , repo , reqPath , RepoRefBlob ); refName != "" {
793787 return refName , RepoRefBlob
794788 }
789+ // FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
790+ // "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
795791 repo .TreePath = reqPath
796792 return repo .Repository .DefaultBranch , RepoRefBranch
797793}
798794
799- func getRefName (ctx * Base , repo * Repository , pathType RepoRefType ) string {
800- path := ctx .PathParam ("*" )
795+ func getRefName (ctx * Base , repo * Repository , path string , pathType RepoRefType ) string {
801796 switch pathType {
802797 case RepoRefBranch :
803798 ref := getRefNameFromPath (repo , path , repo .GitRepo .IsBranchExist )
@@ -900,7 +895,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
900895 }
901896
902897 // Get default branch.
903- if len (ctx .PathParam ("*" )) == 0 {
898+ reqPath := ctx .PathParam ("*" )
899+ if reqPath == "" {
904900 refName = ctx .Repo .Repository .DefaultBranch
905901 if ! ctx .Repo .GitRepo .IsBranchExist (refName ) {
906902 brs , _ , err := ctx .Repo .GitRepo .GetBranches (0 , 1 )
@@ -925,12 +921,12 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
925921 return cancel
926922 }
927923 ctx .Repo .IsViewBranch = true
928- } else {
924+ } else { // there is a path in request
929925 guessLegacyPath := refType == RepoRefUnknown
930926 if guessLegacyPath {
931- refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo )
927+ refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
932928 } else {
933- refName = getRefName (ctx .Base , ctx .Repo , refType )
929+ refName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
934930 }
935931 ctx .Repo .RefName = refName
936932 isRenamedBranch , has := ctx .Data ["IsRenamedBranch" ].(bool )
0 commit comments