An unofficial terminal client for Tangled, optimized for humans and agents. tgcli.wisp.place
cli atproto go tangled
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283package gitutil
import ( "context" "errors" "fmt" "net/url" "os/exec" "slices" "strconv" "strings"
"github.com/alyraffauf/tg/knot" "github.com/bluesky-social/indigo/atproto/syntax")
// HostedGitHost is Tangled's hosted Git proxy.const HostedGitHost = "tangled.org"
// defaultRemote is the conventional name of the primary git remote.const defaultRemote = "origin"
// knotRemoteURL builds an SSH URL for a repository on knotHost. Tangled's// default Knot and callers without a selected Knot use the hosted proxy.// repoPath may be a handle/repo pair or a repository DID.func knotRemoteURL(knotHost string, sshPort int, repoPath string) string { gitHost := knotHost if gitHost == "" || gitHost == knot.DefaultKnot { gitHost = HostedGitHost } if sshPort != 22 { return fmt.Sprintf("ssh://git@%s:%d/%s", gitHost, sshPort, repoPath) } return "git@" + gitHost + ":" + repoPath}
type repositoryDIDRemote struct { Protocol string KnotHost string SSHPort int RepoDID string}
func repositoryDIDRemoteURL(remote repositoryDIDRemote) (string, error) { if _, err := syntax.ParseDID(remote.RepoDID); err != nil { return "", fmt.Errorf("invalid repository DID %q: %w", remote.RepoDID, err) } switch remote.Protocol { case "ssh": return knotRemoteURL(remote.KnotHost, remote.SSHPort, remote.RepoDID), nil case "https": host := remote.KnotHost if host == "" || host == knot.DefaultKnot { host = HostedGitHost } return "https://" + host + "/" + remote.RepoDID, nil default: return "", fmt.Errorf("unsupported clone protocol %q", remote.Protocol) }}
// RepoContext holds an untrusted repository candidate parsed from a git remote// URL. KnotHost is empty for Tangled's hosted Git endpoint; otherwise callers// must verify it against the repository record before trusting the candidate.type RepoContext struct { KnotHost string Handle string Repo string RepoDID string}
// DetectRepoCandidatesFromCWD scans the git remotes in the current directory// for hosted Tangled URLs and untrusted custom-Knot SSH or HTTPS candidates,// checking the default remote first.func (c *Client) DetectRepoCandidatesFromCWD(ctx context.Context) ([]RepoContext, error) { remotes, err := c.gitLines(ctx, "remote") if err != nil { return nil, fmt.Errorf("list git remotes: %w", err) }
var candidates []RepoContext for _, name := range originFirst(remotes) { urls, err := c.gitLines(ctx, "remote", "get-url", "--all", name) if err != nil { return nil, fmt.Errorf("get URLs for remote %q: %w", name, err) } for _, raw := range urls { if candidate, ok := parseRepoCandidate(raw); ok { candidates = append(candidates, *candidate) } } } urls, err := c.branchRemoteURLs(ctx, remotes) if err != nil { return nil, err } for _, raw := range urls { if candidate, ok := parseRepoCandidate(raw); ok { candidates = append(candidates, *candidate) } } if len(candidates) == 0 { return nil, fmt.Errorf("no Tangled remote candidate found among %d remote(s) %q or the current branch's remote settings; pass the repository as handle/repo", len(remotes), remotes) } return candidates, nil}
// branchRemoteURLs returns the current branch's remote settings that are URLs rather than remote names.func (c *Client) branchRemoteURLs(ctx context.Context, remotes []string) ([]string, error) { keys := []string{"remote.pushDefault"} branch, err := c.gitOptionalLines(ctx, "symbolic-ref", "--quiet", "--short", "HEAD") if err != nil { return nil, fmt.Errorf("read current branch: %w", err) } if len(branch) == 1 { keys = []string{"branch." + branch[0] + ".remote", "branch." + branch[0] + ".pushRemote", "remote.pushDefault"} } var urls []string for _, key := range keys { values, err := c.gitOptionalLines(ctx, "config", "--get-all", key) if err != nil { return nil, fmt.Errorf("read git config %s: %w", key, err) } for _, value := range values { if !slices.Contains(remotes, value) && !slices.Contains(urls, value) { urls = append(urls, value) } } } return urls, nil}
func DetectRepoCandidatesFromCWD(ctx context.Context) ([]RepoContext, error) { return defaultClient.DetectRepoCandidatesFromCWD(ctx)}
// originFirst returns remotes with the default remote first (if present),// followed by the rest in their original order.func originFirst(remotes []string) []string { idx := slices.Index(remotes, defaultRemote) if idx <= 0 { return remotes } ordered := make([]string, 0, len(remotes)) ordered = append(ordered, defaultRemote) ordered = append(ordered, remotes[:idx]...) ordered = append(ordered, remotes[idx+1:]...) return ordered}
// parseRepoCandidate parses a hosted Tangled URL or custom SSH/HTTPS URL into// an untrusted repository candidate. Custom hosts must be verified by the app// layer against the repository record.//// Tangled's hosted endpoint supports SCP-like, ssh://, git://, https://, and// http:// URLs. Custom Knot candidates may use SSH or HTTPS.func parseRepoCandidate(raw string) (*RepoContext, bool) { u, err := parseGitURL(strings.TrimSpace(raw)) if err != nil { return nil, false } hosted := strings.EqualFold(u.Hostname(), HostedGitHost) && (u.Scheme != "https" || u.Port() == "" || u.Port() == "443") if !hosted && u.Scheme != "ssh" && u.Scheme != "https" { return nil, false } if u.Hostname() == "" || u.Fragment != "" || u.RawQuery != "" || u.ForceQuery { return nil, false } if u.Scheme == "https" && u.User != nil { return nil, false } if u.Scheme == "https" && u.Port() != "" { port, err := strconv.Atoi(u.Port()) if err != nil || port < 1 || port > 65535 { return nil, false } } path := strings.Trim(strings.TrimPrefix(u.Path, "/"), "/") candidate, ok := splitRepoPath(path) if !ok { return nil, false } if !hosted { candidate.KnotHost = strings.ToLower(u.Hostname()) if u.Scheme == "https" && u.Port() != "" { candidate.KnotHost = strings.ToLower(u.Host) } } return candidate, true}
func splitRepoPath(path string) (*RepoContext, bool) { if strings.Contains(path, "/") { return splitHandleRepo(path) } if _, err := syntax.ParseDID(path); err != nil { return nil, false } return &RepoContext{RepoDID: path}, true}
// parseGitURL parses a git remote URL, including SCP-like syntax// (e.g. git@host:path), which net/url.Parse does not handle.func parseGitURL(raw string) (*url.URL, error) { if raw == "" { return nil, errors.New("empty URL") } // Schemed URLs (ssh://, git://, https://, etc.) are handled by stdlib. if strings.Contains(raw, "://") { return url.Parse(raw) } // SCP-like: [user@]host:path, where ':' comes before any '/'. colon := strings.Index(raw, ":") slash := strings.Index(raw, "/") if colon > 0 && (slash < 0 || colon < slash) { user, host, hasUser := strings.Cut(raw[:colon], "@") if !hasUser { host, user = user, "" } var builder strings.Builder builder.WriteString("ssh://") if user != "" { builder.WriteString(user) builder.WriteString("@") } builder.WriteString(host) builder.WriteString("/") builder.WriteString(raw[colon+1:]) return url.Parse(builder.String()) } // Local path or anything else — let stdlib produce the error. return url.Parse(raw)}
// splitHandleRepo splits "handle/repo" (optionally with a trailing .git).// Leading/trailing slashes are tolerated; returns ok=false for empty// segments or paths with extra segments.func splitHandleRepo(path string) (*RepoContext, bool) { path = strings.Trim(path, "/") parts := strings.SplitN(path, "/", 2) if len(parts) != 2 || parts[0] == "" || parts[1] == "" { return nil, false } if strings.Contains(parts[1], "/") { return nil, false } return &RepoContext{ Handle: parts[0], Repo: strings.TrimSuffix(parts[1], ".git"), }, true}
// gitOptionalLines is gitLines that treats exit status 1 (unset key, detached HEAD) as no lines.func (c *Client) gitOptionalLines(ctx context.Context, args ...string) ([]string, error) { lines, err := c.gitLines(ctx, args...) var exitErr *exec.ExitError if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 { return nil, nil } return lines, err}
// gitLines runs git with the given args and returns non-empty output lines.func (c *Client) gitLines(ctx context.Context, args ...string) ([]string, error) { // Output is intentionally captured; diagnostics still go to the client's sink. cmd := exec.CommandContext(ctx, "git", args...) var captured strings.Builder cmd.Stdout = &captured _, stderr := c.writers() cmd.Stderr = stderr err := cmd.Run() if err != nil { return nil, fmt.Errorf("git %s: %w", strings.Join(args, " "), err) } var lines []string for line := range strings.SplitSeq(captured.String(), "\n") { if line = strings.TrimSpace(line); line != "" { lines = append(lines, line) } } return lines, nil}