From cc744382aea28aa48cad40198c4bf155aedf2216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Sat, 23 Mar 2024 13:20:07 +0100 Subject: [PATCH] feat: allow access to remote state --- packages/ogre/src/commit.test.ts | 18 +++++++++++++++++- packages/ogre/src/commit.ts | 14 +++++++------- packages/ogre/src/repository.ts | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/ogre/src/commit.test.ts b/packages/ogre/src/commit.test.ts index 9c07de4..a7b9225 100644 --- a/packages/ogre/src/commit.test.ts +++ b/packages/ogre/src/commit.test.ts @@ -2,12 +2,13 @@ import { test } from "tap"; import { addOneStep, + ComplexObject, getBaseline, sumChanges, testAuthor, updateHeaderData, } from "./test.utils"; -import { printChangeLog } from "./repository"; +import { printChangeLog, Repository } from "./repository"; test("baseline with 1 commit and zero changelog entries", async (t) => { const [repo] = await getBaseline(); @@ -23,6 +24,21 @@ test("head points to main", async (t) => { t.equal(repo.head(), "refs/heads/main", "head not pointing where it should"); }); +test("changes are available for commit if starting from empty", async (t) => { + const repo = new Repository({}, {}); + repo.data.name = "some data"; + + const dirty = repo.status(); + + t.equal( + dirty.length, + 1, + "Status does not contain the right amount of changes", + ); + await repo.commit("baseline", testAuthor); + t.pass(); +}); + test("no commit without changes", async (t) => { const [repo] = await getBaseline(); diff --git a/packages/ogre/src/commit.ts b/packages/ogre/src/commit.ts index 58e0afe..2ed3c58 100644 --- a/packages/ogre/src/commit.ts +++ b/packages/ogre/src/commit.ts @@ -2,13 +2,13 @@ import { digest } from "./hash"; import { Operation } from "fast-json-patch"; export interface Commit { - // The hash of the commit - // Is an sha256 of: - // - tree object reference (changes?) - // - parent object reference (parent hash) - // - author - // - author commit timestamp with timezone - // - commit message + /*The hash of the commit. Is an sha256 of: + - tree object reference (changes?) + - parent object reference (parent hash) + - author + - author commit timestamp with timezone + - commit message + */ hash: string; tree: string; diff --git a/packages/ogre/src/repository.ts b/packages/ogre/src/repository.ts index 5c55d0e..9187f4a 100644 --- a/packages/ogre/src/repository.ts +++ b/packages/ogre/src/repository.ts @@ -80,6 +80,11 @@ export interface RepositoryObject { * @param shaish */ reset(mode?: "soft" | "hard", shaish?: string): void; + + /** + * Returns the remote references from the initialization of the repository + */ + remote(): Map | undefined; } /** @@ -89,6 +94,8 @@ export class Repository implements RepositoryObject { constructor(obj: Partial, options: RepositoryOptions) { + // FIXME: move this to refs/remote as git would do? + this.remoteRefs = options.history?.refs; this.original = deepClone(obj); // store js ref, so obj can still be modified without going through repo.data this.data = obj as T; @@ -120,11 +127,18 @@ export class Repository data: T; + // stores the remote state upon initialization + private readonly remoteRefs: Map | undefined; + private observer: Observer; private readonly refs: Map; private readonly commits: Commit[]; + remote(): Map | undefined { + return this.remoteRefs; + } + private moveTo(commit: Commit) { const targetTree = treeToObject(commit.tree); const patchToTarget = compare(this.data, targetTree);