@@ -769,35 +769,30 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
769
769
return ""
770
770
}
771
771
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 != "" {
778
776
return refName , RepoRefBranch
779
777
}
780
- if refName := getRefName (ctx , repo , RepoRefTag ); refName != "" {
778
+ if refName := getRefName (ctx , repo , reqRefPath , RepoRefTag ); refName != "" {
781
779
return refName , RepoRefTag
782
780
}
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 ]) {
787
782
// 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
790
785
}
791
-
792
- if refName := getRefName (ctx , repo , RepoRefBlob ); len (refName ) > 0 {
786
+ if refName := getRefName (ctx , repo , reqPath , RepoRefBlob ); refName != "" {
793
787
return refName , RepoRefBlob
794
788
}
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
795
791
repo .TreePath = reqPath
796
792
return repo .Repository .DefaultBranch , RepoRefBranch
797
793
}
798
794
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 {
801
796
switch pathType {
802
797
case RepoRefBranch :
803
798
ref := getRefNameFromPath (repo , path , repo .GitRepo .IsBranchExist )
@@ -900,7 +895,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
900
895
}
901
896
902
897
// Get default branch.
903
- if len (ctx .PathParam ("*" )) == 0 {
898
+ reqPath := ctx .PathParam ("*" )
899
+ if reqPath == "" {
904
900
refName = ctx .Repo .Repository .DefaultBranch
905
901
if ! ctx .Repo .GitRepo .IsBranchExist (refName ) {
906
902
brs , _ , err := ctx .Repo .GitRepo .GetBranches (0 , 1 )
@@ -925,12 +921,12 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
925
921
return cancel
926
922
}
927
923
ctx .Repo .IsViewBranch = true
928
- } else {
924
+ } else { // there is a path in request
929
925
guessLegacyPath := refType == RepoRefUnknown
930
926
if guessLegacyPath {
931
- refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo )
927
+ refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
932
928
} else {
933
- refName = getRefName (ctx .Base , ctx .Repo , refType )
929
+ refName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
934
930
}
935
931
ctx .Repo .RefName = refName
936
932
isRenamedBranch , has := ctx .Data ["IsRenamedBranch" ].(bool )
0 commit comments