Skip to content

Commit a8a1bc9

Browse files
authored
Extracts the git env logic in snapshot.go (#760)
1 parent 7539134 commit a8a1bc9

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

internal/preflight/git.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"slices"
78
"strings"
89
)
910

@@ -46,3 +47,13 @@ func gitOutput(dir string, env []string, debug bool, args ...string) (string, er
4647
func RepositoryRoot(dir string, debug bool) (string, error) {
4748
return gitOutput(dir, nil, debug, "rev-parse", "--show-toplevel")
4849
}
50+
51+
// tempIndexEnv returns a copy of the current environment with GIT_INDEX_FILE
52+
// set to path, stripping any existing GIT_INDEX_FILE entry. This is used to
53+
// direct git commands at a temporary index without affecting the real one.
54+
func tempIndexEnv(path string) []string {
55+
env := slices.DeleteFunc(os.Environ(), func(e string) bool {
56+
return strings.HasPrefix(e, "GIT_INDEX_FILE=")
57+
})
58+
return append(env, "GIT_INDEX_FILE="+path)
59+
}

internal/preflight/snapshot.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ func Snapshot(dir string, preflightID uuid.UUID, opts ...SnapshotOption) (*Snaps
6969
tmp.Close()
7070
defer os.Remove(tmpIndex)
7171

72-
var env []string
73-
for _, e := range os.Environ() {
74-
if !strings.HasPrefix(e, "GIT_INDEX_FILE=") {
75-
env = append(env, e)
76-
}
77-
}
78-
env = append(env, fmt.Sprintf("GIT_INDEX_FILE=%s", tmpIndex))
72+
env := tempIndexEnv(tmpIndex)
7973

8074
// Seed the temp index from HEAD.
8175
if err := gitRun(dir, env, cfg.debug, "read-tree", "HEAD"); err != nil {

0 commit comments

Comments
 (0)