diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index b7970cb7c82f6..812ecc4857029 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -29,6 +29,7 @@ size: 8478 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 2 @@ -60,6 +61,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: true + default_unit: 1 - id: 3 @@ -91,6 +93,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 4 @@ -122,6 +125,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 5 @@ -152,6 +156,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 6 @@ -182,6 +187,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 7 @@ -212,6 +218,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 8 @@ -242,6 +249,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 9 @@ -272,6 +280,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 10 @@ -303,6 +312,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 11 @@ -334,6 +344,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 12 @@ -364,6 +375,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 13 @@ -394,6 +406,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 14 @@ -425,6 +438,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 15 @@ -456,6 +470,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 16 @@ -487,6 +502,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 17 @@ -517,6 +533,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 18 @@ -547,6 +564,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 19 @@ -577,6 +595,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 20 @@ -607,6 +626,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 21 @@ -637,6 +657,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 22 @@ -667,6 +688,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 23 @@ -697,6 +719,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 24 @@ -727,6 +750,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 25 @@ -757,6 +781,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 26 @@ -787,6 +812,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 27 @@ -817,6 +843,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 28 @@ -847,6 +874,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 29 @@ -877,6 +905,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 30 @@ -907,6 +936,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 31 @@ -938,6 +968,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 32 # org public repo @@ -968,6 +999,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 33 @@ -999,6 +1031,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 34 @@ -1029,6 +1062,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 35 @@ -1059,6 +1093,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 36 @@ -1090,6 +1125,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 37 @@ -1121,6 +1157,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 38 @@ -1152,6 +1189,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 39 @@ -1183,6 +1221,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 40 @@ -1214,6 +1253,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 41 @@ -1245,6 +1285,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 42 @@ -1276,6 +1317,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 43 @@ -1306,6 +1348,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 44 @@ -1337,6 +1380,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 45 @@ -1367,6 +1411,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 46 @@ -1398,6 +1443,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 47 @@ -1429,6 +1475,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 48 @@ -1460,6 +1507,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 49 @@ -1491,6 +1539,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 50 @@ -1522,6 +1571,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 51 @@ -1553,6 +1603,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 52 @@ -1584,6 +1635,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 53 @@ -1612,6 +1664,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 54 @@ -1624,6 +1677,7 @@ is_archived: false is_private: true status: 0 + default_unit: 1 - id: 55 @@ -1636,6 +1690,7 @@ is_private: true num_issues: 1 status: 0 + default_unit: 1 - id: 56 @@ -1649,6 +1704,7 @@ is_private: true status: 0 num_issues: 0 + default_unit: 1 - id: 57 @@ -1662,6 +1718,7 @@ is_private: false status: 0 num_issues: 0 + default_unit: 1 - id: 58 # org public repo @@ -1693,6 +1750,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 59 @@ -1706,6 +1764,7 @@ is_private: true status: 0 num_issues: 0 + default_unit: 1 - id: 60 @@ -1737,6 +1796,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 61 @@ -1768,6 +1828,7 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 - id: 62 @@ -1799,3 +1860,4 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + default_unit: 1 diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index ddf20d9542cfe..b5e962cc3b90e 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -364,6 +364,7 @@ func prepareMigrationTasks() []*migration { newMigration(304, "Add index for release sha1", v1_23.AddIndexForReleaseSha1), newMigration(305, "Add Repository Licenses", v1_23.AddRepositoryLicenses), newMigration(306, "Add BlockAdminMergeOverride to ProtectedBranch", v1_23.AddBlockAdminMergeOverrideBranchProtection), + newMigration(307, "Add DefaultUnit to Repository", v1_23.AddDefaultUnitToRepository), } return preparedMigrations } diff --git a/models/migrations/v1_23/v307.go b/models/migrations/v1_23/v307.go new file mode 100644 index 0000000000000..056cb200831e2 --- /dev/null +++ b/models/migrations/v1_23/v307.go @@ -0,0 +1,13 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_23 //nolint + +import "xorm.io/xorm" + +func AddDefaultUnitToRepository(x *xorm.Engine) error { + type Repository struct { + DefaultUnit int `xorm:"NOT NULL DEFAULT 1"` + } + return x.Sync(new(Repository)) +} diff --git a/models/repo/repo.go b/models/repo/repo.go index 68f8e16a21d58..ae642bd5606a3 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -168,6 +168,7 @@ type Repository struct { RenderingMetas map[string]string `xorm:"-"` DocumentRenderingMetas map[string]string `xorm:"-"` Units []*RepoUnit `xorm:"-"` + DefaultUnit unit.Type `xorm:"NOT NULL DEFAULT 1"` PrimaryLanguage *LanguageStat `xorm:"-"` IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"` diff --git a/models/unit/unit.go b/models/unit/unit.go index 3b62e5f982267..d07bb6782249e 100644 --- a/models/unit/unit.go +++ b/models/unit/unit.go @@ -206,7 +206,7 @@ var ( UnitCode = Unit{ TypeCode, "repo.code", - "/", + "/code", "repo.code.desc", 0, perm.AccessModeOwner, diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 7d9281b397387..3dcf3a9a8da36 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -771,8 +771,8 @@ func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) { } } -// Home render repository home page -func Home(ctx *context.Context) { +// HomeWithFeedCheck redirect to default unit or return feed +func HomeWithFeedCheck(ctx *context.Context) { if setting.Other.EnableFeed { isFeed, _, showFeedType := feed.GetFeedType(ctx.PathParam(":reponame"), ctx.Req) if isFeed { @@ -788,6 +788,15 @@ func Home(ctx *context.Context) { } } + defaultURI := ctx.Repo.Repository.MustGetUnit(ctx, ctx.Repo.Repository.DefaultUnit).Unit().URI + if defaultURI == "/" { // support legacy code units + defaultURI = "/code" + } + ctx.Redirect(ctx.Repo.RepoLink+defaultURI, http.StatusMovedPermanently) +} + +// CodeHome render repository code unit page +func CodeHome(ctx *context.Context) { checkHomeCodeViewable(ctx) if ctx.Written() { return diff --git a/routers/web/web.go b/routers/web/web.go index 83d116babd769..8f211d6b60530 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1139,8 +1139,10 @@ func registerRoutes(m *web.Router) { ) // end "/{username}/{reponame}/settings" - // user/org home, including rss feeds - m.Get("/{username}/{reponame}", ignSignIn, context.RepoAssignment, context.RepoRef(), repo.SetEditorconfigIfExists, repo.Home) + // user/org home, witch either redirects to default unit or return rss feeds + m.Get("/{username}/{reponame}", ignSignIn, context.RepoAssignment, context.RepoRef(), repo.SetEditorconfigIfExists, repo.HomeWithFeedCheck) + // show the code unit's home view + m.Get("/{username}/{reponame}/code", ignSignIn, context.RepoAssignment, context.RepoRef(), repo.SetEditorconfigIfExists, repo.CodeHome) // TODO: maybe it should relax the permission to allow "any access" m.Post("/{username}/{reponame}/markup", ignSignIn, context.RepoAssignment, context.RequireRepoReaderOr(unit.TypeCode, unit.TypeIssues, unit.TypePullRequests, unit.TypeReleases, unit.TypeWiki), web.Bind(structs.MarkupOption{}), misc.Markup) @@ -1579,10 +1581,10 @@ func registerRoutes(m *web.Router) { m.Get("/atom/branch/*", context.RepoRefByType(context.RepoRefBranch), feedEnabled, feed.RenderBranchFeed) m.Group("/src", func() { - m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Home) - m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Home) - m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.Home) - m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.Home) // "/*" route is deprecated, and kept for backward compatibility + m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.CodeHome) + m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.CodeHome) + m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.CodeHome) + m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.CodeHome) // "/*" route is deprecated, and kept for backward compatibility }, repo.SetEditorconfigIfExists) m.Get("/forks", context.RepoRef(), repo.Forks) diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index d52891b02a76a..5648e17226313 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -133,7 +133,7 @@ {{if not (or .Repository.IsBeingCreated .Repository.IsBroken)}}