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:
@@ -1,11 +1,13 @@
|
|||||||
package doctor
|
package doctor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BranchCheck detects persistent roles (crew, witness, refinery) that are
|
// 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.
|
// 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 {
|
if len(c.offMainDirs) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastErr error
|
var lastErr error
|
||||||
for _, dir := range c.offMainDirs {
|
for _, dir := range c.offMainDirs {
|
||||||
// git checkout main
|
// git checkout main (local operation, short timeout)
|
||||||
cmd := exec.Command("git", "checkout", "main")
|
cmd := exec.Command("git", "checkout", "main")
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
@@ -103,10 +108,16 @@ func (c *BranchCheck) Fix(ctx *CheckContext) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// git pull --rebase
|
// git pull --rebase (network operation, needs timeout)
|
||||||
cmd = exec.Command("git", "pull", "--rebase")
|
ctx, cancel := context.WithTimeout(context.Background(), gitNetworkTimeout)
|
||||||
|
cmd = exec.CommandContext(ctx, "git", "pull", "--rebase")
|
||||||
cmd.Dir = dir
|
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
|
// Pull failure is not fatal, just warn
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user