Skip to content

Commit

Permalink
Fix isempty detection of git repository (#18746)
Browse files Browse the repository at this point in the history
* Fix isempty detection of git repository

* Fix IsEmpty check
  • Loading branch information
lunny authored Feb 13, 2022
1 parent f1d8030 commit 1b1658d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,20 @@ 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,
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())
}

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
Expand Down

0 comments on commit 1b1658d

Please sign in to comment.