Versioning
@idiomark/versioning provides git-based version control for Idiomark files — full commit history, branching, diffing, three-way merge with conflict resolution, and snapshot-based rollback.
Installation
pnpm add @idiomark/versioning
Quick Start
import { createVersionControl } from "@idiomark/versioning";
// Initialize version control
const vc = await createVersionControl("./idiomark-repos");
// Commit a new version
const commit = await vc.commitVersion(
"file-id",
idiomarkFileContent,
"Added new personality traits",
{ name: "John Doe", email: "john@example.com" },
);
// Get version history
const history = await vc.getHistory("file-id", { limit: 10 });
// Create a branch
await vc.createBranch("file-id", "experiment-v2");
// Rollback
const rollbackMgr = vc.createRollbackManager();
await rollbackMgr.rollback("file-id", commit.sha);
Core Components
VersionControl
Each Idiomark file gets its own git repository:
const vc = await createVersionControl("./idiomark-repos");
// Commit changes
const commit = await vc.commitVersion(
fileId,
content,
"Updated voice settings",
author,
);
// History with filters
const history = await vc.getHistory(fileId, {
limit: 20,
order: "desc",
author: "User",
branch: "main",
});
// Get specific version
const version = await vc.getVersion(fileId, commit.sha);
// Branching
await vc.createBranch(fileId, "feature-branch");
const branches = await vc.listBranches(fileId);
await vc.checkout(fileId, "feature-branch");
DiffGenerator
Compare two versions of a Idiomark file:
import { createDiffGenerator } from "@idiomark/versioning";
const diffGen = createDiffGenerator();
const diff = await diffGen.generateDiff(fromContent, toContent, {
format: "json", // 'json' | 'unified' | 'minimal'
fromSha: "abc123",
toSha: "def456",
});
console.log(
`${diff.summary.additions} additions, ${diff.summary.deletions} deletions`,
);
Diff Formats:
| Format | Description |
|---|---|
json | JSON Patch (RFC 6902) |
unified | Unified diff format |
minimal | Changes only |
MergeEngine
Three-way merge with conflict resolution:
import { createMergeEngine } from "@idiomark/versioning";
const mergeEngine = createMergeEngine();
const result = await mergeEngine.merge(
baseContent, // common ancestor
ourContent, // our changes
theirContent, // their changes
"auto", // strategy
);
if (!result.success) {
for (const conflict of result.conflicts) {
console.log(`Conflict at ${conflict.path}:`);
console.log(` Ours: ${JSON.stringify(conflict.ours)}`);
console.log(` Theirs: ${JSON.stringify(conflict.theirs)}`);
}
// Auto-resolve remaining conflicts
const { resolved, remaining } = await mergeEngine.autoResolve(
result.conflicts,
"ours",
);
}
Merge Strategies:
| Strategy | Description |
|---|---|
auto | Auto-merge, prefer more data |
manual | Require manual resolution |
ours | Always prefer our changes |
theirs | Always prefer their changes |
union | Include both versions |
RollbackManager
Snapshot-based rollback to any previous version:
import { createRollbackManager } from "@idiomark/versioning";
const rollbackMgr = createRollbackManager("./idiomark-snapshots");
// Create a rollback point
const snapshot = await rollbackMgr.createRollbackPoint(
fileId,
commit,
"Stable version before major changes",
"user-id",
);
// Rollback
const result = await rollbackMgr.rollback(fileId, snapshot.id);
// Cleanup old snapshots
await rollbackMgr.cleanup(fileId, {
keepRecent: 10,
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
});
SnapshotManager
Automatic snapshots before risky operations:
import { createSnapshotManager } from "@idiomark/versioning";
const snapshotMgr = createSnapshotManager();
await snapshotMgr.createAutoSnapshot(fileId, content, "pre-update");
await snapshotMgr.createAutoSnapshot(fileId, content, "pre-merge");
REST API
Built-in routes for web integration:
GET /api/versions/:fileId - Version history
GET /api/versions/:fileId/:sha - Get specific version
GET /api/versions/:fileId/diff - Diff between versions
GET /api/versions/:fileId/branches - List branches
GET /api/versions/:fileId/status - Repository status
GET /api/versions/:fileId/rollback - List rollback points
POST /api/versions/:fileId/branch - Create branch
POST /api/versions/:fileId/merge - Merge branches
POST /api/versions/:fileId/rollback - Rollback to version
POST /api/versions/:fileId/snapshot - Create snapshot
DELETE /api/snapshots/:snapshotId - Delete snapshot
import { versionRoutes } from "@idiomark/versioning";
import express from "express";
const app = express();
for (const [route, handler] of Object.entries(versionRoutes)) {
const [method, path] = route.split(" ");
app[method.toLowerCase()](path, handler);
}