diff --git a/src/LastCommitMessage.ts b/src/LastCommitMessage.ts index 6895fce..9859f21 100644 --- a/src/LastCommitMessage.ts +++ b/src/LastCommitMessage.ts @@ -5,9 +5,15 @@ import * as Git from './@types/git'; export class LastCommitMessage { _gitAPI: Git.API; _config = vscode.workspace.getConfiguration('glcm'); + _repo: Git.Repository constructor(context: vscode.ExtensionContext) { this._gitAPI = vscode.extensions.getExtension('vscode.git').exports.getAPI(1); + const add = (repo: any): vscode.Disposable => repo.ui.onDidChange(() => { + if (repo.ui.selected) { this._repo = repo; } + }); + this._gitAPI.repositories.map(add); + this._gitAPI.onDidOpenRepository(add); vscode.commands.registerCommand('glcm.loadLastCommitMessage', () => this.loadLastCommitMessage()); vscode.commands.registerCommand('glcm.chooseLastCommitMessage', () => this.chooseLastCommitMessage()); @@ -17,16 +23,14 @@ export class LastCommitMessage { } async loadLastCommitMessage() { - this._gitAPI.repositories.forEach(async rep => { - try { - const commit = await rep.getCommit(rep.state.HEAD.commit); - if (commit) { - this.setCommitMessageToTextBox(rep, commit); - } - - } catch (ex) { + try { + const commit = await this._repo.getCommit(this._repo.state.HEAD.commit); + if (commit) { + this.setCommitMessageToTextBox(this._repo, commit); } - }); + } catch (ex) { + console.log("load last commit message error: " + ex); + } } private setCommitMessageToTextBox(rep: Git.Repository, commit: Git.Commit) { @@ -37,23 +41,18 @@ export class LastCommitMessage { } async chooseLastCommitMessage() { - const selectedRep = this._gitAPI.repositories.find(x => x.ui.selected); - if (selectedRep != null) { - + const countOfCommitMessagesToChooseFrom = this._config.countOfCommitMessagesToChooseFrom; + const commitMessages = []; + await this.getParentCommitRecursively(0, countOfCommitMessagesToChooseFrom, this._repo, [this._repo.state.HEAD.commit], commitMessages); - const countOfCommitMessagesToChooseFrom = this._config.countOfCommitMessagesToChooseFrom; - const commitMessages = []; - await this.getParentCommitRecursively(0, countOfCommitMessagesToChooseFrom, selectedRep, [selectedRep.state.HEAD.commit], commitMessages); - - vscode.window.showQuickPick(commitMessages, { - canPickMany: false, - placeHolder: "Choose commit message" - }).then(item => { - if (item) { - selectedRep.inputBox.value = item; - } - }); - } + vscode.window.showQuickPick(commitMessages, { + canPickMany: false, + placeHolder: "Choose commit message" + }).then(item => { + if (item) { + this._repo.inputBox.value = item; + } + }); } async getParentCommitRecursively(i: number, limit: number, rep: Git.Repository, commitHashes: string[], messages: string[]): Promise {