0
0

新增git分支显示功能

This commit is contained in:
xubing
2025-11-27 18:34:57 +08:00
parent fecbcdc59b
commit 9f44664b0b
6 changed files with 749 additions and 167 deletions

View File

@@ -64,7 +64,6 @@ interface ProjectData {
interface GitBranch {
name: string;
isCurrent: boolean;
isRemote: boolean;
selected?: boolean;
}
@@ -1300,89 +1299,133 @@ export class ConfigPanel {
// === 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 => {
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/');
let branchName: string;
if (isRemote) {
// 远程分支:移除 refs/remotes/origin/ 前缀
branchName = ref.ref.replace('refs/remotes/origin/', '');
} else {
// 本地分支:移除 refs/heads/ 前缀
branchName = ref.ref.replace('refs/heads/', '');
}
return {
name: branchName,
isCurrent: branchName === 'main' || branchName === 'master',
isRemote: isRemote,
selected: false // 所有分支默认不选中
};
});
console.log('🎯 最终分支列表:', branches);
if (branches.length === 0) {
throw new Error('未找到任何分支');
if (ref.ref.startsWith('refs/remotes/')) {
// 远程分支:移除 refs/remotes/origin/ 前缀
branchName = ref.ref.replace('refs/remotes/origin/', '');
} else {
// 本地分支:移除 refs/heads/ 前缀
branchName = ref.ref.replace('refs/heads/', '');
}
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);
// 只在右下角显示分支获取失败的通知,不模拟分支数据
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
return {
name: branchName,
isCurrent: branchName === 'main' || branchName === 'master',
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: '处理分支数据...' });
// === 新增:构建分支树状结构 ===
const branchTree = this.buildBranchTree(branches);
console.log('🌳 构建的分支树结构:', branchTree);
// 发送分支数据到前端 - 同时包含扁平列表和树状结构
if (!this.isWebviewDisposed) {
this.panel.webview.postMessage({
type: 'branchesFetched',
branches: branches,
branchTree: branchTree, // 新增树状结构数据
repoUrl: url
});
}
progress.report({ increment: 100, message: '完成' });
} catch (error) {
console.error('❌ 使用 listServerRefs 获取分支失败:', error);
// 只在右下角显示分支获取失败的通知,不模拟分支数据
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
}
});
} catch (error) {
console.error('❌ 获取分支失败:', error);
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
}
}
/**
* 构建分支树状结构
*/
private buildBranchTree(branches: GitBranch[]): any[] {
const root: any[] = [];
branches.forEach(branch => {
const parts = branch.name.split('/');
let currentLevel = root;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const isLeaf = i === parts.length - 1;
const fullName = parts.slice(0, i + 1).join('/');
let node = currentLevel.find((n: any) => n.name === part);
if (!node) {
node = {
name: part,
fullName: fullName,
isLeaf: isLeaf,
children: [],
level: i,
expanded: true // 默认展开
};
currentLevel.push(node);
}
if (isLeaf) {
// 叶子节点存储分支信息
node.branch = branch;
}
currentLevel = node.children;
}
});
return root;
}
private async cloneBranches(url: string, branches: string[]): Promise<void> {
try {