From ce5f4221e393d59d103553514e8be9ed7aeb1211 Mon Sep 17 00:00:00 2001 From: Cheng Liu Date: Wed, 29 May 2024 22:26:46 -0700 Subject: [PATCH 1/2] feat: support checkout - --- apps/sparo-lib/src/cli/commands/checkout.ts | 10 +++++----- apps/sparo-lib/src/services/GitService.ts | 18 ++++++++++++++++++ common/reviews/api/sparo-lib.api.md | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/apps/sparo-lib/src/cli/commands/checkout.ts b/apps/sparo-lib/src/cli/commands/checkout.ts index 4d50677..6a87d75 100644 --- a/apps/sparo-lib/src/cli/commands/checkout.ts +++ b/apps/sparo-lib/src/cli/commands/checkout.ts @@ -122,11 +122,11 @@ export class CheckoutCommand implements ICommand { if (!branch) { const checkoutIndex: number = process.argv.findIndex((value: string) => value === 'checkout'); if (checkoutIndex >= 0 && process.argv[checkoutIndex + 1] === '-') { - branch = '-'; - // FIXME: supports "sparo checkout -" - throw new Error( - `Git's "-" token is not yet supported. If this feature is important for your work, please let us know by creating a GitHub issue.` - ); + // - is a shortcut of @{-1} + branch = gitService.getPreviousBranch(1); + if (!branch) { + throw new Error(`Argument "-" is unknown revision or path not in the working tree.`); + } } } diff --git a/apps/sparo-lib/src/services/GitService.ts b/apps/sparo-lib/src/services/GitService.ts index bf9e242..f7a52c0 100644 --- a/apps/sparo-lib/src/services/GitService.ts +++ b/apps/sparo-lib/src/services/GitService.ts @@ -445,6 +445,24 @@ Please specify a directory on the command line return currentBranch; } + /** + * Retrieves the previous branch name using `@{-n}` syntax + * + * Assume: + * git checkout feature + * git checkout main + * --- + * `git checkout @{-1}` equals to run `git checkout feature`. + * Running `getPreviousBranch(1)` works in the similar way and returns "feature" in this case. + */ + public getPreviousBranch(n: number): string { + const result: string = this.executeGitCommandAndCaptureOutput({ + args: ['rev-parse', '--symbolic-full-name', '--abbrev-ref=loose', `@{-${n}}`] + }).trim(); + this._terminalService.terminal.writeDebugLine(`getPreviousBranch ${n}: ${result}`); + return result; + } + /** * Check existence for a list of branch name */ diff --git a/common/reviews/api/sparo-lib.api.md b/common/reviews/api/sparo-lib.api.md index 7a28d3c..18ee7e6 100644 --- a/common/reviews/api/sparo-lib.api.md +++ b/common/reviews/api/sparo-lib.api.md @@ -46,6 +46,7 @@ export class GitService { getIsSparseCheckoutMode(): boolean | undefined; // (undocumented) getObjectType(object: string): IObjectType | undefined; + getPreviousBranch(n: number): string; // (undocumented) getRepoInfo(): GitRepoInfo; get gitPath(): string | undefined; From 7ea9bdcdde23c4b390e92a78c868f9ea6835ee79 Mon Sep 17 00:00:00 2001 From: Cheng Liu Date: Wed, 29 May 2024 22:28:41 -0700 Subject: [PATCH 2/2] chore: rush change --- .../sparo/feat-checkout-dash_2024-05-30-05-28.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/sparo/feat-checkout-dash_2024-05-30-05-28.json diff --git a/common/changes/sparo/feat-checkout-dash_2024-05-30-05-28.json b/common/changes/sparo/feat-checkout-dash_2024-05-30-05-28.json new file mode 100644 index 0000000..1e87e08 --- /dev/null +++ b/common/changes/sparo/feat-checkout-dash_2024-05-30-05-28.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "sparo", + "comment": "\"sparo checkout -\" can checkout to previous branch correctly", + "type": "none" + } + ], + "packageName": "sparo" +} \ No newline at end of file