|
6 | 6 | "fmt" |
7 | 7 | "os" |
8 | 8 | "path/filepath" |
| 9 | + "time" |
9 | 10 |
|
| 11 | + "github.com/avast/retry-go" |
10 | 12 | pkgerrors "github.com/pkg/errors" |
11 | 13 | "github.com/zimmski/osutil" |
12 | 14 | "github.com/zimmski/osutil/bytesutil" |
@@ -73,38 +75,68 @@ func (r *Repository) Validate(logger *log.Logger, language language.Language) (e |
73 | 75 |
|
74 | 76 | // Reset resets a repository back to its "initial" commit. |
75 | 77 | 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 | + } |
82 | 98 |
|
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 |
87 | 100 | }, |
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 |
91 | 109 | } |
92 | 110 |
|
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 | + } |
99 | 129 |
|
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 |
104 | 131 | }, |
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 |
108 | 140 | } |
109 | 141 |
|
110 | 142 | return nil |
|
0 commit comments