Archived
- On startup, after the local state is shown, fetch the superproject and every initialized submodule (all remotes, prune, at most 8 at once). Fetches never prompt; failures show git's message. --no-fetch skips it, f fetches again. - The header shows "fetching n/m", then the fetch age and failures; the detail pane shows each submodule's last fetch and fetch errors. - The git provider polls each repository's metadata (refs, HEAD, index, .gitmodules) every 2 s and reports changes. The app reloads only what changed, keeping old values, expansion and cursor; loads are single-flight with coalesced reruns. Refresh is driven by the repositories, so a fetch in another shell updates the view like ours. - r reloads everything (worktree edits are not watched). - Docs: F50-F53, N1/N8 rewritten, D18-D20 (D12 superseded). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package git
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// fetchTimeout bounds a single fetch, e.g. against an unreachable host.
|
|
const fetchTimeout = 2 * time.Minute
|
|
|
|
// maxFetches bounds concurrent fetches; they wait on the network, not the
|
|
// CPU.
|
|
const maxFetches = 8
|
|
|
|
// sshBatchEnv makes ssh fail instead of asking for a password or host key,
|
|
// which would corrupt the terminal UI. It is nil if the user configured
|
|
// their own ssh command.
|
|
func sshBatchEnv(root string) []string {
|
|
if os.Getenv("GIT_SSH_COMMAND") != "" {
|
|
return nil
|
|
}
|
|
if out, ok, _ := try(context.Background(), root, "config", "core.sshCommand"); ok && out != "" {
|
|
return nil
|
|
}
|
|
return []string{"GIT_SSH_COMMAND=ssh -o BatchMode=yes"}
|
|
}
|
|
|
|
// Fetchable reports whether path ("" is the superproject) is a checked-out
|
|
// repository.
|
|
func (p *Provider) Fetchable(path string) bool {
|
|
_, ok := p.repoDir(path)
|
|
return ok
|
|
}
|
|
|
|
// repoDir returns the worktree of path if it is initialized.
|
|
func (p *Provider) repoDir(path string) (string, bool) {
|
|
if path == "" {
|
|
return p.root, true
|
|
}
|
|
p.mu.RLock()
|
|
in, ok := p.mods[path]
|
|
p.mu.RUnlock()
|
|
if !ok || !initialized(in.dir) {
|
|
return "", false
|
|
}
|
|
return in.dir, true
|
|
}
|
|
|
|
// Fetch fetches all remotes of one repository. Submodules are fetched
|
|
// separately, not recursively. The error is git's first line of output.
|
|
func (p *Provider) Fetch(ctx context.Context, path string) error {
|
|
dir, ok := p.repoDir(path)
|
|
if !ok {
|
|
return fmt.Errorf("%s is not initialized", path)
|
|
}
|
|
select {
|
|
case p.fetchSem <- struct{}{}:
|
|
defer func() { <-p.fetchSem }()
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, fetchTimeout)
|
|
defer cancel()
|
|
|
|
_, err := runEnv(ctx, dir, p.sshEnv, "fetch", "--all", "--prune", "--no-recurse-submodules", "--quiet")
|
|
var ge *gitError
|
|
switch {
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
return fmt.Errorf("timed out after %s", fetchTimeout)
|
|
case errors.As(err, &ge) && ge.Stderr != "":
|
|
line, _, _ := strings.Cut(ge.Stderr, "\n")
|
|
return errors.New(strings.TrimPrefix(line, "fatal: "))
|
|
}
|
|
return err
|
|
}
|