Skip to content

Commit

Permalink
feat: add refetch option to remotes config
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed May 31, 2024
1 parent c2b2db7 commit e1d8aef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bench:

bin/golangci-lint:
@test -x $$(go env GOPATH)/bin/golangci-lint || \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.56.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.59.0

lint: bin/golangci-lint
$$(go env GOPATH)/bin/golangci-lint run --fix
Expand Down
1 change: 1 addition & 0 deletions internal/config/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Remote struct {
// Deprecated
Config string `json:"config,omitempty" mapstructure:"config,omitempty" toml:"config,omitempty" yaml:",omitempty"`
Configs []string `json:"configs,omitempty" mapstructure:"configs,omitempty" toml:"configs,omitempty" yaml:",omitempty"`
Refetch bool `json:"refetch,omitempty" mapstructure:"refetch,omitempty" toml:"refetch,omitempty" yaml:",omitempty"`
}

func (r *Remote) Configured() bool {
Expand Down
5 changes: 5 additions & 0 deletions internal/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (r *Repository) SyncRemote(url, ref string, force bool) error {
return err
}

log.SetName("fetching remotes")
log.StartSpinner()
defer log.StopSpinner()
defer log.UnsetName("fetching remotes")

directoryName := remoteDirectoryName(url, ref)
remotePath := filepath.Join(remotesPath, directoryName)

Expand Down
19 changes: 19 additions & 0 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ func (l *Lefthook) createConfig(path string) error {
return nil
}

func (l *Lefthook) syncHooks(cfg *config.Config, checkHashSum, force bool) error {
for _, remote := range cfg.Remotes {
if remote.Configured() && remote.Refetch {
if err := l.repo.SyncRemote(remote.GitURL, remote.Ref, force); err != nil {
checkHashSum = false
log.Warnf("Couldn't sync remotes. Will continue without them: %s", err)
} else {
// Reread the config file with synced remotes
cfg, err = l.readOrCreateConfig()
if err != nil {
return err
}
}
}
}

return l.createHooksIfNeeded(cfg, checkHashSum, force)
}

func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force bool) error {
if checkHashSum && l.hooksSynchronized() {
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ 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
if err = l.createHooksIfNeeded(cfg, true, false); err != nil {
if err = l.syncHooks(cfg, true, false); err != nil {
log.Warn(
`⚠️ There was a problem with synchronizing git hooks.
Run 'lefthook install' manually.`,
Expand Down

0 comments on commit e1d8aef

Please sign in to comment.