Skip to content

Commit

Permalink
fix: fetch remotes only for non ghost hooks (#744)
Browse files Browse the repository at this point in the history
* fix: fetch remotes only for non ghost hooks

* fix: use absolute paths when concatting remote source dirs

* chore: wrap all install errors
  • Loading branch information
mrexox authored Jun 6, 2024
1 parent a58c11d commit aa98a47
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
33 changes: 18 additions & 15 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -114,26 +115,28 @@ 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
}
}
}

if remotesSynced {
// 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)
}
}

Expand All @@ -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{})
Expand All @@ -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{
Expand All @@ -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)
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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,
),
Expand Down

0 comments on commit aa98a47

Please sign in to comment.