I need to get the commit hash from the HEAD for each workspace when loading my extension, at vscode startup. For some reason, the HEAD was not initialized at boot time. If I add a 1 second sleep, then HEAD becomes initialized. Why is this happening? I suspect that I need to wait for the add-on-git/workspace/etc to initialize and add handler for some event.
Representation:
export async function activate(context: vscode.ExtensionContext) {
// await new Promise(resolve => setTimeout(resolve, 1000)); // if uncomment this, HEAD becomes initialized
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git')!.exports;
const gitApi = gitExtension.getAPI(1);
for (const workSpaceFolder of vscode.workspace.workspaceFolders || []) {
const rootPath = workSpaceFolder.uri.fsPath;
const repo = await gitApi.openRepository(vscode.Uri.parse(rootPath));
const head = repo!.state.HEAD;
console.log(head); // undefined
}
}
part of package.json:
"engines": {
"vscode": "^1.62.0"
},
"activationEvents": [
"onStartupFinished"
],
"extensionDependencies": [
"vscode.git"
],