Skip to content

Commit 913d6f3

Browse files
authored
Fix isempty detection of git repository (#18746) (#18750)
* Fix isempty detection of git repository * Fix IsEmpty check
1 parent 044cb09 commit 913d6f3

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

modules/git/repo.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,19 @@ func InitRepository(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 := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunWithContext(&RunContext{
83+
if err := NewCommandContext(repo.Ctx, "show-ref", "--head", "^HEAD$").RunWithContext(&RunContext{
8484
Timeout: -1,
8585
Dir: repo.Path,
8686
Stdout: &output,
8787
Stderr: &errbuf,
8888
}); err != nil {
89+
if err.Error() == "exit status 1" && errbuf.String() == "" {
90+
return true, nil
91+
}
8992
return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
9093
}
9194

92-
c, err := strconv.Atoi(strings.TrimSpace(output.String()))
93-
if err != nil {
94-
return true, fmt.Errorf("check empty: convert %s to count failed: %v", output.String(), err)
95-
}
96-
return c == 0, nil
95+
return strings.TrimSpace(output.String()) == "", nil
9796
}
9897

9998
// CloneRepoOptions options when clone a repository

0 commit comments

Comments
 (0)