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

@@ -1086,9 +1086,8 @@ class ConfigPanel {
console.log('🌿 过滤后的分支引用:', branchRefs);
// 构建分支数据 - 修复分支名称显示
const branches = branchRefs.map(ref => {
const isRemote = ref.ref.startsWith('refs/remotes/');
let branchName;
if (isRemote) {
if (ref.ref.startsWith('refs/remotes/')) {
// 远程分支:移除 refs/remotes/origin/ 前缀
branchName = ref.ref.replace('refs/remotes/origin/', '');
}
@@ -1099,7 +1098,6 @@ class ConfigPanel {
return {
name: branchName,
isCurrent: branchName === 'main' || branchName === 'master',
isRemote: isRemote,
selected: false // 所有分支默认不选中
};
});
@@ -1108,11 +1106,15 @@ class ConfigPanel {
throw new 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
});
}
@@ -1130,6 +1132,39 @@ class ConfigPanel {
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
}
}
/**
* 构建分支树状结构
*/
buildBranchTree(branches) {
const root = [];
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) => 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;
}
async cloneBranches(url, branches) {
try {
console.log('🚀 开始克隆分支:', { url, branches });

File diff suppressed because one or more lines are too long

View File

@@ -49,8 +49,8 @@ class ConfigView extends BaseView_1.BaseView {
</tr>
`;
}).join('');
// 生成分支选择的 HTML
const branchesHtml = gitBranches.length > 0 ? this.generateBranchesHtml(gitBranches) : '';
// 生成分支选择的 HTML - 使用树状结构
const branchesHtml = gitBranches.length > 0 ? this.generateBranchesTreeHtml(gitBranches) : '';
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
@@ -93,6 +93,65 @@ class ConfigView extends BaseView_1.BaseView {
background: var(--vscode-inputValidation-errorBackground);
opacity: 0.8;
}
/* 树状分支样式 */
.branch-tree {
font-family: 'Courier New', monospace;
user-select: none;
margin: 0;
padding: 0;
}
.branch-node {
padding: 2px 0;
display: flex;
align-items: center;
cursor: pointer;
line-height: 1.2;
}
.branch-node:hover {
background: var(--vscode-list-hoverBackground);
}
.branch-icon {
margin-right: 4px;
font-size: 16px;
width: 16px;
text-align: center;
}
.branch-expand {
margin-right: 2px;
cursor: pointer;
width: 14px;
text-align: center;
}
.branch-checkbox {
margin-right: 6px;
}
.branch-name {
flex: 1;
font-size: 13px;
}
.branch-children {
margin-left: 16px;
border-left: 1px solid var(--vscode-panel-border);
padding-left: 8px;
}
.branch-leaf {
margin-left: 16px;
}
.current-branch {
color: var(--vscode-gitDecoration-untrackedResourceForeground);
font-weight: bold;
}
/* 树图标样式 */
.tree-icon {
font-size: 2em;
}
/* 分支树标题样式 */
.branch-tree-title {
font-size: 1.2em;
}
</style>
</head>
<body>
@@ -150,6 +209,7 @@ class ConfigView extends BaseView_1.BaseView {
let currentConfigId = null;
let selectedBranches = new Set();
let currentRepoUrl = '';
let branchTreeData = [];
// 配置管理功能
function editConfigName(configId, currentName) {
@@ -271,7 +331,7 @@ class ConfigView extends BaseView_1.BaseView {
}
function toggleBranch(branchName) {
const checkbox = document.getElementById('branch-' + branchName.replace(/[^a-zA-Z0-9]/g, '-'));
const checkbox = document.getElementById('branch-' + branchName.replace(/[^a-zA-Z0-9-]/g, '-'));
if (checkbox.checked) {
selectedBranches.add(branchName);
} else {
@@ -332,35 +392,124 @@ class ConfigView extends BaseView_1.BaseView {
});
}
// 动态渲染分支选择区域
function renderBranchSelection(branches, repoUrl) {
// 树状分支功能
function toggleBranchNode(nodeId) {
const node = findNodeById(branchTreeData, nodeId);
if (node && !node.isLeaf) {
node.expanded = !node.expanded;
renderBranchTree(branchTreeData);
}
}
function expandAllBranches() {
setAllExpanded(branchTreeData, true);
renderBranchTree(branchTreeData);
}
function collapseAllBranches() {
setAllExpanded(branchTreeData, false);
renderBranchTree(branchTreeData);
}
function setAllExpanded(nodes, expanded) {
nodes.forEach(node => {
if (!node.isLeaf) {
node.expanded = expanded;
if (node.children) {
setAllExpanded(node.children, expanded);
}
}
});
}
function findNodeById(nodes, nodeId) {
for (const node of nodes) {
if (node.fullName === nodeId) return node;
if (node.children) {
const found = findNodeById(node.children, nodeId);
if (found) return found;
}
}
return null;
}
// 动态渲染树状分支
function renderBranchTree(treeData) {
const container = document.getElementById('branchSelectionContainer');
if (branches.length === 0) {
if (!treeData || treeData.length === 0) {
container.innerHTML = '<div style="text-align: center; padding: 10px; color: var(--vscode-descriptionForeground);">未找到分支</div>';
return;
}
let branchesHtml = '<div class="branch-selection" style="margin: 15px 0; padding: 15px; background: var(--vscode-panel-background); border-radius: 4px;">';
branchesHtml += '<h4>🌿 选择要克隆的分支 (共 ' + branches.length + ' 个)</h4>';
branchesHtml += '<div style="max-height: 200px; overflow-y: auto; margin: 10px 0;">';
let html = '<div class="branch-selection" style="margin: 10px 0; padding: 10px; background: var(--vscode-panel-background); border-radius: 4px;">';
html += '<h4 class="branch-tree-title" style="margin: 0 0 8px 0;"><span class="tree-icon">🌳</span> 分支树</h4>';
branches.forEach(branch => {
branchesHtml += '<div class="branch-item" style="padding: 8px; border-bottom: 1px solid var(--vscode-panel-border); display: flex; align-items: center;">';
branchesHtml += '<input type="checkbox" id="branch-' + branch.name.replace(/[^a-zA-Z0-9]/g, '-') + '" ';
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 += '</label></div>';
// 展开/收起按钮
html += '<div style="margin-bottom: 8px;">';
html += '<button class="back-btn" onclick="expandAllBranches()" style="margin-right: 8px; padding: 2px 6px; font-size: 11px;">展开全部</button>';
html += '<button class="back-btn" onclick="collapseAllBranches()" style="padding: 2px 6px; font-size: 11px;">收起全部</button>';
html += '</div>';
html += '<div class="branch-tree">';
html += renderBranchNodes(treeData, 0);
html += '</div>';
html += '<div style="margin-top: 12px;">';
html += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 8px; padding: 4px 8px; font-size: 12px;">✅ 克隆选中分支</button>';
html += '<button class="back-btn" onclick="cancelBranchSelection()" style="padding: 4px 8px; font-size: 12px;">取消</button>';
html += '</div></div>';
container.innerHTML = html;
}
function renderBranchNodes(nodes, level) {
let html = '';
nodes.forEach(node => {
const indent = level * 20;
const nodeId = node.fullName.replace(/[^a-zA-Z0-9-]/g, '-');
if (node.isLeaf) {
// 叶子节点(实际分支)
html += '<div class="branch-node branch-leaf" style="margin-left: ' + indent + 'px;">';
html += '<input type="checkbox" id="branch-' + nodeId + '" class="branch-checkbox" ';
html += (node.branch && node.branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\\'' + node.fullName + '\\')">';
html += '<span class="branch-icon">🌿</span>';
html += '<span class="branch-name ' + (node.branch && node.branch.isCurrent ? 'current-branch' : '') + '">';
html += node.name;
html += (node.branch && node.branch.isCurrent ? ' ⭐' : '');
html += '</span>';
html += '</div>';
} else {
// 文件夹节点
html += '<div class="branch-node" style="margin-left: ' + indent + 'px;">';
html += '<span class="branch-expand" onclick="toggleBranchNode(\\'' + node.fullName + '\\')">';
html += (node.expanded ? '🪵' : '🪵');
html += '</span>';
html += '<span class="branch-icon">🪵</span>';
html += '<span class="branch-name">' + node.name + '</span>';
html += '</div>';
if (node.expanded && node.children) {
html += '<div class="branch-children">';
html += renderBranchNodes(node.children, level + 1);
html += '</div>';
}
}
});
branchesHtml += '</div>';
branchesHtml += '<div style="margin-top: 15px;">';
branchesHtml += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 10px;">✅ 克隆选中分支</button>';
branchesHtml += '<button class="back-btn" onclick="cancelBranchSelection()">取消</button>';
branchesHtml += '</div></div>';
container.innerHTML = branchesHtml;
return html;
}
function countLeafNodes(nodes) {
let count = 0;
nodes.forEach(node => {
if (node.isLeaf) {
count++;
} else if (node.children) {
count += countLeafNodes(node.children);
}
});
return count;
}
// 对话框函数
@@ -450,7 +599,9 @@ class ConfigView extends BaseView_1.BaseView {
if (message.type === 'branchesFetched') {
console.log('🌿 收到分支数据:', message.branches);
renderBranchSelection(message.branches, message.repoUrl);
console.log('🌳 收到分支树数据:', message.branchTree);
branchTreeData = message.branchTree || [];
renderBranchTree(branchTreeData);
}
});
@@ -462,27 +613,113 @@ class ConfigView extends BaseView_1.BaseView {
</body>
</html>`;
}
generateBranchesHtml(branches) {
generateBranchesTreeHtml(branches) {
if (branches.length === 0)
return '';
let html = '<div class="branch-selection" style="margin: 15px 0; padding: 15px; background: var(--vscode-panel-background); border-radius: 4px;">';
html += '<h4>🌿 选择要克隆的分支 (共 ' + branches.length + ' 个)</h4>';
html += '<div style="max-height: 200px; overflow-y: auto; margin: 10px 0;">';
branches.forEach(branch => {
const branchId = 'branch-' + branch.name.replace(/[^a-zA-Z0-9]/g, '-');
html += '<div class="branch-item" style="padding: 8px; border-bottom: 1px solid var(--vscode-panel-border); display: flex; align-items: center;">';
html += '<input type="checkbox" id="' + branchId + '" ';
html += (branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\'' + branch.name + '\')" style="margin-right: 10px;">';
html += '<label for="' + branchId + '" style="flex: 1; cursor: pointer;">';
html += (branch.isCurrent ? '⭐ ' : '') + branch.name;
html += (branch.isRemote ? '(远程)' : '(本地)') + '</label></div>';
});
html += '<div style="margin-top: 15px;">';
html += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 10px;">✅ 克隆选中分支</button>';
html += '<button class="back-btn" onclick="cancelBranchSelection()">取消</button>';
// 构建分支树
const branchTree = this.buildBranchTree(branches);
let html = '<div class="branch-selection" style="margin: 10px 0; padding: 10px; background: var(--vscode-panel-background); border-radius: 4px;">';
html += '<h4 class="branch-tree-title" style="margin: 0 0 8px 0;"><span class="tree-icon">🌳</span> 分支树</h4>';
html += '<div style="margin-bottom: 8px;">';
html += '<button class="back-btn" onclick="expandAllBranches()" style="margin-right: 8px; padding: 2px 6px; font-size: 11px;">展开全部</button>';
html += '<button class="back-btn" onclick="collapseAllBranches()" style="padding: 2px 6px; font-size: 11px;">收起全部</button>';
html += '</div>';
html += '<div class="branch-tree">';
html += this.renderBranchTreeNodes(branchTree, 0);
html += '</div>';
html += '<div style="margin-top: 12px;">';
html += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 8px; padding: 4px 8px; font-size: 12px;">✅ 克隆选中分支</button>';
html += '<button class="back-btn" onclick="cancelBranchSelection()" style="padding: 4px 8px; font-size: 12px;">取消</button>';
html += '</div></div>';
return html;
}
/**
* 构建分支树状结构
*/
buildBranchTree(branches) {
const root = [];
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 => n.name === part);
if (!node) {
node = {
name: part,
fullName: fullName,
isLeaf: isLeaf,
children: [],
level: i
};
currentLevel.push(node);
}
if (isLeaf) {
node.branch = branch;
}
else {
node.expanded = node.expanded || true; // 默认展开
}
currentLevel = node.children;
}
});
return root;
}
/**
* 渲染分支树节点
*/
renderBranchTreeNodes(nodes, level) {
let html = '';
nodes.forEach(node => {
const indent = level * 20;
const nodeId = node.fullName.replace(/[^a-zA-Z0-9-]/g, '-');
if (node.isLeaf) {
// 叶子节点(实际分支)
html += '<div class="branch-node branch-leaf" style="margin-left: ' + indent + 'px;">';
html += '<input type="checkbox" id="branch-' + nodeId + '" class="branch-checkbox" ';
html += (node.branch && node.branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\'' + node.fullName + '\')">';
html += '<span class="branch-icon">🌿</span>';
html += '<span class="branch-name ' + (node.branch && node.branch.isCurrent ? 'current-branch' : '') + '">';
html += node.name;
html += (node.branch && node.branch.isCurrent ? ' ⭐' : '');
html += '</span>';
html += '</div>';
}
else {
// 文件夹节点
html += '<div class="branch-node" style="margin-left: ' + indent + 'px;">';
html += '<span class="branch-expand" onclick="toggleBranchNode(\'' + node.fullName + '\')">';
html += (node.expanded ? '🪵' : '🪵');
html += '</span>';
html += '<span class="branch-icon">🪵</span>';
html += '<span class="branch-name">' + node.name + '</span>';
html += '</div>';
if (node.expanded && node.children && node.children.length > 0) {
html += '<div class="branch-children">';
html += this.renderBranchTreeNodes(node.children, level + 1);
html += '</div>';
}
}
});
return html;
}
/**
* 计算叶子节点数量
*/
countLeafNodes(nodes) {
let count = 0;
nodes.forEach(node => {
if (node.isLeaf) {
count++;
}
else {
count += this.countLeafNodes(node.children);
}
});
return count;
}
}
exports.ConfigView = ConfigView;
//# sourceMappingURL=ConfigView.js.map

File diff suppressed because one or more lines are too long