@@ -6,27 +6,28 @@ import (
6
6
"io"
7
7
"os"
8
8
"os/exec"
9
+ "strings"
9
10
10
11
"github.com/evilmartians/lefthook/internal/log"
11
12
)
12
13
13
14
type osCmd struct {
14
- noEnvs bool
15
+ excludeEnvs [] string
15
16
}
16
17
17
18
var Cmd = osCmd {}
18
19
19
20
type Command interface {
20
- WithoutEnvs () Command
21
+ WithoutEnvs (... string ) Command
21
22
Run ([]string , string , io.Reader , io.Writer , io.Writer ) error
22
23
}
23
24
24
25
type CommandWithContext interface {
25
26
RunWithContext (context.Context , []string , string , io.Reader , io.Writer , io.Writer ) error
26
27
}
27
28
28
- func (c osCmd ) WithoutEnvs () Command {
29
- c .noEnvs = true
29
+ func (c osCmd ) WithoutEnvs (envs ... string ) Command {
30
+ c .excludeEnvs = envs
30
31
return c
31
32
}
32
33
@@ -47,8 +48,18 @@ func (c osCmd) RunWithContext(
47
48
log .Debug ("[lefthook] cmd: " , command )
48
49
49
50
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" )
52
63
} else {
53
64
cmd .Env = os .Environ ()
54
65
cmd .Env = append (cmd .Env , "LEFTHOOK=0" )
0 commit comments