diff --git a/internal/clients/clients.go b/internal/clients/clients.go index 8e608f6..3e61079 100644 --- a/internal/clients/clients.go +++ b/internal/clients/clients.go @@ -206,8 +206,7 @@ func (r *Registry) List() []Client { func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest) (api.ClientReply, error) { r.mu.Lock() - matches := []*conn{} - matched := []string{} + matches := []entry{} connected := []string{} for _, e := range r.clients { if e.conn == nil { @@ -215,8 +214,7 @@ func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest) } connected = append(connected, fmt.Sprintf("%s (%s)", e.Host, e.ID)) if e.ID == name || e.Host == name { - matches = append(matches, e.conn) - matched = append(matched, e.ID) + matches = append(matches, *e) } } r.mu.Unlock() @@ -225,10 +223,14 @@ func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest) return api.ClientReply{}, fmt.Errorf("no client named %q is connected; connected: %s", name, strings.Join(connected, ", ")) } if len(matches) > 1 { + matched := make([]string, len(matches)) + for i, e := range matches { + matched[i] = e.ID + } slices.Sort(matched) return api.ClientReply{}, fmt.Errorf("%d clients on %s are connected; name one by its id: %s", len(matches), name, strings.Join(matched, ", ")) } - c := matches[0] + c := matches[0].conn req.Id = ids.New(ids.ClientRequest) ch := make(chan api.ClientReply, 1) diff --git a/internal/git/git.go b/internal/git/git.go index fc4c7c8..f01e5ed 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -237,13 +237,10 @@ func SquashCommit(repo, worktree, target, message string, env []string, sign fun } func signCommit(repo, commit string, sign func(payload []byte) ([]byte, error)) (string, error) { - payload, stderr, code, err := runCodeInput(context.Background(), repo, nil, nil, "cat-file", "commit", commit) + payload, err := runRaw(repo, nil, "", "cat-file", "commit", commit) if err != nil { return "", err } - if code != 0 { - return "", fmt.Errorf("git cat-file commit %s: exit status %d: %s", commit, code, stderr) - } sig, err := sign([]byte(payload)) if err != nil { return "", fmt.Errorf("signing the commit: %w", err) @@ -402,17 +399,15 @@ func run(dir string, args ...string) (string, error) { } func runEnv(dir string, env []string, args ...string) (string, error) { - out, stderr, code, err := runCode(dir, env, args...) - if err != nil { - return "", err - } - if code != 0 { - return "", fmt.Errorf("git %s: exit status %d: %s", strings.Join(args, " "), code, stderr) - } - return out, nil + return runInput(dir, env, "", args...) } func runInput(dir string, env []string, input string, args ...string) (string, error) { + out, err := runRaw(dir, env, input, args...) + return strings.TrimSpace(out), err +} + +func runRaw(dir string, env []string, input string, args ...string) (string, error) { out, stderr, code, err := runCodeInput(context.Background(), dir, env, strings.NewReader(input), args...) if err != nil { return "", err @@ -420,7 +415,7 @@ func runInput(dir string, env []string, input string, args ...string) (string, e if code != 0 { return "", fmt.Errorf("git %s: exit status %d: %s", strings.Join(args, " "), code, stderr) } - return strings.TrimSpace(out), nil + return out, nil } func runCode(dir string, env []string, args ...string) (string, string, int, error) { diff --git a/internal/git/rename.go b/internal/git/rename.go index 892a115..f84b1bc 100644 --- a/internal/git/rename.go +++ b/internal/git/rename.go @@ -2,7 +2,6 @@ package git import ( "bufio" - "context" "fmt" "io" "os" @@ -147,7 +146,7 @@ func RewriteNotes(repo string, rewrite func(string) string) error { } func catBlobs(repo string, shas []string) (map[string]string, error) { - out, err := runRaw(repo, strings.Join(shas, "\n")+"\n", "cat-file", "--batch") + out, err := runRaw(repo, nil, strings.Join(shas, "\n")+"\n", "cat-file", "--batch") if err != nil { return nil, err } @@ -182,14 +181,3 @@ func splitLines(out string) []string { } return strings.Split(out, "\n") } - -func runRaw(dir, input string, args ...string) (string, error) { - out, stderr, code, err := runCodeInput(context.Background(), dir, nil, strings.NewReader(input), args...) - if err != nil { - return "", err - } - if code != 0 { - return "", fmt.Errorf("git %s: exit status %d: %s", strings.Join(args, " "), code, stderr) - } - return out, nil -}