From 82bf1f8d1e4eea36bf26a6b521bc730475d73590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Fri, 8 Nov 2024 16:57:07 +0100 Subject: [PATCH] feat/advance_remote_state --- packages/ogre/src/repository.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/ogre/src/repository.ts b/packages/ogre/src/repository.ts index 47b502d..7d21079 100644 --- a/packages/ogre/src/repository.ts +++ b/packages/ogre/src/repository.ts @@ -107,6 +107,11 @@ export interface RepositoryObject { * Cherry returns the commits that are missing from upstream and the refs that have been moved since remote */ cherry(): { commits: Array; refs: Map }; + + /** + * Runs an arbitrary backend push command and after success advances the locally stored remote state + */ + push(pushToBackendFn: () => Promise): Promise } /** @@ -119,12 +124,7 @@ export class Repository 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( - 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; @@ -159,6 +159,15 @@ export class Repository }); } + private storeRemoteState(history: History) { + // FIXME: move this to refs/remote as git would do? + this.remoteRefs = immutableMapCopy(history.refs); + this.remoteCommits = immutableArrayCopy( + history.commits, + (c) => c.hash, + ); + } + private readonly original: T; private _isReady = false; @@ -192,18 +201,26 @@ export class Repository | undefined; // stores the remote state upon initialization - private readonly remoteRefs: + private remoteRefs: | ReadonlyMap> | undefined; // stores the remote state upon initialization - private readonly remoteCommits: ReadonlyArray> | undefined; + private remoteCommits: ReadonlyArray> | undefined; private observer: Observer; private readonly refs: Map; private readonly commits: Array; + async push(pushToBackendFn: () => Promise): Promise { + const success = await pushToBackendFn() + if (success) { + this.storeRemoteState(this.getHistory()) + } + return success + } + cherry(): { commits: Array; refs: Map } { const commits: Array = []; const refs = new Map();