Skip to content

Commit 3b72f8e

Browse files
committed
Link to tree views of submodules if possible
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>`.
1 parent a9577e0 commit 3b72f8e

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

modules/git/commit_submodule_file.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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/web.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package web
55

66
import (
77
"net/http"
8+
"path"
89
"strings"
910

1011
auth_model "code.gitea.io/gitea/models/auth"
@@ -1584,6 +1585,18 @@ func registerRoutes(m *web.Router) {
15841585
m.Get("/*", context.RepoRefByType(""), repo.Home) // "/*" route is deprecated, and kept for backward compatibility
15851586
}, repo.SetEditorconfigIfExists)
15861587

1588+
// Add a /tree/* path to redirect to the deprecated /src/*
1589+
// path. This emulates both Github & Gitlab's URL structure.
1590+
m.Get("/tree/*", func(ctx *context.Context) {
1591+
prefix := "/"
1592+
if setting.AppSubURL != "" {
1593+
prefix = setting.AppSubURL
1594+
}
1595+
1596+
url := path.Join(prefix, ctx.PathParam("username"), ctx.PathParam("reponame"), "src", ctx.PathParam("*"))
1597+
ctx.Redirect(url)
1598+
})
1599+
15871600
m.Get("/forks", context.RepoRef(), repo.Forks)
15881601
m.Get("/commit/{sha:([a-f0-9]{7,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, repo.RawDiff)
15891602
m.Post("/lastcommit/*", context.RepoRefByType(git.RefTypeCommit), repo.LastCommit)

0 commit comments

Comments
 (0)