Browse Source

Fix redirect url of legacy commits route (#2825)

Morlinest 1 year ago
parent
commit
1f04c00e2f
2 changed files with 4 additions and 12 deletions
  1. 3 1
      integrations/links_test.go
  2. 1 11
      modules/context/repo.go

+ 3 - 1
integrations/links_test.go

@@ -7,8 +7,10 @@ package integrations
7 7
 import (
8 8
 	"fmt"
9 9
 	"net/http"
10
+	"path"
10 11
 	"testing"
11 12
 
13
+	"code.gitea.io/gitea/modules/setting"
12 14
 	api "code.gitea.io/sdk/gitea"
13 15
 
14 16
 	"github.com/stretchr/testify/assert"
@@ -50,7 +52,7 @@ func TestRedirectsNoLogin(t *testing.T) {
50 52
 	for link, redirectLink := range redirects {
51 53
 		req := NewRequest(t, "GET", link)
52 54
 		resp := MakeRequest(t, req, http.StatusFound)
53
-		assert.EqualValues(t, redirectLink, RedirectURL(t, resp))
55
+		assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), RedirectURL(t, resp))
54 56
 	}
55 57
 }
56 58
 

+ 1 - 11
modules/context/repo.go

@@ -521,16 +521,6 @@ 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
-
534 524
 // RepoRefByType handles repository reference name for a specific type
535 525
 // of repository reference
536 526
 func RepoRefByType(refType RepoRefType) macaron.Handler {
@@ -617,7 +607,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
617 607
 
618 608
 			if refType == RepoRefLegacy {
619 609
 				// redirect from old URL scheme to new URL scheme
620
-				ctx.Redirect(repoRefRedirect(ctx))
610
+				ctx.Redirect(path.Join(setting.AppSubURL, strings.TrimSuffix(ctx.Req.URL.String(), ctx.Params("*")), ctx.Repo.BranchNameSubURL()))
621 611
 				return
622 612
 			}
623 613
 		}