Skip to content

Commit

Permalink
Added gitCheckout command
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Jan 3, 2025
1 parent 46ea26a commit 9a855b7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Many of the commands take arguments and return values that can only be used with

### Git commands

- `andreas.gitCheckout(branch: string)`
Checkout git branch.
- `andreas.gitCheckoutDefaultBranch()`
Checkout default git branch.
- `andreas.getGitFileURL({ useSelection: boolean, useBranch: boolean }): string`
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "andreas-talon",
"displayName": "Andreas Talon",
"description": "VSCode extension used by Talon Voice",
"version": "3.57.0",
"version": "3.58.0",
"publisher": "AndreasArvidsson",
"license": "MIT",
"main": "./out/extension.js",
Expand Down Expand Up @@ -229,6 +229,11 @@
"title": "Get name for open tag.",
"enablement": "false"
},
{
"command": "andreas.gitCheckout",
"category": "Andreas",
"title": "Checkout git branch."
},
{
"command": "andreas.gitCheckoutDefaultBranch",
"category": "Andreas",
Expand Down
19 changes: 14 additions & 5 deletions src/commands/GitUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class GitUtil {
this.gitApi = gitExtension.getAPI(1);
}

getGitFileURL({ useSelection = false, useBranch = false }: GitParameters): string {
getFileURL({ useSelection = false, useBranch = false }: GitParameters): string {
const { document, selections } = getActiveFileSchemaEditor();
const repository = this.getRepository();
const platform = getPlatform(repository);
Expand All @@ -35,26 +35,35 @@ export class GitUtil {
return platform.getFileUrl(commitOrBranch, relativeFilePath, range);
}

getGitRepoURL(): string {
getRepoURL(): string {
const repository = this.getRepository();
return getPlatform(repository).getRepoUrl();
}

getGitIssuesURL(): string {
getIssuesURL(): string {
const repository = this.getRepository();
return getPlatform(repository).getIssuesUrl();
}

getGitNewIssueURL(): string {
getNewIssueURL(): string {
const repository = this.getRepository();
return getPlatform(repository).getNewIssueUrl();
}

getGitPullRequestsURL(): string {
getPullRequestsURL(): string {
const repository = this.getRepository();
return getPlatform(repository).getPullRequestsURL();
}

async checkout(branch: string) {
const repository = this.getRepository();
try {
await repository.checkout(branch);
} catch (_error) {
throw Error(`Can't checkout branch '${branch}'`);
}
}

async checkoutDefaultBranch() {
const repository = this.getRepository();
const branches = await repository.getBranches({});
Expand Down
1 change: 1 addition & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const commandDescriptions = {
),

// Git commands
gitCheckout: visible("Git", "Checkout git branch.", undefined, "(branch: string)"),
gitCheckoutDefaultBranch: visible("Git", "Checkout default git branch."),
getGitFileURL: hidden(
"Git",
Expand Down
22 changes: 7 additions & 15 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ export function registerCommands(
getClassName: () => getText.getClassName(),
getOpenTagName: () => getText.getOpenTagName(),
// Git
gitCheckout: (branch: string) => git.checkout(branch),
gitCheckoutDefaultBranch: () => git.checkoutDefaultBranch(),
getGitFileURL: (p: GitParameters) => git.getGitFileURL(p),
getGitRepoURL: () => git.getGitRepoURL(),
getGitIssuesURL: () => git.getGitIssuesURL(),
getGitNewIssueURL: () => git.getGitNewIssueURL(),
getGitPullRequestsURL: () => git.getGitPullRequestsURL(),
getGitFileURL: (p: GitParameters) => git.getFileURL(p),
getGitRepoURL: () => git.getRepoURL(),
getGitIssuesURL: () => git.getIssuesURL(),
getGitNewIssueURL: () => git.getNewIssueURL(),
getGitPullRequestsURL: () => git.getPullRequestsURL(),
// Other
getSetting,
setSetting,
Expand All @@ -86,14 +87,5 @@ export function registerCommands(
function registerCommand(command: CommandId, callback: Callback): vscode.Disposable {
const fullCommand = getFullCommand(command);

return vscode.commands.registerCommand(fullCommand, async (...args: unknown[]) => {
try {
return await Promise.resolve<unknown>(callback(...args));
} catch (ex) {
const err = ex as Error;
void vscode.window.showErrorMessage(err.message);
console.error(err.stack);
return undefined;
}
});
return vscode.commands.registerCommand(fullCommand, callback);
}

0 comments on commit 9a855b7

Please sign in to comment.