@@ -184,6 +184,9 @@ func (r *Repository) CanCreateIssueDependencies(user *user_model.User, isPull bo
184
184
185
185
// GetCommitsCount returns cached commit count for current view
186
186
func (r * Repository ) GetCommitsCount () (int64 , error ) {
187
+ if r .Commit == nil {
188
+ return 0 , nil
189
+ }
187
190
var contextName string
188
191
if r .IsViewBranch {
189
192
contextName = r .BranchName
@@ -642,8 +645,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
642
645
if err != nil {
643
646
if strings .Contains (err .Error (), "repository does not exist" ) || strings .Contains (err .Error (), "no such file or directory" ) {
644
647
log .Error ("Repository %-v has a broken repository on the file system: %s Error: %v" , ctx .Repo .Repository , ctx .Repo .Repository .RepoPath (), err )
645
- ctx .Repo .Repository .Status = repo_model .RepositoryBroken
646
- ctx .Repo .Repository .IsEmpty = true
648
+ ctx .Repo .Repository .MarkAsBrokenEmpty ()
647
649
ctx .Data ["BranchName" ] = ctx .Repo .Repository .DefaultBranch
648
650
// Only allow access to base of repo or settings
649
651
if ! isHomeOrSettings {
@@ -878,6 +880,10 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
878
880
return func (ctx * Context ) (cancel context.CancelFunc ) {
879
881
// Empty repository does not have reference information.
880
882
if ctx .Repo .Repository .IsEmpty {
883
+ // assume the user is viewing the (non-existent) default branch
884
+ ctx .Repo .IsViewBranch = true
885
+ ctx .Repo .BranchName = ctx .Repo .Repository .DefaultBranch
886
+ ctx .Data ["TreePath" ] = ctx .Repo .BranchName
881
887
return
882
888
}
883
889
@@ -907,27 +913,30 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
907
913
refName = ctx .Repo .Repository .DefaultBranch
908
914
if ! ctx .Repo .GitRepo .IsBranchExist (refName ) {
909
915
brs , _ , err := ctx .Repo .GitRepo .GetBranchNames (0 , 0 )
910
- if err != nil {
911
- ctx .ServerError ("GetBranches" , err )
912
- return
916
+ if err == nil && len (brs ) != 0 {
917
+ refName = brs [0 ]
913
918
} else if len (brs ) == 0 {
914
- err = fmt .Errorf ("No branches in non-empty repository %s" ,
915
- ctx .Repo .GitRepo .Path )
916
- ctx .ServerError ("GetBranches" , err )
917
- return
919
+ log .Error ("No branches in non-empty repository %s" , ctx .Repo .GitRepo .Path )
920
+ ctx .Repo .Repository .MarkAsBrokenEmpty ()
921
+ } else {
922
+ log .Error ("GetBranches error: %v" , err )
923
+ ctx .Repo .Repository .MarkAsBrokenEmpty ()
918
924
}
919
- refName = brs [0 ]
920
925
}
921
926
ctx .Repo .RefName = refName
922
927
ctx .Repo .BranchName = refName
923
928
ctx .Repo .Commit , err = ctx .Repo .GitRepo .GetBranchCommit (refName )
924
- if err != nil {
929
+ if err == nil {
930
+ ctx .Repo .CommitID = ctx .Repo .Commit .ID .String ()
931
+ } else if strings .Contains (err .Error (), "fatal: not a git repository" ) || strings .Contains (err .Error (), "object does not exist" ) {
932
+ // if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users
933
+ log .Error ("GetBranchCommit: %v" , err )
934
+ ctx .Repo .Repository .MarkAsBrokenEmpty ()
935
+ } else {
925
936
ctx .ServerError ("GetBranchCommit" , err )
926
937
return
927
938
}
928
- ctx .Repo .CommitID = ctx .Repo .Commit .ID .String ()
929
939
ctx .Repo .IsViewBranch = true
930
-
931
940
} else {
932
941
refName = getRefName (ctx , refType )
933
942
ctx .Repo .RefName = refName
0 commit comments