diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 375f2418b9..b84c5993e4 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -574,8 +574,20 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err repo.IsTemplate = *opts.Template } - // Default branch only updated if changed and exist - if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) { + if ctx.Repo.GitRepo == nil { + var err error + ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath()) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err) + return err + } + defer ctx.Repo.GitRepo.Close() + } + + // Default branch only updated if changed and exist or the repository is empty + if opts.DefaultBranch != nil && + repo.DefaultBranch != *opts.DefaultBranch && + (ctx.Repo.Repository.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) { if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil { if !git.IsErrUnsupportedVersion(err) { ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)