Skip to content

Commit 975524f

Browse files
authored
Merge pull request #400 from symflower/fix-git-clean-panic
fix, Retry resetting repository if "git clean" fails
2 parents 815e7cc + 986fc9b commit 975524f

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

evaluate/task/repository.go

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"time"
910

11+
"github.com/avast/retry-go"
1012
pkgerrors "github.com/pkg/errors"
1113
"github.com/zimmski/osutil"
1214
"github.com/zimmski/osutil/bytesutil"
@@ -73,38 +75,68 @@ func (r *Repository) Validate(logger *log.Logger, language language.Language) (e
7375

7476
// Reset resets a repository back to its "initial" commit.
7577
func (r *Repository) Reset(logger *log.Logger) (err error) {
76-
out, err := util.CommandWithResult(context.Background(), logger, &util.Command{
77-
Command: []string{
78-
"git",
79-
"clean",
80-
"-df",
81-
},
78+
retryAttempts := uint(3)
79+
80+
if err := retry.Do(
81+
func() error {
82+
out, err := util.CommandWithResult(context.Background(), logger, &util.Command{
83+
Command: []string{
84+
"git",
85+
"clean",
86+
"-df",
87+
},
88+
89+
Directory: r.dataPath,
90+
Env: map[string]string{ // Overwrite the global and system configs to point to the default one.
91+
"GIT_CONFIG_GLOBAL": filepath.Join(r.dataPath, ".git", "config"),
92+
"GIT_CONFIG_SYSTEM": filepath.Join(r.dataPath, ".git", "config"),
93+
},
94+
})
95+
if err != nil {
96+
return pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to clean git repository", out)))
97+
}
8298

83-
Directory: r.dataPath,
84-
Env: map[string]string{ // Overwrite the global and system configs to point to the default one.
85-
"GIT_CONFIG_GLOBAL": filepath.Join(r.dataPath, ".git", "config"),
86-
"GIT_CONFIG_SYSTEM": filepath.Join(r.dataPath, ".git", "config"),
99+
return nil
87100
},
88-
})
89-
if err != nil {
90-
return pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to clean git repository", out)))
101+
retry.Attempts(retryAttempts),
102+
retry.Delay(5*time.Second),
103+
retry.DelayType(retry.BackOffDelay),
104+
retry.LastErrorOnly(true),
105+
retry.OnRetry(func(n uint, err error) {
106+
logger.Info("git clean retry", "count", n+1, "total", retryAttempts, "error", err)
107+
})); err != nil {
108+
return err
91109
}
92110

93-
out, err = util.CommandWithResult(context.Background(), logger, &util.Command{
94-
Command: []string{
95-
"git",
96-
"restore",
97-
".",
98-
},
111+
if err := retry.Do(
112+
func() error {
113+
out, err := util.CommandWithResult(context.Background(), logger, &util.Command{
114+
Command: []string{
115+
"git",
116+
"restore",
117+
".",
118+
},
119+
120+
Directory: r.dataPath,
121+
Env: map[string]string{ // Overwrite the global and system configs to point to the default one.
122+
"GIT_CONFIG_GLOBAL": filepath.Join(r.dataPath, ".git", "config"),
123+
"GIT_CONFIG_SYSTEM": filepath.Join(r.dataPath, ".git", "config"),
124+
},
125+
})
126+
if err != nil {
127+
return pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to clean git repository", out)))
128+
}
99129

100-
Directory: r.dataPath,
101-
Env: map[string]string{ // Overwrite the global and system configs to point to the default one.
102-
"GIT_CONFIG_GLOBAL": filepath.Join(r.dataPath, ".git", "config"),
103-
"GIT_CONFIG_SYSTEM": filepath.Join(r.dataPath, ".git", "config"),
130+
return nil
104131
},
105-
})
106-
if err != nil {
107-
return pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to clean git repository", out)))
132+
retry.Attempts(retryAttempts),
133+
retry.Delay(5*time.Second),
134+
retry.DelayType(retry.BackOffDelay),
135+
retry.LastErrorOnly(true),
136+
retry.OnRetry(func(n uint, err error) {
137+
logger.Info("git restore retry", "count", n+1, "total", retryAttempts, "error", err)
138+
})); err != nil {
139+
return err
108140
}
109141

110142
return nil

0 commit comments

Comments
 (0)