Skip to content

Commit

Permalink
feat: add refetch option to remotes config (#739)
Browse files Browse the repository at this point in the history
* feat: add refetch option to remotes config

* chore: use latest go toolchain

* fix: replace the old config after hooks synchronization if the remotes were changed

* fix: simplify the code

* docs: add docs about refetch option
  • Loading branch information
mrexox committed Jun 3, 2024
1 parent 38ccbc1 commit c0452db
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 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
23 changes: 20 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Lefthook [supports](#config-file) YAML, JSON, and TOML configuration. In this do
- [`remotes`](#remotes)
- [`git_url`](#git_url-1)
- [`ref`](#ref-1)
- [`refetch`](#refetch)
- [`configs`](#configs)
- [Git hook](#git-hook)
- [`files` (global)](#files-global)
Expand Down Expand Up @@ -464,6 +465,10 @@ remotes:

An optional *branch* or *tag* name.

> [!NOTE]
>
> If you initially had `ref` option, ran `lefthook install`, and then removed it, lefthook won't decide which branch/tag to use as a ref. So, if you added it once, please, use it always to avoid issues in local setups.
**Example**

```yml
Expand All @@ -474,9 +479,21 @@ remotes:
ref: v1.0.0
```

> [!NOTE]
>
> If you initially had `ref` option, ran `lefthook install`, and then removed it, lefthook won't decide which branch/tag to use as a ref. So, if you added it once, please, use it always to avoid issues in local setups.
### `refetch`

**Default:** `false`

Force remote config refetching on every run. Lefthook will be refetching the specified remote every time it is called.

**Example**

```yml
# lefthook.yml

remotes:
- git_url: https://github.com/evilmartians/lefthook
refetch: true
```

### `configs`

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/evilmartians/lefthook

go 1.22

toolchain go1.22.2
toolchain go1.22.3

require (
github.com/MakeNowJust/heredoc v1.0.0
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
49 changes: 41 additions & 8 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 {
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
}
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)
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 @@ -108,6 +114,33 @@ func (l *Lefthook) createConfig(path string) error {
return nil
}

func (l *Lefthook) syncHooks(cfg *config.Config) (*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
}

remotesSynced = true
}
}

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

// 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 {
if checkHashSum && l.hooksSynchronized() {
return nil
Expand Down
5 changes: 4 additions & 1 deletion internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ 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 {
newCfg, err := l.syncHooks(cfg)
if err != nil {
log.Warn(
`⚠️ There was a problem with synchronizing git hooks.
Run 'lefthook install' manually.`,
)
} else {
cfg = newCfg
}
}

Expand Down

0 comments on commit c0452db

Please sign in to comment.