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>
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package git
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// gitEnv keeps git non-interactive, lock-free for reads and its output
|
|
// parseable.
|
|
var gitEnv = append(os.Environ(),
|
|
"GIT_TERMINAL_PROMPT=0",
|
|
"GIT_OPTIONAL_LOCKS=0",
|
|
"LC_ALL=C",
|
|
)
|
|
|
|
// gitError is a failed git command; Stderr is git's message.
|
|
type gitError struct {
|
|
Args []string
|
|
Stderr string
|
|
Err error
|
|
}
|
|
|
|
func (e *gitError) Error() string {
|
|
msg := e.Stderr
|
|
if msg == "" {
|
|
msg = e.Err.Error()
|
|
}
|
|
return "git " + strings.Join(e.Args, " ") + ": " + msg
|
|
}
|
|
|
|
func (e *gitError) Unwrap() error { return e.Err }
|
|
|
|
// run executes git in dir and returns stdout with the trailing newline
|
|
// removed. The error includes git's stderr.
|
|
func run(ctx context.Context, dir string, args ...string) (string, error) {
|
|
return runEnv(ctx, dir, nil, args...)
|
|
}
|
|
|
|
// runEnv is run with additional environment variables.
|
|
func runEnv(ctx context.Context, dir string, env []string, args ...string) (string, error) {
|
|
cmd := exec.CommandContext(ctx, "git", append([]string{"-C", dir}, args...)...)
|
|
cmd.Env = slices.Concat(gitEnv, env) // a fresh slice: run is called concurrently
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
|
if err := cmd.Run(); err != nil {
|
|
if ctx.Err() != nil {
|
|
return "", ctx.Err()
|
|
}
|
|
return "", &gitError{Args: args, Stderr: strings.TrimSpace(stderr.String()), Err: err}
|
|
}
|
|
return strings.TrimRight(stdout.String(), "\n"), nil
|
|
}
|
|
|
|
// try is run for commands whose failure only means "no answer", e.g.
|
|
// "no upstream" or "ref does not exist". Context errors still propagate.
|
|
func try(ctx context.Context, dir string, args ...string) (string, bool, error) {
|
|
out, err := run(ctx, dir, args...)
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
return "", false, err
|
|
}
|
|
return "", false, nil
|
|
}
|
|
return out, true, nil
|
|
}
|