Rey
Rey

Reputation: 125

VS Code Git Extension API

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? :(

Upvotes: 5

Views: 3365

Answers (2)

Aurelien Ribon
Aurelien Ribon

Reputation: 7634

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.

Upvotes: 7

VonC
VonC

Reputation: 1323115

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;
}

Upvotes: 6

Related Questions