Skip to content

Commit 91e8d6f

Browse files
committed
Improve codes
1 parent 9239ffa commit 91e8d6f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

modules/git/repo.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,20 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
7979

8080
// IsEmpty Check if repository is empty.
8181
func (repo *Repository) IsEmpty() (bool, error) {
82-
var errbuf strings.Builder
83-
if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
82+
var errbuf, output strings.Builder
83+
if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").
84+
RunWithContext(&RunContext{
85+
Dir: repo.Path,
86+
Stdout: &output,
87+
Stderr: &errbuf,
88+
}); err != nil {
8489
return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
8590
}
8691

87-
c, _ := strconv.Atoi(errbuf.String())
92+
c, err := strconv.Atoi(strings.TrimSpace(output.String()))
93+
if err != nil {
94+
return true, fmt.Errorf("check empty: convert %s to count failed: %v", output.String(), err)
95+
}
8896
return c == 0, nil
8997
}
9098

0 commit comments

Comments
 (0)