SpaceMIX v0.1.0

Git

experimentalfor bothsince protocol 1

host.git: read-only access to remote git repositories added by URL: log, show, diff, blame, files, trees, refs and grep over a slim local copy.

host.git reads remote git repositories that the user adds by URL. The SpaceMIX server keeps a slim local copy of each (bare and blobless: commits and trees up front, file contents fetched the first time something reads them) in its data folder. Nothing is ever written to a remote, and there is no working tree.

interface SpacemixHost {
  git?: GitApi; // present with `git`
}

interface GitApi {
  repos(): Promise<GitRepo[]>;
  add(url: string): Promise<GitRepo>;
  remove(repo: string): Promise<void>;
  fetch(repo: string): Promise<GitRepo>;
  subscribe(fn: () => void): () => void;
  log(repo: string, options?: { rev?: string; path?: string; query?: string; author?: string; limit?: number; skip?: number }): Promise<GitCommit[]>;
  show(repo: string, rev: string, options?: { path?: string }): Promise<GitCommitDetail>;
  diff(repo: string, options: { from: string; to?: string; path?: string }): Promise<GitDiff>;
  blame(repo: string, path: string, options?: { rev?: string; startLine?: number; endLine?: number }): Promise<GitBlameHunk[]>;
  file(repo: string, path: string, rev?: string): Promise<{ text: string | null; binary: boolean; tooLarge: boolean }>;
  tree(repo: string, options?: { path?: string; rev?: string }): Promise<GitTreeEntry[]>;
  refs(repo: string): Promise<GitRef[]>;
  grep(repo: string, query: string, options?: { rev?: string; path?: string; ignoreCase?: boolean; regex?: boolean; limit?: number }): Promise<GitGrepMatch[]>;
}

The result types (GitRepo, GitCommit, GitCommitDetail, GitDiff, GitFileChange, GitRef, GitBlameHunk, GitGrepMatch, GitTreeEntry) are in packages/sdk/src/index.ts with a comment on every field.

Permission: git. Without it host.git is undefined. The git permission is not limited by net: permissions: it reaches whatever git host the user adds.

Shared repos. Like the files folder, the repo list belongs to the server: every extension with git sees every repo anyone added.

Credentials. Git runs on the server's computer with the user's own git setup: credential helper and SSH keys. It never prompts. An SSH host that isn't trusted yet fails with a message saying to run ssh -T git@<host> once in a terminal.

Logged, and the network switch. Every connection the server makes for an extension (a clone, a fetch, a read that downloads file contents) is a row of the server's egress log, with method git and the remote's host. While the extension's network is switched off (Settings → Extensions → Data leaving, for everyone on the server):

  • add of a repo that isn't added yet rejects before connecting; adding one that is already there resolves with it, since that needs no connection.
  • fetch rejects before connecting.
  • Reads (log, show, diff, blame, file, tree, refs, grep) run with every git transport off: they serve only what is already in the local copy, and a read that would have to download file contents rejects.

Each of these rejects with <Name> isn't allowed to reach <host>: its network access is turned off on this server. and is logged as network-off. See What the server logs, and the network switch.

Arguments

  • repo is a GitRepo.id. An unknown id rejects with No repo with id "<id>". Use git_repos to find valid ids.
  • rev takes anything git rev-parse takes (HEAD~3, a branch, a tag, a hash) and defaults to HEAD, the remote's default branch. A value that is empty, starts with - or contains whitespace rejects with Invalid revision "<rev>". Pass a hash, branch, tag or an expression like HEAD~2.
  • path is relative to the repo's top level, /-separated; .. rejects.
  • Reads may download file contents on first use, so the first read of a file can be slow. A read rejects in the frame after 60 seconds (the server stops git itself after 2 minutes); fetch gets 10 minutes.
  • Dates are milliseconds since the epoch.

host.git.repos

repos(): Promise<GitRepo[]>;

Every added repo with its state: cloning until the first clone finishes, fetching during a fetch, ready, or error with error set to a readable sentence. Bridge method: git.repos.

host.git.add

add(url: string): Promise<GitRepo>;

