From d516fba91f86484972105b2a4c43c72751352368 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 13 Feb 2022 20:10:11 +0800 Subject: [PATCH 1/2] Fix isempty detection of git repository --- modules/git/repo.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index 79a540209c5d1..91983f492a030 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, "rev-list", "--all", "--count", "--max-count=1"). + if err := NewCommand(repo.Ctx, "show-ref", "--head", "^HEAD$"). RunWithContext(&RunContext{ Timeout: -1, Dir: repo.Path, @@ -90,11 +90,7 @@ func (repo *Repository) IsEmpty() (bool, error) { 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 From cb5e97b23c4e1d36147f9da757036503aac4f7f1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 13 Feb 2022 20:45:51 +0800 Subject: [PATCH 2/2] Fix IsEmpty check --- modules/git/repo.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/git/repo.go b/modules/git/repo.go index 91983f492a030..8217521b06048 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -87,6 +87,9 @@ func (repo *Repository) IsEmpty() (bool, error) { 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()) }