From 411ce55851f75e9e9f7278c8f61d01c16d3afb2c Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 4 Jan 2025 18:47:24 +0800 Subject: [PATCH 1/3] Fix empty git repo handling logic (#33101) Fix #33092 --- models/repo/repo.go | 2 ++ options/locale/locale_en-US.ini | 1 + routers/web/repo/view_home.go | 46 +++++++++++++++++++++------------ services/context/repo.go | 3 --- templates/repo/empty.tmpl | 21 +++++++-------- templates/repo/header.tmpl | 2 +- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/models/repo/repo.go b/models/repo/repo.go index 8d211caf40b90..c5060a419f458 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -276,6 +276,8 @@ func (repo *Repository) IsBroken() bool { } // MarkAsBrokenEmpty marks the repo as broken and empty +// FIXME: the status "broken" and "is_empty" were abused, +// The code always set them together, no way to distinguish whether a repo is really "empty" or "broken" func (repo *Repository) MarkAsBrokenEmpty() { repo.Status = RepositoryBroken repo.IsEmpty = true diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 16bd0fe8b6edd..2c10a4dbe1f8e 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1231,6 +1231,7 @@ create_new_repo_command = Creating a new repository on the command line push_exist_repo = Pushing an existing repository from the command line empty_message = This repository does not contain any content. broken_message = The Git data underlying this repository cannot be read. Contact the administrator of this instance or delete this repository. +no_branch = This repository doesn’t have any branches. code = Code code.desc = Access source code, files, commits and branches. diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index b318c4a621a60..1d886a5ffb4ca 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -223,16 +223,37 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) { } } +func updateContextRepoEmptyAndStatus(ctx *context.Context, empty bool, status repo_model.RepositoryStatus) { + ctx.Repo.Repository.IsEmpty = empty + if ctx.Repo.Repository.Status == repo_model.RepositoryReady || ctx.Repo.Repository.Status == repo_model.RepositoryBroken { + ctx.Repo.Repository.Status = status // only handle ready and broken status, leave other status as-is + } + if err := repo_model.UpdateRepositoryCols(ctx, ctx.Repo.Repository, "is_empty", "status"); err != nil { + ctx.ServerError("updateContextRepoEmptyAndStatus: UpdateRepositoryCols", err) + return + } +} + func handleRepoEmptyOrBroken(ctx *context.Context) { showEmpty := true - var err error if ctx.Repo.GitRepo != nil { - showEmpty, err = ctx.Repo.GitRepo.IsEmpty() + reallyEmpty, err := ctx.Repo.GitRepo.IsEmpty() if err != nil { + showEmpty = true // the repo is broken + updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryBroken) log.Error("GitRepo.IsEmpty: %v", err) - ctx.Repo.Repository.Status = repo_model.RepositoryBroken - showEmpty = true ctx.Flash.Error(ctx.Tr("error.occurred"), true) + } else if reallyEmpty { + showEmpty = true // the repo is really empty + updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady) + } else if ctx.Repo.Commit == nil { + showEmpty = true // it is not really empty, but there is no branch + // at the moment, other repo units like "actions" are not able to handle such case, + // so we just mark the repo as empty to prevent from displaying these units. + updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady) + } else { + // the repo is actually not empty and has branches, need to update the database later + showEmpty = false } } if showEmpty { @@ -240,18 +261,11 @@ func handleRepoEmptyOrBroken(ctx *context.Context) { return } - // the repo is not really empty, so we should update the modal in database - // such problem may be caused by: - // 1) an error occurs during pushing/receiving. 2) the user replaces an empty git repo manually - // and even more: the IsEmpty flag is deeply broken and should be removed with the UI changed to manage to cope with empty repos. - // it's possible for a repository to be non-empty by that flag but still 500 - // because there are no branches - only tags -or the default branch is non-extant as it has been 0-pushed. - ctx.Repo.Repository.IsEmpty = false - if err = repo_model.UpdateRepositoryCols(ctx, ctx.Repo.Repository, "is_empty"); err != nil { - ctx.ServerError("UpdateRepositoryCols", err) - return - } - if err = repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil { + // The repo is not really empty, so we should update the model in database, such problem may be caused by: + // 1) an error occurs during pushing/receiving. + // 2) the user replaces an empty git repo manually. + updateContextRepoEmptyAndStatus(ctx, false, repo_model.RepositoryReady) + if err := repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil { ctx.ServerError("UpdateRepoSize", err) return } diff --git a/services/context/repo.go b/services/context/repo.go index 9b5443911030a..1e7c4303471e0 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -908,10 +908,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func refName = brs[0].Name } else if len(brs) == 0 { log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path) - ctx.Repo.Repository.MarkAsBrokenEmpty() } else { log.Error("GetBranches error: %v", err) - ctx.Repo.Repository.MarkAsBrokenEmpty() } } ctx.Repo.RefName = refName @@ -922,7 +920,6 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func } else if strings.Contains(err.Error(), "fatal: not a git repository") || strings.Contains(err.Error(), "object does not exist") { // if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users log.Error("GetBranchCommit: %v", err) - ctx.Repo.Repository.MarkAsBrokenEmpty() } else { ctx.ServerError("GetBranchCommit", err) return cancel diff --git a/templates/repo/empty.tmpl b/templates/repo/empty.tmpl index 7170fe360203d..dfda5b7b2b4d2 100644 --- a/templates/repo/empty.tmpl +++ b/templates/repo/empty.tmpl @@ -14,14 +14,13 @@ {{end}} {{end}} + {{if .Repository.IsBroken}} -
- {{ctx.Locale.Tr "repo.broken_message"}} -
+
{{ctx.Locale.Tr "repo.broken_message"}}
+ {{else if .Repository.IsEmpty}} +
{{ctx.Locale.Tr "repo.no_branch"}}
{{else if .CanWriteCode}} -

- {{ctx.Locale.Tr "repo.quick_guide"}} -

+

{{ctx.Locale.Tr "repo.quick_guide"}}

{{ctx.Locale.Tr "repo.clone_this_repo"}} {{ctx.Locale.Tr "repo.clone_helper" "http://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository"}}

@@ -66,12 +65,10 @@ git push -u origin {{.Repository.DefaultBranch}}
{{end}} - {{else}} -
- {{ctx.Locale.Tr "repo.empty_message"}} -
- {{end}} - + + {{else}} +
{{ctx.Locale.Tr "repo.empty_message"}}
+ {{end}} diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index c3ae697f31fbf..e187ef1a87dd7 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -162,7 +162,7 @@ {{end}} - {{if and .EnableActions (.Permission.CanRead ctx.Consts.RepoUnitTypeActions)}} + {{if and .EnableActions (.Permission.CanRead ctx.Consts.RepoUnitTypeActions) (not .IsEmptyRepo)}} {{svg "octicon-play"}} {{ctx.Locale.Tr "actions.actions"}} {{if .Repository.NumOpenActionRuns}} From cb06f629b5ce543325dc745aba2b96fc3d5d4bd2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 4 Jan 2025 19:13:40 +0800 Subject: [PATCH 2/3] apply a ui fix from main --- web_src/css/base.css | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web_src/css/base.css b/web_src/css/base.css index a0bfefebf5ac1..1a354c96633fe 100644 --- a/web_src/css/base.css +++ b/web_src/css/base.css @@ -336,8 +336,13 @@ a.label, border-color: var(--color-secondary); } +.ui.dropdown .menu > .header { + text-transform: none; /* reset fomantic's "uppercase" */ +} + .ui.dropdown .menu > .header:not(.ui) { color: var(--color-text); + font-size: 0.95em; /* reset fomantic's small font-size */ } .ui.dropdown .menu > .item { @@ -691,11 +696,6 @@ input:-webkit-autofill:active, box-shadow: 0 6px 18px var(--color-shadow) !important; } -.ui.dropdown .menu > .header { - font-size: 0.8em; - text-transform: none; /* reset fomantic's "uppercase" */ -} - .ui .text.left { text-align: left !important; } From 9e3143ee67670139cc11a619a77498bf993b838e Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 5 Jan 2025 20:57:32 +0800 Subject: [PATCH 3/3] backport #33108 (fix mobile view) --- templates/base/head_navbar.tmpl | 6 +++--- templates/repo/commit_page.tmpl | 2 +- templates/user/dashboard/navbar.tmpl | 4 ++-- web_src/css/modules/navbar.css | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index 951ee590d1cc7..bbb2a7ef0172f 100644 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -11,7 +11,7 @@ -