Skip to content

Commit 0710e54

Browse files
committed
fixup: inherit environment variables for git invocations
1 parent 0b51e07 commit 0710e54

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

cmd/goprompt/cmdQuery.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
313313
gitDir, _ := stringExec("git", "rev-parse", "--path-format=absolute", "--git-dir")
314314

315315
subTasks.Go(func(ctx context.Context) error {
316-
317316
headRef := ""
318317
if cherryHeadB, _ := os.ReadFile(filepath.Join(gitDir, "CHERRY_PICK_HEAD")); len(cherryHeadB) > 0 {
319318
headRef = trim(string(cherryHeadB))
@@ -472,3 +471,8 @@ func startPrinter() (func(), func(name string, value interface{})) {
472471
printPart(_partPid, os.Getpid())
473472
return printerStop, printPart
474473
}
474+
475+
func jsonPart(d interface{}) string {
476+
b, _ := json.Marshal(d)
477+
return string(b)
478+
}

cmd/goprompt/utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func stringExec(path string, args ...string) (string, error) {
8282

8383
out, err := shellout.New(ctx,
8484
shellout.Args(path, args...),
85+
shellout.EnvInherit(),
8586
shellout.EnvSet(map[string]string{
8687
"GIT_OPTIONAL_LOCKS": "0",
8788
}),

pkg/shellout/cli.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Cmd struct {
1717
stderr io.Writer
1818
stdout io.Writer
1919

20-
env map[string]string
20+
env []string
2121

2222
cmd *exec.Cmd
2323
}
@@ -53,7 +53,15 @@ func ArgsAddFN(fn func() (args []string)) Option {
5353

5454
func EnvSet(env map[string]string) Option {
5555
return func(ex *Cmd) {
56-
ex.env = env
56+
for k, v := range env {
57+
ex.env = append(ex.env, fmt.Sprintf("%s=%s", k, v))
58+
}
59+
}
60+
}
61+
62+
func EnvInherit() Option {
63+
return func(ex *Cmd) {
64+
ex.env = append(ex.env, os.Environ()...)
5765
}
5866
}
5967

@@ -116,9 +124,7 @@ func (ex *Cmd) Pipe(other *Cmd, tee bool) error {
116124
}
117125

118126
func New(ctx context.Context, options ...Option) *Cmd {
119-
ex := &Cmd{
120-
env: map[string]string{},
121-
}
127+
ex := &Cmd{}
122128
for _, opt := range options {
123129
opt(ex)
124130
}
@@ -127,10 +133,7 @@ func New(ctx context.Context, options ...Option) *Cmd {
127133
cmd.Stdin = ex.stdin
128134
cmd.Stdout = ex.stdout
129135
cmd.Stderr = ex.stderr
130-
131-
for k, v := range ex.env {
132-
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
133-
}
136+
cmd.Env = ex.env
134137

135138
ex.cmd = cmd
136139

0 commit comments

Comments
 (0)