74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import {
|
|
assertNotEquals,
|
|
} from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
|
|
|
/**
|
|
* Proof of Concept: Git Merkle DAG Diffing
|
|
*
|
|
* This module demonstrates using Git's fundamental Merkle Tree structure
|
|
* (`git diff-tree` and `git ls-tree`) to perform zero-overhead, O(1) diffing
|
|
* to find exactly which file hashes changed without reading the file string content.
|
|
*/
|
|
|
|
async function runGitCmd(args: string[]): Promise<string> {
|
|
const cmd = new Deno.Command("git", {
|
|
args,
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
});
|
|
const output = await cmd.output();
|
|
const stdout = new TextDecoder().decode(output.stdout).trim();
|
|
const stderr = new TextDecoder().decode(output.stderr).trim();
|
|
|
|
if (!output.success) {
|
|
throw new Error(`Git command failed: git ${args.join(" ")}\n${stderr}`);
|
|
}
|
|
return stdout;
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
console.log("Running Git Merkle DAG Diffing PoC tests...");
|
|
|
|
try {
|
|
// 1. Get the current HEAD commit hash
|
|
const headHash = await runGitCmd(["rev-parse", "HEAD"]);
|
|
console.log(`Current HEAD: ${headHash}`);
|
|
|
|
// 2. Get the tree hash of HEAD
|
|
const treeHash = await runGitCmd(["rev-parse", "HEAD^{tree}"]);
|
|
console.log(`Tree Hash of HEAD: ${treeHash}`);
|
|
|
|
// 3. Diff HEAD against HEAD~1 (if available) to see what changed purely by hash
|
|
try {
|
|
const diffTreeOutput = await runGitCmd([
|
|
"diff-tree",
|
|
"--no-commit-id",
|
|
"--name-only",
|
|
"-r",
|
|
"HEAD",
|
|
]);
|
|
console.log(
|
|
`\nChanged files in HEAD (O(1) diffing):\n${
|
|
diffTreeOutput || "(No changes or no parent)"
|
|
}`,
|
|
);
|
|
|
|
assertNotEquals(treeHash, "");
|
|
console.log(
|
|
"\n✅ Git Merkle DAG Diffing PoC successful: Read tree hashes without opening files.",
|
|
);
|
|
} catch (e: any) {
|
|
if (e.message.includes("ambiguous argument")) {
|
|
console.log(
|
|
"Skipping diff-tree as there might not be enough commits in this repo for a diff.",
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error("❌ Git Merkle DAG Diffing PoC failed:", err);
|
|
Deno.exit(1);
|
|
}
|
|
}
|