Skip to content

Commit e637008

Browse files
authored
Fix empty git repo handling logic and fix mobile view (#33101) (#33102)
Backport #33101 and UI fix from main (including #33108)
1 parent fd28151 commit e637008

File tree

11 files changed

+56
-44
lines changed

11 files changed

+56
-44
lines changed

Diff for: models/repo/repo.go

+2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ func (repo *Repository) IsBroken() bool {
276276
}
277277

278278
// MarkAsBrokenEmpty marks the repo as broken and empty
279+
// FIXME: the status "broken" and "is_empty" were abused,
280+
// The code always set them together, no way to distinguish whether a repo is really "empty" or "broken"
279281
func (repo *Repository) MarkAsBrokenEmpty() {
280282
repo.Status = RepositoryBroken
281283
repo.IsEmpty = true

Diff for: options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,7 @@ create_new_repo_command = Creating a new repository on the command line
12311231
push_exist_repo = Pushing an existing repository from the command line
12321232
empty_message = This repository does not contain any content.
12331233
broken_message = The Git data underlying this repository cannot be read. Contact the administrator of this instance or delete this repository.
1234+
no_branch = This repository doesn’t have any branches.
12341235
12351236
code = Code
12361237
code.desc = Access source code, files, commits and branches.

Diff for: routers/web/repo/view_home.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -223,35 +223,49 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) {
223223
}
224224
}
225225

226+
func updateContextRepoEmptyAndStatus(ctx *context.Context, empty bool, status repo_model.RepositoryStatus) {
227+
ctx.Repo.Repository.IsEmpty = empty
228+
if ctx.Repo.Repository.Status == repo_model.RepositoryReady || ctx.Repo.Repository.Status == repo_model.RepositoryBroken {
229+
ctx.Repo.Repository.Status = status // only handle ready and broken status, leave other status as-is
230+
}
231+
if err := repo_model.UpdateRepositoryCols(ctx, ctx.Repo.Repository, "is_empty", "status"); err != nil {
232+
ctx.ServerError("updateContextRepoEmptyAndStatus: UpdateRepositoryCols", err)
233+
return
234+
}
235+
}
236+
226237
func handleRepoEmptyOrBroken(ctx *context.Context) {
227238
showEmpty := true
228-
var err error
229239
if ctx.Repo.GitRepo != nil {
230-
showEmpty, err = ctx.Repo.GitRepo.IsEmpty()
240+
reallyEmpty, err := ctx.Repo.GitRepo.IsEmpty()
231241
if err != nil {
242+
showEmpty = true // the repo is broken
243+
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryBroken)
232244
log.Error("GitRepo.IsEmpty: %v", err)
233-
ctx.Repo.Repository.Status = repo_model.RepositoryBroken
234-
showEmpty = true
235245
ctx.Flash.Error(ctx.Tr("error.occurred"), true)
246+
} else if reallyEmpty {
247+
showEmpty = true // the repo is really empty
248+
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady)
249+
} else if ctx.Repo.Commit == nil {
250+
showEmpty = true // it is not really empty, but there is no branch
251+
// at the moment, other repo units like "actions" are not able to handle such case,
252+
// so we just mark the repo as empty to prevent from displaying these units.
253+
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady)
254+
} else {
255+
// the repo is actually not empty and has branches, need to update the database later
256+
showEmpty = false
236257
}
237258
}
238259
if showEmpty {
239260
ctx.HTML(http.StatusOK, tplRepoEMPTY)
240261
return
241262
}
242263

