Skip to content

Commit

Permalink
Improve codes
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 28, 2022
1 parent 9239ffa commit 91e8d6f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,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 strings.Builder
if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
var errbuf, output strings.Builder
if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").
RunWithContext(&RunContext{
Dir: repo.Path,
Stdout: &output,
Stderr: &errbuf,
}); err != nil {
return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
}

c, _ := strconv.Atoi(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
}

Expand Down

0 comments on commit 91e8d6f

Please sign in to comment.