@@ -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+
226237func 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 }
0 commit comments