VSCODE扩展名:调用带有新分支名称的git.branch,避免输入框

发布于 2025-02-08 22:28:32 字数 826 浏览 2 评论 0 原文

我正在尝试写一个小助手扩展,以简化特征分支的创建。我已经有了最近活跃的工作项目和一个按钮,可以从特定项目中创建功能分支。 该名称已经正确格式化了,但是当功能已内置在VSCODE中时,我不想对分支创建逻辑负责。

我的第一个尝试是简单地调用 executecommand 并传递命令并请求的分支名称:

vscode.commands.executeCommand("git.branch", branchName);

可悲的是 branchName 参数被简单地被忽略,并且文本输入出现一样通过命令调色板执行命令时。简单的解决方案是简单地偷窃借用分支创建逻辑或进行CLI调用,但我很固执,并希望在可能的情况下避免这样做。

我几乎可以在任何地方找到有关此主题的任何信息。

https://code.visalstudio.com/api/api/api/references/references/commands/commands git命令

涵盖了通过URIS传递给命令的参数,但这对我也不起作用,

我将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2025-02-15 22:28:32

您可以通过使用GIT扩展曝光的API避免打开输入箱,请参见 git扩展API

git扩展揭示了API,可通过任何其他扩展。

复制 src/api/git.d.t.ts to延长源;

在扩展的汇编中包括 git.d.t.ts

使用以下片段掌握API:

const gitExtension = vscode.extensions.getExtension('vscode.git').exports;
const git = gitExtension.getAPI(1);

以上是来自 @builtin git扩展描述的扩展描述。

因此您可以使用:

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

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
一梦等七年七年为一梦 2025-02-15 22:28:32

似乎 git.branch 命令不支持参数,您可以在此处看到

https://github.com/microsoft/vscode/blob/10e6e876822fa2da15fb68e8a6f1fb9aa8d94ce2/extensions/git/src/commands.ts#L1884-L1887

In在这种情况下,我建议您在VS代码回购中打开一个问题,并要求向VS代码团队扩展命令,以支持这种情况。

他们最近添加了随机分支名称功能,所以我想这样的请求将受到好评。

希望这会有所帮助

It seems the git.branch command does not support parameters, as you can see here

https://github.com/microsoft/vscode/blob/10e6e876822fa2da15fb68e8a6f1fb9aa8d94ce2/extensions/git/src/commands.ts#L1884-L1887

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文