Skip to content

Commit 22f3ab9

Browse files
committed
fix
1 parent 256b94e commit 22f3ab9

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

routers/web/repo/view_home.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,15 @@ func Home(ctx *context.Context) {
413413
ctx.HTML(http.StatusOK, tplRepoHome)
414414
}
415415

416-
// HomeRedirect redirects from /tree/* to /src/* in order to maintain a similar URL structure.
417-
func HomeRedirect(ctx *context.Context) {
418-
remainder := ctx.PathParam("*")
419-
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + util.PathEscapeSegments(remainder))
416+
func RedirectRepoTreeToSrc(ctx *context.Context) {
417+
// Redirect "/owner/repo/tree/*" requests to "/owner/repo/src/*",
418+
// then use the deprecated "/src/*" handler to guess the ref type and render a file list page.
419+
// This is done intentionally so that Gitea's repo URL structure matches other forges (GitHub/GitLab) provide,
420+
// allowing we to construct submodule URLs across forges easily.
421+
// For example, when viewing a submodule, we can simply construct the link as:
422+
// * "https://gitea/owner/repo/tree/{CommitID}"
423+
// * "https://github/owner/repo/tree/{CommitID}"
424+
// * "https://gitlab/owner/repo/tree/{CommitID}"
425+
// Then no matter which forge the submodule is using, the link works.
426+
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.PathParamRaw("*"))
420427
}

routers/web/web.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -1583,13 +1583,7 @@ func registerRoutes(m *web.Router) {
15831583
m.Get("/commit/*", context.RepoRefByType(git.RefTypeCommit), repo.Home)
15841584
m.Get("/*", context.RepoRefByType(""), repo.Home) // "/*" route is deprecated, and kept for backward compatibility
15851585
}, repo.SetEditorconfigIfExists)
1586-
1587-
// Add a /tree/* path to redirect to the /src/* path, which
1588-
// will redirect to the canonical URL for that ref. This is
1589-
// included so that Gitea's repo URL structure matches what
1590-
// other forges provide, allowing clients to construct URLs
1591-
// that work across forges.
1592-
m.Get("/tree/*", repo.HomeRedirect)
1586+
m.Get("/tree/*", repo.RedirectRepoTreeToSrc) // redirect "/owner/repo/tree/*" requests to "/owner/repo/src/*"
15931587

15941588
m.Get("/forks", context.RepoRef(), repo.Forks)
15951589
m.Get("/commit/{sha:([a-f0-9]{7,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, repo.RawDiff)

services/context/repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
897897

898898
if guessLegacyPath {
899899
// redirect from old URL scheme to new URL scheme
900+
// FIXME: the path handling is not right here
900901
prefix := strings.TrimPrefix(setting.AppSubURL+strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path, ctx.PathParam("*"))), strings.ToLower(ctx.Repo.RepoLink))
901902
redirect := path.Join(
902903
ctx.Repo.RepoLink,

tests/integration/links_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ func TestRedirectsNoLogin(t *testing.T) {
5252
redirects := []struct{ from, to string }{
5353
{"/user2/repo1/commits/master", "/user2/repo1/commits/branch/master"},
5454
{"/user2/repo1/src/master", "/user2/repo1/src/branch/master"},
55-
{"/user2/repo1/src/master/file.txt", "/user2/repo1/src/branch/master/file.txt"},
55+
{"/user2/repo1/src/master/a%2fb.txt", "/user2/repo1/src/branch/master/a%2fb.txt"},
5656
{"/user2/repo1/src/master/directory/file.txt", "/user2/repo1/src/branch/master/directory/file.txt"},
57-
{"/user/avatar/Ghost/-1", "/assets/img/avatar_default.png"},
57+
{"/user2/repo1/tree/a%2fb", "/user2/repo1/src/a%2fb"},
58+
{"/user/avatar/GhosT/-1", "/assets/img/avatar_default.png"},
59+
{"/user/avatar/Gitea-ActionS/0", "/assets/img/avatar_default.png"},
5860
{"/api/v1/swagger", "/api/swagger"},
5961
}
6062
for _, c := range redirects {

0 commit comments

Comments
 (0)