2
This commit is contained in:
1 parent
6f5d1ed570
commit
875840147d
17 files changed
+3093
-36
No files matched your search
@@ -0,0 +1,209 @@
|
||||
/// 冲突块中的一段内容
|
||||
class GitConflictSegment {
|
||||
/// 在 [GitConflictBlock] 内的起始行号(0-based,含)
|
||||
final int startLine;
|
||||
|
||||
/// 结束行号(0-based,不含)
|
||||
final int endLine;
|
||||
|
||||
const GitConflictSegment({required this.startLine, required this.endLine});
|
||||
|
||||
/// 行数(可能为 0,表示这一段是空的)
|
||||
int get lineCount => endLine - startLine;
|
||||
}
|
||||
|
||||
/// 文件中的一个冲突块
|
||||
///
|
||||
/// 只记录**位置**,内容由调用方按行号自行取用(避免重复持有全文副本)。
|
||||
class GitConflictBlock {
|
||||
/// `<<<<<<<` 所在行号(0-based,含)
|
||||
final int startLine;
|
||||
|
||||
/// `>>>>>>>` 所在行号(0-based,含)
|
||||
final int endLine;
|
||||
|
||||
/// `<<<<<<<` 后标注的来源(rebase 下是**基线**侧,merge 下是当前分支)
|
||||
final String oursLabel;
|
||||
|
||||
/// `>>>>>>>` 后标注的来源
|
||||
final String theirsLabel;
|
||||
|
||||
final GitConflictSegment ours;
|
||||
final GitConflictSegment theirs;
|
||||
|
||||
/// 共同祖先段;仅 `merge.conflictStyle=diff3` 时存在
|
||||
final GitConflictSegment? base;
|
||||
|
||||
const GitConflictBlock({
|
||||
required this.startLine,
|
||||
required this.endLine,
|
||||
required this.oursLabel,
|
||||
required this.theirsLabel,
|
||||
required this.ours,
|
||||
required this.theirs,
|
||||
this.base,
|
||||
});
|
||||
|
||||
/// 是否为 diff3 风格(带基线段)
|
||||
bool get hasBaseSegment => base != null;
|
||||
|
||||
/// 该块跨越的总行数(含标记行),用于计算替换范围
|
||||
int get replacedLineCount => endLine - startLine + 1;
|
||||
}
|
||||
|
||||
/// 冲突标记解析结果
|
||||
class GitConflictParseResult {
|
||||
final List<GitConflictBlock> blocks;
|
||||
|
||||
/// 格式是否可疑(标记不成对、嵌套、顺序错乱)
|
||||
///
|
||||
/// 为真时**不要**对文件做任何自动替换:宁可退化成"手工编辑",
|
||||
/// 也不能按错误的位置改写用户的代码。
|
||||
final bool isMalformed;
|
||||
|
||||
/// 可疑原因,供 UI 提示
|
||||
final String? malformedReason;
|
||||
|
||||
const GitConflictParseResult({
|
||||
this.blocks = const [],
|
||||
this.isMalformed = false,
|
||||
this.malformedReason,
|
||||
});
|
||||
|
||||
bool get hasConflicts => blocks.isNotEmpty;
|
||||
|
||||
static const GitConflictParseResult none = GitConflictParseResult();
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'GitConflictParseResult(blocks=${blocks.length}, malformed=$isMalformed)';
|
||||
}
|
||||
|
||||
/// 解析文件中的 Git 冲突标记
|
||||
///
|
||||
/// 支持标准风格(`<<<<<<<` / `=======` / `>>>>>>>`)与 diff3 风格
|
||||
/// (额外含 `|||||||` 基线段)。
|
||||
///
|
||||
/// 关键设计:**只信任成对且顺序正确的标记**。文件正文里本来就可能出现
|
||||
/// 类似标记的文本(比如正在编辑这段解析代码本身),一旦结构可疑就整体
|
||||
/// 标记为 malformed,由调用方退化为手工编辑。
|
||||
class GitConflictParser {
|
||||
GitConflictParser._();
|
||||
|
||||
static const String _oursMarker = '<<<<<<<';
|
||||
static const String _baseMarker = '|||||||';
|
||||
static const String _separator = '=======';
|
||||
static const String _theirsMarker = '>>>>>>>';
|
||||
|
||||
static GitConflictParseResult parse(String content) {
|
||||
final lines = content.split('\n');
|
||||
final blocks = <GitConflictBlock>[];
|
||||
|
||||
int? blockStart;
|
||||
String oursLabel = '';
|
||||
String theirsLabel = '';
|
||||
int? oursStart;
|
||||
int? baseStart;
|
||||
int? theirsStart;
|
||||
int? sepLine;
|
||||
bool sawBase = false;
|
||||
|
||||
String? malformedReason;
|
||||
void markMalformed(String reason) {
|
||||
malformedReason ??= reason;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
blockStart = null;
|
||||
oursLabel = '';
|
||||
theirsLabel = '';
|
||||
oursStart = null;
|
||||
baseStart = null;
|
||||
theirsStart = null;
|
||||
sepLine = null;
|
||||
sawBase = false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
final line = lines[i];
|
||||
|
||||
if (line.startsWith(_oursMarker)) {
|
||||
if (blockStart != null) {
|
||||
// 上一个块还没结束就来了新的起始标记 → 嵌套,不可信任
|
||||
markMalformed('检测到嵌套的冲突标记,无法安全自动解析');
|
||||
}
|
||||
reset();
|
||||
blockStart = i;
|
||||
oursLabel = _labelOf(line, _oursMarker);
|
||||
oursStart = i + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (blockStart == null) continue;
|
||||
|
||||
if (line.startsWith(_baseMarker)) {
|
||||
sawBase = true;
|
||||
baseStart = i + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith(_separator)) {
|
||||
if (sepLine != null) {
|
||||
markMalformed('同一个冲突块内出现多个分隔线');
|
||||
}
|
||||
sepLine = i;
|
||||
theirsStart = i + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith(_theirsMarker)) {
|
||||
theirsLabel = _labelOf(line, _theirsMarker);
|
||||
final start = blockStart!;
|
||||
final sep = sepLine;
|
||||
final oursEnd = sawBase ? baseStart! - 1 : sep;
|
||||
|
||||
if (sep == null || oursEnd == null) {
|
||||
// 缺少分隔线 → 结构不完整
|
||||
markMalformed('冲突块缺少 ======= 分隔线');
|
||||
} else {
|
||||
blocks.add(GitConflictBlock(
|
||||
startLine: start,
|
||||
endLine: i,
|
||||
oursLabel: oursLabel,
|
||||
theirsLabel: theirsLabel,
|
||||
ours: GitConflictSegment(startLine: oursStart!, endLine: oursEnd),
|
||||
theirs: GitConflictSegment(startLine: theirsStart!, endLine: i),
|
||||
base: sawBase
|
||||
? GitConflictSegment(startLine: baseStart!, endLine: sep)
|
||||
: null,
|
||||
));
|
||||
}
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
// 走到文件末尾仍在块内 → 标记不成对
|
||||
if (blockStart != null) {
|
||||
markMalformed('冲突标记未闭合');
|
||||
}
|
||||
|
||||
if (malformedReason != null) {
|
||||
return GitConflictParseResult(
|
||||
blocks: const [],
|
||||
isMalformed: true,
|
||||
malformedReason: malformedReason,
|
||||
);
|
||||
}
|
||||
return GitConflictParseResult(blocks: List.unmodifiable(blocks));
|
||||
}
|
||||
|
||||
/// 快速判断文件里是否含冲突标记(不做完整解析,用于列表标注)
|
||||
static bool containsMarkers(String content) {
|
||||
return content.contains(_oursMarker) && content.contains(_theirsMarker);
|
||||
}
|
||||
|
||||
/// 取出标记行后面的来源标注(`<<<<<<< HEAD` → `HEAD`)
|
||||
static String _labelOf(String line, String marker) {
|
||||
return line.length > marker.length ? line.substring(marker.length).trim() : '';
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,9 @@ enum GitOperationErrorKind {
|
||||
/// 目标仓库不存在,或当前账号无权访问(HTTP 404)
|
||||
repositoryNotFound,
|
||||
|
||||
/// 本地路径型远端在容器内不存在(本地裸仓库/目录型 remote 的路径失效)
|
||||
localRemotePathMissing,
|
||||
|
||||
/// 需要代理认证(HTTP 407)
|
||||
proxyAuthRequired,
|
||||
|
||||
@@ -220,10 +223,19 @@ class GitErrorMapper {
|
||||
return const GitErrorInfo(kind: GitOperationErrorKind.requestRejected);
|
||||
}
|
||||
|
||||
// 2h. 仓库不存在 / 无权访问(HTTP 404 或 GitHub 文案)
|
||||
if (text.contains('repository not found') ||
|
||||
text.contains('does not appear to be a git repository') ||
|
||||
text.contains('404')) {
|
||||
// 2h. 本地路径型远端不存在(必须先于通用 404 判断)
|
||||
// 典型输出:`fatal: '/workspace/xxx.git' does not appear to be a git repository`
|
||||
// 这跟账号、令牌、GitHub 完全无关,归到"仓库不存在"会把用户带偏。
|
||||
if (text.contains('does not appear to be a git repository') ||
|
||||
text.contains('could not read from remote repository') ||
|
||||
RegExp(r"fatal: '.*' does not exist").hasMatch(text)) {
|
||||
return const GitErrorInfo(
|
||||
kind: GitOperationErrorKind.localRemotePathMissing,
|
||||
);
|
||||
}
|
||||
|
||||
// 2i. 仓库不存在 / 无权访问(HTTP 404 或 GitHub 文案)
|
||||
if (text.contains('repository not found') || text.contains('404')) {
|
||||
return const GitErrorInfo(
|
||||
kind: GitOperationErrorKind.repositoryNotFound,
|
||||
fixAction: GitErrorFixAction.openAccountManagement,
|
||||
@@ -417,6 +429,8 @@ extension GitOperationErrorKindL10n on GitOperationErrorKind {
|
||||
return l10n.gitErrWritePermissionDenied;
|
||||
case GitOperationErrorKind.repositoryNotFound:
|
||||
return l10n.gitErrRepositoryNotFound;
|
||||
case GitOperationErrorKind.localRemotePathMissing:
|
||||
return l10n.gitErrLocalRemoteMissing;
|
||||
case GitOperationErrorKind.proxyAuthRequired:
|
||||
return l10n.gitErrProxyAuthRequired;
|
||||
case GitOperationErrorKind.requestRejected:
|
||||
@@ -469,6 +483,8 @@ extension GitOperationErrorKindL10n on GitOperationErrorKind {
|
||||
return l10n.gitErrWritePermissionDeniedHint;
|
||||
case GitOperationErrorKind.repositoryNotFound:
|
||||
return l10n.gitErrRepositoryNotFoundHint;
|
||||
case GitOperationErrorKind.localRemotePathMissing:
|
||||
return l10n.gitErrLocalRemoteMissingHint;
|
||||
case GitOperationErrorKind.proxyAuthRequired:
|
||||
return l10n.gitErrProxyAuthRequiredHint;
|
||||
case GitOperationErrorKind.requestRejected:
|
||||
|
||||
Reference in new issue
Block a user