@@ -4,6 +4,10 @@ import { GitService } from "./git";
44import { AiService } from "./ai" ;
55import { AiCommitViewProvider , CommitItem } from "./treeProvider" ;
66import { FAST_PROMPT } from "./prompts" ;
7+ import {
8+ GitRepository ,
9+ resolveRepository ,
10+ } from "../git/shared/repositoryResolver" ;
711import * as path from "path" ;
812import * as fs from "fs" ;
913
@@ -24,6 +28,7 @@ export function activate(context: vscode.ExtensionContext) {
2428 let cachedDiff : string | null = null ;
2529 // 缓存上下文信息,供 "预览 Prompt" 使用
2630 let lastContext : { diff : string ; projectMeta : string } | null = null ;
31+ let lastRepository : GitRepository | null = null ;
2732
2833 // 1. 注册 TreeDataProvider
2934 context . subscriptions . push (
@@ -54,11 +59,9 @@ export function activate(context: vscode.ExtensionContext) {
5459 // ---------------------------------------------------------
5560 // 辅助函数:获取 Project Meta
5661 // ---------------------------------------------------------
57- const getProjectMeta = async ( ) : Promise < string > => {
62+ const getProjectMeta = async ( repoRoot : string ) : Promise < string > => {
5863 try {
59- if ( ! vscode . workspace . workspaceFolders ) return "" ;
60- const rootPath = vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ;
61- const pkgPath = path . join ( rootPath , "package.json" ) ;
64+ const pkgPath = path . join ( repoRoot , "package.json" ) ;
6265 if ( fs . existsSync ( pkgPath ) ) {
6366 const raw = await fs . promises . readFile ( pkgPath , "utf-8" ) ;
6467 const pkg = JSON . parse ( raw ) ;
@@ -70,10 +73,22 @@ export function activate(context: vscode.ExtensionContext) {
7073 return "" ;
7174 } ;
7275
76+ const resolveTargetRepository = async ( ) : Promise < GitRepository > => {
77+ const resolved = await resolveRepository ( {
78+ quickPickTitle : "AI Commit: 请选择目标 Git 仓库" ,
79+ } ) ;
80+ lastRepository = resolved . repository ;
81+ Logger . info (
82+ `[RepoResolver] source=${ resolved . source } , repo=${ resolved . repository . rootUri . fsPath } ` ,
83+ ) ;
84+ return resolved . repository ;
85+ } ;
86+
7387 // ---------------------------------------------------------
7488 // 辅助函数:执行 AI 生成
7589 // ---------------------------------------------------------
7690 const runAiGeneration = async (
91+ repository : GitRepository ,
7792 diff : string ,
7893 changeSummary : string = "(None)" ,
7994 ) => {
@@ -86,7 +101,7 @@ export function activate(context: vscode.ExtensionContext) {
86101 async ( ) => {
87102 try {
88103 // 获取项目元数据
89- const projectMeta = await getProjectMeta ( ) ;
104+ const projectMeta = await getProjectMeta ( repository . rootUri . fsPath ) ;
90105
91106 // 保存上下文供预览
92107 lastContext = { diff, projectMeta } ;
@@ -123,6 +138,8 @@ export function activate(context: vscode.ExtensionContext) {
123138 context . subscriptions . push (
124139 vscode . commands . registerCommand ( "fusi-tools.previewSmartDiff" , async ( ) => {
125140 try {
141+ const repository = await resolveTargetRepository ( ) ;
142+
126143 // 1. 显示加载中
127144 provider . showLoading ( ) ;
128145
@@ -131,18 +148,18 @@ export function activate(context: vscode.ExtensionContext) {
131148
132149 // 2. 分析
133150 Logger . info ( "正在分析文件变更 (智能预处理)..." ) ;
134- const analysis = await gitService . analyzeChanges ( ) ;
151+ const analysis = await gitService . analyzeChanges ( repository ) ;
135152 if ( ! analysis || analysis . length === 0 ) {
136153 provider . clear ( ) ; // 清除加载态
137154 vscode . window . showWarningMessage ( "未发现暂存更改,请先暂存文件。" ) ;
138155 return ;
139156 }
140157
141158 // 3. 格式化并缓存
142- cachedDiff = await gitService . formatSmartDiff ( analysis ) ;
159+ cachedDiff = await gitService . formatSmartDiff ( repository , analysis ) ;
143160
144161 // [New] 预加载上下文,以便用户可以在“生成前”查看 Prompt
145- const projectMeta = await getProjectMeta ( ) ;
162+ const projectMeta = await getProjectMeta ( repository . rootUri . fsPath ) ;
146163 lastContext = { diff : cachedDiff , projectMeta } ;
147164
148165 // 4. 更新 UI 展示文件列表
@@ -170,7 +187,8 @@ export function activate(context: vscode.ExtensionContext) {
170187 if ( ! cachedDiff ) return ; // 如果还是没有 (e.g. 无更改),终止
171188 }
172189
173- await runAiGeneration ( cachedDiff ) ;
190+ const repository = lastRepository || ( await resolveTargetRepository ( ) ) ;
191+ await runAiGeneration ( repository , cachedDiff ) ;
174192 } ) ,
175193 ) ;
176194
@@ -180,25 +198,27 @@ export function activate(context: vscode.ExtensionContext) {
180198 context . subscriptions . push (
181199 vscode . commands . registerCommand ( "fusi-tools.generateDirect" , async ( ) => {
182200 try {
201+ const repository = await resolveTargetRepository ( ) ;
202+
183203 // 1. 显示加载
184204 provider . showLoading ( ) ;
185205
186206 // 2. 获取 diff (不做 UI 预览,直接获取字符串)
187- const analysis = await gitService . analyzeChanges ( ) ;
207+ const analysis = await gitService . analyzeChanges ( repository ) ;
188208 if ( ! analysis || analysis . length === 0 ) {
189209 provider . clear ( ) ;
190210 vscode . window . showWarningMessage ( "未发现暂存更改,请先暂存文件。" ) ;
191211 return ;
192212 }
193213
194- const diff = await gitService . formatSmartDiff ( analysis ) ;
214+ const diff = await gitService . formatSmartDiff ( repository , analysis ) ;
195215 cachedDiff = diff ; // 顺便缓存
196216
197217 // 更新 UI (后台更新,不聚焦,作为副产品)
198218 provider . updatePreProcess ( analysis ) ;
199219
200220 // 3. 直接执行生成
201- await runAiGeneration ( diff ) ;
221+ await runAiGeneration ( repository , diff ) ;
202222 } catch ( error : any ) {
203223 provider . clear ( ) ;
204224 vscode . window . showErrorMessage ( `生成失败: ${ error . message } ` ) ;
@@ -212,7 +232,7 @@ export function activate(context: vscode.ExtensionContext) {
212232 context . subscriptions . push (
213233 vscode . commands . registerCommand (
214234 "fusi-tools.applyCommit" ,
215- ( arg : string | CommitItem ) => {
235+ async ( arg : string | CommitItem ) => {
216236 let message = "" ;
217237
218238 if ( typeof arg === "string" ) {
@@ -228,8 +248,15 @@ export function activate(context: vscode.ExtensionContext) {
228248 }
229249
230250 if ( message ) {
231- Logger . info ( "尝试应用提交信息到 Git 输入框" ) ;
232- gitService . setCommitMessage ( message ) ;
251+ try {
252+ const repository = lastRepository || ( await resolveTargetRepository ( ) ) ;
253+ Logger . info (
254+ `尝试应用提交信息到 Git 输入框: ${ repository . rootUri . fsPath } ` ,
255+ ) ;
256+ gitService . setCommitMessage ( message , repository ) ;
257+ } catch ( error : any ) {
258+ vscode . window . showErrorMessage ( `应用提交信息失败: ${ error . message } ` ) ;
259+ }
233260 }
234261 } ,
235262 ) ,
0 commit comments