- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/client-version.ts?draft=login-timeout&format=json
103 linesFirst import 99% · pinch 1%0 versions changed it
1// Which client this is (#29), for `avcs --version` and for the X-Abso-Client
2// header the command-line client sends with each call: plugin.json's version
3// plus the commit checked out where the client runs (0.1.0+ed45dad). The
4// commit is read from .git without running git, since every command pays for
5// its start-up. (The mods carry their own stamp: src/install.ts.)
7import { readFileSync, statSync } from "node:fs";
8import { dirname, join, resolve } from "node:path";
9import { fileURLToPath } from "node:url";
11export const CLIENT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "..");
13/** The commit checked out in the repository holding `dir`, as 7 hex digits; undefined outside one. */
14export function headCommit(dir: string): string | undefined {
15 for (let d = resolve(dir); ; d = dirname(d)) {
16 const dot = join(d, ".git");
17 let isFile: boolean;
18 try {
19 isFile = statSync(dot).isFile();
20 } catch {
21 if (dirname(d) === d) return undefined;
22 continue;
23 }
24 try {
25 // A worktree's .git is a file naming its folder, whose commondir names the shared one.
26 const gitDir = isFile
27 ? resolve(
28 d,
29 readFileSync(dot, "utf8")
30 .replace(/^gitdir:\s*/, "")
31 .trim(),
32 )
33 : dot;
34 let common = gitDir;
35 try {
36 common = resolve(gitDir, readFileSync(join(gitDir, "commondir"), "utf8").trim());
37 } catch {}
38 const head = readFileSync(join(gitDir, "HEAD"), "utf8").trim();
39 if (!head.startsWith("ref:")) return short(head);
40 const ref = head.slice(4).trim();
41 for (const base of [gitDir, common]) {
42 try {
43 return short(readFileSync(join(base, ...ref.split("/")), "utf8").trim());
44 } catch {}
45 }
46 const packed = readFileSync(join(common, "packed-refs"), "utf8")
47 .split("\n")
48 .find((line) => line.endsWith(` ${ref}`));
49 return packed === undefined ? undefined : short(packed.split(" ")[0]!);
50 } catch {
51 return undefined;
52 }
53 }
54}
56const short = (sha: string) => (/^[0-9a-f]{40,64}$/.test(sha) ? sha.slice(0, 7) : undefined);
58/** Set by the staging bundle (abso issue 109). Absent when this file is run from a checkout. */
59export function clientPlace(): string {
60 return bundledCommit() ? "a downloaded copy" : CLIENT_DIR;
61}
63function bundledCommit(): string | undefined {
64 const stamp = (globalThis as { __ABSO_BUILD_COMMIT__?: unknown }).__ABSO_BUILD_COMMIT__;
65 return typeof stamp === "string" && /^[0-9a-f]{7,64}$/.test(stamp) ? stamp.slice(0, 7) : undefined;
66}
68let version: string | undefined;
70/**
71 * When this client was bundled for a server and that server is now a different
72 * commit. Undefined when they match, when the server can't be asked, or when
73 * this client runs from a checkout, which abso sync updates and no server serves.
74 */
75export async function newerCommitNote(server: string): Promise<string | undefined> {
76 const mine = bundledCommit();
77 if (!mine) return undefined;
78 try {
79 const response = await fetch(new URL("/version", server), { signal: AbortSignal.timeout(3000) });
80 const body = (await response.json()) as { commit?: unknown };
81 const theirs = typeof body.commit === "string" ? body.commit.slice(0, 7) : "";
82 if (theirs === "" || theirs === mine) return undefined;
83 return `This abso is ${mine} and the server is ${theirs}. Download ${new URL("/client/abso.mjs", server)} and check it against /client/abso.mjs.sha256.`;
84 } catch {
85 return undefined;
86 }
87}
89/** This client's version: 0.1.0+ed45dad, or plugin.json's alone outside a git checkout. */
90export function clientVersion(): string {
91 if (version === undefined) {
92 let base = "0.0.0";
93 try {
94 base = String(
95 (JSON.parse(readFileSync(join(CLIENT_DIR, ".claude-plugin", "plugin.json"), "utf8")) as { version?: unknown })
96 .version,
97 ).split("+")[0]!;
98 } catch {}
99 const commit = bundledCommit() ?? headCommit(CLIENT_DIR);
100 version = commit ? `${base}+${commit}` : base;
101 }
102 return version;
103}