Skip to content

Commit 1b1658d

Browse files
authored
Fix isempty detection of git repository (#18746)
* Fix isempty detection of git repository * Fix IsEmpty check
1 parent f1d8030 commit 1b1658d

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

modules/git/repo.go

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

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

10099
// CloneRepoOptions options when clone a repository

0 commit comments

Comments
 (0)