@@ -765,35 +765,30 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
765
765
return ""
766
766
}
767
767
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 != "" {
774
772
return refName , RepoRefBranch
775
773
}
776
- if refName := getRefName (ctx , repo , RepoRefTag ); refName != "" {
774
+ if refName := getRefName (ctx , repo , reqRefPath , RepoRefTag ); refName != "" {
777
775
return refName , RepoRefTag
778
776
}
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 ]) {
783
778
// 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
786
781
}
787
-
788
- if refName := getRefName (ctx , repo , RepoRefBlob ); len (refName ) > 0 {
782
+ if refName := getRefName (ctx , repo , reqPath , RepoRefBlob ); refName != "" {
789
783
return refName , RepoRefBlob
790
784
}
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
791
787
repo .TreePath = reqPath
792
788
return repo .Repository .DefaultBranch , RepoRefBranch
793
789
}
794
790
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 {
797
792
switch pathType {
798
793
case RepoRefBranch :
799
794
ref := getRefNameFromPath (repo , path , repo .GitRepo .IsBranchExist )
@@ -889,7 +884,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
889
884
}
890
885
891
886
// Get default branch.
892
- if len (ctx .PathParam ("*" )) == 0 {
887
+ reqPath := ctx .PathParam ("*" )
888
+ if reqPath == "" {
893
889
refName = ctx .Repo .Repository .DefaultBranch
894
890
if ! ctx .Repo .GitRepo .IsBranchExist (refName ) {
895
891
brs , _ , err := ctx .Repo .GitRepo .GetBranches (0 , 1 )
@@ -914,12 +910,12 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
914
910
return
915
911
}
916
912
ctx .Repo .IsViewBranch = true
917
- } else {
913
+ } else { // there is a path in request
918
914
guessLegacyPath := refType == RepoRefUnknown
919
915
if guessLegacyPath {
920
- refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo )
916
+ refName , refType = getRefNameLegacy (ctx .Base , ctx .Repo , reqPath , "" )
921
917
} else {
922
- refName = getRefName (ctx .Base , ctx .Repo , refType )
918
+ refName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
923
919
}
924
920
ctx .Repo .RefName = refName
925
921
isRenamedBranch , has := ctx .Data ["IsRenamedBranch" ].(bool )
0 commit comments