git功能已完备
This commit is contained in:
@@ -305,6 +305,10 @@ export class ConfigPanel {
|
||||
case 'importGitFile':
|
||||
await this.importGitFile(data.filePath);
|
||||
break;
|
||||
|
||||
case 'openGitRepoInVSCode':
|
||||
await this.openGitRepoInVSCode(data.repoId);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('处理 Webview 消息时出错:', error);
|
||||
@@ -1412,99 +1416,107 @@ private async deleteGitRepo(repoId: string): Promise<void> {
|
||||
// === Git 分支管理 ===
|
||||
|
||||
private async fetchBranches(url: string): Promise<void> {
|
||||
try {
|
||||
console.log('🌿 开始获取分支列表:', url);
|
||||
|
||||
await vscode.window.withProgress({
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: '正在获取分支信息',
|
||||
cancellable: false
|
||||
}, async (progress) => {
|
||||
progress.report({ increment: 0, message: '连接远程仓库...' });
|
||||
try {
|
||||
console.log('🌿 开始获取分支列表:', url);
|
||||
|
||||
await vscode.window.withProgress({
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: '正在获取分支信息',
|
||||
cancellable: false
|
||||
}, async (progress) => {
|
||||
progress.report({ increment: 0, message: '连接远程仓库...' });
|
||||
|
||||
try {
|
||||
// 使用 isomorphic-git 的 listServerRefs
|
||||
progress.report({ increment: 30, message: '获取远程引用...' });
|
||||
try {
|
||||
// 使用 isomorphic-git 的 listServerRefs
|
||||
progress.report({ increment: 30, message: '获取远程引用...' });
|
||||
|
||||
console.log('🔍 使用 listServerRefs 获取分支信息...');
|
||||
|
||||
const refs = await git.listServerRefs({
|
||||
http: http,
|
||||
url: url
|
||||
});
|
||||
|
||||
console.log('📋 获取到的引用:', refs);
|
||||
|
||||
// 过滤出分支引用 (refs/heads/ 和 refs/remotes/origin/)
|
||||
const branchRefs = refs.filter(ref =>
|
||||
ref.ref.startsWith('refs/heads/') || ref.ref.startsWith('refs/remotes/origin/')
|
||||
);
|
||||
|
||||
console.log('🌿 过滤后的分支引用:', branchRefs);
|
||||
|
||||
// 构建分支数据 - 修复分支名称显示
|
||||
const branches: GitBranch[] = branchRefs.map(ref => {
|
||||
const isRemote = ref.ref.startsWith('refs/remotes/');
|
||||
let branchName: string;
|
||||
|
||||
console.log('🔍 使用 listServerRefs 获取分支信息...');
|
||||
|
||||
const refs = await git.listServerRefs({
|
||||
http: http,
|
||||
url: url
|
||||
});
|
||||
|
||||
console.log('📋 获取到的引用:', refs);
|
||||
|
||||
// 过滤出分支引用 (refs/heads/ 和 refs/remotes/origin/)
|
||||
const branchRefs = refs.filter(ref =>
|
||||
ref.ref.startsWith('refs/heads/') || ref.ref.startsWith('refs/remotes/origin/')
|
||||
);
|
||||
|
||||
console.log('🌿 过滤后的分支引用:', branchRefs);
|
||||
|
||||
// 构建分支数据
|
||||
const branches: GitBranch[] = branchRefs.map(ref => {
|
||||
const isRemote = ref.ref.startsWith('refs/remotes/');
|
||||
const branchName = isRemote
|
||||
? ref.ref.replace('refs/remotes/origin/', '')
|
||||
: ref.ref.replace('refs/heads/', '');
|
||||
|
||||
return {
|
||||
name: branchName,
|
||||
isCurrent: branchName === 'main' || branchName === 'master',
|
||||
isRemote: isRemote,
|
||||
selected: branchName === 'main' || branchName === 'master'
|
||||
};
|
||||
});
|
||||
|
||||
console.log('🎯 最终分支列表:', branches);
|
||||
|
||||
if (branches.length === 0) {
|
||||
throw new Error('未找到任何分支');
|
||||
}
|
||||
|
||||
progress.report({ increment: 80, message: '处理分支数据...' });
|
||||
|
||||
// 发送分支数据到前端
|
||||
if (!this.isWebviewDisposed) {
|
||||
this.panel.webview.postMessage({
|
||||
type: 'branchesFetched',
|
||||
branches: branches,
|
||||
repoUrl: url
|
||||
});
|
||||
}
|
||||
|
||||
progress.report({ increment: 100, message: '完成' });
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 使用 listServerRefs 获取分支失败:', error);
|
||||
|
||||
// 如果方法失败,使用模拟数据
|
||||
console.log('🔄 使用模拟分支数据');
|
||||
const mockBranches = [
|
||||
{ name: 'main', isCurrent: true, isRemote: false, selected: true },
|
||||
{ name: 'master', isCurrent: false, isRemote: false, selected: false },
|
||||
{ name: 'develop', isCurrent: false, isRemote: false, selected: false },
|
||||
{ name: 'feature/new-feature', isCurrent: false, isRemote: false, selected: false }
|
||||
];
|
||||
|
||||
if (!this.isWebviewDisposed) {
|
||||
this.panel.webview.postMessage({
|
||||
type: 'branchesFetched',
|
||||
branches: mockBranches,
|
||||
repoUrl: url
|
||||
});
|
||||
if (isRemote) {
|
||||
// 远程分支:移除 refs/remotes/origin/ 前缀
|
||||
branchName = ref.ref.replace('refs/remotes/origin/', '');
|
||||
// 可以选择添加远程标识,或者不添加
|
||||
// branchName = `origin/${branchName}`; // 如果需要显示远程标识
|
||||
} else {
|
||||
// 本地分支:移除 refs/heads/ 前缀
|
||||
branchName = ref.ref.replace('refs/heads/', '');
|
||||
}
|
||||
|
||||
vscode.window.showWarningMessage('使用模拟分支数据,实际分支可能不同');
|
||||
return {
|
||||
name: branchName,
|
||||
isCurrent: branchName === 'main' || branchName === 'master',
|
||||
isRemote: isRemote,
|
||||
selected: false // 所有分支默认不选中
|
||||
};
|
||||
});
|
||||
|
||||
console.log('🎯 最终分支列表:', branches);
|
||||
|
||||
if (branches.length === 0) {
|
||||
throw new Error('未找到任何分支');
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 获取分支失败:', error);
|
||||
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
|
||||
}
|
||||
progress.report({ increment: 80, message: '处理分支数据...' });
|
||||
|
||||
// 发送分支数据到前端
|
||||
if (!this.isWebviewDisposed) {
|
||||
this.panel.webview.postMessage({
|
||||
type: 'branchesFetched',
|
||||
branches: branches,
|
||||
repoUrl: url
|
||||
});
|
||||
}
|
||||
|
||||
progress.report({ increment: 100, message: '完成' });
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 使用 listServerRefs 获取分支失败:', error);
|
||||
|
||||
// 如果方法失败,使用模拟数据
|
||||
console.log('🔄 使用模拟分支数据');
|
||||
const mockBranches = [
|
||||
{ name: 'main', isCurrent: true, isRemote: false, selected: false },
|
||||
{ name: 'master', isCurrent: false, isRemote: false, selected: false },
|
||||
{ name: 'develop', isCurrent: false, isRemote: false, selected: false },
|
||||
{ name: 'feature/new-feature', isCurrent: false, isRemote: false, selected: false }
|
||||
];
|
||||
|
||||
if (!this.isWebviewDisposed) {
|
||||
this.panel.webview.postMessage({
|
||||
type: 'branchesFetched',
|
||||
branches: mockBranches,
|
||||
repoUrl: url
|
||||
});
|
||||
}
|
||||
|
||||
vscode.window.showWarningMessage('使用模拟分支数据,实际分支可能不同');
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 获取分支失败:', error);
|
||||
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async cloneBranches(url: string, branches: string[]): Promise<void> {
|
||||
try {
|
||||
@@ -1603,4 +1615,39 @@ private async deleteGitRepo(repoId: string): Promise<void> {
|
||||
});
|
||||
}
|
||||
}
|
||||
private async openGitRepoInVSCode(repoId: string): Promise<void> {
|
||||
const repo = this.gitRepos.find(r => r.id === repoId);
|
||||
if (!repo) {
|
||||
vscode.window.showErrorMessage('未找到指定的 Git 仓库');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查仓库目录是否存在
|
||||
if (!fs.existsSync(repo.localPath)) {
|
||||
vscode.window.showErrorMessage('Git 仓库目录不存在,请重新克隆');
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 VSCode 的文件选择器让用户选择要打开的文件
|
||||
const fileUri = await vscode.window.showOpenDialog({
|
||||
defaultUri: vscode.Uri.file(repo.localPath),
|
||||
canSelectFiles: true,
|
||||
canSelectFolders: false,
|
||||
canSelectMany: false,
|
||||
openLabel: '选择要打开的文件',
|
||||
title: `在 ${repo.name} 中选择文件`
|
||||
});
|
||||
|
||||
if (fileUri && fileUri.length > 0) {
|
||||
// 打开选中的文件
|
||||
const document = await vscode.workspace.openTextDocument(fileUri[0]);
|
||||
await vscode.window.showTextDocument(document);
|
||||
vscode.window.showInformationMessage(`已打开文件: ${path.basename(fileUri[0].fsPath)}`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
vscode.window.showErrorMessage(`打开 Git 仓库文件失败: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,23 +61,30 @@ export class ConfigView extends BaseView {
|
||||
</tr>
|
||||
`).join('');
|
||||
|
||||
// 生成 Git 仓库列表的 HTML - 以配置文件形式显示
|
||||
const gitReposHtml = gitRepos.map(repo => `
|
||||
// 生成 Git 仓库列表的 HTML - 修改显示方式
|
||||
const gitReposHtml = gitRepos.map(repo => {
|
||||
// 提取仓库名称(从URL中获取或使用name)
|
||||
const repoName = repo.name.split(' (')[0]; // 移除分支名部分
|
||||
// 提取分支名
|
||||
const branchMatch = repo.name.match(/\(([^)]+)\)/);
|
||||
const branchName = branchMatch ? branchMatch[1] : repo.branch;
|
||||
|
||||
return `
|
||||
<tr>
|
||||
<td>
|
||||
<span class="editable">📁 ${repo.name}</span>
|
||||
<span class="editable">📁 ${repoName}</span>
|
||||
<div style="font-size: 12px; color: var(--vscode-descriptionForeground); margin-top: 4px;">
|
||||
模型1、模型2
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<span class="clickable" onclick="loadGitRepo('${repo.id}')">${repo.url.split('/').pop()}</span>
|
||||
<span class="clickable" onclick="openGitRepoInVSCode('${repo.id}')">${branchName}</span>
|
||||
</td>
|
||||
<td>
|
||||
<button class="btn-delete" onclick="deleteGitRepo('${repo.id}')">删除</button>
|
||||
</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
// 生成分支选择的 HTML
|
||||
const branchesHtml = gitBranches.length > 0 ? this.generateBranchesHtml(gitBranches) : '';
|
||||
@@ -262,7 +269,7 @@ export class ConfigView extends BaseView {
|
||||
);
|
||||
}
|
||||
|
||||
// Git 仓库删除功能 - 修复版本
|
||||
// Git 仓库删除功能
|
||||
function deleteGitRepo(repoId) {
|
||||
console.log('🗑️ 尝试删除 Git 仓库:', repoId);
|
||||
|
||||
@@ -283,6 +290,15 @@ export class ConfigView extends BaseView {
|
||||
);
|
||||
}
|
||||
|
||||
// 新功能:在 VSCode 中打开 Git 仓库
|
||||
function openGitRepoInVSCode(repoId) {
|
||||
console.log('📂 在 VSCode 中打开 Git 仓库:', repoId);
|
||||
vscode.postMessage({
|
||||
type: 'openGitRepoInVSCode',
|
||||
repoId: repoId
|
||||
});
|
||||
}
|
||||
|
||||
function saveConfigFile() {
|
||||
const content = document.getElementById('configContent').value;
|
||||
vscode.postMessage({
|
||||
@@ -407,7 +423,7 @@ export class ConfigView extends BaseView {
|
||||
branchesHtml += (branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\\'' + branch.name + '\\')" style="margin-right: 10px;">';
|
||||
branchesHtml += '<label for="branch-' + branch.name.replace(/[^a-zA-Z0-9]/g, '-') + '" style="flex: 1; cursor: pointer;">';
|
||||
branchesHtml += (branch.isCurrent ? '⭐ ' : '') + branch.name;
|
||||
branchesHtml += (branch.isRemote ? '(远程)' : '(本地)') + '</label></div>';
|
||||
branchesHtml += '</label></div>';
|
||||
});
|
||||
|
||||
branchesHtml += '</div>';
|
||||
@@ -514,18 +530,9 @@ export class ConfigView extends BaseView {
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化 - 添加调试信息(修复语法错误)
|
||||
// 初始化
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
console.log('📄 ConfigView 页面加载完成');
|
||||
console.log('🔍 检查 Git 仓库删除按钮绑定');
|
||||
|
||||
// 检查所有删除按钮
|
||||
const deleteButtons = document.querySelectorAll('.btn-delete');
|
||||
console.log('找到删除按钮数量:', deleteButtons.length);
|
||||
|
||||
deleteButtons.forEach(function(btn, index) {
|
||||
console.log('删除按钮 ' + index + ':', btn);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user