Skip to content

Commit

Permalink
Merge pull request #189 from dotindustries/11-08-feat_advance_remote_…
Browse files Browse the repository at this point in the history
…state

feat: advance remote state on push
  • Loading branch information
nadilas authored Nov 8, 2024
2 parents be464f1 + 82bf1f8 commit 4c84f6f
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 4c84f6f

Please sign in to comment.