Skip to content

Commit aa98a47

Browse files
authoredJun 6, 2024
fix: fetch remotes only for non ghost hooks (#744)
* fix: fetch remotes only for non ghost hooks * fix: use absolute paths when concatting remote source dirs * chore: wrap all install errors
1 parent a58c11d commit aa98a47

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed
 

Diff for: ‎internal/lefthook/install.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/md5"
66
"encoding/hex"
77
"errors"
8+
"fmt"
89
"io"
910
"os"
1011
"path/filepath"
@@ -114,26 +115,28 @@ func (l *Lefthook) createConfig(path string) error {
114115
return nil
115116
}
116117

117-
func (l *Lefthook) syncHooks(cfg *config.Config) (*config.Config, error) {
118+
func (l *Lefthook) syncHooks(cfg *config.Config, fetchRemotes bool) (*config.Config, error) {
118119
var remotesSynced bool
119120
var err error
120121

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+
}
127129

128-
remotesSynced = true
130+
remotesSynced = true
131+
}
129132
}
130133
}
131134

132135
if remotesSynced {
133136
// Reread the config file with synced remotes
134137
cfg, err = l.readOrCreateConfig()
135138
if err != nil {
136-
return nil, err
139+
return nil, fmt.Errorf("failed to reread the config: %w", err)
137140
}
138141
}
139142

@@ -157,11 +160,11 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
157160

158161
checksum, err := l.configChecksum()
159162
if err != nil {
160-
return err
163+
return fmt.Errorf("could not calculate checksum: %w", err)
161164
}
162165

163166
if err = l.ensureHooksDirExists(); err != nil {
164-
return err
167+
return fmt.Errorf("could not create hooks dir: %w", err)
165168
}
166169

167170
rootsMap := make(map[string]struct{})
@@ -185,7 +188,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
185188
hookNames = append(hookNames, hook)
186189

187190
if err = l.cleanHook(hook, force); err != nil {
188-
return err
191+
return fmt.Errorf("could not replace the hook: %w", err)
189192
}
190193

191194
templateArgs := templates.Args{
@@ -194,7 +197,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
194197
Roots: roots,
195198
}
196199
if err = l.addHook(hook, templateArgs); err != nil {
197-
return err
200+
return fmt.Errorf("could not add the hook: %w", err)
198201
}
199202
}
200203

@@ -208,7 +211,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b
208211
}
209212

210213
if err = l.addChecksumFile(checksum); err != nil {
211-
return err
214+
return fmt.Errorf("could not create a checksum file: %w", err)
212215
}
213216

214217
success = true
@@ -328,7 +331,7 @@ func (l *Lefthook) configChecksum() (checksum string, err error) {
328331
func (l *Lefthook) addChecksumFile(checksum string) error {
329332
timestamp, err := l.configLastUpdateTimestamp()
330333
if err != nil {
331-
return err
334+
return fmt.Errorf("unable to get config update timestamp: %w", err)
332335
}
333336

334337
return afero.WriteFile(

Diff for: ‎internal/lefthook/run.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
7373
// prepare-commit-msg hook is used for seamless synchronization of hooks with config.
7474
// See: internal/lefthook/install.go
7575
_, ok := cfg.Hooks[hookName]
76+
var isGhostHook bool
7677
if hookName == config.GhostHookName && !ok && !verbose {
78+
isGhostHook = true
7779
log.SetLevel(log.WarnLevel)
7880
}
7981

@@ -97,11 +99,10 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
9799

98100
if !args.NoAutoInstall {
99101
// This line controls updating the git hook if config has changed
100-
newCfg, err := l.syncHooks(cfg)
102+
newCfg, err := l.syncHooks(cfg, !isGhostHook)
101103
if err != nil {
102-
log.Warn(
103-
`⚠️ There was a problem with synchronizing git hooks.
104-
Run 'lefthook install' manually.`,
104+
log.Warnf(
105+
"⚠️ There was a problem with synchronizing git hooks. Run 'lefthook install' manually.\n Error: %s", err,
105106
)
106107
} else {
107108
cfg = newCfg
@@ -147,6 +148,7 @@ Run 'lefthook install' manually.`,
147148
sourceDirs = append(
148149
sourceDirs,
149150
filepath.Join(
151+
l.repo.RootPath,
150152
l.repo.RemoteFolder(remote.GitURL, remote.Ref),
151153
cfg.SourceDir,
152154
),

0 commit comments

Comments
 (0)