Skip to content

Commit

Permalink
Fix IsEmpty check
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 28, 2022
1 parent abee927 commit 9239ffa
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,12 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
// IsEmpty Check if repository is empty.
func (repo *Repository) IsEmpty() (bool, error) {
var errbuf strings.Builder
if err := NewCommandContext(repo.Ctx, "log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
defaultBranch, err := repo.GetDefaultBranch()
if err != nil {
return false, err
}
if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") ||
strings.Contains(errbuf.String(), fmt.Sprintf("fatal: your current branch '%s' does not have any commits yet", defaultBranch)) {
return true, nil
}
if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
}

return false, nil
c, _ := strconv.Atoi(errbuf.String())
return c == 0, nil
}

// CloneRepoOptions options when clone a repository
Expand Down

0 comments on commit 9239ffa

Please sign in to comment.