I am trying to write a little helper extension to ease the creation of feature branches. I already have a view of recently active work items and a button to create a feature branch from a specific item. The name is already being formatted correctly, but I do not want to be responsible for the branch creation logic when the functionality is already built into vscode.
My first attempt was to simply call executeCommand
and pass in the command and requested branch name like this:
vscode.commands.executeCommand("git.branch", branchName);
Sadly the branchName
parameter is simply being ignored and a text input appears just as it would when executing the command via the command palette. The easy solution would be to simply steal borrow the branch creation logic or make a cli call, but I am stubborn and would like to avoid doing that if possible.
I can barely find any information regarding this topic anywhere.
https://code.visualstudio.com/api/references/commands does not cover git commands
https://code.visualstudio.com/api/extension-guides/command#command-uris covers passing parameters to commands via uris, but this too does not work for me
I would appreciate any leads.
CodePudding user response:
You can avoid the InputBox opening by using the API that the git extension exposes, see git extension api.
The Git extension exposes an API, reachable by any other extension.
Copy
src/api/git.d.ts
to your extension's sources;Include
git.d.ts
in your extension's compilation.Get a hold of the API with the following snippet:
const gitExtension = vscode.extensions.getExtension('vscode.git').exports;
const git = gitExtension.getAPI(1);
The above is from the extension description for the @builtin
git extension description.
So you could use:
const branchName = 'test';
try {
const extension = vscode.extensions.getExtension('vscode.git');
if (extension !== undefined) {
const gitExtension = extension.isActive ? extension.exports : await extension.activate();
const gitAPI = gitExtension.getAPI(1);
const activeEditorUri = vscode.window.activeTextEditor.document.uri;
const wsFolderUri = vscode.workspace.getWorkspaceFolder(activeEditorUri).uri;
const theRepository = gitAPI.getRepository(wsFolderUri);
await theRepository.createBranch('test', true);
}
} catch (error) {} // log error
CodePudding user response:
It seems the git.branch
command does not support parameters, as you can see here
In this case, I suggest you should to open an issue in VS Code repo and ask to the VS Code team to expand the command, to support such scenario.
They recently added a random branch name feature, so I suppose such request would be well received.
Hope this helps