aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2023-08-17 13:12:14 +0800
committerGitHub <noreply@github.com>2023-08-17 05:12:14 +0000
commit7da85fa0c3d7ddf4907ca74ef81ee532d768df87 (patch)
tree65241271c4b8d2aa5e9710ff5cf489d8ff730942
parent0ac8b774e95cecd1050d7a759f3830b3198b63fd (diff)
downloadgitea-7da85fa0c3d7ddf4907ca74ef81ee532d768df87.tar.gz
gitea-7da85fa0c3d7ddf4907ca74ef81ee532d768df87.zip
Sync repo's IsEmpty status correctly (#26517) (#26560)
Backport #26517 by @wxiaoguang Close #26509 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
-rw-r--r--modules/git/repo.go2
-rw-r--r--routers/web/repo/editor.go8
-rw-r--r--services/repository/files/update.go4
3 files changed, 10 insertions, 4 deletions
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 61930ab31d..5681d39c3b 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -80,7 +80,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
// IsEmpty Check if repository is empty.
func (repo *Repository) IsEmpty() (bool, error) {
var errbuf, output strings.Builder
- if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("show-ref", "--head", "^HEAD$").
+ if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("rev-list", "-n", "1", "--all").
Run(&RunOpts{
Dir: repo.Path,
Stdout: &output,
diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go
index 4290c88ccd..9e30b30343 100644
--- a/routers/web/repo/editor.go
+++ b/routers/web/repo/editor.go
@@ -370,7 +370,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
}
if ctx.Repo.Repository.IsEmpty {
- _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
+ if isEmpty, err := ctx.Repo.GitRepo.IsEmpty(); err == nil && !isEmpty {
+ _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
+ }
}
redirectForCommitChoice(ctx, form.CommitChoice, branchName, form.TreePath)
@@ -763,7 +765,9 @@ func UploadFilePost(ctx *context.Context) {
}
if ctx.Repo.Repository.IsEmpty {
- _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
+ if isEmpty, err := ctx.Repo.GitRepo.IsEmpty(); err == nil && !isEmpty {
+ _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
+ }
}
redirectForCommitChoice(ctx, form.CommitChoice, branchName, form.TreePath)
diff --git a/services/repository/files/update.go b/services/repository/files/update.go
index 01bf2ace00..38aea52aa4 100644
--- a/services/repository/files/update.go
+++ b/services/repository/files/update.go
@@ -340,7 +340,9 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
}
if repo.IsEmpty {
- _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
+ if isEmpty, err := gitRepo.IsEmpty(); err == nil && !isEmpty {
+ _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
+ }
}
return filesResponse, nil