mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-26 20:45:37 +02:00
fix(repocache): pass explicit env to remote-facing git subprocesses (#1029)
fix(repocache): pass explicit env to remote-facing git subprocesses
This commit is contained in:
@@ -14,6 +14,14 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// gitEnv returns an environment for git subprocesses that contact remotes.
|
||||
// It passes the full daemon environment so credential helpers (e.g. gh) can
|
||||
// locate their config, and disables TTY prompting so auth failures produce
|
||||
// clear errors instead of blocking on a non-existent terminal.
|
||||
func gitEnv() []string {
|
||||
return append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
||||
}
|
||||
|
||||
// RepoInfo describes a repository to cache.
|
||||
type RepoInfo struct {
|
||||
URL string
|
||||
@@ -157,6 +165,7 @@ const modernFetchRefspec = "+refs/heads/*:refs/remotes/origin/*"
|
||||
|
||||
func gitCloneBare(url, dest string) error {
|
||||
cmd := exec.Command("git", "clone", "--bare", url, dest)
|
||||
cmd.Env = gitEnv()
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
// Clean up partial clone.
|
||||
os.RemoveAll(dest)
|
||||
@@ -195,7 +204,9 @@ func gitFetch(barePath string) error {
|
||||
// getRemoteDefaultBranch, but the modern-cache default-branch-change
|
||||
// path (the only path that can't be recovered any other way) relies
|
||||
// on this call.
|
||||
_ = exec.Command("git", "-C", barePath, "remote", "set-head", "origin", "--auto").Run()
|
||||
cmd := exec.Command("git", "-C", barePath, "remote", "set-head", "origin", "--auto")
|
||||
cmd.Env = gitEnv()
|
||||
_ = cmd.Run()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -203,6 +214,7 @@ func gitFetch(barePath string) error {
|
||||
// gitFetch, which migrates legacy caches first.
|
||||
func runGitFetch(barePath string) error {
|
||||
cmd := exec.Command("git", "-C", barePath, "fetch", "origin")
|
||||
cmd.Env = gitEnv()
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("git fetch: %s: %w", strings.TrimSpace(string(out)), err)
|
||||
}
|
||||
@@ -235,7 +247,9 @@ func ensureRemoteTrackingLayout(barePath string) error {
|
||||
}
|
||||
// Set refs/remotes/origin/HEAD so getRemoteDefaultBranch can read it.
|
||||
// Non-fatal: if this fails we fall back to origin/main, origin/master.
|
||||
_ = exec.Command("git", "-C", barePath, "remote", "set-head", "origin", "--auto").Run()
|
||||
cmd := exec.Command("git", "-C", barePath, "remote", "set-head", "origin", "--auto")
|
||||
cmd.Env = gitEnv()
|
||||
_ = cmd.Run()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,39 @@ func testLogger() *slog.Logger {
|
||||
return slog.Default()
|
||||
}
|
||||
|
||||
func TestGitEnv(t *testing.T) {
|
||||
t.Parallel()
|
||||
env := gitEnv()
|
||||
|
||||
// Must contain GIT_TERMINAL_PROMPT=0.
|
||||
found := false
|
||||
for _, entry := range env {
|
||||
if entry == "GIT_TERMINAL_PROMPT=0" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("gitEnv() must include GIT_TERMINAL_PROMPT=0")
|
||||
}
|
||||
|
||||
// Must contain HOME from the current environment.
|
||||
home := os.Getenv("HOME")
|
||||
if home == "" {
|
||||
t.Skip("HOME not set in test environment")
|
||||
}
|
||||
foundHome := false
|
||||
for _, entry := range env {
|
||||
if entry == "HOME="+home {
|
||||
foundHome = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundHome {
|
||||
t.Error("gitEnv() must include HOME from os.Environ()")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBareDirName(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
|
||||
Reference in New Issue
Block a user