mirror of
https://github.com/go-gitea/gitea.git
synced 2025-01-31 09:29:29 +08:00
Link to tree views of submodules if possible (#33424)
This is a follow-up to https://github.com/go-gitea/gitea/pull/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.
This commit is contained in:
parent
dc7ddaee2a
commit
ac2d97cb61
|
@ -46,9 +46,9 @@ func (sf *CommitSubmoduleFile) SubmoduleWebLink(ctx context.Context, optCommitID
|
|||
if len(optCommitID) == 2 {
|
||||
commitLink = sf.repoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
|
||||
} else if len(optCommitID) == 1 {
|
||||
commitLink = sf.repoLink + "/commit/" + optCommitID[0]
|
||||
commitLink = sf.repoLink + "/tree/" + optCommitID[0]
|
||||
} else {
|
||||
commitLink = sf.repoLink + "/commit/" + sf.refID
|
||||
commitLink = sf.repoLink + "/tree/" + sf.refID
|
||||
}
|
||||
return &SubmoduleWebLink{RepoWebLink: sf.repoLink, CommitWebLink: commitLink}
|
||||
}
|
||||
|
|
|
@ -15,11 +15,11 @@ func TestCommitSubmoduleLink(t *testing.T) {
|
|||
|
||||
wl := sf.SubmoduleWebLink(context.Background())
|
||||
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
|
||||
assert.Equal(t, "https://github.com/user/repo/commit/aaaa", wl.CommitWebLink)
|
||||
assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink)
|
||||
|
||||
wl = sf.SubmoduleWebLink(context.Background(), "1111")
|
||||
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
|
||||
assert.Equal(t, "https://github.com/user/repo/commit/1111", wl.CommitWebLink)
|
||||
assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink)
|
||||
|
||||
wl = sf.SubmoduleWebLink(context.Background(), "1111", "2222")
|
||||
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
|
||||
|
|
|
@ -412,3 +412,9 @@ func Home(ctx *context.Context) {
|
|||
|
||||
ctx.HTML(http.StatusOK, tplRepoHome)
|
||||
}
|
||||
|
||||
// HomeRedirect redirects from /tree/* to /src/* in order to maintain a similar URL structure.
|
||||
func HomeRedirect(ctx *context.Context) {
|
||||
remainder := ctx.PathParam("*")
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + util.PathEscapeSegments(remainder))
|
||||
}
|
||||
|
|
|
@ -1584,6 +1584,13 @@ func registerRoutes(m *web.Router) {
|
|||
m.Get("/*", context.RepoRefByType(""), repo.Home) // "/*" route is deprecated, and kept for backward compatibility
|
||||
}, repo.SetEditorconfigIfExists)
|
||||
|
||||
// Add a /tree/* path to redirect to the /src/* path, which
|
||||
// will redirect to the canonical URL for that ref. This is
|
||||
// included so that Gitea's repo URL structure matches what
|
||||
// other forges provide, allowing clients to construct URLs
|
||||
// that work across forges.
|
||||
m.Get("/tree/*", repo.HomeRedirect)
|
||||
|
||||
m.Get("/forks", context.RepoRef(), repo.Forks)
|
||||
m.Get("/commit/{sha:([a-f0-9]{7,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, repo.RawDiff)
|
||||
m.Post("/lastcommit/*", context.RepoRefByType(git.RefTypeCommit), repo.LastCommit)
|
||||
|
|
|
@ -230,7 +230,7 @@ func TestSubmoduleInfo(t *testing.T) {
|
|||
assert.EqualValues(t, "name", sdi.SubmoduleRepoLinkHTML(ctx))
|
||||
|
||||
sdi.SubmoduleFile = git.NewCommitSubmoduleFile("https://github.com/owner/repo", "1234")
|
||||
assert.EqualValues(t, `<a href="https://github.com/owner/repo/commit/1111">1111</a>`, sdi.CommitRefIDLinkHTML(ctx, "1111"))
|
||||
assert.EqualValues(t, `<a href="https://github.com/owner/repo/tree/1111">1111</a>`, sdi.CommitRefIDLinkHTML(ctx, "1111"))
|
||||
assert.EqualValues(t, `<a href="https://github.com/owner/repo/compare/aaaa...bbbb">aaaa...bbbb</a>`, sdi.CompareRefIDLinkHTML(ctx))
|
||||
assert.EqualValues(t, `<a href="https://github.com/owner/repo">name</a>`, sdi.SubmoduleRepoLinkHTML(ctx))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user