Skip to content

Commit c5f1502

Browse files
authored
fix: remote cloning issues (#969)
1 parent 2d0fc7f commit c5f1502

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

internal/config/skip_checker_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type mockCmd struct{}
1313

14-
func (mc mockCmd) WithoutEnvs() system.Command {
14+
func (mc mockCmd) WithoutEnvs(...string) system.Command {
1515
return mc
1616
}
1717

internal/git/command_executor.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func NewExecutor(cmd system.Command) *CommandExecutor {
2121
return &CommandExecutor{cmd: cmd}
2222
}
2323

24-
func (c CommandExecutor) WithoutEnvs() CommandExecutor {
25-
return CommandExecutor{cmd: c.cmd.WithoutEnvs(), root: c.root}
24+
func (c CommandExecutor) WithoutEnvs(envs ...string) CommandExecutor {
25+
return CommandExecutor{cmd: c.cmd.WithoutEnvs(envs...), root: c.root}
2626
}
2727

2828
// Cmd runs plain string command. Trims spaces around output.

internal/git/remote.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (r *Repository) updateRemote(path, ref string) error {
6666
log.Debugf("Updating remote config repository: %s", path)
6767

6868
// This is overwriting ENVs for worktrees, otherwise it does not work.
69-
git := r.Git.WithoutEnvs()
69+
git := r.Git.WithoutEnvs("GIT_DIR", "GIT_INDEX_FILE")
7070

7171
if len(ref) != 0 {
7272
_, err := git.Cmd([]string{
@@ -102,7 +102,7 @@ func (r *Repository) cloneRemote(dest, directoryName, url, ref string) error {
102102
}
103103
cmdClone = append(cmdClone, url, directoryName)
104104

105-
_, err := r.Git.WithoutEnvs().Cmd(cmdClone)
105+
_, err := r.Git.WithoutEnvs("GIT_DIR", "GIT_INDEX_FILE").Cmd(cmdClone)
106106
if err != nil {
107107
return err
108108
}

internal/git/repository_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type gitCmd struct {
1414
cases map[string]string
1515
}
1616

17-
func (g gitCmd) WithoutEnvs() system.Command {
17+
func (g gitCmd) WithoutEnvs(...string) system.Command {
1818
return g
1919
}
2020

internal/lefthook/run_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
type gitCmd struct{}
1717

18-
func (g gitCmd) WithoutEnvs() system.Command {
18+
func (g gitCmd) WithoutEnvs(...string) system.Command {
1919
return g
2020
}
2121

internal/lefthook/runner/runner_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (e cmd) RunWithContext(context.Context, []string, string, io.Reader, io.Wri
4444
return nil
4545
}
4646

47-
func (g *gitCmd) WithoutEnvs() system.Command {
47+
func (g *gitCmd) WithoutEnvs(...string) system.Command {
4848
return g
4949
}
5050

internal/system/system.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@ import (
66
"io"
77
"os"
88
"os/exec"
9+
"strings"
910

1011
"github.com/evilmartians/lefthook/internal/log"
1112
)
1213

1314
type osCmd struct {
14-
noEnvs bool
15+
excludeEnvs []string
1516
}
1617

1718
var Cmd = osCmd{}
1819

1920
type Command interface {
20-
WithoutEnvs() Command
21+
WithoutEnvs(...string) Command
2122
Run([]string, string, io.Reader, io.Writer, io.Writer) error
2223
}
2324

2425
type CommandWithContext interface {
2526
RunWithContext(context.Context, []string, string, io.Reader, io.Writer, io.Writer) error
2627
}
2728

28-
func (c osCmd) WithoutEnvs() Command {
29-
c.noEnvs = true
29+
func (c osCmd) WithoutEnvs(envs ...string) Command {
30+
c.excludeEnvs = envs
3031
return c
3132
}
3233

@@ -47,8 +48,18 @@ func (c osCmd) RunWithContext(
4748
log.Debug("[lefthook] cmd: ", command)
4849

4950
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
50-
if c.noEnvs {
51-
cmd.Env = []string{"LEFTHOOK=0"}
51+
if len(c.excludeEnvs) > 0 {
52+
loop:
53+
for _, env := range os.Environ() {
54+
for _, noenv := range c.excludeEnvs {
55+
if strings.HasPrefix(env, noenv) {
56+
log.Debug("[lefthook] noenv ", env)
57+
continue loop
58+
}
59+
}
60+
cmd.Env = append(cmd.Env, env)
61+
}
62+
cmd.Env = append(cmd.Env, "LEFTHOOK=0")
5263
} else {
5364
cmd.Env = os.Environ()
5465
cmd.Env = append(cmd.Env, "LEFTHOOK=0")

0 commit comments

Comments
 (0)