我希望在我的扩展中使用 vscode git api 来进行 git clone 和其他任务。这个 api 可以从 vscode api 中访问吗? 代码位于这里..api
我希望在我的扩展中使用 vscode git api 来进行 git clone 和其他任务。这个 api 可以从 vscode api 中访问吗? 代码位于这里..api
Twitter 来拯救! 我在那里询问并被指向了这里的 API 定义:https://github.com/Microsoft/vscode/blob/master/extensions/git/src/api/git.d.ts
// 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];
GitExtension
的导入行是什么? - David Lechner在 VS Code 扩展中使用 Git API 的示例代码:
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 });
你还需要在 package.json 中更新 extensionDependencies:
"extensionDependencies": [
"vscode.git"
]
When depending on the API of another extension add an
extensionDependencies
-entry topackage.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));
checkout
、branch
等),而不是使用 VSCode 中已经内置的 Git 功能。 - Daniel