0
0

给项目页面、飞行器页面

This commit is contained in:
xubing
2025-12-05 21:15:33 +08:00
parent 2c314b9b0b
commit 46c184b920
18 changed files with 498 additions and 57 deletions

View File

@@ -164,17 +164,38 @@ class GitService {
* 初始化Git仓库
*/
static async initRepository(localPath, branchName) {
// 使用 -b 参数初始化时直接设置分支名
await execAsync(`git init && git checkout -b "${branchName}"`, { cwd: localPath });
}
/**
* 移除远程仓库
*/
static async removeRemote(localPath) {
try {
await execAsync('git remote remove origin', { cwd: localPath });
}
catch (error) {
// 忽略“没有该远程仓库”的错误
if (error.stderr && !error.stderr.includes("No such remote")) {
throw error;
}
if (error.stdout && !error.stdout.includes("No such remote")) {
throw error;
}
}
}
/**
* 添加远程仓库
*/
static async addRemote(localPath, repoUrl, username, token) {
let finalUrl = repoUrl;
if (username && token) {
const u = encodeURIComponent(username);
const t = encodeURIComponent(token);
finalUrl = repoUrl.replace('://', `://${u}:${t}@`);
if (username || token) {
const u = encodeURIComponent(username || '');
const t = encodeURIComponent(token || '');
// 注意: 仅在 URL 是 http/https 时添加 auth
if (repoUrl.startsWith('http')) {
finalUrl = repoUrl.replace('://', `://${u}:${t}@`);
}
}
await execAsync(`git remote add origin "${finalUrl}"`, { cwd: localPath });
}
@@ -202,7 +223,7 @@ class GitService {
await execAsync(`git push -u origin "${branchName}" --force`, { cwd: localPath });
}
/**
* 使用Git命令提交并推送
* 使用Git命令提交并推送 (用于原有的 Git 模块更新)
*/
static async commitAndPush(localPath) {
await execAsync('git add .', { cwd: localPath });
@@ -238,6 +259,29 @@ class GitService {
];
await execAsync(commands.join(' && '), { cwd: localPath });
}
/**
* 提交并推送到指定分支和远程 (用于 Project/Aircraft/Container 上传)
*/
static async commitAndPushToBranch(localPath, branchName, repoUrl, // 这里的 repoUrl 是为了构造带 auth 的 finalUrl
username, token) {
let finalUrl = repoUrl || 'origin';
if (repoUrl && (username || token)) {
const u = encodeURIComponent(username || '');
const t = encodeURIComponent(token || '');
// 注意: 仅在 URL 是 http/https 时添加 auth
if (repoUrl.startsWith('http')) {
finalUrl = repoUrl.replace('://', `://${u}:${t}@`);
}
}
const commands = [
'git add .',
// 允许没有更改时 commit 失败 (Exit code 1),所以使用 || true
`git commit -m "Auto commit from DCSP - ${new Date().toLocaleString()}" || true`,
`git checkout -B "${branchName}"`,
`git push -u "${finalUrl}" "${branchName}" --force` // 强制推送
];
await execAsync(commands.join(' && '), { cwd: localPath });
}
/**
* 生成模块文件夹名称
*/
@@ -257,9 +301,9 @@ class GitService {
/**
* 构建文件树
* 这里显式忽略:
* - .git 目录
* - .dcsp-data.json
* - 其它以 . 开头的隐藏文件/目录
* - .git 目录
* - .dcsp-data.json
* - 其它以 . 开头的隐藏文件/目录
*/
static async buildFileTree(dir, relativePath = '') {
try {

File diff suppressed because one or more lines are too long