Adding tests and fixing stuff
This commit is contained in:
291
src/repo.ts
291
src/repo.ts
@ -2,7 +2,7 @@ import SHA from "jssha";
|
||||
import Path from "./helper/path";
|
||||
|
||||
export interface IDataStore {
|
||||
get(key: string): Promise<Uint8Array>;
|
||||
get(key: string): Promise<Uint8Array | undefined>;
|
||||
set(key: string, data: Uint8Array): Promise<void>;
|
||||
has(key: string): Promise<boolean>;
|
||||
close?: () => Promise<void>;
|
||||
@ -22,6 +22,14 @@ export type Commit = {
|
||||
date: Date;
|
||||
};
|
||||
|
||||
export type NodeLog = {
|
||||
/**
|
||||
* ObjectID of the data. Can be undefined if element was deleted!
|
||||
*/
|
||||
id: string | undefined;
|
||||
commit: Commit
|
||||
}
|
||||
|
||||
// TODOs:
|
||||
// - HEAD locks
|
||||
// - HEAD/Tree Cache
|
||||
@ -29,12 +37,16 @@ export type Commit = {
|
||||
// - Add DataStore Locking for access from multiple sources
|
||||
|
||||
export default class Repository {
|
||||
//#region local variables
|
||||
#store: IDataStore;
|
||||
//#endregion
|
||||
|
||||
constructor(store: IDataStore) {
|
||||
this.#store = store;
|
||||
}
|
||||
|
||||
//#region private
|
||||
|
||||
private sha1(data: Uint8Array) {
|
||||
const s = new SHA("SHA-1", "UINT8ARRAY");
|
||||
s.update(data);
|
||||
@ -50,11 +62,9 @@ export default class Repository {
|
||||
private splitPath(path: string) {
|
||||
const resolved = Path.resolve(path).slice(1);
|
||||
if (resolved == "") return [];
|
||||
return resolved.split(Path.delimiter);
|
||||
return resolved.split(Path.sep);
|
||||
}
|
||||
|
||||
private getHeadLock() {}
|
||||
|
||||
private async writeObject(data: Uint8Array | string): Promise<string> {
|
||||
if (typeof data == "string") {
|
||||
data = new TextEncoder().encode(data);
|
||||
@ -68,9 +78,9 @@ export default class Repository {
|
||||
return this.#store.has("objects/" + id);
|
||||
}
|
||||
|
||||
private async readObject(id: string, string: true): Promise<string>;
|
||||
private async readObject(id: string, string?: false): Promise<Uint8Array>;
|
||||
private async readObject(id: string, string = false): Promise<Uint8Array | string> {
|
||||
private async readObject(id: string, string: true): Promise<string | undefined>;
|
||||
private async readObject(id: string, string?: false): Promise<Uint8Array | undefined>;
|
||||
private async readObject(id: string, string = false): Promise<Uint8Array | string | undefined> {
|
||||
let data = await this.#store.get("objects/" + id);
|
||||
if (string) {
|
||||
return new TextDecoder().decode(data);
|
||||
@ -79,94 +89,7 @@ export default class Repository {
|
||||
}
|
||||
}
|
||||
|
||||
async clean() {
|
||||
// TODO: Cleanup broken things
|
||||
}
|
||||
|
||||
async readdir(path: string): Promise<TreeEntry[] | undefined> {
|
||||
const parts = this.splitPath(path);
|
||||
console.log({ parts });
|
||||
const head = await this.readHead();
|
||||
if (!head) return undefined;
|
||||
|
||||
const treeID = await this.treeFindObjectID(head.root, parts, "tree");
|
||||
|
||||
if (!treeID) return undefined;
|
||||
|
||||
return this.readTree(treeID);
|
||||
}
|
||||
|
||||
async read(path: string): Promise<Uint8Array | undefined> {
|
||||
const parts = this.splitPath(path);
|
||||
const head = await this.readHead();
|
||||
if (!head) return undefined;
|
||||
|
||||
const objectID = await this.treeFindObjectID(head.root, parts, "blob");
|
||||
|
||||
if (!objectID) return undefined;
|
||||
|
||||
return this.readObject(objectID);
|
||||
}
|
||||
|
||||
async write(path: string, data: Uint8Array) {
|
||||
const parts = this.splitPath(path);
|
||||
|
||||
const objectID = await this.writeObject(data);
|
||||
|
||||
const lock = await this.#store.getLock();
|
||||
try {
|
||||
//TODO: Improve need of locking.
|
||||
const head = await this.readHead();
|
||||
|
||||
const makeTree = async (treeID: string | undefined, parts: string[]) => {
|
||||
let tree: TreeEntry[];
|
||||
if (treeID) {
|
||||
tree = await this.readTree(treeID);
|
||||
} else {
|
||||
tree = [];
|
||||
}
|
||||
|
||||
const current = parts[0];
|
||||
|
||||
let existing = tree.findIndex(([, , name]) => name == current);
|
||||
|
||||
let entry: TreeEntry;
|
||||
|
||||
if (parts.length == 1) {
|
||||
entry = ["blob", objectID, current];
|
||||
} else {
|
||||
entry = [
|
||||
"tree",
|
||||
await makeTree(existing >= 0 ? tree[existing][1] : undefined, parts.slice(1)),
|
||||
current,
|
||||
];
|
||||
}
|
||||
|
||||
if (existing >= 0) {
|
||||
let ex = tree[existing];
|
||||
if (parts.length == 1 && ex[0] == "tree")
|
||||
throw new Error("This change would overwrite a folder!");
|
||||
tree[existing] = entry;
|
||||
} else {
|
||||
tree.push(entry);
|
||||
}
|
||||
|
||||
let treeString = tree.map(([type, hash, name]) => `${type} ${hash} ${name}`).join("\n");
|
||||
|
||||
let newTreeID = await this.writeObject(treeString);
|
||||
|
||||
return newTreeID;
|
||||
};
|
||||
|
||||
let newTree = await makeTree(head?.root, parts);
|
||||
let commit = await this.makeCommit(newTree, head);
|
||||
await this.writeHead(commit);
|
||||
} finally {
|
||||
await lock();
|
||||
}
|
||||
}
|
||||
|
||||
async treeFindObjectID(
|
||||
private async treeFindObjectID(
|
||||
treeID: string,
|
||||
parts: string[],
|
||||
type: NodeType
|
||||
@ -192,9 +115,10 @@ export default class Repository {
|
||||
}
|
||||
}
|
||||
|
||||
async readTree(id: string): Promise<TreeEntry[]> {
|
||||
private async readTree(id: string): Promise<TreeEntry[]> {
|
||||
const tree = await this.readObject(id, true);
|
||||
return tree.split("\n").map((e) => {
|
||||
if (tree == undefined) throw new Error("Invalid treeID");
|
||||
return tree.split("\n").filter(e => e !== "").map((e) => {
|
||||
const entry = e.split(" ") as TreeEntry;
|
||||
const [type] = entry;
|
||||
|
||||
@ -210,7 +134,7 @@ export default class Repository {
|
||||
});
|
||||
}
|
||||
|
||||
async makeCommit(treeID: string, old?: Commit) {
|
||||
private async makeCommit(treeID: string, old?: Commit) {
|
||||
if (!old) {
|
||||
// Could be called once more than necessary, if no HEAD exists.
|
||||
old = await this.readHead();
|
||||
@ -221,10 +145,10 @@ export default class Repository {
|
||||
return await this.writeObject(commitStr);
|
||||
}
|
||||
|
||||
async readCommit(id: string): Promise<Commit> {
|
||||
if (!(await this.hasObject(id))) throw new Error(`Commit with id ${id} not found!`);
|
||||
|
||||
private async readCommit(id: string): Promise<Commit> {
|
||||
const commitStr = await this.readObject(id, true);
|
||||
if (!commitStr)
|
||||
throw new Error(`Commit with id ${id} not found!`);
|
||||
|
||||
let commit: Commit = { id } as any;
|
||||
for (const entry of commitStr.split("\n")) {
|
||||
@ -236,8 +160,10 @@ export default class Repository {
|
||||
break;
|
||||
case "before": // TODO: Simple validity checks
|
||||
commit.before = value;
|
||||
break;
|
||||
case "date":
|
||||
commit.date = new Date(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -248,18 +174,177 @@ export default class Repository {
|
||||
return commit;
|
||||
}
|
||||
|
||||
async readHead(): Promise<Commit | undefined> {
|
||||
private async readHead(): Promise<Commit | undefined> {
|
||||
if (!(await this.#store.has("HEAD"))) return undefined;
|
||||
const head = new TextDecoder().decode(await this.#store.get("HEAD"));
|
||||
|
||||
return this.readCommit(head);
|
||||
}
|
||||
|
||||
async writeHead(commitID: string): Promise<void> {
|
||||
private async writeHead(commitID: string): Promise<void> {
|
||||
await this.#store.set("HEAD", new TextEncoder().encode(commitID));
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region public
|
||||
async clean() {
|
||||
// TODO: Cleanup broken things
|
||||
}
|
||||
|
||||
async readdir(path: string): Promise<TreeEntry[]> {
|
||||
const parts = this.splitPath(path);
|
||||
const head = await this.readHead();
|
||||
if (!head) return [];
|
||||
|
||||
const treeID = await this.treeFindObjectID(head.root, parts, "tree");
|
||||
|
||||
if (!treeID) return [];
|
||||
|
||||
return this.readTree(treeID);
|
||||
}
|
||||
|
||||
async read(path: string): Promise<Uint8Array | undefined> {
|
||||
const parts = this.splitPath(path);
|
||||
const head = await this.readHead();
|
||||
if (!head) return undefined;
|
||||
|
||||
const objectID = await this.treeFindObjectID(head.root, parts, "blob");
|
||||
|
||||
if (!objectID) return undefined;
|
||||
|
||||
return this.readObject(objectID);
|
||||
}
|
||||
|
||||
async readByID(id: string) {
|
||||
return this.readObject(id);
|
||||
}
|
||||
|
||||
async log(path: string): Promise<any[]> {
|
||||
const parts = this.splitPath(path);
|
||||
const head = await this.readHead();
|
||||
if (!head) return [];
|
||||
|
||||
let currObjectID = await this.treeFindObjectID(head.root, parts, "blob");
|
||||
|
||||
let history: NodeLog[] = [];
|
||||
|
||||
if (currObjectID) {
|
||||
history.push({
|
||||
id: currObjectID,
|
||||
commit: head
|
||||
})
|
||||
}
|
||||
|
||||
let currCommit = head.before;
|
||||
|
||||
while (currCommit !== undefined) {
|
||||
try {
|
||||
let commit = await this.readCommit(currCommit);
|
||||
let res = await this.treeFindObjectID(commit.root, parts, "blob");
|
||||
if (res !== currObjectID) {
|
||||
history.push({
|
||||
id: res,
|
||||
commit,
|
||||
});
|
||||
}
|
||||
currObjectID = res;
|
||||
currCommit = commit.before;
|
||||
} catch (err) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
|
||||
|
||||
async delete(path: string) {
|
||||
return this.write(path, undefined);
|
||||
}
|
||||
|
||||
async write(path: string, data: Uint8Array | undefined) {
|
||||
const parts = this.splitPath(path);
|
||||
|
||||
let objectID: string | undefined = undefined;
|
||||
if (data) {
|
||||
objectID = await this.writeObject(data);
|
||||
}
|
||||
|
||||
const lock = await this.#store.getLock();
|
||||
//TODO: Improve need of locking.
|
||||
try {
|
||||
const head = await this.readHead();
|
||||
|
||||
const makeTree = async (treeID: string | undefined, parts: string[]) => {
|
||||
let tree: TreeEntry[];
|
||||
if (treeID) {
|
||||
tree = await this.readTree(treeID);
|
||||
} else {
|
||||
tree = [];
|
||||
}
|
||||
|
||||
const current = parts[0];
|
||||
|
||||
let existing = tree.findIndex(([, , name]) => name == current);
|
||||
|
||||
let entry: TreeEntry | undefined = undefined;
|
||||
|
||||
if (parts.length == 1) {
|
||||
if (objectID) {
|
||||
entry = ["blob", objectID, current];
|
||||
}
|
||||
} else {
|
||||
let newTreeID = await makeTree(existing >= 0 ? tree[existing][1] : undefined, parts.slice(1));
|
||||
if (newTreeID) {
|
||||
entry = [
|
||||
"tree",
|
||||
newTreeID,
|
||||
current,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (existing >= 0) {
|
||||
let ex = tree[existing];
|
||||
if (parts.length == 1 && ex[0] == "tree")
|
||||
throw new Error("This change would overwrite a folder!");
|
||||
if (entry)
|
||||
tree[existing] = entry;
|
||||
else {
|
||||
tree = [...tree.slice(0, existing), ...tree.slice(existing + 1)];
|
||||
}
|
||||
} else {
|
||||
if (entry)
|
||||
tree.push(entry);
|
||||
}
|
||||
|
||||
if (tree.length > 0) {
|
||||
let treeString = tree.map(([type, hash, name]) => `${type} ${hash} ${name}`).join("\n");
|
||||
|
||||
let newTreeID = await this.writeObject(treeString);
|
||||
|
||||
return newTreeID;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
let newTree = await makeTree(head?.root, parts);
|
||||
if (!newTree) { //TODO: Is this what i want?
|
||||
newTree = await this.writeObject("");
|
||||
}
|
||||
let commit = await this.makeCommit(newTree, head);
|
||||
await this.writeHead(commit);
|
||||
} finally {
|
||||
await lock();
|
||||
}
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (this.#store.close) return this.#store?.close();
|
||||
}
|
||||
|
||||
//#endregion
|
||||
}
|
||||
|
Reference in New Issue
Block a user