fix async work with repository state
This commit is contained in:
@@ -1,21 +1,41 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { GitExtension } from './git';
|
import { GitExtension, Repository } from './git';
|
||||||
|
|
||||||
|
class UserConf {
|
||||||
|
domain: string = "";
|
||||||
|
name: string = "";
|
||||||
|
email:string = "";
|
||||||
|
|
||||||
|
valid: boolean = false;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
var conf = vscode.workspace.getConfiguration("gitcheck");
|
||||||
|
const _domain = conf.get<string>("domain");
|
||||||
|
const _name = conf.get<string>("name");
|
||||||
|
const _email = conf.get<string>("email");
|
||||||
|
if (!(_domain && _name && _email)) {
|
||||||
|
console.log("Missing config params");
|
||||||
|
this.valid = false;
|
||||||
|
} else {
|
||||||
|
this.domain = _domain;
|
||||||
|
this.name = _name;
|
||||||
|
this.email = _email;
|
||||||
|
this.valid = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function activate() {
|
export async function activate() {
|
||||||
var conf = vscode.workspace.getConfiguration("gitcheck");
|
const userConf = new UserConf();
|
||||||
const domain = conf.get<string>("domain");
|
if (!userConf.valid) {
|
||||||
const name = conf.get<string>("name");
|
return;
|
||||||
const email = conf.get<string>("email");
|
}
|
||||||
|
|
||||||
if (!vscode.workspace.workspaceFolders) {
|
if (!vscode.workspace.workspaceFolders) {
|
||||||
console.log("No opened workspace");
|
console.log("No opened workspace");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(domain && name && email)) {
|
|
||||||
console.log("Missing config params");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const gitExt = vscode.extensions.getExtension<GitExtension>('vscode.git')!;
|
const gitExt = vscode.extensions.getExtension<GitExtension>('vscode.git')!;
|
||||||
const git = gitExt.exports.getAPI(1);
|
const git = gitExt.exports.getAPI(1);
|
||||||
@@ -25,16 +45,29 @@ export async function activate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const repository = await git.openRepository(vscode.workspace.workspaceFolders[0].uri);
|
const repository = await git.openRepository(vscode.workspace.workspaceFolders[0].uri);
|
||||||
|
|
||||||
if (!repository) {
|
if (!repository) {
|
||||||
console.error("Cannot open git repository");
|
console.error("Cannot open git repository");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
repository.state.remotes.forEach(async remote => {
|
let remotesLen:number = 0;
|
||||||
|
repository.state.onDidChange(() => {
|
||||||
|
if (remotesLen > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
remotesLen = repository.state.remotes.length;
|
||||||
|
|
||||||
|
if (remotesLen > 0) {
|
||||||
|
gitCheck(repository, userConf);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function gitCheck(repo:Repository, conf:UserConf) {
|
||||||
|
repo.state.remotes.forEach(async remote => {
|
||||||
const pushUrl = remote.pushUrl;
|
const pushUrl = remote.pushUrl;
|
||||||
if (pushUrl && pushUrl.includes(domain)) {
|
if (pushUrl && pushUrl.includes(conf.domain)) {
|
||||||
var gitConfig = await repository.getConfigs();
|
var gitConfig = await repo.getConfigs();
|
||||||
var gitName:string = "";
|
var gitName:string = "";
|
||||||
var gitEmail:string = "";
|
var gitEmail:string = "";
|
||||||
gitConfig = gitConfig.filter( elem => {
|
gitConfig = gitConfig.filter( elem => {
|
||||||
@@ -44,8 +77,8 @@ export async function activate() {
|
|||||||
console.error("Missing user creds in git");
|
console.error("Missing user creds in git");
|
||||||
const option = await vscode.window.showErrorMessage("Missing user settings in git", "Overwrite");
|
const option = await vscode.window.showErrorMessage("Missing user settings in git", "Overwrite");
|
||||||
if (option && option === "Overwrite") {
|
if (option && option === "Overwrite") {
|
||||||
await repository.setConfig("user.name", name);
|
await repo.setConfig("user.name", conf.name);
|
||||||
await repository.setConfig("user.email", email);
|
await repo.setConfig("user.email", conf.email);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -63,12 +96,12 @@ export async function activate() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (gitName !== name || gitEmail !== email) {
|
if (gitName !== conf.name || gitEmail !== conf.email) {
|
||||||
console.error("Wrong user settings in git");
|
console.error("Wrong user settings in git");
|
||||||
const option = await vscode.window.showErrorMessage("Wrong user settings in git", "Overwrite");
|
const option = await vscode.window.showErrorMessage("Wrong user settings in git", "Overwrite");
|
||||||
if (option && option === "Overwrite") {
|
if (option && option === "Overwrite") {
|
||||||
await repository.setConfig("user.name", name);
|
await repo.setConfig("user.name", conf.name);
|
||||||
await repository.setConfig("user.email", email);
|
await repo.setConfig("user.email", conf.email);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user