Skip to content

Commit

Permalink
feat: allow access to remote state
Browse files Browse the repository at this point in the history
  • Loading branch information
nadilas committed Mar 23, 2024
1 parent d0cf2b6 commit cc74438
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
18 changes: 17 additions & 1 deletion packages/ogre/src/commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<ComplexObject>({}, {});
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();

Expand Down
14 changes: 7 additions & 7 deletions packages/ogre/src/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions packages/ogre/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export interface RepositoryObject<T extends { [k: string]: any }> {
* @param shaish
*/
reset(mode?: "soft" | "hard", shaish?: string): void;

/**
* Returns the remote references from the initialization of the repository
*/
remote(): Map<string, Reference> | undefined;
}

/**
Expand All @@ -89,6 +94,8 @@ export class Repository<T extends { [k: PropertyKey]: any }>
implements RepositoryObject<T>
{
constructor(obj: Partial<T>, options: RepositoryOptions<T>) {
// 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;
Expand Down Expand Up @@ -120,11 +127,18 @@ export class Repository<T extends { [k: PropertyKey]: any }>

data: T;

// stores the remote state upon initialization
private readonly remoteRefs: Map<string, Reference> | undefined;

private observer: Observer<T>;

private readonly refs: Map<string, Reference>;
private readonly commits: Commit[];

remote(): Map<string, Reference> | undefined {
return this.remoteRefs;
}

private moveTo(commit: Commit) {
const targetTree = treeToObject(commit.tree);
const patchToTarget = compare(this.data, targetTree);
Expand Down

0 comments on commit cc74438

Please sign in to comment.