Starts cloning and resolves at once with the repo in state cloning; follow it through repos() and subscribe. Accepts https://, http://, ssh://, git:// and scp-style git@host:owner/repo.git. A Studio preview, a member's build and anything on a hosted server reach only public addresses unless the owner exempted the host: git follows no redirects for them, http(s) connections are pinned to the checked addresses (git 2.37 or later), ssh is only checked before connecting, and git:// is refused. Local paths, file:// and <transport>:: forms reject with "<url>" isn't a remote git URL. Use https://host/owner/repo.git, ssh://git@host/owner/repo.git or git@host:owner/repo.git. Adding a URL that is already added (ignoring case, a trailing / and .git) resolves with the existing repo. Bridge method: git.add.

host.git.remove

remove(repo: string): Promise<void>;

Deletes the local copy and forgets the repo, for every extension. Bridge method: git.remove.

host.git.fetch

fetch(repo: string): Promise<GitRepo>;

Fetches new commits, branches and tags. Resolves when done with the updated repo. Frame timeout: 10 minutes. Bridge method: git.fetch.

host.git.subscribe

subscribe(fn: () => void): () => void;

Calls fn whenever the repo list or a repo's state changes, from any window or browser. Re-read with repos(). Channel: git.

host.git.log

log(repo: string, options?: { rev?: string; path?: string; query?: string; author?: string; limit?: number; skip?: number }): Promise<GitCommit[]>;

Commits, newest first, from rev. path keeps commits touching that file or folder (following renames for a single file); query matches the message and author the author's name or email, both case-insensitive. limit defaults to 20, at most 200; skip pages. refs lists the branch and tag names pointing at each commit. Bridge method: git.log.

host.git.show

show(repo: string, rev: string, options?: { path?: string }): Promise<GitCommitDetail>;

One commit with its body, changed files and patch (optionally only for path). The patch is cut at 200 KB and truncated says so. Bridge method: git.show.

host.git.diff

diff(repo: string, options: { from: string; to?: string; path?: string }): Promise<GitDiff>;

Changes from from to to (default HEAD). Same 200 KB patch limit. additions/deletions are null for binary files. Bridge method: git.diff.

host.git.blame

blame(repo: string, path: string, options?: { rev?: string; startLine?: number; endLine?: number }): Promise<GitBlameHunk[]>;

Runs of consecutive lines last changed by the same commit. Lines are 1-based; at most 5 000 lines at a time. A longer file without a range rejects with a message asking for startLine/endLine. Bridge method: git.blame.

host.git.file

file(repo: string, path: string, rev?: string): Promise<{ text: string | null; binary: boolean; tooLarge: boolean }>;

A file's contents at rev. Over 5 MB: tooLarge. Binary: binary, text: null. A folder rejects with "<path>" is a folder at "<rev>"; use git_tree. and a missing file with No file "<path>" at "<rev>". Bridge method: git.file.

host.git.tree

tree(repo: string, options?: { path?: string; rev?: string }): Promise<GitTreeEntry[]>;

What is directly inside path (default: the top level), folders first. kind is file, dir, submodule or symlink; size is set for files. Bridge method: git.tree.

host.git.refs

refs(repo: string): Promise<GitRef[]>;

Branches and tags, with the commit each points at; isDefault marks the remote's default branch. Bridge method: git.refs.

host.git.grep

grep(repo: string, query: string, options?: { rev?: string; path?: string; ignoreCase?: boolean; regex?: boolean; limit?: number }): Promise<GitGrepMatch[]>;

git grep over rev. query is a fixed string unless regex: true (extended regular expression); it can't span lines. Case-sensitive unless ignoreCase. Binary files are skipped. limit defaults to 50, at most 500. Bridge method: git.grep.

Example

import { host } from "@spacemix/sdk";

export async function recentSubjects(url: string): Promise<string[]> {
  const git = host.git;
  if (!git) return [];
  const repo = (await git.repos()).find((r) => r.url === url) ?? (await git.add(url));
  if (repo.state !== "ready") return [];
  return (await git.log(repo.id, { limit: 10 })).map((c) => `${c.short} ${c.subject}`);
}

Stability

Experimental: the API is complete but hasn't been exercised against many hosts yet. Error messages mention the assistant's tool names (git_repos, git_tree), which is a known wart.

This page as markdown · All pages as one file