0
0

修复了上传的bug,现在功能是完全的,ui待完善

This commit is contained in:
xubing
2025-11-28 16:54:23 +08:00
parent e2a4b7a80d
commit cd58f6d8ed
7 changed files with 260 additions and 181 deletions

View File

@@ -1942,23 +1942,33 @@ private async uploadGitModuleFolder(folderId: string, username: string, password
try {
progress.report({ increment: 0, message: '检查更改...' });
// 使用命令行 Git 提交并推送
// 使用改进的 Git 提交并推送
await this.commitAndPushUsingCommandLine(fullPath);
progress.report({ increment: 100, message: '完成' });
// 添加上传成功标记
folder.uploaded = true;
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`✅ Git 仓库上传成功: ${folder.name}`);
// 更新 Webview 显示
this.updateWebview();
} catch (error) {
console.error('❌ Git 上传失败:', error);
vscode.window.showErrorMessage(`Git 仓库上传失败: ${error.message}`);
// 如果是"没有更改"的错误,显示友好提示
if (error.message.includes('没有需要提交的更改')) {
vscode.window.showWarningMessage(`没有检测到更改,无需上传: ${folder.name}`);
} else {
vscode.window.showErrorMessage(`推送失败: ${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') {
@@ -2008,15 +2018,19 @@ private async uploadLocalModuleFolder(folderId: string, repoUrl: string, branchN
progress.report({ increment: 100, message: '完成' });
// 更新文件夹类型为 git
// 更新文件夹类型为 git 并标记已上传
folder.type = 'git';
folder.uploaded = true;
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`本地文件夹成功上传到 Git 仓库: ${folder.name} -> ${branchName}`);
// 更新 Webview 显示
this.updateWebview();
} catch (error) {
console.error('❌ 本地文件夹上传失败:', error);
vscode.window.showErrorMessage(`本地文件夹上传失败: ${error}`);
vscode.window.showErrorMessage(`推送失败: ${error.message}`);
// 清理:删除可能创建的 .git 文件夹
try {
@@ -2032,7 +2046,7 @@ private async uploadLocalModuleFolder(folderId: string, repoUrl: string, branchN
}
/**
* 使用命令行 Git 初始化仓库
* 使用命令行 Git 初始化仓库(简化版本)
*/
private async initGitRepository(fullPath: string, branchName: string): Promise<void> {
return new Promise((resolve, reject) => {
@@ -2046,7 +2060,7 @@ private async initGitRepository(fullPath: string, branchName: string): Promise<v
}, (error, stdout, stderr) => {
if (error) {
console.error('❌ Git 初始化失败:', error);
reject(new Error(`Git 初始化失败: ${stderr || error.message}`));
reject(new Error(stderr || error.message));
return;
}
@@ -2057,7 +2071,7 @@ private async initGitRepository(fullPath: string, branchName: string): Promise<v
}
/**
* 使用命令行 Git 添加远程仓库
* 使用命令行 Git 添加远程仓库(简化版本)
*/
private async addGitRemote(fullPath: string, repoUrl: string): Promise<void> {
return new Promise((resolve, reject) => {
@@ -2071,7 +2085,7 @@ private async addGitRemote(fullPath: string, repoUrl: string): Promise<void> {
}, (error, stdout, stderr) => {
if (error) {
console.error('❌ 添加远程仓库失败:', error);
reject(new Error(`添加远程仓库失败: ${stderr || error.message}`));
reject(new Error(stderr || error.message));
return;
}
@@ -2082,7 +2096,7 @@ private async addGitRemote(fullPath: string, repoUrl: string): Promise<void> {
}
/**
* 使用命令行 Git 提交初始文件
* 使用命令行 Git 提交初始文件(简化版本)
*/
private async commitInitialFiles(fullPath: string): Promise<void> {
return new Promise((resolve, reject) => {
@@ -2108,7 +2122,7 @@ private async commitInitialFiles(fullPath: string): Promise<void> {
}
console.error('❌ Git 提交失败:', error);
reject(new Error(`Git 提交失败: ${stderr || error.message}`));
reject(new Error(stderr || error.message));
return;
}
@@ -2119,7 +2133,7 @@ private async commitInitialFiles(fullPath: string): Promise<void> {
}
/**
* 使用命令行 Git 强制推送到远程仓库
* 使用命令行 Git 强制推送到远程仓库(简化版本)
*/
private async pushToRemoteWithForce(fullPath: string, branchName: string): Promise<void> {
return new Promise((resolve, reject) => {
@@ -2136,19 +2150,7 @@ private async pushToRemoteWithForce(fullPath: string, branchName: string): Promi
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));
reject(new Error(stderr || error.message));
return;
}
@@ -2259,7 +2261,7 @@ private async checkGitRemote(fullPath: string): Promise<void> {
}
/**
* 使用命令行 Git 提交并推送
* 改进的 Git 提交并推送方法
*/
private async commitAndPushUsingCommandLine(fullPath: string): Promise<void> {
return new Promise((resolve, reject) => {
@@ -2268,49 +2270,77 @@ private async commitAndPushUsingCommandLine(fullPath: string): Promise<void> {
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(' && '), {
// 先检查是否有更改
exec('git status --porcelain', {
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}`));
}, (statusError, statusStdout, statusStderr) => {
if (statusError) {
console.error('❌ 检查 Git 状态失败:', statusError);
reject(new Error(`检查 Git 状态失败: ${statusStderr || statusError.message}`));
return;
}
console.log('✅ Git 提交并推送成功');
resolve();
// 如果没有更改
if (!statusStdout.trim()) {
console.log(' 没有需要提交的更改');
reject(new Error('没有需要提交的更改'));
return;
}
console.log('📋 检测到更改:', statusStdout);
// 有更改时才提交并推送
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) {
console.error('❌ Git 提交/推送失败:', error);
reject(new Error(stderr || error.message));
return;
}
console.log('✅ Git 提交并推送成功');
resolve();
});
});
});
}
/**
* 简化的推送方法
*/
private simplePush(fullPath: string, resolve: any, reject: any): void {
const { exec } = require('child_process');
exec('git push', {
cwd: fullPath,
encoding: 'utf8'
}, (pushError, pushStdout, pushStderr) => {
console.log('📋 Git push 输出:', pushStdout);
console.log('📋 Git push 错误:', pushStderr);
if (pushError) {
console.error('❌ Git push 失败');
// 直接使用 Git 的错误信息
reject(new Error(pushStderr || pushError.message));
return;
}
// 只要没有错误就认为是成功
console.log('✅ Git 推送成功');
resolve();
});
}
}