Skip to content

Commit

Permalink
feat: allow access to remote state (#158)
Browse files Browse the repository at this point in the history
### TL;DR
This pull request introduces changes and enhancements in the Repository class in 'repository.ts' and corresponding tests in 'commit.test.ts'. It also includes updates to the interface description in 'commit.ts'.

### What changed?
The Repository class now has a new member 'remoteRefs' and a respective accessor function 'remote'. The member keeps track of the state of the remote during initialization. The new test checks for the status and commit operations on a Repository instance.

In 'commit.ts', block comments format has been introduced in place of line comments for commit hash details.

### How to test?
Reviewers can run the included test in 'commit.test.ts' to verify the new changes functionality. Check updated commenting style in 'commit.ts'.

### Why make this change?
The changes enhance the functionality of the Repository class and improve code readability in 'commit.ts' with proper block comments.

---
  • Loading branch information
nadilas authored Mar 23, 2024
2 parents ea23900 + e2d03f2 commit 96d0cf6
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 96d0cf6

Please sign in to comment.