Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the api for git in visual studio code

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

like image 592
sreenivas Avatar asked Oct 01 '17 10:10

sreenivas


People also ask

How do I access my Git repository in Visual Studio?

In the Visual Studio IDE, select the Git menu, select Local Repositories, and then select Open Local Repository.

How does Git connect to VS Code?

Step 1: Download and install Visual Studio Code in your system using the official website. Step 2: Download and install git in your system using the official website. Step 3: Now create an account on GitHub.

What is API in Visual Studio Code?

The Testing API allows Visual Studio Code extensions to discover tests in the workspace and publish results. Users can execute tests in the Test Explorer view, from decorations, and inside commands. With these new APIs, Visual Studio Code supports richer displays of outputs and diffs than was previously possible.


1 Answers

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"
  ]
like image 133
Nishant Avatar answered Sep 28 '22 14:09

Nishant