mirror of
https://github.com/tiktok/sparo.git
synced 2024-11-14 19:35:12 -05:00
Merge pull request #75 from tiktok/feat-checkout-dash
Supports "sparo checkout -"
This commit is contained in:
commit
a045b7f2a1
4 changed files with 34 additions and 5 deletions
|
@ -122,11 +122,11 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
|
|||
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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"changes": [
|
||||
{
|
||||
"packageName": "sparo",
|
||||
"comment": "\"sparo checkout -\" can checkout to previous branch correctly",
|
||||
"type": "none"
|
||||
}
|
||||
],
|
||||
"packageName": "sparo"
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue