diff --git a/internal/lefthook/install.go b/internal/lefthook/install.go index 507cac75..6b9c297e 100644 --- a/internal/lefthook/install.go +++ b/internal/lefthook/install.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "encoding/hex" "errors" + "fmt" "io" "os" "path/filepath" @@ -114,18 +115,20 @@ func (l *Lefthook) createConfig(path string) error { return nil } -func (l *Lefthook) syncHooks(cfg *config.Config) (*config.Config, error) { +func (l *Lefthook) syncHooks(cfg *config.Config, fetchRemotes bool) (*config.Config, error) { var remotesSynced bool var err error - for _, remote := range cfg.Remotes { - if remote.Configured() && remote.Refetch { - if err = l.repo.SyncRemote(remote.GitURL, remote.Ref, false); err != nil { - log.Warnf("Couldn't sync from %s. Will continue anyway: %s", remote.GitURL, err) - continue - } + if fetchRemotes { + for _, remote := range cfg.Remotes { + if remote.Configured() && remote.Refetch { + if err = l.repo.SyncRemote(remote.GitURL, remote.Ref, false); err != nil { + log.Warnf("Couldn't sync from %s. Will continue anyway: %s", remote.GitURL, err) + continue + } - remotesSynced = true + remotesSynced = true + } } } @@ -133,7 +136,7 @@ func (l *Lefthook) syncHooks(cfg *config.Config) (*config.Config, error) { // Reread the config file with synced remotes cfg, err = l.readOrCreateConfig() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to reread the config: %w", err) } } @@ -157,11 +160,11 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b checksum, err := l.configChecksum() if err != nil { - return err + return fmt.Errorf("could not calculate checksum: %w", err) } if err = l.ensureHooksDirExists(); err != nil { - return err + return fmt.Errorf("could not create hooks dir: %w", err) } rootsMap := make(map[string]struct{}) @@ -185,7 +188,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b hookNames = append(hookNames, hook) if err = l.cleanHook(hook, force); err != nil { - return err + return fmt.Errorf("could not replace the hook: %w", err) } templateArgs := templates.Args{ @@ -194,7 +197,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b Roots: roots, } if err = l.addHook(hook, templateArgs); err != nil { - return err + return fmt.Errorf("could not add the hook: %w", err) } } @@ -208,7 +211,7 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force b } if err = l.addChecksumFile(checksum); err != nil { - return err + return fmt.Errorf("could not create a checksum file: %w", err) } success = true @@ -328,7 +331,7 @@ func (l *Lefthook) configChecksum() (checksum string, err error) { func (l *Lefthook) addChecksumFile(checksum string) error { timestamp, err := l.configLastUpdateTimestamp() if err != nil { - return err + return fmt.Errorf("unable to get config update timestamp: %w", err) } return afero.WriteFile( diff --git a/internal/lefthook/run.go b/internal/lefthook/run.go index 2185ccb6..1906574d 100644 --- a/internal/lefthook/run.go +++ b/internal/lefthook/run.go @@ -73,7 +73,9 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error { // prepare-commit-msg hook is used for seamless synchronization of hooks with config. // See: internal/lefthook/install.go _, ok := cfg.Hooks[hookName] + var isGhostHook bool if hookName == config.GhostHookName && !ok && !verbose { + isGhostHook = true log.SetLevel(log.WarnLevel) } @@ -97,11 +99,10 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error { if !args.NoAutoInstall { // This line controls updating the git hook if config has changed - newCfg, err := l.syncHooks(cfg) + newCfg, err := l.syncHooks(cfg, !isGhostHook) if err != nil { - log.Warn( - `⚠️ There was a problem with synchronizing git hooks. -Run 'lefthook install' manually.`, + log.Warnf( + "⚠️ There was a problem with synchronizing git hooks. Run 'lefthook install' manually.\n Error: %s", err, ) } else { cfg = newCfg @@ -147,6 +148,7 @@ Run 'lefthook install' manually.`, sourceDirs = append( sourceDirs, filepath.Join( + l.repo.RootPath, l.repo.RemoteFolder(remote.GitURL, remote.Ref), cfg.SourceDir, ),