Skip to content

Commit

Permalink
feat/advance_remote_state
Browse files Browse the repository at this point in the history
  • Loading branch information
nadilas committed Nov 8, 2024
1 parent be464f1 commit 82bf1f8
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/ogre/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export interface RepositoryObject<T extends { [k: string]: any }> {
* Cherry returns the commits that are missing from upstream and the refs that have been moved since remote
*/
cherry(): { commits: Array<Commit>; refs: Map<string, Reference> };

/**
* Runs an arbitrary backend push command and after success advances the locally stored remote state
*/
push(pushToBackendFn: () => Promise<boolean>): Promise<boolean>
}

/**
Expand All @@ -119,12 +124,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
this.hashFn = options.overrides?.calculateCommitHashFn;
this.serializeObjectFn = options.overrides?.serializeObjectFn;
this.deserializeObjectFn = options.overrides?.deserializeObjectFn;
// FIXME: move this to refs/remote as git would do?
this.remoteRefs = immutableMapCopy(options.history?.refs);
this.remoteCommits = immutableArrayCopy<Commit, string>(
options.history?.commits,
(c) => c.hash,
);
options.history && this.storeRemoteState(options.history);
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 @@ -159,6 +159,15 @@ export class Repository<T extends { [k: PropertyKey]: any }>
});
}

private storeRemoteState(history: History) {
// FIXME: move this to refs/remote as git would do?
this.remoteRefs = immutableMapCopy(history.refs);
this.remoteCommits = immutableArrayCopy<Commit, string>(
history.commits,
(c) => c.hash,
);
}

private readonly original: T;
private _isReady = false;

Expand Down Expand Up @@ -192,18 +201,26 @@ export class Repository<T extends { [k: PropertyKey]: any }>
| undefined;

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

// stores the remote state upon initialization
private readonly remoteCommits: ReadonlyArray<Readonly<string>> | undefined;
private remoteCommits: ReadonlyArray<Readonly<string>> | undefined;

private observer: Observer<T>;

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

async push(pushToBackendFn: () => Promise<boolean>): Promise<boolean> {
const success = await pushToBackendFn()
if (success) {
this.storeRemoteState(this.getHistory())
}
return success
}

cherry(): { commits: Array<Commit>; refs: Map<string, Reference> } {
const commits: Array<Commit> = [];
const refs = new Map<string, Reference>();
Expand Down

0 comments on commit 82bf1f8

Please sign in to comment.