Skip to content

Commit 5cf4009

Browse files
authored
xec: Always set GIT_SPICE environment variable (#989)
Now that all os/exec usage has been centralized, always set GIT_SPICE=1 for any subprocesses, so that custom command and scripts can do something useful with that.
1 parent 763b5c5 commit 5cf4009

File tree

9 files changed

+80
-7
lines changed

9 files changed

+80
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: Added
2+
body: >-
3+
git-spice now sets the `GIT_SPICE=1` environment variable
4+
for all commands it spawns, including git, editors, etc.
5+
This can be used, e.g., in git hooks to detect when they are
6+
being run in the context of git-spice.
7+
time: 2026-01-11T11:33:11.036339-08:00

doc/mkdocs.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ nav:
6767
- start/install.md
6868
- start/stack.md
6969
- start/submit.md
70+
- Setting up: &setup
71+
- setup/index.md
72+
- setup/auth.md
73+
- setup/shell.md
7074
- User Guide: &user-guide
7175
- guide/index.md
7276
- guide/concepts.md
@@ -75,10 +79,6 @@ nav:
7579
- guide/limits.md
7680
- guide/troubleshooting.md
7781
- guide/internals.md
78-
- Setting up: &setup
79-
- setup/index.md
80-
- setup/auth.md
81-
- setup/shell.md
8282
- CLI: &cli
8383
- cli/index.md
8484
- cli/reference.md

doc/src/setup/auth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Select an authentication method: {red}Personal Access Token{reset}
185185
you want to use git-spice with
186186
- in the *Repository permissions* section,
187187
grant **Read and write** access to **Pull requests** and **Contents**
188-
- (<!-- gs:version 0.21.0 --> or higher)
188+
- (<!-- gs:version v0.21.0 --> or higher)
189189
in the *Organization permissions* section,
190190
grant **Read-only** access to **Members**
191191

help.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func helpPrinter(_ kong.HelpOptions, ctx *kong.Context) error {
8282
w.Print("Aliases can be combined to form shorthands for commands. For example:")
8383
w.Printf(" %s bc => %s branch create", ctx.Model.Name, ctx.Model.Name)
8484
w.Printf(" %s cc => %s commit create", ctx.Model.Name, ctx.Model.Name)
85+
86+
w.Print("")
87+
w.Print("Environment variables:")
88+
w.Print(" GIT_SPICE=1 Set for all commands spawned by git-spice")
8589
}
8690

8791
return nil

internal/xec/cmd.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
//
2020
// - use Stderr to redirect stderr elsewhere
2121
// - use WithLogPrefix to change the prefix for log messages
22+
//
23+
// # Environment variables
24+
//
25+
// All commands spawned via this package
26+
// always receive the environment variable "GIT_SPICE=1".
27+
//
28+
// Additional environment variables may be set
29+
// with the AppendEnv method.
2230
package xec
2331

2432
import (
@@ -36,6 +44,8 @@ import (
3644
"go.abhg.dev/gs/internal/silog"
3745
)
3846

47+
const _gitSpiceEnv = "GIT_SPICE=1"
48+
3949
var _osEnviron = os.Environ
4050

4151
// Cmd is an external command being prepared or run.
@@ -63,7 +73,7 @@ func Command(ctx context.Context, log *silog.Logger, name string, args ...string
6373
stderr, wrap := outputLogWriter("stderr", logger)
6474
cmd := exec.CommandContext(ctx, name, args...)
6575
cmd.Stderr = stderr
66-
cmd.Env = _osEnviron()
76+
cmd.Env = append(_osEnviron(), _gitSpiceEnv)
6777
return &Cmd{
6878
cmd: cmd,
6979
log: logger,

internal/xec/cmd_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ func TestCmd_AppendEnv(t *testing.T) {
179179
require.NoError(t, err)
180180
assert.Equal(t, "value1 value2", output)
181181
})
182+
183+
t.Run("GitSpiceExecSet", func(t *testing.T) {
184+
cmd := Command(ctx, log, "sh", "-c", "echo $GIT_SPICE")
185+
186+
output, err := cmd.OutputChomp()
187+
require.NoError(t, err)
188+
assert.Equal(t, "1", output)
189+
})
182190
}
183191

184192
func TestCmd_Run(t *testing.T) {

internal/xec/edit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package xec
22

3-
import "os/exec"
3+
import (
4+
"os"
5+
"os/exec"
6+
)
47

58
// EditCommand constructs a command to open the editor
69
// with the given editor command.
@@ -16,5 +19,6 @@ func EditCommand(editCmd string, args ...string) *exec.Cmd {
1619
args = append([]string{"-c", editCmd + ` "$@"`, "--"}, args...)
1720
cmd = exec.Command("sh", args...)
1821
}
22+
cmd.Env = append(os.Environ(), _gitSpiceEnv)
1923
return cmd
2024
}

internal/xec/edit_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package xec
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestEditCommand_setsGitSpiceExec(t *testing.T) {
13+
// Subprocess mode: print GIT_SPICE value and exit.
14+
if os.Getenv("INSIDE_TEST") == "1" {
15+
fmt.Print(os.Getenv("GIT_SPICE"))
16+
os.Exit(0)
17+
}
18+
19+
t.Run("SimpleEditor", func(t *testing.T) {
20+
cmd := EditCommand(_testBinary, "-test.run", "^"+t.Name()+"$")
21+
cmd.Env = append(cmd.Env, "INSIDE_TEST=1")
22+
23+
output, err := cmd.Output()
24+
require.NoError(t, err)
25+
assert.Equal(t, "1", string(output))
26+
})
27+
28+
t.Run("ShellEditor", func(t *testing.T) {
29+
cmd := EditCommand(fmt.Sprintf("%q -test.run '^%s$'", _testBinary, t.Name()))
30+
cmd.Env = append(cmd.Env, "INSIDE_TEST=1")
31+
cmd.Stderr = t.Output()
32+
33+
output, err := cmd.Output()
34+
require.NoError(t, err)
35+
assert.Equal(t, "1", string(output))
36+
})
37+
}

testdata/help/gs.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,6 @@ Run "gs <command> --help" for more information on a command.
8080
Aliases can be combined to form shorthands for commands. For example:
8181
gs bc => gs branch create
8282
gs cc => gs commit create
83+
84+
Environment variables:
85+
GIT_SPICE=1 Set for all commands spawned by git-spice

0 commit comments

Comments
 (0)