0
0

修改上传逻辑,增加了分支名输入的功能

This commit is contained in:
xubing
2025-12-03 21:40:53 +08:00
parent 9a34cd24e4
commit 5aae34ea36
7 changed files with 375 additions and 136 deletions

View File

@@ -1,4 +1,3 @@
// src/panels/ConfigPanel.ts
import * as vscode from 'vscode';
import * as path from 'path';
import { ProjectView } from './views/ProjectView';
@@ -41,10 +40,6 @@ export class ConfigPanel {
private currentContainerId: string = '';
private currentModuleFolderId: string = '';
// 上传相关
private pendingUploadFolderId: string | null = null;
private pendingGitUploadFolderId: string | null = null;
// 仓库配置
private repoConfigs: RepoConfigItem[] = [];
private currentRepoForBranches: RepoConfigItem | undefined;
@@ -125,7 +120,7 @@ export class ConfigPanel {
}
/**
* 弹出仓库选择弹窗
* 弹出仓库选择弹窗(仅用于“获取仓库 -> 获取分支”)
*/
private async openRepoSelect(): Promise<void> {
await this.loadRepoConfigs();
@@ -144,7 +139,28 @@ export class ConfigPanel {
}
/**
* 仓库选择确认后的统一入口
* 弹出“上传代码”时的仓库 + 分支选择弹窗
*/
private async openUploadRepoSelect(folderId: string, folderType: 'git' | 'local'): Promise<void> {
await this.loadRepoConfigs();
if (this.repoConfigs.length === 0) {
vscode.window.showWarningMessage('尚未配置任何仓库,请先点击右上角 "仓库配置" 按钮编辑 dcsp-repos.json。');
return;
}
if (this.isWebviewDisposed) return;
this.panel.webview.postMessage({
type: 'showUploadRepoSelect',
repos: this.repoConfigs.map(r => ({ name: r.name })),
folderId,
folderType
});
}
/**
* “获取仓库”弹窗确认后:用于拉取分支
*/
private async handleRepoSelectedForBranches(repoName: string): Promise<void> {
await this.loadRepoConfigs();
@@ -155,51 +171,55 @@ export class ConfigPanel {
return;
}
// 1⃣ Local 模块上传local -> git
if (this.pendingUploadFolderId) {
const localFolderId = this.pendingUploadFolderId;
this.pendingUploadFolderId = null;
await this.processLocalUpload(localFolderId, repo);
return;
}
// 2⃣ Git 模块上传git -> repo
if (this.pendingGitUploadFolderId) {
const gitFolderId = this.pendingGitUploadFolderId;
this.pendingGitUploadFolderId = null;
await this.processGitUpload(gitFolderId, repo);
return;
}
// 3⃣ 默认:获取分支(用于"获取仓库"-> 克隆)
this.currentRepoForBranches = repo;
await this.fetchBranchesForRepo(repo);
}
private async processLocalUpload(folderId: string, repo: RepoConfigItem): Promise<void> {
const folder = this.projectService.getModuleFolder(folderId);
if (!folder || folder.type !== 'local') {
vscode.window.showErrorMessage("未找到要上传的本地模块文件夹");
/**
* “上传代码”弹窗确认后:根据 folderType 决定上传逻辑,并使用用户输入的 branchName
*/
private async handleUploadRepoSelected(
folderId: string,
folderType: 'git' | 'local',
repoName: string,
branchName: string
): Promise<void> {
const trimmedBranch = (branchName || '').trim();
if (!trimmedBranch) {
vscode.window.showErrorMessage('分支名称不能为空');
return;
}
const fullPath = this.projectService.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage("无法确定本地模块文件夹路径");
await this.loadRepoConfigs();
const repo = this.repoConfigs.find(r => r.name === repoName);
if (!repo) {
vscode.window.showErrorMessage(`在仓库配置中未找到名为 "${repoName}" 的仓库`);
return;
}
const branchName = path.basename(fullPath);
await this.uploadLocalModuleFolder(
folderId,
repo.url,
branchName,
repo.username,
repo.token
);
if (folderType === 'local') {
// 本地模块 -> 选中仓库 + 指定分支
await this.uploadLocalModuleFolder(
folderId,
repo.url,
trimmedBranch,
repo.username,
repo.token
);
} else {
// Git 模块 -> 选中仓库 + 指定分支
await this.processGitUploadWithBranch(folderId, repo, trimmedBranch);
}
}
private async processGitUpload(folderId: string, repo: RepoConfigItem): Promise<void> {
/**
* Git 模块上传(可能换仓库 / 改名),使用用户指定的分支
*/
private async processGitUploadWithBranch(
folderId: string,
repo: RepoConfigItem,
branchName: string
): Promise<void> {
const folder = this.projectService.getModuleFolder(folderId);
if (!folder || folder.type !== 'git') {
vscode.window.showErrorMessage("未找到要上传的 Git 模块文件夹");
@@ -218,13 +238,13 @@ export class ConfigPanel {
return;
}
// 2.1 未改名 + 还是原来的仓库 → 直接在原分支上 push更新代码
// 未改名 + 还是原来的仓库 → 直接在原分支上 push更新代码
if (!isRenamed && isSameRepo) {
await this.uploadGitModuleFolder(folderId);
return;
}
// 2.2 改了名字 或 换了仓库 → 使用"当前文件夹名"作为分支,推送到选中仓库
// 改了名字 或 换了仓库 → 使用“用户输入的分支名”进行推送
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: `正在上传 Git 仓库: ${folder.name}`,
@@ -236,12 +256,11 @@ export class ConfigPanel {
await GitService.pushToRepoUrl(
fullPath,
repo.url,
newFolderName,
branchName,
repo.username,
repo.token
);
this.projectService.updateModuleFolder(folderId, {
uploaded: true,
originalFolderName: newFolderName,
@@ -251,7 +270,7 @@ export class ConfigPanel {
await this.saveCurrentProjectData();
progress.report({ increment: 100, message: '完成' });
vscode.window.showInformationMessage(`✅ Git 仓库已上传到 ${repo.name} 的分支 ${newFolderName}`);
vscode.window.showInformationMessage(`✅ Git 仓库已上传到 ${repo.name} 的分支 ${branchName}`);
this.updateWebview();
} catch (error: any) {
console.error('❌ Git 上传到新仓库/分支失败:', error);
@@ -336,10 +355,17 @@ export class ConfigPanel {
// 上传功能
'uploadGitModuleFolder': (data) => this.uploadGitModuleFolder(data.folderId, data.username, data.password),
'openRepoSelectForUpload': (data) => this.handleOpenRepoSelectForUpload(data.folderId),
'openRepoSelectForUpload': (data) => this.openUploadRepoSelect(data.folderId, 'local'),
'uploadLocalModuleFolder': (data) => this.uploadLocalModuleFolder(data.folderId, data.repoUrl, data.branchName),
'openRepoSelectForGitUpload': (data) => this.openRepoSelectForGitUpload(data.folderId),
'repoSelectedForGitUpload': (data) => this.handleRepoSelectedForGitUpload(data.folderId, data.repoName)
'openRepoSelectForGitUpload': (data) => this.openUploadRepoSelect(data.folderId, 'git'),
// 旧的 repoSelectedForGitUpload 保留兼容的话可以转发到 handleRepoSelectedForBranches但现在不再使用
// 新增:上传时仓库 + 分支选择确认
'uploadRepoSelected': (data) => this.handleUploadRepoSelected(
data.folderId,
data.folderType,
data.repoName,
data.branchName
)
};
const handler = messageHandlers[data.type];
@@ -409,24 +435,6 @@ export class ConfigPanel {
this.updateWebview();
}
private async handleOpenRepoSelectForUpload(folderId: string): Promise<void> {
console.log("📌 Local 上传:收到 openRepoSelectForUploadfolderId =", folderId);
this.pendingUploadFolderId = folderId;
await this.openRepoSelect();
}
private async openRepoSelectForGitUpload(folderId: string): Promise<void> {
console.log('📌 Git 上传:收到 openRepoSelectForGitUploadfolderId =', folderId);
this.pendingGitUploadFolderId = folderId;
await this.openRepoSelect();
}
private async handleRepoSelectedForGitUpload(folderId: string, repoName: string): Promise<void> {
// 兼容:如果前端用的是 repoSelectedForGitUpload 事件,则这里转成统一逻辑
this.pendingGitUploadFolderId = folderId;
await this.handleRepoSelectedForBranches(repoName);
}
// =============================================
// 项目管理方法
// =============================================
@@ -755,7 +763,14 @@ export class ConfigPanel {
console.log(`📥 开始克隆分支: ${branch}`);
try {
const folderNames = GitService.generateModuleFolderName(url, branch);
await this.addGitModuleFolder(url,repoDisplayName,folderNames.folderName,branch,this.currentRepoForBranches?.username,this.currentRepoForBranches?.token);
await this.addGitModuleFolder(
url,
repoDisplayName,
folderNames.folderName,
branch,
this.currentRepoForBranches?.username,
this.currentRepoForBranches?.token
);
successCount++;
console.log(`✅ 分支克隆成功: ${branch}`);
@@ -779,13 +794,13 @@ export class ConfigPanel {
}
private async addGitModuleFolder(
url: string,
displayName: string,
folderName: string,
branch?: string,
username?: string,
token?: string
): Promise<void> {
url: string,
displayName: string,
folderName: string,
branch?: string,
username?: string,
token?: string
): Promise<void> {
try {
if (!url || !url.startsWith('http')) {
vscode.window.showErrorMessage('请输入有效的 Git 仓库 URL');
@@ -1100,7 +1115,7 @@ export class ConfigPanel {
await GitService.initRepository(fullPath, branchName);
progress.report({ increment: 20, message: '添加远程仓库...' });
await GitService.addRemote(fullPath, repoUrl);
await GitService.addRemote(fullPath, repoUrl, username, token);
progress.report({ increment: 40, message: '提交初始文件...' });
await GitService.commitInitialFiles(fullPath);
@@ -1505,4 +1520,4 @@ export class ConfigPanel {
});
}
}
}
}