Skip to content

Commit 7da85fa

Browse files
GiteaBotwxiaoguang
andauthored
Sync repo's IsEmpty status correctly (#26517) (#26560)
Backport #26517 by @wxiaoguang Close #26509 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 0ac8b77 commit 7da85fa

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

modules/git/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
8080
// IsEmpty Check if repository is empty.
8181
func (repo *Repository) IsEmpty() (bool, error) {
8282
var errbuf, output strings.Builder
83-
if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("show-ref", "--head", "^HEAD$").
83+
if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("rev-list", "-n", "1", "--all").
8484
Run(&RunOpts{
8585
Dir: repo.Path,
8686
Stdout: &output,

routers/web/repo/editor.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
370370
}
371371

372372
if ctx.Repo.Repository.IsEmpty {
373-
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
373+
if isEmpty, err := ctx.Repo.GitRepo.IsEmpty(); err == nil && !isEmpty {
374+
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
375+
}
374376
}
375377

376378
redirectForCommitChoice(ctx, form.CommitChoice, branchName, form.TreePath)
@@ -763,7 +765,9 @@ func UploadFilePost(ctx *context.Context) {
763765
}
764766

765767
if ctx.Repo.Repository.IsEmpty {
766-
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
768+
if isEmpty, err := ctx.Repo.GitRepo.IsEmpty(); err == nil && !isEmpty {
769+
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
770+
}
767771
}
768772

769773
redirectForCommitChoice(ctx, form.CommitChoice, branchName, form.TreePath)

services/repository/files/update.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
340340
}
341341

342342
if repo.IsEmpty {
343-
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
343+
if isEmpty, err := gitRepo.IsEmpty(); err == nil && !isEmpty {
344+
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
345+
}
344346
}
345347

346348
return filesResponse, nil

0 commit comments

Comments
 (0)