OXYGENHUB

Coverage / src/api.ts 90.3%

102 of 113 lines run, 8 of 8 functions, 30 of 32 branches · run #26 on main · run missed

10×
// The API this extension exports to others (spec §9, "extension API"): the
20×
// same idea as the built-in Git extension's `getAPI()`. A forge extension
30×
// such as oxygenhub-vscode consumes it instead of running oxy itself, and
40×
// registers a remote provider so this extension can offer "Open on …"
50×
// links without knowing any hub.
60×
//
70×
// Pure functions live here so they can be tested with plain Node; the
80×
// vscode-facing object is assembled in extension.ts.
91×
100×
/** What a remote-aware extension registers. */
11
export interface RemoteProvider {
12
  /** A stable id, e.g. `oxygenhub`. */
13
  readonly id: string;
14
  /** Shown in menus: "Open on OxygenHub". */
15
  readonly label: string;
16
  /** Whether this provider understands a remote URL. */
17
  matches(remoteUrl: string): boolean;
18
  /** A web URL for a thing in that remote, or undefined when the provider has none. */
19
  urlFor(remoteUrl: string, target: RemoteTarget): string | undefined;
20
}
211×
22
export type RemoteTarget =
23
  | { kind: "repository" }
24
  | { kind: "bookmark"; name: string }
25
  | { kind: "change"; hash: string }
26
  | { kind: "file"; reference: string; path: string; line?: number; endLine?: number }
27
  | { kind: "proposals" }
28
  | { kind: "issues" };
291×
300×
/** A minimal view of one repository, stable across versions. */
31
export interface RepositoryHandle {
32
  readonly root: string;
33
  /** `oxy status --json` as last refreshed, or undefined before the first refresh. */
34
  readonly status: unknown;
35
  readonly bookmarks: { name: string; heads: string[]; current?: boolean }[];
36
  readonly currentBookmark: string | undefined;
37
  /** The repository's remotes, with the CA file each names for a self-signed hub. */
38
  remotes(): Promise<{ name: string; url: string; ca?: string }[]>;
39
  /** Run an oxy command in this repository and parse its JSON output. */
40
  json<T>(args: string[]): Promise<T>;
41
  refresh(): Promise<void>;
42
}
431×
44
export interface OxygenAPI {
45
  readonly version: 1;
46
  readonly repositories: readonly RepositoryHandle[];
47
  /** Fires with the repository whose status changed. */
48
  readonly onDidChangeStatus: (listener: (repo: RepositoryHandle) => void) => { dispose(): void };
49
  /** Fires when repositories are discovered or removed. */
50
  readonly onDidChangeRepositories: (listener: () => void) => { dispose(): void };
51
  getRepository(fsPath: string): RepositoryHandle | undefined;
52
  registerRemoteProvider(provider: RemoteProvider): { dispose(): void };
53
  /** The providers registered so far, for callers that build their own menus. */
54
  remoteProviders(): readonly RemoteProvider[];
55
}
561×
570×
/** The provider (and remote) to use for a repository's remotes, if any. */
581×
export function pickProvider(providers: readonly RemoteProvider[], remotes: { name: string; url: string }[]): { provider: RemoteProvider; remote: { name: string; url: string } } | undefined {
591×
  // Prefer a remote called origin or earth, then any.
601×
  const ordered = [...remotes].sort((a, b) => rank(a.name) - rank(b.name));
611×
  for (const remote of ordered) {
621×
    for (const provider of providers) {
631×
      if (provider.matches(remote.url)) {
641×
        return { provider, remote };
651×
      }
661×
    }
671×
  }
681×
  return undefined;
692×
}
702×
712×
function rank(name: string): number {
722×
  return name === "origin" ? 0 : name === "hub" ? 1 : 2;
733×
}
743×
753×
/** The repository handle a folder path belongs to: the longest root that prefixes it. */
763×
export function repositoryFor(roots: string[], fsPath: string): string | undefined {
773×
  let best: string | undefined;
783×
  for (const r of roots) {
799×
    if ((fsPath === r || fsPath.startsWith(r.endsWith("/") ? r : r + "/")) && (!best || r.length > best.length)) {
803×
      best = r;
813×
    }
829×
  }
833×
  return best;
843×
}
851×
861×
/**
871×
 * A state address for permalinks, as OxygenHub writes it: the state's head
881×
 * hashes, sorted and joined with `+`. Undefined without heads.
891×
 */
901×
export function stateSpec(heads: readonly string[]): string | undefined {
915×
  const hex = [...new Set(heads.map((h) => h.toLowerCase()))].filter((h) => /^[0-9a-f]{64}$/.test(h)).sort();
925×
  return hex.length > 0 && hex.length === new Set(heads).size ? hex.join("+") : undefined;
937×
}
947×
957×
/**
967×
 * The 1-based lines to link: the selection when the clicked line is inside it
977×
 * (or nothing was clicked), otherwise the clicked line alone. `selection` is
987×
 * 1-based and inclusive; a selection ending at column 0 of a line does not
997×
 * include that line.
1007×
 */
1017×
export function linkedLines(clicked: number | undefined, selection: { start: number; end: number; endColumn: number; empty: boolean }): { line: number; endLine?: number } {
1027×
  let end = selection.end;
1037×
  if (!selection.empty && selection.endColumn === 0 && end > selection.start) {
1041×
    end -= 1;
1051×
  }
1067×
  if (clicked !== undefined && (selection.empty || clicked < selection.start || clicked > end)) {
1072×
    return { line: clicked };
1082×
  }
1097×
  if (selection.empty || end === selection.start) {
1102×
    return { line: selection.start };
1112×
  }
1127×
  return { line: selection.start, endLine: end };
1137×
}