Skip to content

Commit c5e9d8e

Browse files
committed
📦 更新(package.json): 升级版本号至"0.0.5",添加新的devDependencies和dependencies
🐛 修复(src/git-utils.ts): 接收参数repo并使用其对应的rootPath,修复多个 Git 无法识别的问题
1 parent 76f5e51 commit c5e9d8e

5 files changed

Lines changed: 45 additions & 24 deletions

File tree

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ai-commit",
33
"displayName": "AI Commit",
44
"description": "Use Azure/OpenAI API to review Git changes, generate conventional commit messages that meet the conventions, simplify the commit process, and keep the commit conventions consistent.",
5-
"version": "0.0.4",
5+
"version": "0.0.5",
66
"engines": {
77
"node": ">=16",
88
"vscode": "^1.77.0"
@@ -146,6 +146,7 @@
146146
"watch-tests": "tsc -p . -w --outDir out"
147147
},
148148
"devDependencies": {
149+
"@types/fs-extra": "^11.0.4",
149150
"@types/glob": "^8.1.0",
150151
"@types/mocha": "^10.0.1",
151152
"@types/node": "16.x",
@@ -163,10 +164,11 @@
163164
},
164165
"dependencies": {
165166
"openai": "^4.14.2",
166-
"simple-git": "^3.17.0"
167+
"simple-git": "^3.17.0",
168+
"fs-extra": "^11.0.4"
167169
},
168170
"resolutions": {
169171
"@types/node": "16.x"
170172
},
171173
"license": "MIT"
172-
}
174+
}

src/ai-commit-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { errMsg, infoMsg } from './utils';
22
import { generateCommitMsg } from './generate-commit-msg';
3-
export default async function aiCommitController() {
3+
export default async function aiCommitController(arg) {
44
try {
55
infoMsg('Generating commit message...');
6-
await generateCommitMsg();
6+
await generateCommitMsg(arg);
77
} catch (error) {
88
errMsg('Generate commit message failed', error);
99
}

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import aiCommitController from './ai-commit-controller';
44

55
export async function activate(context: vscode.ExtensionContext) {
66
context.subscriptions.push(
7-
vscode.commands.registerCommand('extension.ai-commit', async () => {
8-
await aiCommitController();
7+
vscode.commands.registerCommand('extension.ai-commit', async (arg) => {
8+
await aiCommitController(arg);
99
})
1010
);
1111
}

src/generate-commit-msg.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getDiffStaged } from './git-utils';
22
import { ConfigKeys, getConfig } from './config';
33
import * as vscode from 'vscode';
4+
import * as fs from 'fs-extra';
45
import { errMsg, infoMsg } from './utils';
56
import { ChatGPTAPI } from './openai-utils';
67
import { getMainCommitPrompt } from './prompts';
@@ -16,22 +17,38 @@ const generateCommitMessageChatCompletionPrompt = async (diff: string) => {
1617
return chatContextAsCompletionRequest;
1718
};
1819

19-
export async function generateCommitMsg() {
20-
const diff = await getDiffStaged();
20+
export async function getRepo(arg) {
21+
const gitApi = vscode.extensions.getExtension('vscode.git').exports.getAPI(1);
22+
if (typeof arg === 'object' && arg.rootUri) {
23+
const resourceUri = arg.rootUri;
24+
const realResourcePath: string = fs.realpathSync(resourceUri!.fsPath);
25+
let i = 0;
26+
for (const x of gitApi.repositories) {
27+
if (
28+
realResourcePath.startsWith(x.rootUri.fsPath) &&
29+
x.rootUri.fsPath === gitApi.repositories[i].rootUri.fsPath
30+
) {
31+
return gitApi.repositories[i];
32+
}
33+
i++;
34+
}
35+
}
36+
return gitApi.repositories[0];
37+
}
38+
39+
export async function generateCommitMsg(arg) {
40+
const repo = await getRepo(arg);
2141
const apiKey = getConfig<string>(ConfigKeys.OPENAI_API_KEY);
42+
const diff = await getDiffStaged(repo);
2243

2344
if (!apiKey) {
2445
infoMsg('OpenAI API Key Not Set');
2546
return;
2647
}
2748

28-
const sourceControlView = vscode.extensions
29-
.getExtension('vscode.git')
30-
.exports.getAPI(1).repositories[0];
31-
32-
const scmInputBox = sourceControlView.inputBox as vscode.SourceControlInputBox;
49+
infoMsg('gitRootPath: ' + repo.rootUri.fsPath);
50+
const scmInputBox = repo.inputBox as vscode.SourceControlInputBox;
3351
const messages = await generateCommitMessageChatCompletionPrompt(diff);
34-
3552
if (scmInputBox) {
3653
const edit = new vscode.WorkspaceEdit();
3754

@@ -43,9 +60,8 @@ export async function generateCommitMsg() {
4360
.catch((err) => {
4461
errMsg('API ERROR: ', err);
4562
});
46-
4763
await vscode.workspace.applyEdit(edit);
4864
} else {
49-
vscode.window.showErrorMessage('Unable to find the SCM input box.');
65+
errMsg('Unable to find the SCM input box.', '');
5066
}
5167
}

src/git-utils.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import simpleGit, { SimpleGit } from 'simple-git';
22
import * as vscode from 'vscode';
33

4-
const rootPath = vscode.workspace.workspaceFolders
5-
? vscode.workspace.workspaceFolders[0].uri.fsPath
6-
: null;
7-
8-
const git: SimpleGit = simpleGit(rootPath);
9-
10-
export async function getDiffStaged(): Promise<string> {
4+
export async function getDiffStaged(repo): Promise<string> {
5+
let rootPath = null;
6+
if (typeof repo === 'object' && repo.rootUri) {
7+
rootPath = repo.rootUri.fsPath;
8+
} else {
9+
rootPath = vscode.workspace.workspaceFolders
10+
? vscode.workspace.workspaceFolders[0].uri.fsPath
11+
: null;
12+
}
13+
const git: SimpleGit = simpleGit(rootPath);
1114
try {
1215
const diff = await git.diff(['--staged']);
1316
return diff;

0 commit comments

Comments
 (0)