-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Checkpoints logging tweaks + defensive catches #945
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68ea2e8
Checkpoints logging tweaks
cte 6b35187
Add duration logging
cte 37c2a17
Check for empty stash commits
cte d896078
Store initial checkpoint
cte be2e0b7
Merge pull request #946 from RooVetGit/cte/store-initial-checkpoint
cte d9ac8ef
Complete the comment
cte 94cb09f
Log restoreCheckpoint duration
cte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,22 +132,64 @@ export class CheckpointService { | |
| stashSha: string | ||
| force?: boolean | ||
| }) { | ||
| if (force) { | ||
| await this.git.checkout(["-f", this.mainBranch]) | ||
| } else { | ||
| await this.git.checkout(this.mainBranch) | ||
| let currentBranch = await this.git.revparse(["--abbrev-ref", "HEAD"]) | ||
|
|
||
| if (currentBranch !== this.mainBranch) { | ||
| if (force) { | ||
| try { | ||
| await this.git.checkout(["-f", this.mainBranch]) | ||
| } catch (err) { | ||
| this.log( | ||
| `[restoreMain] failed to force checkout ${this.mainBranch}: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| } | ||
| } else { | ||
| try { | ||
| await this.git.checkout(this.mainBranch) | ||
| } catch (err) { | ||
| this.log( | ||
| `[restoreMain] failed to checkout ${this.mainBranch}: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
|
|
||
| // Escalate to a forced checkout if we can't checkout the | ||
| // main branch under normal circumstances. | ||
| currentBranch = await this.git.revparse(["--abbrev-ref", "HEAD"]) | ||
|
|
||
| if (currentBranch !== this.mainBranch) { | ||
| await this.git.checkout(["-f", this.mainBranch]).catch(() => {}) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| currentBranch = await this.git.revparse(["--abbrev-ref", "HEAD"]) | ||
|
|
||
| if (currentBranch !== this.mainBranch) { | ||
| throw new Error(`Unable to restore ${this.mainBranch}`) | ||
| } | ||
|
|
||
| if (stashSha) { | ||
| this.log(`[restoreMain] applying stash ${stashSha}`) | ||
| await this.git.raw(["stash", "apply", "--index", stashSha]) | ||
|
|
||
| try { | ||
| await this.git.raw(["stash", "apply", "--index", stashSha]) | ||
| } catch (err) { | ||
| this.log(`[restoreMain] Failed to apply stash: ${err instanceof Error ? err.message : String(err)}`) | ||
| } | ||
| } | ||
|
|
||
| this.log(`[restoreMain] restoring from ${branch}`) | ||
| await this.git.raw(["restore", "--source", branch, "--worktree", "--", "."]) | ||
| this.log(`[restoreMain] restoring from ${branch} branch`) | ||
|
|
||
| try { | ||
| await this.git.raw(["restore", "--source", branch, "--worktree", "--", "."]) | ||
| } catch (err) { | ||
| this.log(`[restoreMain] Failed to restore branch: ${err instanceof Error ? err.message : String(err)}`) | ||
| } | ||
| } | ||
|
|
||
| public async saveCheckpoint(message: string) { | ||
| const startTime = Date.now() | ||
|
|
||
| await this.ensureBranch(this.mainBranch) | ||
|
|
||
| const stashSha = (await this.git.raw(["stash", "create"])).trim() | ||
|
|
@@ -172,15 +214,13 @@ export class CheckpointService { | |
| */ | ||
| try { | ||
| await this.git.add(["-A"]) | ||
| const status = await this.git.status() | ||
| this.log(`[saveCheckpoint] status: ${JSON.stringify(status)}`) | ||
| } catch (err) { | ||
| await this.git.checkout(["-f", this.mainBranch]) | ||
| await this.git.branch(["-D", stashBranch]).catch(() => {}) | ||
|
|
||
| throw new Error( | ||
| `[saveCheckpoint] Failed in stage stash phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| this.log( | ||
| `[saveCheckpoint] failed in stage stash phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| await this.restoreMain({ branch: stashBranch, stashSha, force: true }) | ||
| await this.git.branch(["-D", stashBranch]).catch(() => {}) | ||
| throw err | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -192,17 +232,25 @@ export class CheckpointService { | |
| * - UNDO: Create branch | ||
| * - UNDO: Change branch | ||
| */ | ||
| let stashCommit | ||
|
|
||
| try { | ||
| // TODO: Add a test to see if empty commits break this. | ||
| const tempCommit = await this.git.commit(message, undefined, { "--no-verify": null }) | ||
| this.log(`[saveCheckpoint] tempCommit: ${message} -> ${JSON.stringify(tempCommit)}`) | ||
| stashCommit = await this.git.commit(message, undefined, { "--no-verify": null }) | ||
| this.log(`[saveCheckpoint] stashCommit: ${message} -> ${JSON.stringify(stashCommit)}`) | ||
| } catch (err) { | ||
| await this.git.checkout(["-f", this.mainBranch]) | ||
| this.log( | ||
| `[saveCheckpoint] failed in stash commit phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| await this.restoreMain({ branch: stashBranch, stashSha, force: true }) | ||
| await this.git.branch(["-D", stashBranch]).catch(() => {}) | ||
| throw err | ||
| } | ||
|
|
||
| throw new Error( | ||
| `[saveCheckpoint] Failed in stash commit phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| if (!stashCommit) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't generate a stash commit then there are no changes to store in a checkpoint. This has handled by the diff step below, but we can short circuit that. |
||
| this.log("[saveCheckpoint] no stash commit") | ||
| await this.restoreMain({ branch: stashBranch, stashSha }) | ||
| await this.git.branch(["-D", stashBranch]) | ||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -219,12 +267,10 @@ export class CheckpointService { | |
| try { | ||
| diff = await this.git.diff([latestSha, stashBranch]) | ||
| } catch (err) { | ||
| this.log(`[saveCheckpoint] failed in diff phase: ${err instanceof Error ? err.message : String(err)}`) | ||
| await this.restoreMain({ branch: stashBranch, stashSha, force: true }) | ||
| await this.git.branch(["-D", stashBranch]).catch(() => {}) | ||
|
|
||
| throw new Error( | ||
| `[saveCheckpoint] Failed in diff phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| throw err | ||
| } | ||
|
|
||
| if (!diff) { | ||
|
|
@@ -249,12 +295,10 @@ export class CheckpointService { | |
| await this.git.reset(["--hard", this.mainBranch]) | ||
| this.log(`[saveCheckpoint] reset ${this.hiddenBranch}`) | ||
| } catch (err) { | ||
| this.log(`[saveCheckpoint] failed in reset phase: ${err instanceof Error ? err.message : String(err)}`) | ||
| await this.restoreMain({ branch: stashBranch, stashSha, force: true }) | ||
| await this.git.branch(["-D", stashBranch]).catch(() => {}) | ||
|
|
||
| throw new Error( | ||
| `[saveCheckpoint] Failed in reset phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| throw err | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -289,25 +333,33 @@ export class CheckpointService { | |
| this.currentCheckpoint = commit | ||
| this.log(`[saveCheckpoint] cherry-pick commit = ${commit}`) | ||
| } catch (err) { | ||
| this.log( | ||
| `[saveCheckpoint] failed in cherry pick phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| await this.git.reset(["--hard", latestSha]).catch(() => {}) | ||
| await this.restoreMain({ branch: stashBranch, stashSha, force: true }) | ||
| await this.git.branch(["-D", stashBranch]).catch(() => {}) | ||
|
|
||
| throw new Error( | ||
| `[saveCheckpoint] Failed in cherry pick phase: ${err instanceof Error ? err.message : String(err)}`, | ||
| ) | ||
| throw err | ||
| } | ||
|
|
||
| await this.restoreMain({ branch: stashBranch, stashSha }) | ||
| await this.git.branch(["-D", stashBranch]) | ||
|
|
||
| // We've gotten reports that checkpoints can be slow in some cases, so | ||
| // we'll log the duration of the checkpoint save. | ||
| const duration = Date.now() - startTime | ||
| this.log(`[saveCheckpoint] saved checkpoint ${commit} in ${duration}ms`) | ||
|
|
||
| return { commit } | ||
| } | ||
|
|
||
| public async restoreCheckpoint(commitHash: string) { | ||
| const startTime = Date.now() | ||
| await this.ensureBranch(this.mainBranch) | ||
| await this.git.clean([CleanOptions.FORCE, CleanOptions.RECURSIVE]) | ||
| await this.git.raw(["restore", "--source", commitHash, "--worktree", "--", "."]) | ||
| const duration = Date.now() - startTime | ||
| this.log(`[restoreCheckpoint] restored checkpoint ${commitHash} in ${duration}ms`) | ||
| this.currentCheckpoint = commitHash | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logging was too verbose.