Skip to content

Commit d28a64e

Browse files
GnarwhalGusted
authored andcommitted
fix: handle viewing a submodule entry (go-gitea#7261)
- When trying to view a submodule directory via the normal `/src/branch/` path, generate a redirect link to the submodule location. - Resolves forgejo/forgejo#5267 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7261 Reviewed-by: Gusted <[email protected]> Co-authored-by: Gnarwhal <[email protected]> Co-committed-by: Gnarwhal <[email protected]>
1 parent 77b0275 commit d28a64e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

routers/web/repo/view.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,15 @@ func renderHomeCode(ctx *context.Context) {
10441044
return
10451045
}
10461046

1047-
if entry.IsDir() {
1047+
if entry.IsSubModule() {
1048+
subModuleURL, err := ctx.Repo.Commit.GetSubModule(entry.Name())
1049+
if err != nil {
1050+
HandleGitError(ctx, "Repo.Commit.GetSubModule", err)
1051+
return
1052+
}
1053+
subModuleFile := git.NewSubModuleFile(ctx.Repo.Commit, subModuleURL, entry.ID.String())
1054+
ctx.Redirect(subModuleFile.RefURL(setting.AppURL, ctx.Repo.Repository.FullName(), setting.SSH.Domain))
1055+
} else if entry.IsDir() {
10481056
renderDirectory(ctx)
10491057
} else {
10501058
renderFile(ctx, entry)

tests/integration/repo_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,12 @@ func TestRepoSubmoduleView(t *testing.T) {
14261426

14271427
htmlDoc := NewHTMLParser(t, resp.Body)
14281428
htmlDoc.AssertElement(t, fmt.Sprintf(`tr[data-entryname="repo1"] a[href="%s"]`, u.JoinPath("/user2/repo1").String()), true)
1429+
1430+
// Check that a link to the submodule returns a redirect and that the redirect link is correct.
1431+
req = NewRequest(t, "GET", "/"+repo.FullName()+"/src/branch/"+repo.DefaultBranch+"/repo1")
1432+
resp = MakeRequest(t, req, http.StatusSeeOther)
1433+
1434+
assert.Equal(t, u.JoinPath("/user2/repo1").String(), resp.Header().Get("Location"))
14291435
})
14301436
}
14311437

0 commit comments

Comments
 (0)