Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: worktrees remote issue #963

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/config/skip_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type mockCmd struct{}

func (mc mockCmd) WithEnvs(...string) system.Command {
func (mc mockCmd) WithoutEnvs() system.Command {
return mc
}

Expand Down
4 changes: 2 additions & 2 deletions internal/git/command_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions internal/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type gitCmd struct{}

func (g gitCmd) WithEnvs(...string) system.Command {
func (g gitCmd) WithoutEnvs() system.Command {
return g
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
30 changes: 10 additions & 20 deletions internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package system

import (
"context"
"fmt"
"io"
"os"
"os/exec"
Expand All @@ -12,34 +11,22 @@ 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
}

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
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading