VS Code Git扩展API[英] VS Code Git Extension API

本文是小编为大家收集整理的关于VS Code Git扩展API的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否可以在某个地方获得有关如何创建使用Git扩展API的扩展名的更多文档?

at https://github.com/microsoft/VSCODE/BLOB/MASTER/EXTENSIONS/git/readme.md Microsoft提供的唯一文档是:

Visual Studio代码的GIT集成

注意:此扩展名与Visual Studio代码捆绑在一起.它可以被禁用但不能卸载.

功能

请参阅 vs code中的git支持这个扩展.

API

git扩展揭示了一个API,可通过任何其他扩展.

  1. 复制src/api/git.d.ts与扩展的来源;
  2. 包括git.d.ts在您的扩展程序中.
  3. 使用以下片段掌握API:

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

它确实无济于事,当我尝试使用这2行时,扩展程序运行时,但是如果我尝试检查,例如,git.repositories [0],它将返回未定义. IDK如果我做错了什么? :(

推荐答案

您可以查看 eamodio/vscode-gitlens ,基于主要扩展在git扩展上.

Its src/git/gitService.ts确实调用git扩展名:

static async getBuiltInGitApi(): Promise<BuiltInGitApi | undefined> {
    try {
        const extension = extensions.getExtension('vscode.git') as Extension<GitExtension>;
        if (extension !== undefined) {
            const gitExtension = extension.isActive ? extension.exports : await extension.activate();

            return gitExtension.getAPI(1);
        }
    } catch {}

    return undefined;
}

其他推荐答案

进一步补充现有答案,对于那些不在打字稿中编码而是RAW JavaScript的人:

A call to gitExtension.getAPI(1) will give you an instance of API 仅对其.repositories成员有用.该成员是本文地址:https://www.itbaoku.cn/post/1937830.html

问题描述

Is there somewhere one can obtain more documentation on how to create an extension that uses the Git Extension API?

At https://github.com/microsoft/vscode/blob/master/extensions/git/README.md the only documentation Microsoft provides is this:

Git integration for Visual Studio Code

Notice: This extension is bundled with Visual Studio Code. It can be disabled but not uninstalled.

Features

See Git support in VS Code to learn about the features of this extension.

API

The Git extension exposes an API, reachable by any other extension.

  1. Copy src/api/git.d.ts to your extension's sources;
  2. Include git.d.ts in your extension's compilation.
  3. Get a hold of the API with the following snippet:

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

It really doesn't help and when I try to use those 2 lines the extension runs but if I try to check, for example, git.repositories[0] it returns undefined. Idk if I'm doing something wrong? :(

推荐答案

You can have a look at eamodio/vscode-gitlens, the main extension based on Git extension.

Its src/git/gitService.ts does call the GIt extension:

static async getBuiltInGitApi(): Promise<BuiltInGitApi | undefined> {
    try {
        const extension = extensions.getExtension('vscode.git') as Extension<GitExtension>;
        if (extension !== undefined) {
            const gitExtension = extension.isActive ? extension.exports : await extension.activate();

            return gitExtension.getAPI(1);
        }
    } catch {}

    return undefined;
}

其他推荐答案

To further complement the existing answer, and for those who do not code in TypeScript but raw JavaScript:

A call to gitExtension.getAPI(1) will give you an instance of API that is only useful for its .repositories member. This member is an array of Repository instances, one for each of your workspace repo. From such instances, you can do all git actions.

However, I must admit that this API is still poorly documented, as most functions accept strings, and it's not always obvious what they should be if you are not familiar with git language. I ended up using simple-git node module. It has proper documentation and is faster to achieve the functionality you want without losing too much time here.