Skip to content

Commit

Permalink
fix: simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jun 3, 2024
1 parent 75e624b commit fba8480
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,23 @@ func (l *Lefthook) Install(force bool) error {
return err
}

var remotesSynced bool
for _, remote := range cfg.Remotes {
if remote.Configured() {
if err := l.repo.SyncRemote(remote.GitURL, remote.Ref, force); err != nil {
if err = l.repo.SyncRemote(remote.GitURL, remote.Ref, force); err != nil {
log.Warnf("Couldn't sync from %s. Will continue anyway: %s", remote.GitURL, err)
} else {
// Reread the config file with synced remotes
cfg, err = l.readOrCreateConfig()
if err != nil {
return err
}
continue
}

remotesSynced = true
}
}

if remotesSynced {
// Reread the config file with synced remotes
cfg, err = l.readOrCreateConfig()
if err != nil {
return err
}
}

Expand Down Expand Up @@ -109,27 +115,30 @@ func (l *Lefthook) createConfig(path string) error {
}

func (l *Lefthook) syncHooks(cfg *config.Config) (*config.Config, error) {
checkHashSum := true
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
}

// Re-install the hooks
checkHashSum = false
remotesSynced = true
}
}

// Reread the config file with synced remotes
cfg, err = l.readOrCreateConfig()
if err != nil {
return nil, err
}
if remotesSynced {
// Reread the config file with synced remotes
cfg, err = l.readOrCreateConfig()
if err != nil {
return nil, err
}
}

return cfg, l.createHooksIfNeeded(cfg, checkHashSum, false)
// Don't rely on config checksum if remotes were refetched
return cfg, l.createHooksIfNeeded(cfg, !remotesSynced, false)
}

func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force bool) error {
Expand Down

0 comments on commit fba8480

Please sign in to comment.