@@ -521,6 +521,16 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
521
521
return ""
522
522
}
523
523
524
+ // URL to redirect to for deprecated URL scheme
525
+ func repoRefRedirect (ctx * Context ) string {
526
+ urlPath := ctx .Req .URL .String ()
527
+ idx := strings .LastIndex (urlPath , ctx .Params ("*" ))
528
+ if idx < 0 {
529
+ idx = len (urlPath )
530
+ }
531
+ return path .Join (urlPath [:idx ], ctx .Repo .BranchNameSubURL ())
532
+ }
533
+
524
534
// RepoRefByType handles repository reference name for a specific type
525
535
// of repository reference
526
536
func RepoRefByType (refType RepoRefType ) macaron.Handler {
@@ -548,6 +558,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
548
558
// Get default branch.
549
559
if len (ctx .Params ("*" )) == 0 {
550
560
refName = ctx .Repo .Repository .DefaultBranch
561
+ ctx .Repo .BranchName = refName
551
562
if ! ctx .Repo .GitRepo .IsBranchExist (refName ) {
552
563
brs , err := ctx .Repo .GitRepo .GetBranches ()
553
564
if err != nil {
@@ -571,6 +582,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
571
582
572
583
} else {
573
584
refName = getRefName (ctx , refType )
585
+ ctx .Repo .BranchName = refName
574
586
if ctx .Repo .GitRepo .IsBranchExist (refName ) {
575
587
ctx .Repo .IsViewBranch = true
576
588
@@ -605,15 +617,11 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
605
617
606
618
if refType == RepoRefUnknown {
607
619
// redirect from old URL scheme to new URL scheme
608
- ctx .Redirect (path .Join (
609
- ctx .Repo .BranchNameSubURL (),
610
- ctx .Repo .TreePath ,
611
- ))
620
+ ctx .Redirect (repoRefRedirect (ctx ))
612
621
return
613
622
}
614
623
}
615
624
616
- ctx .Repo .BranchName = refName
617
625
ctx .Data ["BranchName" ] = ctx .Repo .BranchName
618
626
ctx .Data ["BranchNameSubURL" ] = ctx .Repo .BranchNameSubURL ()
619
627
ctx .Data ["CommitID" ] = ctx .Repo .CommitID
0 commit comments