diff --git a/AGENTS.md b/AGENTS.md index a8ba9e5..87a51ea 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -197,3 +197,5 @@ Live runs pipeline **filesystem scan → registry upsert → parallel per-repo w - **`make test-race` is slow (~60–75s)** because `internal/git` tests shell out to the real `git` binary. This is expected, not a hang. - **Running the CLI mutates user state.** It persists a registry at `~/.config/git-rain/repos.toml` and config at `~/.config/git-rain/config.toml`, so repos discovered in one run are remembered on the next. For isolated runs/tests, always pass `--path ` (and `--dry-run` to preview without touching git). - **Manual end-to-end check:** create a bare remote + a clone under a scan dir, advance the remote, then `./git-rain --sync --path ` fast-forwards the local branch. Requires `git user.name`/`user.email` to be set (already configured here). +- **Network fetch timeouts:** `global.fetch_timeout_sec` (default `120`, `0` disables), `--fetch-timeout`, and `GIT_RAIN_GLOBAL_FETCH_TIMEOUT_SEC` bound per-repo `git fetch` via `RunNetworkGit`. Stalled remotes should surface `timed out after Ns` and not block the run summary. +- **Interrupts:** live runs use `signal.NotifyContext` (SIGINT/SIGTERM) so Ctrl-C cancels in-flight network git and prints a partial summary with exit code `1`. diff --git a/README.md b/README.md index e2fe6d5..ff21e05 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,7 @@ git-rain --init | `--prune` | Pass `--prune` on fetch for this run (highest precedence; cannot combine with `--no-prune`) | | `--no-prune` | Never pass `--prune` on fetch for this run (overrides `--prune`, config, registry, and `rain.fetchprune`) | | `--tags` | Also pass `--tags` on fetch operations | +| `--fetch-timeout ` | Per-fetch network deadline in seconds (`0` = disabled; default from `global.fetch_timeout_sec`) | | `--path ` | Scan path override (default: config `global.scan_path`) | | `--no-scan` | Skip filesystem scan; hydrate only known registry repos | | `--risky` | Allow destructive local branch realignment after creating backup refs | @@ -340,6 +341,7 @@ scan_path = "/home/you/projects" # root to discover repos under scan_depth = 5 # max directory depth (default in app: 10) scan_workers = 8 # parallel scan workers fetch_workers = 4 # parallel per-repo operations (default in app: 4) +fetch_timeout_sec = 120 # per-fetch network deadline (0 = no timeout) risky_mode = false # allow destructive realignment on full hydration path branch_mode = "mainline" # full hydration: mainline | checked-out | all-local | all-branches fetch_prune = false # pass --prune on fetch when true (default off; see README warning) @@ -363,8 +365,14 @@ All options can be overridden with environment variables using the `GIT_RAIN_` p ```bash GIT_RAIN_GLOBAL_RISKY_MODE=true git-rain GIT_RAIN_GLOBAL_SCAN_PATH=/tmp/repos git-rain +GIT_RAIN_GLOBAL_FETCH_TIMEOUT_SEC=60 git-rain ``` +Network fetches set `GIT_TERMINAL_PROMPT=0` and, unless you already configured +`GIT_SSH_COMMAND`, `core.sshCommand`, or the HTTP low-speed variables, add +batch-mode SSH (`BatchMode=yes`, `ConnectTimeout=30`) and HTTP stall detection +(`GIT_HTTP_LOW_SPEED_LIMIT` / `GIT_HTTP_LOW_SPEED_TIME`). + ### Garden mode tuning (advanced) Picking `rain_animation_mode = "garden"` swaps the rain background for a diff --git a/internal/git/command.go b/internal/git/command.go index 2808601..680223c 100644 --- a/internal/git/command.go +++ b/internal/git/command.go @@ -7,7 +7,10 @@ import ( ) // PrepareNetworkGit configures cmd for fetch/push operations that may contact -// a remote. Delegates to git-harness so credential behavior stays aligned with git-fire. +// a remote. Delegates to git-harness for GIT_TERMINAL_PROMPT=0, then adds +// fail-fast SSH and HTTP env unless the user already set GIT_SSH_COMMAND, +// GIT_SSH, GIT_HTTP_LOW_SPEED_*, or core.sshCommand. func PrepareNetworkGit(cmd *exec.Cmd) { harnessgit.PrepareNetworkGit(cmd) + applyFailFastNetworkEnv(cmd) } diff --git a/internal/git/command_test.go b/internal/git/command_test.go index 790adbf..24cceac 100644 --- a/internal/git/command_test.go +++ b/internal/git/command_test.go @@ -3,6 +3,7 @@ package git import ( "net/http" "net/http/httptest" + "os" "os/exec" "strings" "testing" @@ -18,6 +19,106 @@ func TestFetchFailureReason_TerminalPromptsDisabled(t *testing.T) { } } +func isolatedPrepareNetworkEnv(t *testing.T) []string { + t.Helper() + return []string{ + "PATH=" + os.Getenv("PATH"), + "HOME=" + os.Getenv("HOME"), + "USER=" + os.Getenv("USER"), + "GIT_CONFIG_NOSYSTEM=1", + "GIT_CONFIG_GLOBAL=/dev/null", + "GIT_CONFIG_SYSTEM=/dev/null", + } +} + +func TestPrepareNetworkGit_SetsFailFastEnv(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-default"}) + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = isolatedPrepareNetworkEnv(t) + PrepareNetworkGit(cmd) + + env := strings.Join(cmd.Env, "\n") + if !strings.Contains(env, "GIT_TERMINAL_PROMPT=0") { + t.Fatalf("expected GIT_TERMINAL_PROMPT=0 in env") + } + if !strings.Contains(env, "GIT_HTTP_LOW_SPEED_LIMIT="+defaultHTTPLowSpeedLimit) { + t.Fatalf("expected HTTP low-speed limit in env, got %q", env) + } + if !strings.Contains(env, "GIT_SSH_COMMAND=ssh -o BatchMode=yes") { + t.Fatalf("expected batch-mode GIT_SSH_COMMAND in env, got %q", env) + } +} + +func TestPrepareNetworkGit_PreservesUserGitSSHCommand(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-user-ssh"}) + t.Setenv("GIT_SSH_COMMAND", "ssh -o IdentityFile=/tmp/custom") + + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = append(isolatedPrepareNetworkEnv(t), "GIT_SSH_COMMAND=ssh -o IdentityFile=/tmp/custom") + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_SSH_COMMAND=") && !strings.Contains(e, "IdentityFile=/tmp/custom") { + t.Fatalf("clobbered user GIT_SSH_COMMAND: %s", e) + } + } +} + +func TestPrepareNetworkGit_PreservesUserGitSSH(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-user-git-ssh"}) + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = append(isolatedPrepareNetworkEnv(t), "GIT_SSH=/usr/bin/custom-ssh") + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_SSH_COMMAND=") { + t.Fatalf("should not set GIT_SSH_COMMAND when GIT_SSH is set, got %s", e) + } + } +} + +func TestPrepareNetworkGit_PreservesCoreSSHCommand(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-core-ssh"}) + runGit := exec.Command("git", "-C", repo, "config", "core.sshCommand", "ssh -o IdentityFile=/tmp/from-core") + if out, err := runGit.CombinedOutput(); err != nil { + t.Fatalf("git config core.sshCommand: %v (%s)", err, out) + } + + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = isolatedPrepareNetworkEnv(t) + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_SSH_COMMAND=") { + t.Fatalf("should not set GIT_SSH_COMMAND when core.sshCommand is configured, got %s", e) + } + } +} + +func TestPrepareNetworkGit_PreservesUserHTTPLowSpeed(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-http"}) + t.Setenv("GIT_HTTP_LOW_SPEED_LIMIT", "42") + t.Setenv("GIT_HTTP_LOW_SPEED_TIME", "99") + + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = append(isolatedPrepareNetworkEnv(t), + "GIT_HTTP_LOW_SPEED_LIMIT=42", + "GIT_HTTP_LOW_SPEED_TIME=99", + ) + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_HTTP_LOW_SPEED_LIMIT=") && !strings.Contains(e, "=42") { + t.Fatalf("clobbered GIT_HTTP_LOW_SPEED_LIMIT: %s", e) + } + } +} + func TestNetworkFetch_UnauthenticatedHTTPFailsWithoutPrompt(t *testing.T) { authServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("WWW-Authenticate", `Basic realm="git-rain-test"`) diff --git a/internal/git/network_env.go b/internal/git/network_env.go new file mode 100644 index 0000000..0304477 --- /dev/null +++ b/internal/git/network_env.go @@ -0,0 +1,60 @@ +package git + +import ( + "fmt" + "os" + "os/exec" + "strings" +) + +const ( + defaultSSHConnectTimeoutSec = 30 + defaultHTTPLowSpeedLimit = "1000" + defaultHTTPLowSpeedTimeSec = "30" +) + +func applyFailFastNetworkEnv(cmd *exec.Cmd) { + env := cmd.Env + if env == nil { + env = os.Environ() + } + env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_LIMIT", defaultHTTPLowSpeedLimit) + env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_TIME", defaultHTTPLowSpeedTimeSec) + + // GIT_SSH_COMMAND overrides GIT_SSH; only inject when neither is set and + // core.sshCommand is unset (same preserve path as an explicit SSH wrapper). + if !envHasPrefix(env, "GIT_SSH_COMMAND=") && !envHasPrefix(env, "GIT_SSH=") && !repoHasCoreSSHCommand(cmd.Dir, env) { + sshCmd := fmt.Sprintf("ssh -o BatchMode=yes -o ConnectTimeout=%d", defaultSSHConnectTimeoutSec) + env = append(env, "GIT_SSH_COMMAND="+sshCmd) + } + cmd.Env = env +} + +func setEnvIfUnset(env []string, key, value string) []string { + prefix := key + "=" + for _, e := range env { + if strings.HasPrefix(e, prefix) { + return env + } + } + return append(env, prefix+value) +} + +func envHasPrefix(env []string, prefix string) bool { + for _, e := range env { + if strings.HasPrefix(e, prefix) { + return true + } + } + return false +} + +func repoHasCoreSSHCommand(repoDir string, env []string) bool { + if strings.TrimSpace(repoDir) == "" { + return false + } + probe := exec.Command("git", "-C", repoDir, "config", "--get", "core.sshCommand") + probe.Env = env + out, err := probe.Output() + return err == nil && strings.TrimSpace(string(out)) != "" +}