fix(doctor): add 30s timeout to git pull in branch check

The Fix method in BranchCheck was calling git pull --rebase without
a timeout, which could hang indefinitely when network or authentication
issues occurred. Added a 30-second timeout using exec.CommandContext.

Fixes: gt-i5qb

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
valkyrie
2026-01-26 12:40:24 -08:00
committed by John Ogle
parent 2a789c8440
commit 0e19529186

View File

@@ -1,11 +1,13 @@
package doctor
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
// BranchCheck detects persistent roles (crew, witness, refinery) that are
@@ -87,15 +89,18 @@ func (c *BranchCheck) Run(ctx *CheckContext) *CheckResult {
}
}
// gitNetworkTimeout is the timeout for git network operations (pull, fetch).
const gitNetworkTimeout = 30 * time.Second
// Fix switches all off-main directories to main branch.
func (c *BranchCheck) Fix(ctx *CheckContext) error {
func (c *BranchCheck) Fix(checkCtx *CheckContext) error {
if len(c.offMainDirs) == 0 {
return nil
}
var lastErr error
for _, dir := range c.offMainDirs {
// git checkout main
// git checkout main (local operation, short timeout)
cmd := exec.Command("git", "checkout", "main")
cmd.Dir = dir
if err := cmd.Run(); err != nil {
@@ -103,10 +108,16 @@ func (c *BranchCheck) Fix(ctx *CheckContext) error {
continue
}
// git pull --rebase
cmd = exec.Command("git", "pull", "--rebase")
// git pull --rebase (network operation, needs timeout)
ctx, cancel := context.WithTimeout(context.Background(), gitNetworkTimeout)
cmd = exec.CommandContext(ctx, "git", "pull", "--rebase")
cmd.Dir = dir
if err := cmd.Run(); err != nil {
err := cmd.Run()
cancel()
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
lastErr = fmt.Errorf("%s: git pull timed out after %v", dir, gitNetworkTimeout)
}
// Pull failure is not fatal, just warn
continue
}