Skip to content

Commit

Permalink
feature: Support remote scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>
  • Loading branch information
mrexox committed Nov 1, 2022
1 parent ccb67cc commit 95811e7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
7 changes: 6 additions & 1 deletion internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func mergeAll(fs afero.Fs, path string) (*viper.Viper, error) {
return extends, nil
}

// mergeRemote merges remote config to the current one.
func mergeRemote(fs afero.Fs, v *viper.Viper) error {
var remote Remote
err := v.UnmarshalKey("remote", &remote)
Expand Down Expand Up @@ -123,7 +124,10 @@ func mergeRemote(fs afero.Fs, v *viper.Viper) error {
return nil
}

if err := merge(fs, configPath, v); err != nil {
// TODO: Rewrite using common merge()
v.SetConfigName("remote")
v.SetConfigFile(configPath)
if err := v.MergeInConfig(); err != nil {
return err
}

Expand All @@ -139,6 +143,7 @@ func extend(fs afero.Fs, v *viper.Viper) error {
return nil
}

// FIXME: Use MergeInConfig because MergeConfigMap destroys scripts names.
func merge(fs afero.Fs, path string, v *viper.Viper) error {
name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))

Expand Down
11 changes: 11 additions & 0 deletions internal/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ func RemoteFolder(url string) (string, error) {
), nil
}

func (r *Repository) RemoteFolder(url string) string {
remotesPath := filepath.Join(r.InfoPath, remotesFolder)

return filepath.Join(
remotesPath,
filepath.Base(
strings.TrimSuffix(url, filepath.Ext(url)),
),
)
}

// SyncRemote clones or pulls the latest changes for a git repository that was
// specified as a remote config repository. If successful, the path to the root
// of the repository will be returned.
Expand Down
24 changes: 19 additions & 5 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -70,7 +71,7 @@ func (l *Lefthook) Run(hookName string, gitArgs []string) error {
// This line controls updating the git hook if config has changed
if err = l.createHooksIfNeeded(cfg, false); err != nil {
log.Warn(
`⚠️ There was a problem with synchronizing git hooks.
`⚠️ There was a problem with synchronizing git hooks.
Run 'lefthook install' manually.`,
)
}
Expand All @@ -92,11 +93,24 @@ Run 'lefthook install' manually.`,
resultChan := make(chan runner.Result, len(hook.Commands)+len(hook.Scripts))
run := runner.NewRunner(l.Fs, l.repo, hook, gitArgs, resultChan, logSettings)

go func() {
run.RunAll(
hookName,
[]string{cfg.SourceDir, cfg.SourceDirLocal},
sourceDirs := []string{
filepath.Join(l.repo.RootPath, cfg.SourceDir),
filepath.Join(l.repo.RootPath, cfg.SourceDirLocal),
}

if cfg.Remote.Configured() {
// Apend only source_dir, because source_dir_local doesn't make sense
sourceDirs = append(
sourceDirs,
filepath.Join(
l.repo.RemoteFolder(cfg.Remote.GitURL),
cfg.SourceDir,
),
)
}

go func() {
run.RunAll(hookName, sourceDirs)
close(resultChan)
}()

Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *Runner) RunAll(hookName string, sourceDirs []string) {
scriptDirs := make([]string, len(sourceDirs))
for _, sourceDir := range sourceDirs {
scriptDirs = append(scriptDirs, filepath.Join(
r.repo.RootPath, sourceDir, hookName,
sourceDir, hookName,
))
}

Expand Down

0 comments on commit 95811e7

Please sign in to comment.