Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/cli/src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ function ownTarget(session: RemoteSession, version?: SessionDetail['versions'][n

const normalizeId = (id: string) => id.trim().replace(/^#/, '').toLowerCase();

export function selectVersion(versions: SessionDetail['versions'], rawRef: string): SessionDetail['versions'][number] {
const ref = rawRef.trim();
const stored = versions.filter((v) => v.status === 'stored');
let matches: SessionDetail['versions'];
if (/^id:[1-9]\d*$/.test(ref)) matches = stored.filter((v) => v.id === Number(ref.slice(3)));
else if (/^v?[1-9]\d*$/i.test(ref)) matches = stored.filter((v) => v.seq === Number(ref.replace(/^v/i, '')));
else if (/^[A-Za-z0-9_-]+$/.test(ref)) matches = stored.filter((v) => v.contentHash.startsWith(ref));
else matches = [];
if (matches.length !== 1) throw new Error(`--rev ${rawRef} matches ${matches.length} versions; use --versions to list them.`);
return matches[0];
}

/** A pull id (own session or share), optionally a version by number `3` / `v3` or a content-hash prefix. */
async function resolveTarget(config: Config, id: string, versionRef: string | undefined): Promise<Target> {
const { sessions, shared } = await fetchList(config);
Expand All @@ -134,17 +146,11 @@ async function resolveTarget(config: Config, id: string, versionRef: string | un
if (session) {
if (!versionRef) return ownTarget(session);
const detail = await fetchDetail(config, session.id);
const ref = versionRef.trim().replace(/^v/i, '');
const matches = detail.versions.filter(
(v) => v.status === 'stored' && (byRowId !== null ? v.id === byRowId : String(v.seq) === ref || v.contentHash.startsWith(ref))
);
if (matches.length !== 1) throw new Error(`--rev ${versionRef} matches ${matches.length} versions; run \`vibi pull ${id} --versions\`.`);
return ownTarget(session, matches[0]);
return ownTarget(session, selectVersion(detail.versions, versionRef));
}
const share = shared.find((s) => s.pullId === wanted);
if (share) {
const refId = byRowId !== null ? byRowId : versionRef ? Number(versionRef.replace(/^v/i, '')) : null;
if (refId !== null && refId !== share.versionId) {
if (versionRef !== undefined && byRowId !== share.versionId) {
throw new Error('shared sessions refer to one version; --rev does not apply.');
}
return sharedTarget(share);
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/test/revisions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { selectVersion } from '../src/commands/pull';
import type { SessionDetail } from '@vibivibi/shared/sessions';

const versions = [{id: 80, seq: 1, status: 'stored', contentHash: 'vHash_ABC'}, {id: 90, seq: 2, status: 'stored', contentHash: '1different'}, {id: 91, seq: 3, status: 'pending', contentHash: 'pending'}] as SessionDetail['versions'];
test('version numbers cannot collide with hash prefixes and hashes retain their first character', () => {
assert.equal(selectVersion(versions, '1'), versions[0]);
assert.equal(selectVersion(versions, ' v2 '), versions[1]);
assert.equal(selectVersion(versions, 'vHash'), versions[0]);
assert.equal(selectVersion(versions, 'id:90'), versions[1]);
for (const ref of ['', ' ', '3', 'id:91', 'id:NaN', 'v0', 'v999', 'VHash']) assert.throws(() => selectVersion(versions, ref), /matches 0/);
assert.throws(() => selectVersion([...versions, {...versions[0], id: 95, contentHash: 'vHash_other'}], 'vHash'), /matches 2/);
});
Loading