21

I want to use the vscode git api in one my extension to do git clone and other tasks. Is it accessible from the vscode api ? The code is present here.. api

3
  • I was wondering the same... In the docs there are only two (2!) items regarding the git api. And when I checked out the code of two of my often used Git extensions (gitHistoryVSCode, vscode-gitlens), I noticed that the authors coded basic git functionality (like checkout, branch ec.) from scratch instead of using some already build-in git functionality from VSCode. Commented Apr 26, 2018 at 22:13
  • 1
    Same here, and apparently no news on this. This open issue on the repo seems to try and make the extension's API available: github.com/Microsoft/vscode/issues/31103 Commented Jun 13, 2018 at 2:44
  • Official README now on github.com/microsoft/vscode/blob/main/extensions/git/README.md Commented May 13 at 9:08

3 Answers 3

17

Twitter to the rescue! I asked there and was pointed to the API definitions here: https://github.com/Microsoft/vscode/blob/master/extensions/git/src/api/git.d.ts

...and an example here: https://github.com/microsoft/vscode-pull-request-github/blob/0068c135d1c3e5ce601c1d5c7f7007904e59901e/src/extension.ts#L53

// Import the git.d.ts file
import { API as GitAPI, GitExtension, APIState } from './typings/git'; 

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

const rootPath = vscode.workspace.rootPath;
const repository = api.repositories.filter(r => isDescendant(r.rootUri.fsPath, rootPath))[0];
Sign up to request clarification or add additional context in comments.

2 Comments

what is the import line to import GitExtension?
Could you please clarify this bit of code ? It is very confusing, however, I tried but couldn't get nowhere...
13

Sample code for using git api in vscode extension :

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

const repo = api.repositories[0];
const head = repo.state.HEAD;

// Get the branch and commit 
const {commit,name: branch} = head;

// Get head of any other branch
const mainBranch = 'master'
const branchDetails = await repo.getBranch(mainBranch);

// Get last merge commit
const lastMergeCommit = await repo.getMergeBase(branch, mainBranch);

const status = await repo.status();

console.log({ branch, commit, lastMergeCommit, needsSync: lastMergeCommit !== commit });

You also have to update extensionDependencies in you package.json:

"extensionDependencies": [
    "vscode.git"
  ]

1 Comment

FYI, I decided to use simple-git npm module instead of relying on git extension.
-1

According to the extension API, to access APIs that provided by another extension:

When depending on the API of another extension add an extensionDependencies-entry to package.json, and use the getExtension-function and the exports-property, like below:

let mathExt = extensions.getExtension('genius.math');
let importedApi = mathExt.exports;

console.log(importedApi.mul(42, 1));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.