Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: advance remote state on push #189

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +216 to +222
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and type safety.

The push implementation could be enhanced with better error handling and type safety.

Consider these improvements:

-  async push(pushToBackendFn: () => Promise<boolean>): Promise<boolean> {
-    const success = await pushToBackendFn()
-    if (success) {
-      this.storeRemoteState(this.getHistory())
-    }
-    return success
+  async push(pushToBackendFn: () => Promise<boolean>): Promise<boolean> {
+    try {
+      const success: boolean = await pushToBackendFn()
+      if (success) {
+        this.storeRemoteState(this.getHistory())
+      }
+      return success
+    } catch (error) {
+      // Preserve the original error stack
+      throw error instanceof Error 
+        ? error 
+        : new Error('Push to backend failed: ' + String(error))
+    }
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async push(pushToBackendFn: () => Promise<boolean>): Promise<boolean> {
const success = await pushToBackendFn()
if (success) {
this.storeRemoteState(this.getHistory())
}
return success
}
async push(pushToBackendFn: () => Promise<boolean>): Promise<boolean> {
try {
const success: boolean = await pushToBackendFn()
if (success) {
this.storeRemoteState(this.getHistory())
}
return success
} catch (error) {
// Preserve the original error stack
throw error instanceof Error
? error
: new Error('Push to backend failed: ' + String(error))
}
}


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