Coverage / src/oxy.ts 74.4%
444 of 597 lines run, 79 of 89 functions, 112 of 128 branches · run #26 on main · run missed
| 1 | 0× | // The oxy process wrapper: one function per command the extension uses, |
| 2 | 0× | // all through `oxy --plain --json` (spec §4). This module never imports |
| 3 | 0× | // the vscode API so it can be tested with plain Node. |
| 4 | 1× | |
| 5 | 1× | import { execFile } from "node:child_process"; |
| 6 | 1× | import { SigningInfo, parseIdentityShow } from "./commitbox"; |
| 7 | 1× | import { existsSync } from "node:fs"; |
| 8 | 1× | import { homedir } from "node:os"; |
| 9 | 1× | import { join } from "node:path"; |
| 10 | 1× | |
| 11 | 1× | export interface RunResult { |
| 12 | 1× | code: number; |
| 13 | 1× | stdout: Buffer; |
| 14 | 1× | stderr: string; |
| 15 | 1× | } |
| 16 | 1× | |
| 17 | 1× | export class OxyError extends Error { |
| 18 | 1× | constructor( |
| 19 | 6× | public readonly args: string[], |
| 20 | 6× | public readonly code: number, |
| 21 | 6× | public readonly stderr: string, |
| 22 | 6× | ) { |
| 23 | 6× | super(stderr.trim() || `oxy ${args.join(" ")} exited with status ${code}`); |
| 24 | 6× | this.name = "OxyError"; |
| 25 | 6× | } |
| 26 | 1× | } |
| 27 | 1× | |
| 28 | 1× | /** Where to find `oxy`: the configured path, else `oxy` on PATH, else ~/.local/bin/oxy. */ |
| 29 | 1× | export function findBinary(configured: string | undefined): string { |
| 30 | 0× | if (configured && configured.trim()) { |
| 31 | 0× | return configured.trim(); |
| 32 | 0× | } |
| 33 | 0× | const local = join(homedir(), ".local", "bin", "oxy"); |
| 34 | 0× | return existsSync(local) ? local : "oxy"; |
| 35 | 0× | } |
| 36 | 1× | |
| 37 | 1× | export type Logger = (line: string) => void; |
| 38 | 1× | |
| 39 | 1× | export class Oxy { |
| 40 | 1× | constructor( |
| 41 | 1× | public readonly binary: string, |
| 42 | 1× | private readonly logger: Logger = () => {}, |
| 43 | 1× | ) {} |
| 44 | 1× | |
| 45 | 1× | /** Run oxy in `cwd`; resolves with the exit status, never rejects on a non-zero status. */ |
| 46 | 1× | run(cwd: string, args: string[]): Promise<RunResult> { |
| 47 | 133× | const started = Date.now(); |
| 48 | 133× | const full = ["--plain", ...args]; |
| 49 | 133× | return new Promise((resolve, reject) => { |
| 50 | 133× | execFile( |
| 51 | 133× | this.binary, |
| 52 | 133× | full, |
| 53 | 133× | { cwd, maxBuffer: 256 * 1024 * 1024, encoding: "buffer", env: { ...process.env, NO_COLOR: "1" } }, |
| 54 | 133× | (error, stdout, stderr) => { |
| 55 | 133× | const err = stderr.toString("utf8"); |
| 56 | 133× | const code = error && typeof (error as NodeJS.ErrnoException).code === "number" ? ((error as NodeJS.ErrnoException).code as unknown as number) : error ? -1 : 0; |
| 57 | 133× | this.logger(`oxy ${args.join(" ")} (${cwd}) → ${code} in ${Date.now() - started} ms${err.trim() ? `: ${err.trim()}` : ""}`); |
| 58 | 133× | if (error && (error as NodeJS.ErrnoException).code === "ENOENT") { |
| 59 | 0× | reject(new OxyError(full, -1, `oxy not found at ${this.binary}`)); |
| 60 | 0× | return; |
| 61 | 0× | } |
| 62 | 133× | resolve({ code, stdout: stdout as Buffer, stderr: err }); |
| 63 | 133× | }, |
| 64 | 133× | ); |
| 65 | 133× | }); |
| 66 | 133× | } |
| 67 | 1× | |
| 68 | 1× | /** Run and require success (exit 0, or one of `okCodes`). */ |
| 69 | 1× | async check(cwd: string, args: string[], okCodes: number[] = [0]): Promise<RunResult> { |
| 70 | 130× | const r = await this.run(cwd, args); |
| 71 | 130× | if (!okCodes.includes(r.code)) { |
| 72 | 6× | throw new OxyError(args, r.code, r.stderr); |
| 73 | 6× | } |
| 74 | 130× | return r; |
| 75 | 130× | } |
| 76 | 1× | |
| 77 | 1× | async json<T>(cwd: string, args: string[], okCodes: number[] = [0]): Promise<T> { |
| 78 | 51× | const r = await this.check(cwd, [...args, "--json"], okCodes); |
| 79 | 49× | const text = r.stdout.toString("utf8").trim(); |
| 80 | 49× | try { |
| 81 | 49× | return JSON.parse(text) as T; |
| 82 | 51× | } catch (e) { |
| 83 | 0× | throw new OxyError(args, r.code, `oxy ${args.join(" ")} did not print JSON: ${text.slice(0, 200)}`); |
| 84 | 0× | } |
| 85 | 51× | } |
| 86 | 1× | |
| 87 | 1× | // ---------------------------------------------------------------- commands |
| 88 | 1× | |
| 89 | 1× | async version(): Promise<VersionInfo> { |
| 90 | 2× | return this.json<VersionInfo>(process.cwd(), ["version"]); |
| 91 | 2× | } |
| 92 | 1× | |
| 93 | 1× | /** The repository containing `cwd`, or null when there is none. */ |
| 94 | 1× | async root(cwd: string): Promise<RootInfo | null> { |
| 95 | 2× | const r = await this.run(cwd, ["root", "--json"]); |
| 96 | 2× | if (r.code !== 0) { |
| 97 | 1× | return null; |
| 98 | 1× | } |
| 99 | 1× | const v = JSON.parse(r.stdout.toString("utf8")); |
| 100 | 2× | return v === null ? null : (v as RootInfo); |
| 101 | 2× | } |
| 102 | 1× | |
| 103 | 1× | status(root: string): Promise<StatusInfo> { |
| 104 | 10× | return this.json<StatusInfo>(root, ["status"], [0, 2]); |
| 105 | 10× | } |
| 106 | 1× | |
| 107 | 1× | async prompt(root: string, format?: string): Promise<string> { |
| 108 | 1× | const args = ["prompt", "--glyphs"]; |
| 109 | 1× | if (format) { |
| 110 | 0× | args.push("--format", format); |
| 111 | 0× | } |
| 112 | 1× | const r = await this.run(root, args); |
| 113 | 1× | return r.code === 0 ? r.stdout.toString("utf8").trim() : ""; |
| 114 | 1× | } |
| 115 | 1× | |
| 116 | 1× | /** File content at a state; `change` may be a hash prefix, id prefix, bookmark, or tag. */ |
| 117 | 1× | async cat(root: string, path: string, change?: string): Promise<Buffer> { |
| 118 | 8× | const args = ["cat", path]; |
| 119 | 8× | if (change) { |
| 120 | 1× | args.push("-r", change); |
| 121 | 1× | } |
| 122 | 8× | return (await this.check(root, args)).stdout; |
| 123 | 8× | } |
| 124 | 1× | |
| 125 | 1× | log(root: string, limit?: number, path?: string): Promise<LogInfo> { |
| 126 | 10× | const args = ["log"]; |
| 127 | 10× | if (limit !== undefined) { |
| 128 | 10× | args.push("-n", String(limit)); |
| 129 | 10× | } |
| 130 | 10× | if (path) { |
| 131 | 1× | args.push("--path", path); |
| 132 | 1× | } |
| 133 | 10× | return this.json<LogInfo>(root, args); |
| 134 | 10× | } |
| 135 | 1× | |
| 136 | 1× | commit(root: string, message: string, paths: string[] = []): Promise<void> { |
| 137 | 18× | return this.check(root, ["commit", "-m", message, ...paths]).then(() => undefined); |
| 138 | 18× | } |
| 139 | 1× | |
| 140 | 1× | /** The local bookmarks; with `all`, remotes' bookmarks with no local one too, named `REMOTE/NAME`. */ |
| 141 | 1× | bookmarks(root: string, all = false): Promise<BookmarkInfo[]> { |
| 142 | 6× | return this.json<BookmarkInfo[]>(root, ["bookmark", "list", ...(all ? ["--all"] : [])]); |
| 143 | 6× | } |
| 144 | 1× | |
| 145 | 1× | /** Restore paths to the working copy's base. */ |
| 146 | 1× | discard(root: string, paths: string[]): Promise<void> { |
| 147 | 1× | return this.check(root, ["discard", ...paths]).then(() => undefined); |
| 148 | 1× | } |
| 149 | 1× | |
| 150 | 1× | discardHunks(root: string, hunks: number[]): Promise<void> { |
| 151 | 0× | return this.check(root, ["discard", "--hunks", hunks.join(",")]).then(() => undefined); |
| 152 | 0× | } |
| 153 | 1× | |
| 154 | 1× | hunks(root: string): Promise<HunkInfo[]> { |
| 155 | 1× | return this.json<HunkInfo[]>(root, ["hunks"]); |
| 156 | 1× | } |
| 157 | 1× | |
| 158 | 1× | commitHunks(root: string, message: string, hunks: number[]): Promise<void> { |
| 159 | 1× | return this.check(root, ["commit", "-m", message, "--hunks", hunks.join(",")]).then(() => undefined); |
| 160 | 1× | } |
| 161 | 1× | |
| 162 | 1× | /** Fold the working copy into the head it sits on; with `paths`, only those (the rest stays uncommitted). */ |
| 163 | 1× | amend(root: string, message?: string, paths: string[] = []): Promise<void> { |
| 164 | 2× | const args = ["amend"]; |
| 165 | 2× | if (message) { |
| 166 | 1× | args.push("-m", message); |
| 167 | 1× | } |
| 168 | 2× | return this.check(root, [...args, ...paths]).then(() => undefined); |
| 169 | 2× | } |
| 170 | 1× | |
| 171 | 1× | undo(root: string): Promise<string> { |
| 172 | 2× | return this.check(root, ["undo"]).then((r) => r.stdout.toString("utf8").trim()); |
| 173 | 2× | } |
| 174 | 1× | |
| 175 | 1× | opLog(root: string): Promise<OperationInfo[]> { |
| 176 | 1× | return this.json<OperationInfo[]>(root, ["op", "log"]); |
| 177 | 1× | } |
| 178 | 1× | |
| 179 | 1× | // ---------------------------------------------------------------- bookmarks |
| 180 | 1× | |
| 181 | 1× | bookmarkCreate(root: string, name: string, changes: string[] = []): Promise<void> { |
| 182 | 4× | return this.check(root, ["bookmark", "create", name, ...changes]).then(() => undefined); |
| 183 | 4× | } |
| 184 | 1× | |
| 185 | 1× | bookmarkSwitch(root: string, name: string, discard = false): Promise<void> { |
| 186 | 8× | const args = ["bookmark", "switch", name]; |
| 187 | 8× | if (discard) { |
| 188 | 0× | args.push("--discard"); |
| 189 | 0× | } |
| 190 | 8× | return this.check(root, args).then(() => undefined); |
| 191 | 8× | } |
| 192 | 1× | |
| 193 | 1× | bookmarkDelete(root: string, name: string): Promise<void> { |
| 194 | 1× | return this.check(root, ["bookmark", "delete", name]).then(() => undefined); |
| 195 | 1× | } |
| 196 | 1× | |
| 197 | 1× | /** Union a bookmark's heads into the current one: a local bookmark, or a remote's (`origin/develop`) as last fetched. */ |
| 198 | 1× | merge(root: string, name: string): Promise<string> { |
| 199 | 4× | return this.check(root, ["merge", name]).then((r) => r.stdout.toString("utf8").trim()); |
| 200 | 4× | } |
| 201 | 1× | |
| 202 | 1× | // ---------------------------------------------------------------- remotes and sync |
| 203 | 1× | |
| 204 | 1× | remotes(root: string): Promise<RemoteInfo[]> { |
| 205 | 1× | return this.json<RemoteInfo[]>(root, ["remote", "list"]); |
| 206 | 1× | } |
| 207 | 1× | |
| 208 | 1× | fetch(root: string, remote?: string): Promise<FetchInfo> { |
| 209 | 2× | return this.json<FetchInfo>(root, ["fetch", ...(remote ? [remote] : [])]); |
| 210 | 2× | } |
| 211 | 1× | |
| 212 | 1× | pull(root: string, remote?: string): Promise<PullInfo> { |
| 213 | 1× | return this.json<PullInfo>(root, ["pull", ...(remote ? [remote] : [])]); |
| 214 | 1× | } |
| 215 | 1× | |
| 216 | 1× | push(root: string, remote?: string, bookmark?: string, force = false): Promise<PushResult[]> { |
| 217 | 3× | const args = ["push", ...(remote ? [remote] : [])]; |
| 218 | 3× | if (bookmark) { |
| 219 | 1× | args.push("-b", bookmark); |
| 220 | 1× | } |
| 221 | 3× | if (force) { |
| 222 | 0× | args.push("--force"); |
| 223 | 0× | } |
| 224 | 3× | return this.json<PushResult[]>(root, args, [0, 1]); |
| 225 | 3× | } |
| 226 | 1× | |
| 227 | 1× | /** Clone `url` into `dest`; `depth` makes it shallow (0: the latest state only, from the remote's checkpoint). */ |
| 228 | 1× | clone(cwd: string, url: string, dest: string, depth?: number): Promise<void> { |
| 229 | 2× | return this.check(cwd, ["clone", ...(depth !== undefined ? ["--depth", String(depth)] : []), url, dest]).then(() => undefined); |
| 230 | 2× | } |
| 231 | 1× | |
| 232 | 1× | // ---------------------------------------------------------------- history and blame |
| 233 | 1× | |
| 234 | 1× | blame(root: string, path: string): Promise<BlameLine[]> { |
| 235 | 1× | return this.json<BlameLine[]>(root, ["blame", path]); |
| 236 | 1× | } |
| 237 | 1× | |
| 238 | 1× | show(root: string, change: string): Promise<ShowInfo> { |
| 239 | 2× | return this.json<ShowInfo>(root, ["show", change]); |
| 240 | 2× | } |
| 241 | 1× | |
| 242 | 1× | /** The rendered diff of a change (or the working copy), as `oxy diff` prints it. */ |
| 243 | 1× | diffText(root: string, change?: string): Promise<string> { |
| 244 | 1× | return this.check(root, ["diff", ...(change ? [change] : [])]).then((r) => r.stdout.toString("utf8")); |
| 245 | 1× | } |
| 246 | 1× | |
| 247 | 1× | describe(root: string, change: string, message: string): Promise<void> { |
| 248 | 0× | return this.check(root, ["describe", change, "-m", message]).then(() => undefined); |
| 249 | 0× | } |
| 250 | 1× | |
| 251 | 1× | abandon(root: string, change: string): Promise<string> { |
| 252 | 0× | return this.check(root, ["abandon", change]).then((r) => r.stdout.toString("utf8").trim()); |
| 253 | 0× | } |
| 254 | 1× | |
| 255 | 1× | /** The configured identity and how commits are signed; `null` without one. */ |
| 256 | 1× | identity(root: string): Promise<SigningInfo | null> { |
| 257 | 2× | return this.check(root, ["identity", "show"]).then((r) => parseIdentityShow(r.stdout.toString("utf8"))); |
| 258 | 2× | } |
| 259 | 1× | |
| 260 | 1× | /** Split a head: the edits to `paths` become a separate first change. Returns oxy's summary line. */ |
| 261 | 1× | split(root: string, change: string, paths: string[]): Promise<string> { |
| 262 | 1× | return this.check(root, ["split", change, ...paths]).then((r) => r.stdout.toString("utf8").trim()); |
| 263 | 1× | } |
| 264 | 1× | |
| 265 | 1× | /** Fold `change` into its dependency `into`, which keeps its ChangeId. */ |
| 266 | 1× | squash(root: string, change: string, into: string): Promise<string> { |
| 267 | 1× | return this.check(root, ["squash", change, "--into", into]).then((r) => r.stdout.toString("utf8").trim()); |
| 268 | 1× | } |
| 269 | 1× | |
| 270 | 1× | /** Re-express `change` against the state of `onto` (changes, a bookmark or a tag). */ |
| 271 | 1× | rebase(root: string, change: string, onto: string[]): Promise<string> { |
| 272 | 1× | return this.check(root, ["rebase", change, "--onto", ...onto]).then((r) => r.stdout.toString("utf8").trim()); |
| 273 | 1× | } |
| 274 | 1× | |
| 275 | 1× | checkout(root: string, changes: string[], discard = false): Promise<void> { |
| 276 | 0× | return this.check(root, ["checkout", ...changes, ...(discard ? ["--discard"] : [])]).then(() => undefined); |
| 277 | 0× | } |
| 278 | 1× | |
| 279 | 1× | tagCreate(root: string, name: string, changes: string[] = [], message = ""): Promise<void> { |
| 280 | 3× | return this.check(root, ["tag", "create", name, ...changes, "-m", message]).then(() => undefined); |
| 281 | 3× | } |
| 282 | 1× | |
| 283 | 1× | /** |
| 284 | 1× | * Withdraw a tag (`oxy tag delete`): a signed withdrawal kept beside the |
| 285 | 1× | * tag's creation, which push sends on; the name is never used again. |
| 286 | 1× | * Returns oxy's summary line. |
| 287 | 1× | */ |
| 288 | 1× | tagWithdraw(root: string, name: string, reason: string): Promise<string> { |
| 289 | 1× | return this.check(root, ["tag", "delete", name, "-r", reason]).then((r) => r.stdout.toString("utf8").trim().split("\n")[0].replace(/^\* /, "")); |
| 290 | 1× | } |
| 291 | 1× | |
| 292 | 1× | // ---------------------------------------------------------------- settling conflicts |
| 293 | 1× | |
| 294 | 1× | /** |
| 295 | 1× | * Settle the replace conflicts (binary files, subrepo pins) at `paths` |
| 296 | 1× | * with what the working copy holds there (oxygen#140); the next commit |
| 297 | 1× | * records the resolution. Returns the status after it. |
| 298 | 1× | */ |
| 299 | 1× | resolvePaths(root: string, paths: string[]): Promise<StatusInfo> { |
| 300 | 2× | return this.json<StatusInfo>(root, ["resolve", ...paths], [0, 2]); |
| 301 | 2× | } |
| 302 | 1× | |
| 303 | 1× | /** |
| 304 | 1× | * Settle adjacent insertions (two changes adding lines at the same place) |
| 305 | 1× | * by keeping every side, earliest change first, or latest with `reverse` |
| 306 | 1× | * (oxygen#121). No paths: every conflicted text file. What was kept and |
| 307 | 1× | * what keeps its markers, and why. |
| 308 | 1× | */ |
| 309 | 1× | resolveBoth(root: string, paths: string[] = [], reverse = false): Promise<KeptBoth> { |
| 310 | 1× | return this.check(root, ["resolve", "--both", ...(reverse ? ["--reverse"] : []), ...paths], [0, 2]).then((r) => parseKeptBoth(r.stdout.toString("utf8"))); |
| 311 | 1× | } |
| 312 | 1× | |
| 313 | 1× | // ---------------------------------------------------------------- transparency log |
| 314 | 1× | |
| 315 | 1× | /** |
| 316 | 1× | * Audit a hub's whole transparency log (`oxy tlog verify`): its checkpoint |
| 317 | 1× | * against the log key and the last one seen, and every entry against the |
| 318 | 1× | * root; then this repository's entries. `hub` defaults to the remote. |
| 319 | 1× | */ |
| 320 | 1× | tlogVerify(root: string, hub?: string): Promise<TlogInfo> { |
| 321 | 1× | return this.json<TlogInfo>(root, ["tlog", "verify", ...(hub ? [hub] : [])]); |
| 322 | 1× | } |
| 323 | 1× | |
| 324 | 1× | // ---------------------------------------------------------------- subrepos, bisect, trust, tags, workspaces |
| 325 | 1× | |
| 326 | 1× | submodules(root: string): Promise<SubrepoInfo[]> { |
| 327 | 1× | return this.json<SubrepoInfo[]>(root, ["submodule", "status"]); |
| 328 | 1× | } |
| 329 | 1× | |
| 330 | 1× | submoduleUpdate(root: string, init: boolean, paths: string[] = []): Promise<string> { |
| 331 | 0× | return this.check(root, ["submodule", "update", ...(init ? ["--init"] : []), ...paths], [0, 1]).then((r) => r.stdout.toString("utf8")); |
| 332 | 0× | } |
| 333 | 1× | |
| 334 | 1× | bisectStatus(root: string): Promise<BisectStatus | null> { |
| 335 | 2× | return this.json<BisectStatus | null>(root, ["bisect", "status"]); |
| 336 | 2× | } |
| 337 | 1× | |
| 338 | 1× | bisectStart(root: string): Promise<void> { |
| 339 | 1× | return this.check(root, ["bisect", "start"]).then(() => undefined); |
| 340 | 1× | } |
| 341 | 1× | |
| 342 | 1× | bisectMark(root: string, verdict: "good" | "bad" | "skip", changes: string[] = []): Promise<string> { |
| 343 | 0× | return this.check(root, ["bisect", verdict, ...changes]).then((r) => r.stdout.toString("utf8").trim()); |
| 344 | 0× | } |
| 345 | 1× | |
| 346 | 1× | bisectRun(root: string, command: string[]): Promise<string> { |
| 347 | 0× | return this.check(root, ["bisect", "run", ...command], [0, 1]).then((r) => r.stdout.toString("utf8").trim()); |
| 348 | 0× | } |
| 349 | 1× | |
| 350 | 1× | bisectReset(root: string): Promise<void> { |
| 351 | 1× | return this.check(root, ["bisect", "reset"]).then(() => undefined); |
| 352 | 1× | } |
| 353 | 1× | |
| 354 | 1× | policyShow(root: string): Promise<PolicyInfo | null> { |
| 355 | 1× | return this.json<PolicyInfo | null>(root, ["policy", "show"]); |
| 356 | 1× | } |
| 357 | 1× | |
| 358 | 1× | verify(root: string, change?: string): Promise<VerifyInfo[]> { |
| 359 | 1× | return this.json<VerifyInfo[]>(root, ["verify", ...(change ? [change] : [])], [0, 1]); |
| 360 | 1× | } |
| 361 | 1× | |
| 362 | 1× | tags(root: string): Promise<TagInfo[]> { |
| 363 | 2× | return this.json<TagInfo[]>(root, ["tag", "list"]); |
| 364 | 2× | } |
| 365 | 1× | |
| 366 | 1× | workspaces(root: string): Promise<WorkspaceInfo[]> { |
| 367 | 1× | return this.json<WorkspaceInfo[]>(root, ["workspace", "list"]); |
| 368 | 1× | } |
| 369 | 1× | |
| 370 | 1× | workspaceAdd(root: string, path: string, changes: string[] = []): Promise<void> { |
| 371 | 0× | return this.check(root, ["workspace", "add", path, ...changes]).then(() => undefined); |
| 372 | 0× | } |
| 373 | 1× | } |
| 374 | 1× | |
| 375 | 1× | export interface SubrepoInfo { |
| 376 | 1× | path: string; |
| 377 | 1× | pin: PinInfo; |
| 378 | 1× | actual: PinInfo | null; |
| 379 | 1× | at_pin: boolean; |
| 380 | 1× | dirty: boolean; |
| 381 | 1× | url: string | null; |
| 382 | 1× | } |
| 383 | 1× | |
| 384 | 1× | export type PinInfo = { kind: "oxy"; repo: string; heads: string[]; bookmark: string } | { kind: "git"; sha: string; branch: string }; |
| 385 | 1× | |
| 386 | 1× | export type BisectStatus = |
| 387 | 1× | | { state: "need-marks"; good: number; bad: number } |
| 388 | 1× | | { state: "testing"; heads: string[]; remaining: number; steps_left: number } |
| 389 | 1× | | { state: "found"; culprit: string } |
| 390 | 1× | | { state: "inconclusive"; candidates: string[] }; |
| 391 | 1× | |
| 392 | 1× | export interface PolicyInfo { |
| 393 | 1× | hash: string; |
| 394 | 1× | serial: number; |
| 395 | 1× | expires_ms: number; |
| 396 | 1× | expired: boolean; |
| 397 | 1× | root_threshold: number; |
| 398 | 1× | roots: { key: string; name: string; email: string }[]; |
| 399 | 1× | rules: { pattern: string; keys: { key: string; name: string; email: string }[]; threshold: number; require: string; allow_conflicts: boolean }[]; |
| 400 | 1× | revoked: { key: string; name: string; email: string }[]; |
| 401 | 1× | } |
| 402 | 1× | |
| 403 | 1× | export interface VerifyInfo { |
| 404 | 1× | hash: string; |
| 405 | 1× | change_id: string; |
| 406 | 1× | status: string; |
| 407 | 1× | key: string | null; |
| 408 | 1× | name: string | null; |
| 409 | 1× | revoked: boolean; |
| 410 | 1× | reason: string | null; |
| 411 | 1× | } |
| 412 | 1× | |
| 413 | 1× | /** What `oxy resolve --both` did: the conflicts kept on every side, and those left with markers. */ |
| 414 | 1× | export interface KeptBoth { |
| 415 | 1× | kept: { path: string; conflicts: number[] }[]; |
| 416 | 1× | left: { path: string; conflict: number; why: string }[]; |
| 417 | 1× | } |
| 418 | 1× | |
| 419 | 1× | /** Read `oxy resolve --both`'s report lines (`kept both PATH: conflict 1, 2`, `markers PATH: conflict 3: why`). */ |
| 420 | 1× | export function parseKeptBoth(text: string): KeptBoth { |
| 421 | 3× | const out: KeptBoth = { kept: [], left: [] }; |
| 422 | 3× | for (const line of text.split(/\r?\n/)) { |
| 423 | 12× | const kept = /^kept both\s+(.+): conflicts? ([\d, ]+)$/.exec(line); |
| 424 | 12× | if (kept) { |
| 425 | 2× | out.kept.push({ path: kept[1], conflicts: kept[2].split(",").map((n) => Number(n.trim())).filter((n) => n > 0) }); |
| 426 | 2× | continue; |
| 427 | 2× | } |
| 428 | 10× | const left = /^markers\s+(.+): conflict (\d+): (.+)$/.exec(line); |
| 429 | 12× | if (left) { |
| 430 | 2× | out.left.push({ path: left[1], conflict: Number(left[2]), why: left[3] }); |
| 431 | 2× | } |
| 432 | 12× | } |
| 433 | 3× | return out; |
| 434 | 11× | } |
| 435 | 11× | |
| 436 | 11× | /** `oxy tlog verify --json`: the checkpoint verified, who cosigned it, and the entries listed. */ |
| 437 | 11× | export interface TlogInfo { |
| 438 | 11× | hub: string; |
| 439 | 11× | origin: string; |
| 440 | 11× | /** Entries in the log the checkpoint covers. */ |
| 441 | 11× | size: number; |
| 442 | 11× | root: string; |
| 443 | 11× | witnesses: { name: string; time_ms: number }[]; |
| 444 | 11× | entries: { index: number; kind: string; repo: string; object: string; signers: string[]; received_ms: number }[]; |
| 445 | 11× | } |
| 446 | 11× | |
| 447 | 11× | /** |
| 448 | 11× | * A remote's bookmark that `bookmark list --all` adds because there is no |
| 449 | 11× | * local bookmark of that name: named `REMOTE/NAME`, never current, with no |
| 450 | 11× | * ahead or behind counts. `oxy merge` takes the name as it is. |
| 451 | 11× | */ |
| 452 | 11× | export function isRemoteBookmark(b: BookmarkInfo): boolean { |
| 453 | 11× | return !!b.remote && b.name.startsWith(`${b.remote}/`) && !b.current && b.ahead === null && b.behind === null; |
| 454 | 2× | } |
| 455 | 2× | |
| 456 | 2× | /** What Merge Bookmark offers: the other local bookmarks first, then remotes' bookmarks. */ |
| 457 | 2× | export function mergeChoices(bookmarks: BookmarkInfo[]): { name: string; remote: string | null; heads: number }[] { |
| 458 | 2× | const local = bookmarks.filter((b) => !b.current && !isRemoteBookmark(b)).map((b) => ({ name: b.name, remote: null, heads: b.heads.length })); |
| 459 | 2× | const remote = bookmarks.filter(isRemoteBookmark).map((b) => ({ name: b.name, remote: b.remote ?? null, heads: b.heads.length })); |
| 460 | 2× | return [...local, ...remote]; |
| 461 | 2× | } |
| 462 | 1× | |
| 463 | 0× | export interface TagInfo { |
| 464 | 0× | name: string; |
| 465 | 0× | state: string; |
| 466 | 0× | heads: string[]; |
| 467 | 0× | message: string; |
| 468 | 0× | signer: string | null; |
| 469 | 0× | } |
| 470 | 1× | |
| 471 | 0× | export interface WorkspaceInfo { |
| 472 | 0× | id: string; |
| 473 | 0× | path: string; |
| 474 | 0× | current: boolean; |
| 475 | 0× | working_copy: string | null; |
| 476 | 0× | } |
| 477 | 1× | |
| 478 | 0× | export interface BlameLine { |
| 479 | 0× | line: number; |
| 480 | 0× | change_id: string; |
| 481 | 0× | hash: string; |
| 482 | 0× | index: number; |
| 483 | 0× | author: string; |
| 484 | 0× | when_ms: number; |
| 485 | 0× | } |
| 486 | 1× | |
| 487 | 0× | export interface ShowInfo extends LogEntry { |
| 488 | 0× | files: { path: string; kind: string; from: string | null }[]; |
| 489 | 0× | } |
| 490 | 1× | |
| 491 | 0× | export interface RemoteInfo { |
| 492 | 0× | name: string; |
| 493 | 0× | url: string; |
| 494 | 0× | /** The CA file `remote.NAME.ca` names (a self-signed hub), if any. */ |
| 495 | 0× | ca?: string | null; |
| 496 | 0× | bookmarks: { name: string; heads: string[] }[]; |
| 497 | 0× | } |
| 498 | 1× | |
| 499 | 0× | export interface FetchInfo { |
| 500 | 0× | remote: string; |
| 501 | 0× | refs: { name: string; heads: string[]; new: number }[]; |
| 502 | 0× | new_tags: number; |
| 503 | 0× | unverified: boolean; |
| 504 | 0× | } |
| 505 | 1× | |
| 506 | 0× | export interface PullInfo { |
| 507 | 0× | remote: string; |
| 508 | 0× | heads: string[]; |
| 509 | 0× | changed: boolean; |
| 510 | 0× | checked_out: boolean; |
| 511 | 0× | conflicted: boolean; |
| 512 | 0× | } |
| 513 | 1× | |
| 514 | 0× | export interface PushResult { |
| 515 | 0× | name: string; |
| 516 | 0× | ok: boolean; |
| 517 | 0× | reason: string; |
| 518 | 0× | } |
| 519 | 1× | |
| 520 | 0× | export interface HunkInfo { |
| 521 | 0× | index: number; |
| 522 | 0× | path: string; |
| 523 | 0× | text: string; |
| 524 | 0× | } |
| 525 | 1× | |
| 526 | 0× | export interface OperationInfo { |
| 527 | 0× | id: string; |
| 528 | 0× | when_ms: number; |
| 529 | 0× | command: string; |
| 530 | 0× | snapshot: boolean; |
| 531 | 0× | current: boolean; |
| 532 | 0× | } |
| 533 | 1× | |
| 534 | 0× | // ---------------------------------------------------------------- types |
| 535 | 1× | |
| 536 | 0× | export interface VersionInfo { |
| 537 | 0× | version: string; |
| 538 | 0× | protocol: number; |
| 539 | 0× | format: number; |
| 540 | 0× | } |
| 541 | 1× | |
| 542 | 0× | export interface RootInfo { |
| 543 | 0× | root: string; |
| 544 | 0× | workspace: string; |
| 545 | 0× | bookmark: string; |
| 546 | 0× | } |
| 547 | 1× | |
| 548 | 0× | export type PathStatus = "Added" | "Modified" | "Removed" | "Renamed" | "ModeChanged" | "ConflictEdited" | "Resolved"; |
| 549 | 1× | |
| 550 | 0× | export interface StatusInfo { |
| 551 | 0× | bookmark: string; |
| 552 | 0× | working_copy: string; |
| 553 | 0× | heads: number; |
| 554 | 0× | clean: boolean; |
| 555 | 0× | conflicted: boolean; |
| 556 | 0× | paths: { path: string; status: PathStatus }[]; |
| 557 | 0× | conflicts: ConflictInfo[]; |
| 558 | 0× | } |
| 559 | 1× | |
| 560 | 0× | export interface ConflictInfo { |
| 561 | 0× | kind: string; |
| 562 | 0× | path?: string; |
| 563 | 0× | node?: string; |
| 564 | 0× | nodes?: string[]; |
| 565 | 0× | changes?: string[]; |
| 566 | 0× | resolved?: boolean | "markers"; |
| 567 | 0× | } |
| 568 | 1× | |
| 569 | 0× | export interface LogEntry { |
| 570 | 0× | hash: string; |
| 571 | 0× | change_id: string; |
| 572 | 0× | author: string; |
| 573 | 0× | author_key: string; |
| 574 | 0× | when_ms: number; |
| 575 | 0× | message: string; |
| 576 | 0× | kind: string; |
| 577 | 0× | deps: string[]; |
| 578 | 0× | head: boolean; |
| 579 | 0× | working_copy: boolean; |
| 580 | 0× | signature: { status: string; key?: string; name?: string; revoked?: boolean; reason?: string } | null; |
| 581 | 0× | } |
| 582 | 1× | |
| 583 | 0× | export interface LogInfo { |
| 584 | 0× | bookmark: string; |
| 585 | 0× | total: number; |
| 586 | 0× | changes: LogEntry[]; |
| 587 | 0× | } |
| 588 | 1× | |
| 589 | 0× | export interface BookmarkInfo { |
| 590 | 0× | name: string; |
| 591 | 0× | heads: string[]; |
| 592 | 0× | current: boolean; |
| 593 | 0× | ahead: number | null; |
| 594 | 0× | behind: number | null; |
| 595 | 0× | /** The remote it tracks, or for a remote's bookmark (`--all`) the remote it is on. */ |
| 596 | 0× | remote?: string | null; |
| 597 | 0× | } |