5
5
"crypto/md5"
6
6
"encoding/hex"
7
7
"errors"
8
+ "fmt"
8
9
"io"
9
10
"os"
10
11
"path/filepath"
@@ -114,26 +115,28 @@ func (l *Lefthook) createConfig(path string) error {
114
115
return nil
115
116
}
116
117
117
- func (l * Lefthook ) syncHooks (cfg * config.Config ) (* config.Config , error ) {
118
+ func (l * Lefthook ) syncHooks (cfg * config.Config , fetchRemotes bool ) (* config.Config , error ) {
118
119
var remotesSynced bool
119
120
var err error
120
121
121
- for _ , remote := range cfg .Remotes {
122
- if remote .Configured () && remote .Refetch {
123
- if err = l .repo .SyncRemote (remote .GitURL , remote .Ref , false ); err != nil {
124
- log .Warnf ("Couldn't sync from %s. Will continue anyway: %s" , remote .GitURL , err )
125
- continue
126
- }
122
+ if fetchRemotes {
123
+ for _ , remote := range cfg .Remotes {
124
+ if remote .Configured () && remote .Refetch {
125
+ if err = l .repo .SyncRemote (remote .GitURL , remote .Ref , false ); err != nil {
126
+ log .Warnf ("Couldn't sync from %s. Will continue anyway: %s" , remote .GitURL , err )
127
+ continue
128
+ }
127
129
128
- remotesSynced = true
130
+ remotesSynced = true
131
+ }
129
132
}
130
133
}
131
134
132
135
if remotesSynced {
133
136
// Reread the config file with synced remotes
134
137
cfg , err = l .readOrCreateConfig ()
135
138
if err != nil {
136
- return nil , err
139
+ return nil , fmt . Errorf ( "failed to reread the config: %w" , err )
137
140
}
138
141
}
139
142
@@ -157,11 +160,11 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
157
160
158
161
checksum , err := l .configChecksum ()
159
162
if err != nil {
160
- return err
163
+ return fmt . Errorf ( "could not calculate checksum: %w" , err )
161
164
}
162
165
163
166
if err = l .ensureHooksDirExists (); err != nil {
164
- return err
167
+ return fmt . Errorf ( "could not create hooks dir: %w" , err )
165
168
}
166
169
167
170
rootsMap := make (map [string ]struct {})
@@ -185,7 +188,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
185
188
hookNames = append (hookNames , hook )
186
189
187
190
if err = l .cleanHook (hook , force ); err != nil {
188
- return err
191
+ return fmt . Errorf ( "could not replace the hook: %w" , err )
189
192
}
190
193
191
194
templateArgs := templates.Args {
@@ -194,7 +197,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
194
197
Roots : roots ,
195
198
}
196
199
if err = l .addHook (hook , templateArgs ); err != nil {
197
- return err
200
+ return fmt . Errorf ( "could not add the hook: %w" , err )
198
201
}
199
202
}
200
203
@@ -208,7 +211,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
208
211
}
209
212
210
213
if err = l .addChecksumFile (checksum ); err != nil {
211
- return err
214
+ return fmt . Errorf ( "could not create a checksum file: %w" , err )
212
215
}
213
216
214
217
success = true
@@ -328,7 +331,7 @@ func (l *Lefthook) configChecksum() (checksum string, err error) {
328
331
func (l * Lefthook ) addChecksumFile (checksum string ) error {
329
332
timestamp , err := l .configLastUpdateTimestamp ()
330
333
if err != nil {
331
- return err
334
+ return fmt . Errorf ( "unable to get config update timestamp: %w" , err )
332
335
}
333
336
334
337
return afero .WriteFile (
0 commit comments