243-
// the repo is not really empty, so we should update the modal in database
244-
// such problem may be caused by:
245-
// 1) an error occurs during pushing/receiving. 2) the user replaces an empty git repo manually
246-
// and even more: the IsEmpty flag is deeply broken and should be removed with the UI changed to manage to cope with empty repos.
247-
// it's possible for a repository to be non-empty by that flag but still 500
248-
// because there are no branches - only tags -or the default branch is non-extant as it has been 0-pushed.
249-
ctx.Repo.Repository.IsEmpty = false
250-
if err = repo_model.UpdateRepositoryCols(ctx, ctx.Repo.Repository, "is_empty"); err != nil {
251-
ctx.ServerError("UpdateRepositoryCols", err)
252-
return
253-
}
254-
if err = repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil {
264+
// The repo is not really empty, so we should update the model in database, such problem may be caused by:
265+
// 1) an error occurs during pushing/receiving.
266+
// 2) the user replaces an empty git repo manually.
267+
updateContextRepoEmptyAndStatus(ctx, false, repo_model.RepositoryReady)
268+
if err := repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil {
255269
ctx.ServerError("UpdateRepoSize", err)
256270
return
257271
}

Diff for: services/context/repo.go

-3
Original file line numberDiff line numberDiff line change
@@ -908,10 +908,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
908908
refName = brs[0].Name
909909
} else if len(brs) == 0 {
910910
log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path)
911-
ctx.Repo.Repository.MarkAsBrokenEmpty()
912911
} else {
913912
log.Error("GetBranches error: %v", err)
914-
ctx.Repo.Repository.MarkAsBrokenEmpty()
915913
}
916914
}
917915
ctx.Repo.RefName = refName
@@ -922,7 +920,6 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
922920
} else if strings.Contains(err.Error(), "fatal: not a git repository") || strings.Contains(err.Error(), "object does not exist") {
923921
// if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users
924922
log.Error("GetBranchCommit: %v", err)
925-
ctx.Repo.Repository.MarkAsBrokenEmpty()
926923
} else {
927924
ctx.ServerError("GetBranchCommit", err)
928925
return cancel

Diff for: templates/base/head_navbar.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</a>
1212

1313
<!-- mobile right menu, it must be here because in mobile view, each item is a flex column, the first item is a full row column -->
14-
<div class="ui secondary menu item navbar-mobile-right only-mobile">
14+
<div class="ui secondary menu navbar-mobile-right only-mobile">
1515
{{if and .IsSigned EnableTimetracking .ActiveStopwatch}}
1616
<a id="mobile-stopwatch-icon" class="active-stopwatch item tw-mx-0" href="{{.ActiveStopwatch.IssueLink}}" title="{{ctx.Locale.Tr "active_stopwatch"}}" data-seconds="{{.ActiveStopwatch.Seconds}}">
1717
<div class="tw-relative">
@@ -70,7 +70,7 @@
7070
<span class="not-mobile">{{svg "octicon-triangle-down"}}</span>
7171
</span>
7272
<div class="menu user-menu">
73-
<div class="ui header">
73+
<div class="header">
7474
{{ctx.Locale.Tr "signed_in_as"}} <strong>{{.SignedUser.Name}}</strong>
7575
</div>
7676

@@ -128,7 +128,7 @@
128128
<span class="not-mobile">{{svg "octicon-triangle-down"}}</span>
129129
</span>
130130
<div class="menu user-menu">
131-
<div class="ui header">
131+
<div class="header">
132132
{{ctx.Locale.Tr "signed_in_as"}} <strong>{{.SignedUser.Name}}</strong>
133133
</div>
134134

Diff for: templates/repo/commit_page.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{{ctx.Locale.Tr "repo.commit.operations"}}
3131
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
3232
<div class="menu">
33-
<div class="ui header">{{ctx.Locale.Tr "repo.commit.operations"}}</div>
33+
<div class="header">{{ctx.Locale.Tr "repo.commit.operations"}}</div>
3434
<div class="divider"></div>
3535
<div class="item show-create-branch-modal"
3636
data-content="{{ctx.Locale.Tr "repo.branch.new_branch_from" (.CommitID)}}" {{/* used by the form */}}

Diff for: templates/repo/empty.tmpl

+9-12
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
{{end}}
1515
</div>
1616
{{end}}
17+
1718
{{if .Repository.IsBroken}}
18-
<div class="ui segment center">
19-
{{ctx.Locale.Tr "repo.broken_message"}}
20-
</div>
19+
<div class="ui segment center">{{ctx.Locale.Tr "repo.broken_message"}}</div>
20+
{{else if .Repository.IsEmpty}}
21+
<div class="ui segment center">{{ctx.Locale.Tr "repo.no_branch"}}</div>
2122
{{else if .CanWriteCode}}
22-
<h4 class="ui top attached header">
23-
{{ctx.Locale.Tr "repo.quick_guide"}}
24-
</h4>
23+
<h4 class="ui top attached header">{{ctx.Locale.Tr "repo.quick_guide"}}</h4>
2524
<div class="ui attached guide table segment empty-repo-guide">
2625
<div class="item">
2726
<h3>{{ctx.Locale.Tr "repo.clone_this_repo"}} <small>{{ctx.Locale.Tr "repo.clone_helper" "http://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository"}}</small></h3>
@@ -66,12 +65,10 @@ git push -u origin {{.Repository.DefaultBranch}}</code></pre>
6665
</div>
6766
</div>
6867
{{end}}
69-
{{else}}
70-
<div class="ui segment center">
71-
{{ctx.Locale.Tr "repo.empty_message"}}
72-
</div>
73-
{{end}}
74-
</div>
68+
</div>
69+
{{else}}
70+
<div class="ui segment center">{{ctx.Locale.Tr "repo.empty_message"}}</div>
71+
{{end}}
7572
</div>
7673
</div>
7774
</div>

Diff for: templates/repo/header.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
</a>
163163
{{end}}
164164

165-
{{if and .EnableActions (.Permission.CanRead ctx.Consts.RepoUnitTypeActions)}}
165+
{{if and .EnableActions (.Permission.CanRead ctx.Consts.RepoUnitTypeActions) (not .IsEmptyRepo)}}
166166
<a class="{{if .PageIsActions}}active {{end}}item" href="{{.RepoLink}}/actions">
167167
{{svg "octicon-play"}} {{ctx.Locale.Tr "actions.actions"}}
168168
{{if .Repository.NumOpenActionRuns}}

Diff for: templates/user/dashboard/navbar.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{{svg "octicon-triangle-down" 14 "dropdown icon tw-ml-1"}}
1313
</span>
1414
<div class="context user overflow menu">
15-
<div class="ui header">
15+
<div class="header">
1616
{{ctx.Locale.Tr "home.switch_dashboard_context"}}
1717
</div>
1818
<div class="scrolling menu items">
@@ -56,7 +56,7 @@
5656
</span>
5757
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
5858
<div class="context user overflow menu">
59-
<div class="ui header">
59+
<div class="header">
6060
{{ctx.Locale.Tr "home.filter_by_team_repositories"}}
6161
</div>
6262
<div class="scrolling menu items">

Diff for: web_src/css/base.css

+5-5
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,13 @@ a.label,
336336
border-color: var(--color-secondary);
337337
}
338338

339+
.ui.dropdown .menu > .header {
340+
text-transform: none; /* reset fomantic's "uppercase" */
341+
}
342+
339343
.ui.dropdown .menu > .header:not(.ui) {
340344
color: var(--color-text);
345+
font-size: 0.95em; /* reset fomantic's small font-size */
341346
}
342347

343348
.ui.dropdown .menu > .item {
@@ -691,11 +696,6 @@ input:-webkit-autofill:active,
691696
box-shadow: 0 6px 18px var(--color-shadow) !important;
692697
}
693698

694-
.ui.dropdown .menu > .header {
695-
font-size: 0.8em;
696-
text-transform: none; /* reset fomantic's "uppercase" */
697-
}
698-
699699
.ui .text.left {
700700
text-align: left !important;
701701
}

Diff for: web_src/css/modules/navbar.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
align-items: stretch;
6767
}
6868
/* hide all items */
69-
#navbar .item {
69+
#navbar .navbar-left > .item,
70+
#navbar .navbar-right > .item {
7071
display: none;
7172
}
7273
#navbar #navbar-logo {

0 commit comments

Comments
 (0)