Skip to content

Commit ce272f2

Browse files
authored
Fix broken when no commits and default branch is not master (#18424)
* Fix broken when no commits and default branch is not master * Fix IsEmpty check * Improve codes
1 parent 9d9ad1b commit ce272f2

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

modules/git/repo.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ func InitRepository(repoPath string, bare bool) error {
7575

7676
// IsEmpty Check if repository is empty.
7777
func (repo *Repository) IsEmpty() (bool, error) {
78-
var errbuf strings.Builder
79-
if err := NewCommand("log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
80-
if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") ||
81-
strings.Contains(errbuf.String(), "fatal: your current branch 'master' does not have any commits yet") {
82-
return true, nil
83-
}
78+
var errbuf, output strings.Builder
79+
if err := NewCommand("rev-list", "--all", "--count", "--max-count=1").RunInDirPipeline(repo.Path, &output, &errbuf); err != nil {
8480
return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
8581
}
8682

87-
return false, nil
83+
c, err := strconv.Atoi(strings.TrimSpace(output.String()))
84+
if err != nil {
85+
return true, fmt.Errorf("check empty: convert %s to count failed: %v", output.String(), err)
86+
}
87+
return c == 0, nil
8888
}
8989

9090
// CloneRepoOptions options when clone a repository

0 commit comments

Comments
 (0)