Skip to content

Commit ac2d97c

Browse files
authored
Link to tree views of submodules if possible (#33424)
This is a follow-up to #33097. When linking a submodule at a commit in either the repo view, or a diff when adding a new submodule, link to the tree view of that submodules intead of the individual commit. This shows the user the full tree, instead of the diff of the commit. This makes the assumption that the tree for a given SHA is at `<repo_url>/tree/<sha>`. This URL format is supported by both Github & Gitlab, but not Gitea. To fix this, add a redirect from `<username>/<repo>/tree/<ref>` to `<username>/<repo>/src/<ref>`, so that Gitea can support this URL structure.
1 parent dc7ddae commit ac2d97c

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

modules/git/commit_submodule_file.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func (sf *CommitSubmoduleFile) SubmoduleWebLink(ctx context.Context, optCommitID
4646
if len(optCommitID) == 2 {
4747
commitLink = sf.repoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
4848
} else if len(optCommitID) == 1 {
49-
commitLink = sf.repoLink + "/commit/" + optCommitID[0]
49+
commitLink = sf.repoLink + "/tree/" + optCommitID[0]
5050
} else {
51-
commitLink = sf.repoLink + "/commit/" + sf.refID
51+
commitLink = sf.repoLink + "/tree/" + sf.refID
5252
}
5353
return &SubmoduleWebLink{RepoWebLink: sf.repoLink, CommitWebLink: commitLink}
5454
}

modules/git/commit_submodule_file_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ func TestCommitSubmoduleLink(t *testing.T) {
1515

1616
wl := sf.SubmoduleWebLink(context.Background())
1717
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
18-
assert.Equal(t, "https://github.com/user/repo/commit/aaaa", wl.CommitWebLink)
18+
assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink)
1919

2020
wl = sf.SubmoduleWebLink(context.Background(), "1111")
2121
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
22-
assert.Equal(t, "https://github.com/user/repo/commit/1111", wl.CommitWebLink)
22+
assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink)
2323

2424
wl = sf.SubmoduleWebLink(context.Background(), "1111", "2222")
2525
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)

routers/web/repo/view_home.go

+6
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,9 @@ func Home(ctx *context.Context) {
412412

413413
ctx.HTML(http.StatusOK, tplRepoHome)
414414
}
415+
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))
420+
}

routers/web/web.go

+7
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,13 @@ func registerRoutes(m *web.Router) {
15841584
m.Get("/*", context.RepoRefByType(""), repo.Home) // "/*" route is deprecated, and kept for backward compatibility
15851585
}, repo.SetEditorconfigIfExists)
15861586

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)
1593+
15871594
m.Get("/forks", context.RepoRef(), repo.Forks)
15881595
m.Get("/commit/{sha:([a-f0-9]{7,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, repo.RawDiff)
15891596
m.Post("/lastcommit/*", context.RepoRefByType(git.RefTypeCommit), repo.LastCommit)

services/gitdiff/submodule_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func TestSubmoduleInfo(t *testing.T) {
230230
assert.EqualValues(t, "name", sdi.SubmoduleRepoLinkHTML(ctx))
231231

232232
sdi.SubmoduleFile = git.NewCommitSubmoduleFile("https://github.com/owner/repo", "1234")
233-
assert.EqualValues(t, `<a href="https://github.com/owner/repo/commit/1111">1111</a>`, sdi.CommitRefIDLinkHTML(ctx, "1111"))
233+
assert.EqualValues(t, `<a href="https://github.com/owner/repo/tree/1111">1111</a>`, sdi.CommitRefIDLinkHTML(ctx, "1111"))
234234
assert.EqualValues(t, `<a href="https://github.com/owner/repo/compare/aaaa...bbbb">aaaa...bbbb</a>`, sdi.CompareRefIDLinkHTML(ctx))
235235
assert.EqualValues(t, `<a href="https://github.com/owner/repo">name</a>`, sdi.SubmoduleRepoLinkHTML(ctx))
236236
}

0 commit comments

Comments
 (0)