Skip to content

Commit 4ab5817

Browse files
committed
fix
1 parent 2b064b8 commit 4ab5817

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

models/repo/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ func (repo *Repository) IsBroken() bool {
279279
}
280280

281281
// MarkAsBrokenEmpty marks the repo as broken and empty
282+
// FIXME: the status "broken" and "is_empty" were abused,
283+
// The code always set them together, no way to distinguish whether a repo is really "empty" or "broken"
282284
func (repo *Repository) MarkAsBrokenEmpty() {
283285
repo.Status = RepositoryBroken
284286
repo.IsEmpty = true

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,7 @@ create_new_repo_command = Creating a new repository on the command line
12351235
push_exist_repo = Pushing an existing repository from the command line
12361236
empty_message = This repository does not contain any content.
12371237
broken_message = The Git data underlying this repository cannot be read. Contact the administrator of this instance or delete this repository.
1238+
no_branch = This repository doesn’t have any branches.
12381239
12391240
code = Code
12401241
code.desc = Access source code, files, commits and branches.

routers/web/repo/view_home.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,42 @@ 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 {
232242
log.Error("GitRepo.IsEmpty: %v", err)
233243
ctx.Repo.Repository.Status = repo_model.RepositoryBroken
234244
showEmpty = true
235245
ctx.Flash.Error(ctx.Tr("error.occurred"), true)
236246
}
247+
if err != nil {
248+
showEmpty = true // the repo is broken
249+
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryBroken)
250+
} else if reallyEmpty {
251+
showEmpty = true // the repo is really empty
252+
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady)
253+
} else if ctx.Repo.Commit == nil {
254+
showEmpty = true // it is not really empty, but there is no branch
255+
// at the moment, other repo units like "actions" are not able to handle such case,
256+
// so we just mark the repo as empty to prevent from displaying these units.
257+
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady)
258+
} else {
259+
// the repo is actually not empty and has branches, need to update the database later
260+
showEmpty = false
261+
}
237262
}
238263
if showEmpty {
239264
ctx.HTML(http.StatusOK, tplRepoEMPTY)
@@ -246,12 +271,8 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
246271
// and even more: the IsEmpty flag is deeply broken and should be removed with the UI changed to manage to cope with empty repos.
247272
// it's possible for a repository to be non-empty by that flag but still 500
248273
// 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 {
274+
updateContextRepoEmptyAndStatus(ctx, false, repo_model.RepositoryReady)
275+
if err := repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil {
255276
ctx.ServerError("UpdateRepoSize", err)
256277
return
257278
}

services/context/repo.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,10 +897,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
897897
refName = brs[0].Name
898898
} else if len(brs) == 0 {
899899
log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path)
900-
ctx.Repo.Repository.MarkAsBrokenEmpty()
901900
} else {
902901
log.Error("GetBranches error: %v", err)
903-
ctx.Repo.Repository.MarkAsBrokenEmpty()
904902
}
905903
}
906904
ctx.Repo.RefName = refName
@@ -911,7 +909,6 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
911909
} else if strings.Contains(err.Error(), "fatal: not a git repository") || strings.Contains(err.Error(), "object does not exist") {
912910
// if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users
913911
log.Error("GetBranchCommit: %v", err)
914-
ctx.Repo.Repository.MarkAsBrokenEmpty()
915912
} else {
916913
ctx.ServerError("GetBranchCommit", err)
917914
return

templates/repo/empty.tmpl

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

templates/repo/header.tmpl

Lines changed: 1 addition & 1 deletion
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}}

0 commit comments

Comments
 (0)