Skip to content

Commit 9239ffa

Browse files
committed
Fix IsEmpty check
1 parent abee927 commit 9239ffa

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

modules/git/repo.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,12 @@ 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 strings.Builder
83-
if err := NewCommandContext(repo.Ctx, "log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
84-
defaultBranch, err := repo.GetDefaultBranch()
85-
if err != nil {
86-
return false, err
87-
}
88-
if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") ||
89-
strings.Contains(errbuf.String(), fmt.Sprintf("fatal: your current branch '%s' does not have any commits yet", defaultBranch)) {
90-
return true, nil
91-
}
83+
if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
9284
return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
9385
}
9486

95-
return false, nil
87+
c, _ := strconv.Atoi(errbuf.String())
88+
return c == 0, nil
9689
}
9790

9891
// CloneRepoOptions options when clone a repository

0 commit comments

Comments
 (0)