如何在visual studio代码中访问git的api

时间:2017-10-01 10:43:46

标签: visual-studio-code vscode-extensions

我想在我的扩展程序中使用vscode git api来执行git clone和其他任务。可以从vscode api访问吗? 代码在此处显示.. api

3 个答案:

答案 0 :(得分:3)

在vscode扩展中使用git api的示例代码:

containerProps = {{ className: 'chartContainer' }}

您还必须在package.json中更新extensionDependencies:

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

答案 1 :(得分:2)

Twitter进行救援! 我在那里询问,并在这里指向API定义:https://github.com/Microsoft/vscode/blob/master/extensions/git/src/api/git.d.ts

...以及此处的示例:https://github.com/Microsoft/vscode-pull-request-github/blob/master/src/extension.ts#L53

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

答案 2 :(得分:0)

根据the extension API,访问另一个扩展提供的API:

根据其他扩展程序的API添加一个 extensionDependencies-进入package.json,然后使用 getExtension功能和exports属性,如下所示:

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

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