Skip to content

Commit c23c702

Browse files
authored
🐞 fix(extension.ts): show template (#77)
* 🐞 fix(extension.ts): show template registerCommand: uri._rootPath is undefined * 🐳 chore(build): version upgrade version 1.1.0 -> 1.1.1
1 parent e97ec48 commit c23c702

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "git-commit-plugin",
33
"displayName": "git-commit-plugin",
44
"description": "Automatically generate git commit",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"engines": {
77
"vscode": "^1.42.0"
88
},
@@ -117,4 +117,4 @@
117117
"vsce": "^1.95.0",
118118
"vscode-nls-i18n": "^0.2.4"
119119
}
120-
}
120+
}

src/extension.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
4-
import { GitExtension } from './types/git';
4+
import { GitExtension, Repository } from './types/git';
55
import GetCommitTypes, { CommitType } from './config/commit-type';
66
import {
77
GetCommitDetailType,
@@ -37,9 +37,7 @@ export function activate(context: vscode.ExtensionContext) {
3737

3838
//获取是否在git扩展内 Gets whether it is in the git extension
3939
function getGitExtension() {
40-
const vscodeGit = vscode.extensions.getExtension<GitExtension>('vscode.git');
41-
const gitExtension = vscodeGit && vscodeGit.exports;
42-
return gitExtension;
40+
return vscode.extensions.getExtension<GitExtension>('vscode.git')?.exports;
4341
}
4442
//Commit message config
4543
const message_config: GitMessage = {
@@ -100,8 +98,15 @@ export function activate(context: vscode.ExtensionContext) {
10098
return false;
10199
}
102100

103-
//获取当前的 git仓库实例 Get git repo instance
104-
let repo: any = gitExtension.getAPI(1).repositories[0];
101+
const getSelectedRepo = (): Repository | null => {
102+
const repositories = gitExtension.getAPI(1).repositories;
103+
104+
if (repositories?.length > 0) {
105+
return repositories.find(rp => rp.ui.selected) || repositories[0];
106+
}
107+
108+
return null;
109+
};
105110

106111
//输入提交详情 Input message detail
107112
const inputMessageDetail = (_key: string | number) => {
@@ -127,13 +132,20 @@ export function activate(context: vscode.ExtensionContext) {
127132
});
128133
};
129134
//是否存在模板 If has template
130-
const existTemplete = () => {
135+
const existTemplate = () => {
131136
return Array.isArray(CommitTemplate) && CommitTemplate.length > 0;
132137
};
133138
//完成输入 Complete input message
134139
const completeInputMessage = (select?: boolean) => {
135140
vscode.commands.executeCommand('workbench.view.scm');
136-
if (existTemplete() && !select) {
141+
const repo = getSelectedRepo();
142+
143+
if (!repo) {
144+
vscode.window.showErrorMessage('No repositories found', ...['ok']);
145+
return;
146+
}
147+
148+
if (existTemplate() && !select) {
137149
const defaultTemp = CommitTemplate.find(item => item.default);
138150
if (defaultTemp !== undefined) {
139151
message_config.templateName = defaultTemp.templateName;
@@ -153,9 +165,8 @@ export function activate(context: vscode.ExtensionContext) {
153165
);
154166
_CommitDetailType.map((item: any) => {
155167
if (item.isEdit) {
156-
item.description = `${item.description} 👍 >> ${
157-
message_config[item.key || '']
158-
}`;
168+
item.description = `${item.description} 👍 >> ${message_config[item.key || '']
169+
}`;
159170
}
160171
return item;
161172
});
@@ -225,20 +236,9 @@ export function activate(context: vscode.ExtensionContext) {
225236
});
226237
};
227238
//点击图标触发快捷选项 Click the icon to trigger shortcut options
228-
let disposable = vscode.commands.registerCommand(
229-
'extension.showGitCommit',
230-
(uri?) => {
231-
if (uri) {
232-
//如果有多个repo 寻找当前的 进行填充 If there are multiple repos looking for the current to populate
233-
repo = gitExtension.getAPI(1).repositories.find(repo => {
234-
return repo.rootUri.path === uri._rootUri.path;
235-
});
236-
}
237-
startMessageInput();
238-
},
239-
);
239+
const disposable = vscode.commands.registerCommand('extension.showGitCommit', () => startMessageInput());
240240
context.subscriptions.push(disposable);
241241
}
242242

243243
// this method is called when your extension is deactivated
244-
export function deactivate() {}
244+
export function deactivate() { }

0 commit comments

Comments
 (0)