添加了代码上传功能,但是存在bug待修复
This commit is contained in:
@@ -311,6 +311,14 @@ export class ConfigPanel {
|
||||
case 'openTheModuleFolder':
|
||||
await this.openTheModuleFolder(data.moduleType, data.id);
|
||||
break;
|
||||
|
||||
case 'uploadGitModuleFolder':
|
||||
await this.uploadGitModuleFolder(data.folderId, data.username, data.password);
|
||||
break;
|
||||
|
||||
case 'uploadLocalModuleFolder':
|
||||
await this.uploadLocalModuleFolder(data.folderId, data.repoUrl, data.branchName);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('处理 Webview 消息时出错:', error);
|
||||
@@ -1911,4 +1919,398 @@ export class ConfigPanel {
|
||||
const folderName = pathParts[pathParts.length - 1];
|
||||
return path.join(projectPath, aircraft.name, container.name, folderName);
|
||||
}
|
||||
|
||||
private async uploadGitModuleFolder(folderId: string, username: string, password: string): Promise<void> {
|
||||
const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
if (!folder || folder.type !== 'git') {
|
||||
vscode.window.showErrorMessage('未找到指定的 Git 模块文件夹');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取完整路径
|
||||
const fullPath = this.getModuleFolderFullPath(folder);
|
||||
if (!fullPath) {
|
||||
vscode.window.showErrorMessage('无法获取模块文件夹的完整路径');
|
||||
return;
|
||||
}
|
||||
|
||||
await vscode.window.withProgress({
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: `正在上传 Git 仓库: ${folder.name}`,
|
||||
cancellable: false
|
||||
}, async (progress) => {
|
||||
try {
|
||||
progress.report({ increment: 0, message: '检查更改...' });
|
||||
|
||||
// 使用命令行 Git 提交并推送
|
||||
await this.commitAndPushUsingCommandLine(fullPath);
|
||||
|
||||
progress.report({ increment: 100, message: '完成' });
|
||||
|
||||
vscode.window.showInformationMessage(`✅ Git 仓库上传成功: ${folder.name}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Git 上传失败:', error);
|
||||
vscode.window.showErrorMessage(`Git 仓库上传失败: ${error.message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传本地模块文件夹到新的 Git 仓库(使用命令行 Git)
|
||||
*/
|
||||
private async uploadLocalModuleFolder(folderId: string, repoUrl: string, branchName: string): Promise<void> {
|
||||
const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
if (!folder || folder.type !== 'local') {
|
||||
vscode.window.showErrorMessage('未找到指定的本地模块文件夹');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取完整路径
|
||||
const fullPath = this.getModuleFolderFullPath(folder);
|
||||
if (!fullPath) {
|
||||
vscode.window.showErrorMessage('无法获取模块文件夹的完整路径');
|
||||
return;
|
||||
}
|
||||
|
||||
await vscode.window.withProgress({
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: `正在上传本地文件夹到 Git 仓库: ${folder.name}`,
|
||||
cancellable: false
|
||||
}, async (progress) => {
|
||||
try {
|
||||
progress.report({ increment: 0, message: '检查目录...' });
|
||||
|
||||
// 检查目录是否存在
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
throw new Error('本地文件夹不存在');
|
||||
}
|
||||
|
||||
progress.report({ increment: 10, message: '初始化 Git 仓库...' });
|
||||
|
||||
// 使用命令行 Git 初始化仓库
|
||||
await this.initGitRepository(fullPath, branchName);
|
||||
|
||||
progress.report({ increment: 20, message: '添加远程仓库...' });
|
||||
|
||||
// 使用命令行 Git 添加远程仓库
|
||||
await this.addGitRemote(fullPath, repoUrl);
|
||||
|
||||
progress.report({ increment: 40, message: '提交初始文件...' });
|
||||
|
||||
// 使用命令行 Git 提交初始文件
|
||||
await this.commitInitialFiles(fullPath);
|
||||
|
||||
progress.report({ increment: 60, message: '推送到远程仓库...' });
|
||||
|
||||
// 使用命令行 Git 推送到远程仓库
|
||||
await this.pushToRemoteWithForce(fullPath, branchName);
|
||||
|
||||
progress.report({ increment: 100, message: '完成' });
|
||||
|
||||
// 更新文件夹类型为 git
|
||||
folder.type = 'git';
|
||||
await this.saveCurrentProjectData();
|
||||
|
||||
vscode.window.showInformationMessage(`本地文件夹成功上传到 Git 仓库: ${folder.name} -> ${branchName}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 本地文件夹上传失败:', error);
|
||||
vscode.window.showErrorMessage(`本地文件夹上传失败: ${error}`);
|
||||
|
||||
// 清理:删除可能创建的 .git 文件夹
|
||||
try {
|
||||
const gitDir = path.join(fullPath, '.git');
|
||||
if (fs.existsSync(gitDir)) {
|
||||
await fs.promises.rm(gitDir, { recursive: true, force: true });
|
||||
}
|
||||
} catch (cleanupError) {
|
||||
console.error('清理 .git 文件夹失败:', cleanupError);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用命令行 Git 初始化仓库
|
||||
*/
|
||||
private async initGitRepository(fullPath: string, branchName: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('📁 初始化 Git 仓库...');
|
||||
|
||||
exec(`git init && git checkout -b ${branchName}`, {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error('❌ Git 初始化失败:', error);
|
||||
reject(new Error(`Git 初始化失败: ${stderr || error.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ Git 初始化成功');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用命令行 Git 添加远程仓库
|
||||
*/
|
||||
private async addGitRemote(fullPath: string, repoUrl: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('📡 添加远程仓库...');
|
||||
|
||||
exec(`git remote add origin ${repoUrl}`, {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error('❌ 添加远程仓库失败:', error);
|
||||
reject(new Error(`添加远程仓库失败: ${stderr || error.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ 远程仓库添加成功');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用命令行 Git 提交初始文件
|
||||
*/
|
||||
private async commitInitialFiles(fullPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('💾 提交初始文件...');
|
||||
|
||||
const commands = [
|
||||
'git add .',
|
||||
`git commit -m "Initial commit from DCSP - ${new Date().toLocaleString()}"`
|
||||
];
|
||||
|
||||
exec(commands.join(' && '), {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
// 检查是否是"没有更改可提交"的错误
|
||||
if (stderr.includes('nothing to commit') || stdout.includes('nothing to commit')) {
|
||||
console.log('ℹ️ 没有需要提交的更改');
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
console.error('❌ Git 提交失败:', error);
|
||||
reject(new Error(`Git 提交失败: ${stderr || error.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ 初始文件提交成功');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用命令行 Git 强制推送到远程仓库
|
||||
*/
|
||||
private async pushToRemoteWithForce(fullPath: string, branchName: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('🚀 强制推送到远程仓库...');
|
||||
|
||||
exec(`git push -u origin ${branchName} --force`, {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
console.log('📋 Git push stdout:', stdout);
|
||||
console.log('📋 Git push stderr:', stderr);
|
||||
|
||||
if (error) {
|
||||
console.error('❌ Git 推送失败:', error);
|
||||
|
||||
// 提供更友好的错误信息
|
||||
let userMessage = stderr || error.message;
|
||||
|
||||
if (stderr.includes('Authentication failed') || stderr.includes('401')) {
|
||||
userMessage = '认证失败:请确保 Git 凭据已正确配置,或者使用 Personal Access Token';
|
||||
} else if (stderr.includes('Permission denied') || stderr.includes('403')) {
|
||||
userMessage = '权限被拒绝:请检查您是否有权限推送到该仓库';
|
||||
} else if (stderr.includes('could not read Username')) {
|
||||
userMessage = '认证配置问题:请检查 Git 凭据存储配置';
|
||||
}
|
||||
|
||||
reject(new Error(userMessage));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ Git 推送成功');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async getCurrentBranch(dir: string): Promise<string> {
|
||||
try {
|
||||
const currentBranch = await git.currentBranch({
|
||||
fs: fs,
|
||||
dir: dir,
|
||||
fullname: false
|
||||
});
|
||||
|
||||
if (!currentBranch) {
|
||||
// 如果没有当前分支,尝试从 HEAD 文件读取
|
||||
const headPath = path.join(dir, '.git', 'HEAD');
|
||||
if (fs.existsSync(headPath)) {
|
||||
const headContent = await fs.promises.readFile(headPath, 'utf8');
|
||||
const match = headContent.match(/ref: refs\/heads\/(.+)/);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
}
|
||||
throw new Error('无法确定当前分支');
|
||||
}
|
||||
|
||||
return currentBranch;
|
||||
} catch (error) {
|
||||
console.error('获取当前分支失败:', error);
|
||||
throw new Error(`获取当前分支失败: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用命令行 Git 进行推送(带详细调试)
|
||||
*/
|
||||
private async pushUsingCommandLine(fullPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('🚀 使用命令行 Git 推送...');
|
||||
console.log(`📁 工作目录: ${fullPath}`);
|
||||
|
||||
// 先检查 Git 状态
|
||||
exec('git status', {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (statusError, statusStdout, statusStderr) => {
|
||||
console.log('🔍 Git 状态:', statusStdout);
|
||||
|
||||
// 然后执行推送
|
||||
exec('git push', {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
console.log('📋 Git push stdout:', stdout);
|
||||
console.log('📋 Git push stderr:', stderr);
|
||||
|
||||
if (error) {
|
||||
console.error('❌ Git push 失败:', error);
|
||||
reject(new Error(`Git 推送失败: ${stderr || error.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查实际的推送结果
|
||||
if (stdout.includes('Everything up-to-date')) {
|
||||
console.log('ℹ️ 没有需要推送的更改');
|
||||
resolve();
|
||||
} else if (stdout.includes('To http') || stdout.includes('To https') || stdout.includes('To git@')) {
|
||||
console.log('✅ Git 推送成功');
|
||||
resolve();
|
||||
} else {
|
||||
console.log('⚠️ 推送结果不明确,可能需要手动检查');
|
||||
reject(new Error('推送结果不明确,请检查远程仓库'));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 Git 远程配置
|
||||
*/
|
||||
private async checkGitRemote(fullPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('🔍 检查 Git 远程配置...');
|
||||
|
||||
exec('git remote -v', {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error('检查远程配置失败:', error);
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('📡 Git 远程配置:', stdout);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用命令行 Git 提交并推送
|
||||
*/
|
||||
private async commitAndPushUsingCommandLine(fullPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { exec } = require('child_process');
|
||||
|
||||
console.log('🚀 使用命令行 Git 提交并推送...');
|
||||
console.log(`📁 工作目录: ${fullPath}`);
|
||||
|
||||
// 先添加所有更改并提交,然后推送
|
||||
const commands = [
|
||||
'git add .',
|
||||
`git commit -m "Auto commit from DCSP - ${new Date().toLocaleString()}"`,
|
||||
'git push'
|
||||
];
|
||||
|
||||
exec(commands.join(' && '), {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (error, stdout, stderr) => {
|
||||
console.log('📋 Git 命令输出:', stdout);
|
||||
console.log('📋 Git 命令错误:', stderr);
|
||||
|
||||
if (error) {
|
||||
// 检查是否是"没有更改可提交"的错误
|
||||
if (stderr.includes('nothing to commit') || stdout.includes('nothing to commit')) {
|
||||
console.log('ℹ️ 没有需要提交的更改,直接推送');
|
||||
// 如果没有更改,直接推送
|
||||
exec('git push', {
|
||||
cwd: fullPath,
|
||||
encoding: 'utf8'
|
||||
}, (pushError, pushStdout, pushStderr) => {
|
||||
if (pushError) {
|
||||
console.error('❌ Git push 失败:', pushError);
|
||||
reject(new Error(`Git 推送失败: ${pushStderr || pushError.message}`));
|
||||
return;
|
||||
}
|
||||
console.log('✅ Git 推送成功');
|
||||
resolve();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
console.error('❌ Git 提交/推送失败:', error);
|
||||
reject(new Error(`Git 操作失败: ${stderr || error.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ Git 提交并推送成功');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user