From 913d6f3ff3cc0a122086eff4841f674ad4812ff5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 14 Feb 2022 00:33:35 +0800 Subject: [PATCH] Fix isempty detection of git repository (#18746) (#18750) * Fix isempty detection of git repository * Fix IsEmpty check --- modules/git/repo.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index 5dca619ef6..ec95bc7a81 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -80,20 +80,19 @@ func InitRepository(repoPath string, bare bool) error { // IsEmpty Check if repository is empty. func (repo *Repository) IsEmpty() (bool, error) { var errbuf, output strings.Builder - if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunWithContext(&RunContext{ + if err := NewCommandContext(repo.Ctx, "show-ref", "--head", "^HEAD$").RunWithContext(&RunContext{ Timeout: -1, Dir: repo.Path, Stdout: &output, Stderr: &errbuf, }); err != nil { + if err.Error() == "exit status 1" && errbuf.String() == "" { + return true, nil + } return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String()) } - c, err := strconv.Atoi(strings.TrimSpace(output.String())) - if err != nil { - return true, fmt.Errorf("check empty: convert %s to count failed: %v", output.String(), err) - } - return c == 0, nil + return strings.TrimSpace(output.String()) == "", nil } // CloneRepoOptions options when clone a repository