This commit is contained in:
LZX committed 2026-09-18 18:16:21 +08:00
1 parent 7a3db86a10
commit 3df0cbc7c0
10 files changed
+892 -38

No files matched your search

+30 -3
View File
@@ -157,10 +157,14 @@ class GitService {
}
}
/// 自动确保 Git 基础配置就绪(safe.directory 豁免 + 默认提交者占位符)
/// 自动确保 Git 基础配置就绪(safe.directory 豁免 + 默认提交者占位符 + 避免 link2symlink 逃逸)
Future<void> ensureGitUserConfigured() async {
try {
await _runProcess('git', ['config', '--global', '--add', 'safe.directory', '*']);
// 核心修复:强制 Git 使用 rename 而非 link() 创建对象,
// 根除 PRoot --link2symlink 将对象伪装软链接放入容器内部 rootfs/.l2s,
// 保证容器重建时项目目录内的 Git loose objects 绝不会丢失!
await _runProcess('git', ['config', '--global', 'core.createObject', 'rename']);
final nameCheck = await _runProcess('git', ['config', '--global', 'user.name']);
if (nameCheck.stdout.toString().trim().isEmpty) {
await _runProcess('git', ['config', '--global', 'user.name', 'CodeEditor User']);
@@ -495,16 +499,39 @@ class GitService {
}
}
/// 删除本地分支 (`git branch -d` / `-D`)
/// 删除本地分支 (`git branch -d` / `-D`),支持强力清理坏损无对象分支
Future<GitCommandResult> deleteBranch(
String repoPath,
String branchName, {
bool force = false,
}) async {
return runGitCommand(
final res = await runGitCommand(
['branch', force ? '-D' : '-d', branchName],
workingDir: repoPath,
);
if (res.success) return res;
// 针对树对象损坏或缺失的分支(如 fatal: reference is not a tree),
// 允许通过物理删除 .git/refs/heads/<branchName> 解除阻断
if (force ||
res.stderr.contains('reference is not a tree') ||
res.stderr.contains('not a valid object name') ||
res.stderr.contains('unable to resolve reference')) {
try {
final refFile = File(p.join(repoPath, '.git', 'refs', 'heads', branchName));
if (refFile.existsSync()) {
refFile.deleteSync();
return const GitCommandResult(
success: true,
exitCode: 0,
stdout: 'Deleted damaged branch reference',
stderr: '',
);
}
} catch (_) {}
}
return res;
}
/// 获取本地标签列表 (`git tag -l`)
+46 -1
View File
@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import '../l10n/app_localizations.dart';
@@ -106,7 +107,28 @@ class InternalEngineService extends ChangeNotifier {
// 2. 清理终端中属于 ubuntu 的活跃会话
terminalProvider?.removeSessionsForDistro(DistroRepository.defaultSystemName);
// 3. 彻底删除 ubuntu 容器目录
// 3. 保护 .l2s 目录,避免外部工程中已建立的 PRoot 硬链接替身文件随容器重装被误删
Directory? preservedL2s;
try {
final currentRootfs = await DistroManager().getSystemRootDir(DistroRepository.defaultSystemName);
final oldL2s = Directory(p.join(currentRootfs.path, '.l2s'));
if (oldL2s.existsSync()) {
final tempDir = await getTemporaryDirectory();
final backupDir = Directory(p.join(tempDir.path, 'preserved_l2s_${DateTime.now().millisecondsSinceEpoch}'));
backupDir.createSync(recursive: true);
final entries = oldL2s.listSync();
for (final entry in entries) {
if (entry is File) {
entry.copySync(p.join(backupDir.path, p.basename(entry.path)));
}
}
preservedL2s = backupDir;
}
} catch (e) {
debugPrint('备份 .l2s 异常 (非阻塞): $e');
}
// 彻底删除 ubuntu 容器目录
if (customEngineDir != null) {
if (customEngineDir!.existsSync()) {
customEngineDir!.deleteSync(recursive: true);
@@ -135,6 +157,29 @@ class InternalEngineService extends ChangeNotifier {
},
);
// 5. 还原被保护的 .l2s 替身目录到新解压的容器中
if (success && preservedL2s != null && preservedL2s.existsSync()) {
try {
final newRootfs = await DistroManager().getSystemRootDir(DistroRepository.defaultSystemName);
final newL2s = Directory(p.join(newRootfs.path, '.l2s'));
if (!newL2s.existsSync()) {
newL2s.createSync(recursive: true);
}
for (final entity in preservedL2s.listSync()) {
if (entity is File) {
final target = p.join(newL2s.path, p.basename(entity.path));
if (!File(target).existsSync()) {
entity.copySync(target);
}
}
}
preservedL2s.deleteSync(recursive: true);
debugPrint('成功恢复 .l2s 替身目录到新容器');
} catch (e) {
debugPrint('恢复 .l2s 目录异常: $e');
}
}
_isChecking = false;
notifyListeners();
return success;