| bed495a9 Andy McCall ↶ |
1 |
// The oxy process wrapper: one function per command the extension uses, |
| 2 |
// all through `oxy --plain --json` (spec §4). This module never imports |
| 3 |
// the vscode API so it can be tested with plain Node. |
| 4 |
|
| 5 |
import { execFile } from "node:child_process"; |
| 44bfd5ad Andy McCall ↶ |
6 |
import { SigningInfo, parseIdentityShow } from "./commitbox"; |
| bed495a9 Andy McCall ↶ |
7 |
import { existsSync } from "node:fs"; |
| 8 |
import { homedir } from "node:os"; |
| 9 |
import { join } from "node:path"; |
| 10 |
|
| 11 |
export interface RunResult { |
| 12 |
code: number; |
| 13 |
stdout: Buffer; |
| 14 |
stderr: string; |
| 15 |
} |
| 16 |
|
| 17 |
export class OxyError extends Error { |
| 18 |
constructor( |
| 19 |
public readonly args: string[], |
| 20 |
public readonly code: number, |
| 21 |
public readonly stderr: string, |
| 22 |
) { |
| 23 |
super(stderr.trim() || `oxy ${args.join(" ")} exited with status ${code}`); |
| 24 |
this.name = "OxyError"; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/** Where to find `oxy`: the configured path, else `oxy` on PATH, else ~/.local/bin/oxy. */ |
| 29 |
export function findBinary(configured: string | undefined): string { |
| 30 |
if (configured && configured.trim()) { |
| 31 |
return configured.trim(); |
| 32 |
} |
| 33 |
const local = join(homedir(), ".local", "bin", "oxy"); |
| 34 |
return existsSync(local) ? local : "oxy"; |
| 35 |
} |
| 36 |
|
| 37 |
export type Logger = (line: string) => void; |
| 38 |
|
| 39 |
export class Oxy { |
| 40 |
constructor( |
| 41 |
public readonly binary: string, |
| 42 |
private readonly logger: Logger = () => {}, |
| 43 |
) {} |
| 44 |
|
| 45 |
/** Run oxy in `cwd`; resolves with the exit status, never rejects on a non-zero status. */ |
| 46 |
run(cwd: string, args: string[]): Promise<RunResult> { |
| 47 |
const started = Date.now(); |
| 48 |
const full = ["--plain", ...args]; |
| 49 |
return new Promise((resolve, reject) => { |
| 50 |
execFile( |
| 51 |
this.binary, |
| 52 |
full, |
| 53 |
{ cwd, maxBuffer: 256 * 1024 * 1024, encoding: "buffer", env: { ...process.env, NO_COLOR: "1" } }, |
| 54 |
(error, stdout, stderr) => { |
| 55 |
const err = stderr.toString("utf8"); |
| 56 |
const code = error && typeof (error as NodeJS.ErrnoException).code === "number" ? ((error as NodeJS.ErrnoException).code as unknown as number) : error ? -1 : 0; |
| 57 |
this.logger(`oxy ${args.join(" ")} (${cwd}) → ${code} in ${Date.now() - started} ms${err.trim() ? `: ${err.trim()}` : ""}`); |
| 58 |
if (error && (error as NodeJS.ErrnoException).code === "ENOENT") { |
| 59 |
reject(new OxyError(full, -1, `oxy not found at ${this.binary}`)); |
| 60 |
return; |
| 61 |
} |
| 62 |
resolve({ code, stdout: stdout as Buffer, stderr: err }); |
| 63 |
}, |
| 64 |
); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
/** Run and require success (exit 0, or one of `okCodes`). */ |
| 69 |
async check(cwd: string, args: string[], okCodes: number[] = [0]): Promise<RunResult> { |
| 70 |
const r = await this.run(cwd, args); |
| 71 |
if (!okCodes.includes(r.code)) { |
| 72 |
throw new OxyError(args, r.code, r.stderr); |
| 73 |
} |
| 74 |
return r; |
| 75 |
} |
| 76 |
|
| 77 |
async json<T>(cwd: string, args: string[], okCodes: number[] = [0]): Promise<T> { |
| 78 |
const r = await this.check(cwd, [...args, "--json"], okCodes); |
| 79 |
const text = r.stdout.toString("utf8").trim(); |
| 80 |
try { |
| 81 |
return JSON.parse(text) as T; |
| 82 |
} catch (e) { |
| 83 |
throw new OxyError(args, r.code, `oxy ${args.join(" ")} did not print JSON: ${text.slice(0, 200)}`); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// ---------------------------------------------------------------- commands |
| 88 |
|
| 89 |
async version(): Promise<VersionInfo> { |
| 90 |
return this.json<VersionInfo>(process.cwd(), ["version"]); |
| 91 |
} |
| 92 |
|
| 93 |
/** The repository containing `cwd`, or null when there is none. */ |
| 94 |
async root(cwd: string): Promise<RootInfo | null> { |
| 95 |
const r = await this.run(cwd, ["root", "--json"]); |
| 96 |
if (r.code !== 0) { |
| 97 |
return null; |
| 98 |
} |
| 99 |
const v = JSON.parse(r.stdout.toString("utf8")); |
| 100 |
return v === null ? null : (v as RootInfo); |
| 101 |
} |
| 102 |
|
| 103 |
status(root: string): Promise<StatusInfo> { |
| 104 |
return this.json<StatusInfo>(root, ["status"], [0, 2]); |
| 105 |
} |
| 106 |
|
| 107 |
async prompt(root: string, format?: string): Promise<string> { |
| 108 |
const args = ["prompt", "--glyphs"]; |
| 109 |
if (format) { |
| 110 |
args.push("--format", format); |
| 111 |
} |
| 112 |
const r = await this.run(root, args); |
| 113 |
return r.code === 0 ? r.stdout.toString("utf8").trim() : ""; |
| 114 |
} |
| 115 |
|
| 116 |
/** File content at a state; `change` may be a hash prefix, id prefix, bookmark, or tag. */ |
| 117 |
async cat(root: string, path: string, change?: string): Promise<Buffer> { |
| 118 |
const args = ["cat", path]; |
| 119 |
if (change) { |
| 120 |
args.push("-r", change); |
| 121 |
} |
| 122 |
return (await this.check(root, args)).stdout; |
| 123 |
} |
| 124 |
|
| 125 |
log(root: string, limit?: number, path?: string): Promise<LogInfo> { |
| 126 |
const args = ["log"]; |
| 127 |
if (limit !== undefined) { |
| 128 |
args.push("-n", String(limit)); |
| 129 |
} |
| 130 |
if (path) { |
| 131 |
args.push("--path", path); |
| 132 |
} |
| 133 |
return this.json<LogInfo>(root, args); |
| 134 |
} |
| 135 |
|
| 136 |
commit(root: string, message: string, paths: string[] = []): Promise<void> { |
| 137 |
return this.check(root, ["commit", "-m", message, ...paths]).then(() => undefined); |
| 138 |
} |
| 139 |
|
| 140 |
bookmarks(root: string): Promise<BookmarkInfo[]> { |
| 141 |
return this.json<BookmarkInfo[]>(root, ["bookmark", "list"]); |
| 142 |
} |
| 35d56b58 Andy McCall ↶ |
143 |
|
| 144 |
/** Restore paths to the working copy's base. */ |
| 145 |
discard(root: string, paths: string[]): Promise<void> { |
| 146 |
return this.check(root, ["discard", ...paths]).then(() => undefined); |
| 147 |
} |
| 148 |
|
| 149 |
discardHunks(root: string, hunks: number[]): Promise<void> { |
| 150 |
return this.check(root, ["discard", "--hunks", hunks.join(",")]).then(() => undefined); |
| 151 |
} |
| 152 |
|
| 153 |
hunks(root: string): Promise<HunkInfo[]> { |
| 154 |
return this.json<HunkInfo[]>(root, ["hunks"]); |
| 155 |
} |
| 156 |
|
| 157 |
commitHunks(root: string, message: string, hunks: number[]): Promise<void> { |
| 158 |
return this.check(root, ["commit", "-m", message, "--hunks", hunks.join(",")]).then(() => undefined); |
| 159 |
} |
| 160 |
|
| 161 |
amend(root: string, message?: string): Promise<void> { |
| 162 |
const args = ["amend"]; |
| 163 |
if (message) { |
| 164 |
args.push("-m", message); |
| 165 |
} |
| 166 |
return this.check(root, args).then(() => undefined); |
| 167 |
} |
| 168 |
|
| 169 |
undo(root: string): Promise<string> { |
| 170 |
return this.check(root, ["undo"]).then((r) => r.stdout.toString("utf8").trim()); |
| 171 |
} |
| 172 |
|
| 173 |
opLog(root: string): Promise<OperationInfo[]> { |
| 174 |
return this.json<OperationInfo[]>(root, ["op", "log"]); |
| 175 |
} |
| c8f95589 Andy McCall ↶ |
176 |
|
| 177 |
// ---------------------------------------------------------------- bookmarks |
| 178 |
|
| 179 |
bookmarkCreate(root: string, name: string, changes: string[] = []): Promise<void> { |
| 180 |
return this.check(root, ["bookmark", "create", name, ...changes]).then(() => undefined); |
| 181 |
} |
| 182 |
|
| 183 |
bookmarkSwitch(root: string, name: string, discard = false): Promise<void> { |
| 184 |
const args = ["bookmark", "switch", name]; |
| 185 |
if (discard) { |
| 186 |
args.push("--discard"); |
| 187 |
} |
| 188 |
return this.check(root, args).then(() => undefined); |
| 189 |
} |
| 190 |
|
| 191 |
bookmarkDelete(root: string, name: string): Promise<void> { |
| 192 |
return this.check(root, ["bookmark", "delete", name]).then(() => undefined); |
| 193 |
} |
| 194 |
|
| 195 |
merge(root: string, name: string): Promise<string> { |
| 196 |
return this.check(root, ["merge", name]).then((r) => r.stdout.toString("utf8").trim()); |
| 197 |
} |
| 198 |
|
| 199 |
// ---------------------------------------------------------------- remotes and sync |
| 200 |
|
| 201 |
remotes(root: string): Promise<RemoteInfo[]> { |
| 202 |
return this.json<RemoteInfo[]>(root, ["remote", "list"]); |
| 203 |
} |
| 204 |
|
| 205 |
fetch(root: string, remote?: string): Promise<FetchInfo> { |
| 206 |
return this.json<FetchInfo>(root, ["fetch", ...(remote ? [remote] : [])]); |
| 207 |
} |
| 208 |
|
| 209 |
pull(root: string, remote?: string): Promise<PullInfo> { |
| 210 |
return this.json<PullInfo>(root, ["pull", ...(remote ? [remote] : [])]); |
| 211 |
} |
| 212 |
|
| 213 |
push(root: string, remote?: string, bookmark?: string, force = false): Promise<PushResult[]> { |
| 214 |
const args = ["push", ...(remote ? [remote] : [])]; |
| 215 |
if (bookmark) { |
| 216 |
args.push("-b", bookmark); |
| 217 |
} |
| 218 |
if (force) { |
| 219 |
args.push("--force"); |
| 220 |
} |
| 221 |
return this.json<PushResult[]>(root, args, [0, 1]); |
| 222 |
} |
| 223 |
|
| 224 |
clone(cwd: string, url: string, dest: string): Promise<void> { |
| 225 |
return this.check(cwd, ["clone", url, dest]).then(() => undefined); |
| 226 |
} |
| ab81294a Andy McCall ↶ |
227 |
|
| 228 |
// ---------------------------------------------------------------- history and blame |
| 229 |
|
| 230 |
blame(root: string, path: string): Promise<BlameLine[]> { |
| 231 |
return this.json<BlameLine[]>(root, ["blame", path]); |
| 232 |
} |
| 233 |
|
| 234 |
show(root: string, change: string): Promise<ShowInfo> { |
| 235 |
return this.json<ShowInfo>(root, ["show", change]); |
| 236 |
} |
| 237 |
|
| 238 |
/** The rendered diff of a change (or the working copy), as `oxy diff` prints it. */ |
| 239 |
diffText(root: string, change?: string): Promise<string> { |
| 240 |
return this.check(root, ["diff", ...(change ? [change] : [])]).then((r) => r.stdout.toString("utf8")); |
| 241 |
} |
| 242 |
|
| 243 |
describe(root: string, change: string, message: string): Promise<void> { |
| 244 |
return this.check(root, ["describe", change, "-m", message]).then(() => undefined); |
| 245 |
} |
| 246 |
|
| 247 |
abandon(root: string, change: string): Promise<string> { |
| 248 |
return this.check(root, ["abandon", change]).then((r) => r.stdout.toString("utf8").trim()); |
| 44bfd5ad Andy McCall ↶ |
249 |
} |
| 250 |
|
| 251 |
/** The configured identity and how commits are signed; `null` without one. */ |
| 252 |
identity(root: string): Promise<SigningInfo | null> { |
| 253 |
return this.check(root, ["identity", "show"]).then((r) => parseIdentityShow(r.stdout.toString("utf8"))); |
| ab81294a Andy McCall ↶ |
254 |
} |
| 255 |
|
| b4626290 Andy McCall ↶ |
256 |
/** Split a head: the edits to `paths` become a separate first change. Returns oxy's summary line. */ |
| 257 |
split(root: string, change: string, paths: string[]): Promise<string> { |
| 258 |
return this.check(root, ["split", change, ...paths]).then((r) => r.stdout.toString("utf8").trim()); |
| 259 |
} |
| 260 |
|
| 261 |
/** Fold `change` into its dependency `into`, which keeps its ChangeId. */ |
| 262 |
squash(root: string, change: string, into: string): Promise<string> { |
| 263 |
return this.check(root, ["squash", change, "--into", into]).then((r) => r.stdout.toString("utf8").trim()); |
| 264 |
} |
| 265 |
|
| 266 |
/** Re-express `change` against the state of `onto` (changes, a bookmark or a tag). */ |
| 267 |
rebase(root: string, change: string, onto: string[]): Promise<string> { |
| 268 |
return this.check(root, ["rebase", change, "--onto", ...onto]).then((r) => r.stdout.toString("utf8").trim()); |
| 269 |
} |
| 270 |
|
| ab81294a Andy McCall ↶ |
271 |
checkout(root: string, changes: string[], discard = false): Promise<void> { |
| 272 |
return this.check(root, ["checkout", ...changes, ...(discard ? ["--discard"] : [])]).then(() => undefined); |
| 273 |
} |
| 274 |
|
| 275 |
tagCreate(root: string, name: string, changes: string[] = [], message = ""): Promise<void> { |
| 276 |
return this.check(root, ["tag", "create", name, ...changes, "-m", message]).then(() => undefined); |
| 277 |
} |
| d2c9a584 Andy McCall ↶ |
278 |
|
| 279 |
// ---------------------------------------------------------------- subrepos, bisect, trust, tags, workspaces |
| 280 |
|
| 281 |
submodules(root: string): Promise<SubrepoInfo[]> { |
| 282 |
return this.json<SubrepoInfo[]>(root, ["submodule", "status"]); |
| 283 |
} |
| 284 |
|
| 285 |
submoduleUpdate(root: string, init: boolean, paths: string[] = []): Promise<string> { |
| 286 |
return this.check(root, ["submodule", "update", ...(init ? ["--init"] : []), ...paths], [0, 1]).then((r) => r.stdout.toString("utf8")); |
| 287 |
} |
| 288 |
|
| 289 |
bisectStatus(root: string): Promise<BisectStatus | null> { |
| 290 |
return this.json<BisectStatus | null>(root, ["bisect", "status"]); |
| 291 |
} |
| 292 |
|
| 293 |
bisectStart(root: string): Promise<void> { |
| 294 |
return this.check(root, ["bisect", "start"]).then(() => undefined); |
| 295 |
} |
| 296 |
|
| 297 |
bisectMark(root: string, verdict: "good" | "bad" | "skip", changes: string[] = []): Promise<string> { |
| 298 |
return this.check(root, ["bisect", verdict, ...changes]).then((r) => r.stdout.toString("utf8").trim()); |
| 299 |
} |
| 300 |
|
| 301 |
bisectRun(root: string, command: string[]): Promise<string> { |
| 302 |
return this.check(root, ["bisect", "run", ...command], [0, 1]).then((r) => r.stdout.toString("utf8").trim()); |
| 303 |
} |
| 304 |
|
| 305 |
bisectReset(root: string): Promise<void> { |
| 306 |
return this.check(root, ["bisect", "reset"]).then(() => undefined); |
| 307 |
} |
| 308 |
|
| 309 |
policyShow(root: string): Promise<PolicyInfo | null> { |
| 310 |
return this.json<PolicyInfo | null>(root, ["policy", "show"]); |
| 311 |
} |
| 312 |
|
| 313 |
verify(root: string, change?: string): Promise<VerifyInfo[]> { |
| 314 |
return this.json<VerifyInfo[]>(root, ["verify", ...(change ? [change] : [])], [0, 1]); |
| 315 |
} |
| 316 |
|
| 317 |
tags(root: string): Promise<TagInfo[]> { |
| 318 |
return this.json<TagInfo[]>(root, ["tag", "list"]); |
| 319 |
} |
| 320 |
|
| 321 |
workspaces(root: string): Promise<WorkspaceInfo[]> { |
| 322 |
return this.json<WorkspaceInfo[]>(root, ["workspace", "list"]); |
| 323 |
} |
| 324 |
|
| 325 |
workspaceAdd(root: string, path: string, changes: string[] = []): Promise<void> { |
| 326 |
return this.check(root, ["workspace", "add", path, ...changes]).then(() => undefined); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
export interface SubrepoInfo { |
| 331 |
path: string; |
| 332 |
pin: PinInfo; |
| 333 |
actual: PinInfo | null; |
| 334 |
at_pin: boolean; |
| 335 |
dirty: boolean; |
| 336 |
url: string | null; |
| 337 |
} |
| 338 |
|
| 339 |
export type PinInfo = { kind: "oxy"; repo: string; heads: string[]; bookmark: string } | { kind: "git"; sha: string; branch: string }; |
| 340 |
|
| 341 |
export type BisectStatus = |
| 342 |
| { state: "need-marks"; good: number; bad: number } |
| 343 |
| { state: "testing"; heads: string[]; remaining: number; steps_left: number } |
| 344 |
| { state: "found"; culprit: string } |
| 345 |
| { state: "inconclusive"; candidates: string[] }; |
| 346 |
|
| 347 |
export interface PolicyInfo { |
| 348 |
hash: string; |
| 349 |
serial: number; |
| 350 |
expires_ms: number; |
| 351 |
expired: boolean; |
| 352 |
root_threshold: number; |
| 353 |
roots: { key: string; name: string; email: string }[]; |
| 354 |
rules: { pattern: string; keys: { key: string; name: string; email: string }[]; threshold: number; require: string; allow_conflicts: boolean }[]; |
| 355 |
revoked: { key: string; name: string; email: string }[]; |
| 356 |
} |
| 357 |
|
| 358 |
export interface VerifyInfo { |
| 359 |
hash: string; |
| 360 |
change_id: string; |
| 361 |
status: string; |
| 362 |
key: string | null; |
| 363 |
name: string | null; |
| 364 |
revoked: boolean; |
| 365 |
reason: string | null; |
| 366 |
} |
| 367 |
|
| 368 |
export interface TagInfo { |
| 369 |
name: string; |
| 370 |
state: string; |
| 371 |
heads: string[]; |
| 372 |
message: string; |
| 373 |
signer: string | null; |
| 374 |
} |
| 375 |
|
| 376 |
export interface WorkspaceInfo { |
| 377 |
id: string; |
| 378 |
path: string; |
| 379 |
current: boolean; |
| 380 |
working_copy: string | null; |
| ab81294a Andy McCall ↶ |
381 |
} |
| 382 |
|
| 383 |
export interface BlameLine { |
| 384 |
line: number; |
| 385 |
change_id: string; |
| 386 |
hash: string; |
| 387 |
index: number; |
| 388 |
author: string; |
| 389 |
when_ms: number; |
| 390 |
} |
| 391 |
|
| 392 |
export interface ShowInfo extends LogEntry { |
| 393 |
files: { path: string; kind: string; from: string | null }[]; |
| c8f95589 Andy McCall ↶ |
394 |
} |
| 395 |
|
| 396 |
export interface RemoteInfo { |
| 397 |
name: string; |
| 398 |
url: string; |
| 0c26846a Andy McCall ↶ |
399 |
/** The CA file `remote.NAME.ca` names (a self-signed hub), if any. */ |
| 400 |
ca?: string | null; |
| c8f95589 Andy McCall ↶ |
401 |
bookmarks: { name: string; heads: string[] }[]; |
| 402 |
} |
| 403 |
|
| 404 |
export interface FetchInfo { |
| 405 |
remote: string; |
| 406 |
refs: { name: string; heads: string[]; new: number }[]; |
| 407 |
new_tags: number; |
| 408 |
unverified: boolean; |
| 409 |
} |
| 410 |
|
| 411 |
export interface PullInfo { |
| 412 |
remote: string; |
| 413 |
heads: string[]; |
| 414 |
changed: boolean; |
| 415 |
checked_out: boolean; |
| 416 |
conflicted: boolean; |
| 417 |
} |
| 418 |
|
| 419 |
export interface PushResult { |
| 420 |
name: string; |
| 421 |
ok: boolean; |
| 422 |
reason: string; |
| 35d56b58 Andy McCall ↶ |
423 |
} |
| 424 |
|
| 425 |
export interface HunkInfo { |
| 426 |
index: number; |
| 427 |
path: string; |
| 428 |
text: string; |
| 429 |
} |
| 430 |
|
| 431 |
export interface OperationInfo { |
| 432 |
id: string; |
| 433 |
when_ms: number; |
| 434 |
command: string; |
| 435 |
snapshot: boolean; |
| 436 |
current: boolean; |
| bed495a9 Andy McCall ↶ |
437 |
} |
| 438 |
|
| 439 |
// ---------------------------------------------------------------- types |
| 440 |
|
| 441 |
export interface VersionInfo { |
| 442 |
version: string; |
| 443 |
protocol: number; |
| 444 |
format: number; |
| 445 |
} |
| 446 |
|
| 447 |
export interface RootInfo { |
| 448 |
root: string; |
| 449 |
workspace: string; |
| 450 |
bookmark: string; |
| 451 |
} |
| 452 |
|
| 453 |
export type PathStatus = "Added" | "Modified" | "Removed" | "Renamed" | "ModeChanged" | "ConflictEdited" | "Resolved"; |
| 454 |
|
| 455 |
export interface StatusInfo { |
| 456 |
bookmark: string; |
| 457 |
working_copy: string; |
| 458 |
heads: number; |
| 459 |
clean: boolean; |
| 460 |
conflicted: boolean; |
| 461 |
paths: { path: string; status: PathStatus }[]; |
| 462 |
conflicts: ConflictInfo[]; |
| 463 |
} |
| 464 |
|
| 465 |
export interface ConflictInfo { |
| 466 |
kind: string; |
| 467 |
path?: string; |
| 468 |
node?: string; |
| 469 |
nodes?: string[]; |
| 470 |
changes?: string[]; |
| 471 |
resolved?: boolean | "markers"; |
| 472 |
} |
| 473 |
|
| 474 |
export interface LogEntry { |
| 475 |
hash: string; |
| 476 |
change_id: string; |
| 477 |
author: string; |
| 478 |
author_key: string; |
| 479 |
when_ms: number; |
| 480 |
message: string; |
| 481 |
kind: string; |
| 482 |
deps: string[]; |
| 483 |
head: boolean; |
| 484 |
working_copy: boolean; |
| 485 |
signature: { status: string; key?: string; name?: string; revoked?: boolean; reason?: string } | null; |
| 486 |
} |
| 487 |
|
| 488 |
export interface LogInfo { |
| 489 |
bookmark: string; |
| 490 |
total: number; |
| 491 |
changes: LogEntry[]; |
| 492 |
} |
| 493 |
|
| 494 |
export interface BookmarkInfo { |
| 495 |
name: string; |
| 496 |
heads: string[]; |
| 497 |
current: boolean; |
| 498 |
ahead: number | null; |
| 499 |
behind: number | null; |
| 500 |
} |