From a9dd3ad96298602f2054cd2edf147911f56b2995 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Wed, 26 Feb 2025 10:29:32 +0300 Subject: [PATCH] fix: worktrees remote issue --- internal/config/skip_checker_test.go | 2 +- internal/git/command_executor.go | 4 ++-- internal/git/remote.go | 9 +++----- internal/git/repository_test.go | 2 +- internal/lefthook/run_test.go | 2 +- internal/lefthook/runner/runner_test.go | 2 +- internal/system/system.go | 30 +++++++++---------------- schema.json | 2 +- 8 files changed, 20 insertions(+), 33 deletions(-) diff --git a/internal/config/skip_checker_test.go b/internal/config/skip_checker_test.go index 0e42d79a..5c835ea0 100644 --- a/internal/config/skip_checker_test.go +++ b/internal/config/skip_checker_test.go @@ -11,7 +11,7 @@ import ( type mockCmd struct{} -func (mc mockCmd) WithEnvs(...string) system.Command { +func (mc mockCmd) WithoutEnvs() system.Command { return mc } diff --git a/internal/git/command_executor.go b/internal/git/command_executor.go index 0c62bab2..507eef07 100644 --- a/internal/git/command_executor.go +++ b/internal/git/command_executor.go @@ -21,8 +21,8 @@ func NewExecutor(cmd system.Command) *CommandExecutor { return &CommandExecutor{cmd: cmd} } -func (c CommandExecutor) WithEnvs(envs ...string) CommandExecutor { - return CommandExecutor{cmd: c.cmd.WithEnvs(envs...), root: c.root} +func (c CommandExecutor) WithoutEnvs() CommandExecutor { + return CommandExecutor{cmd: c.cmd.WithoutEnvs(), root: c.root} } // Cmd runs plain string command. Trims spaces around output. diff --git a/internal/git/remote.go b/internal/git/remote.go index 44062382..ad2f761f 100644 --- a/internal/git/remote.go +++ b/internal/git/remote.go @@ -65,11 +65,8 @@ func (r *Repository) SyncRemote(url, ref string, force bool) error { func (r *Repository) updateRemote(path, ref string) error { log.Debugf("Updating remote config repository: %s", path) - // This is overwriting values for worktrees, otherwise it does not work. - git := r.Git.WithEnvs( - "GIT_DIR", filepath.Join(path, ".git"), - "GIT_INDEX_FILE", filepath.Join(path, ".git", "index"), - ) + // This is overwriting ENVs for worktrees, otherwise it does not work. + git := r.Git.WithoutEnvs() if len(ref) != 0 { _, err := git.Cmd([]string{ @@ -105,7 +102,7 @@ func (r *Repository) cloneRemote(dest, directoryName, url, ref string) error { } cmdClone = append(cmdClone, url, directoryName) - _, err := r.Git.Cmd(cmdClone) + _, err := r.Git.WithoutEnvs().Cmd(cmdClone) if err != nil { return err } diff --git a/internal/git/repository_test.go b/internal/git/repository_test.go index 1888074a..ce10b404 100644 --- a/internal/git/repository_test.go +++ b/internal/git/repository_test.go @@ -14,7 +14,7 @@ type gitCmd struct { cases map[string]string } -func (g gitCmd) WithEnvs(...string) system.Command { +func (g gitCmd) WithoutEnvs() system.Command { return g } diff --git a/internal/lefthook/run_test.go b/internal/lefthook/run_test.go index 14fb75d6..73297f3e 100644 --- a/internal/lefthook/run_test.go +++ b/internal/lefthook/run_test.go @@ -15,7 +15,7 @@ import ( type gitCmd struct{} -func (g gitCmd) WithEnvs(...string) system.Command { +func (g gitCmd) WithoutEnvs() system.Command { return g } diff --git a/internal/lefthook/runner/runner_test.go b/internal/lefthook/runner/runner_test.go index 34b2e417..aa17d0aa 100644 --- a/internal/lefthook/runner/runner_test.go +++ b/internal/lefthook/runner/runner_test.go @@ -44,7 +44,7 @@ func (e cmd) RunWithContext(context.Context, []string, string, io.Reader, io.Wri return nil } -func (g *gitCmd) WithEnvs(...string) system.Command { +func (g *gitCmd) WithoutEnvs() system.Command { return g } diff --git a/internal/system/system.go b/internal/system/system.go index 15811b5f..59e8dd61 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -3,7 +3,6 @@ package system import ( "context" - "fmt" "io" "os" "os/exec" @@ -12,13 +11,13 @@ import ( ) type osCmd struct { - env []string + noEnvs bool } var Cmd = osCmd{} type Command interface { - WithEnvs(...string) Command + WithoutEnvs() Command Run([]string, string, io.Reader, io.Writer, io.Writer) error } @@ -26,20 +25,8 @@ type CommandWithContext interface { RunWithContext(context.Context, []string, string, io.Reader, io.Writer, io.Writer) error } -func (c osCmd) WithEnvs(envs ...string) Command { - if len(envs)%2 != 0 { - panic("usage: WithEnvs(name, value, name, value...") - } - - if c.env == nil { - //nolint:mnd - c.env = make([]string, 0, len(envs)/2) - } - - for i := 0; i < len(envs); i += 2 { - c.env = append(c.env, fmt.Sprintf("%s=%s", envs[i], envs[i+1])) - } - +func (c osCmd) WithoutEnvs() Command { + c.noEnvs = true return c } @@ -60,9 +47,12 @@ func (c osCmd) RunWithContext( log.Debug("[lefthook] cmd: ", command) cmd := exec.CommandContext(ctx, command[0], command[1:]...) - cmd.Env = append(cmd.Env, os.Environ()...) - cmd.Env = append(cmd.Env, c.env...) - cmd.Env = append(cmd.Env, "LEFTHOOK=0") + if c.noEnvs { + cmd.Env = []string{"LEFTHOOK=0"} + } else { + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, "LEFTHOOK=0") + } if len(root) > 0 { cmd.Dir = root diff --git a/schema.json b/schema.json index ad329f94..8603ffd3 100644 --- a/schema.json +++ b/schema.json @@ -412,7 +412,7 @@ "type": "object" } }, - "$comment": "Last updated on 2025.02.24.", + "$comment": "Last updated on 2025.02.26.", "properties": { "min_version": { "type": "string",