This commit is contained in:
LZX committed 2026-09-19 21:07:45 +08:00
1 parent 4614825b7f
commit 6f5d1ed570
19 files changed
+9864 -99

No files matched your search

+277
View File
@@ -38,6 +38,259 @@ class GitFileStatus {
}
}
/// 推送目标远程的解析来源
///
/// 用于在 UI 上解释「为什么推送会发到这个远程」,避免多远程或 fork 场景下
/// 用户对推送去向了哪个仓库产生误解。
enum GitRemoteSource {
/// 无可用远程
none,
/// 分支级配置 `branch.<name>.pushRemote`
pushRemote,
/// 仓库级配置 `remote.pushDefault`
pushDefault,
/// 当前分支的上游远程(`git status` 的 branch.upstream)
upstream,
/// 分支配置 `branch.<name>.remote`(尚未建立上游追踪时)
branchRemote,
/// 兜底:优先名为 origin 的远程,否则取列表第一个
fallback,
/// 用户在本会话中手动指定(优先级最高,不落盘)
manualOverride,
}
/// 远端追踪分支(`origin/feature-x` 这类)
///
/// 与 [GitRemote] 不同:后者是"远程仓库"(一个名字 + 地址),
/// 这里指的是远端仓库里的**某一条分支**在本地留下的追踪引用。
class GitRemoteBranch {
/// 完整引用名(如 `origin/feature-x`)
final String name;
/// 所属远程仓库名(如 `origin`)
final String remoteName;
/// 远端分支名(如 `feature-x`)
final String branchName;
/// 是否已被某个本地分支检出(`git branch -r` 中的 `*`)
final bool isCheckedOut;
/// 同名的本地分支名 —— 即基于此远端分支创建后本地会叫这个名字
String get localBranchName => branchName;
const GitRemoteBranch({
required this.name,
required this.remoteName,
required this.branchName,
this.isCheckedOut = false,
});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is GitRemoteBranch &&
runtimeType == other.runtimeType &&
name == other.name;
@override
int get hashCode => name.hashCode;
@override
String toString() => 'GitRemoteBranch($name${isCheckedOut ? ", checkedOut" : ""})';
}
/// 仓库完整性自检结果(`git fsck`)
class GitFsckReport {
/// 对象库是否健康(无缺失、无损坏且退出码为 0)
final bool isHealthy;
/// 是否存在对象丢失 —— 这是会导致提交失败(Error building trees)的严重问题
final bool hasObjectLoss;
/// 缺失/断链的对象描述
final List<String> missing;
/// 损坏的对象或无法归类的错误行
final List<String> corrupt;
/// 悬空对象(正常现象,不影响使用)
final List<String> dangling;
final int exitCode;
const GitFsckReport({
required this.isHealthy,
required this.hasObjectLoss,
this.missing = const [],
this.corrupt = const [],
this.dangling = const [],
this.exitCode = 0,
});
/// 需要用户关注的问题总数(不含 dangling)
int get problemCount => missing.length + corrupt.length;
@override
String toString() =>
'GitFsckReport(healthy=$isHealthy, missing=${missing.length}, '
'corrupt=${corrupt.length}, dangling=${dangling.length})';
}
/// Git 远程仓库描述符(name / fetchUrl / pushUrl)
class GitRemote {
final String name;
/// 拉取地址(`git remote -v` 的 fetch 行)
final String fetchUrl;
/// 推送地址:仅当与 [fetchUrl] 不同时才非空
final String? pushUrl;
const GitRemote({
required this.name,
required this.fetchUrl,
this.pushUrl,
});
/// 推送实际使用的地址(未单独配置则回退到 fetch 地址)
String get effectivePushUrl => pushUrl ?? fetchUrl;
/// 是否为 SSH 协议地址(`git@host:path` 或 `ssh://`)
bool get isSsh =>
fetchUrl.startsWith('git@') ||
fetchUrl.startsWith('ssh://') ||
fetchUrl.startsWith('git+ssh://');
/// 是否为 HTTPS 协议地址(走 ~/.git-credentials 免密)
bool get isHttps =>
fetchUrl.startsWith('https://') || fetchUrl.startsWith('http://');
/// 提取主机名(供账号匹配与展示)
String? get host {
final uri = Uri.tryParse(fetchUrl);
if (uri != null && uri.host.isNotEmpty) return uri.host;
// 兼容 scp 风格 `git@github.com:owner/repo.git`
final match = RegExp(r'^[^@]+@([^:]+):').firstMatch(fetchUrl);
return match?.group(1);
}
/// 提取仓库路径(如 `owner/repo.git`)
String? get repositoryPath {
final uri = Uri.tryParse(fetchUrl);
if (uri != null && uri.path.isNotEmpty) {
return uri.path.replaceFirst(RegExp(r'^/+'), '');
}
final match = RegExp(r'^[^@]+@[^:]+:(.+)$').firstMatch(fetchUrl);
return match?.group(1);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is GitRemote &&
runtimeType == other.runtimeType &&
name == other.name &&
fetchUrl == other.fetchUrl &&
pushUrl == other.pushUrl;
@override
int get hashCode => Object.hash(name, fetchUrl, pushUrl);
@override
String toString() => 'GitRemote($name: $fetchUrl)';
}
/// 本地分支的上游追踪状态(ahead/behind 是云端功能 UI 的中枢信号)
class GitBranchTracking {
/// 本地分支名
final String branch;
/// 上游引用(如 `origin/main`);为 null 表示尚未设置上游
final String? upstream;
/// 领先上游的提交数
final int ahead;
/// 落后上游的提交数
final int behind;
/// 上游分支已被远端删除(`git status` 中的 `[gone]`)
final bool isGone;
const GitBranchTracking({
required this.branch,
this.upstream,
this.ahead = 0,
this.behind = 0,
this.isGone = false,
});
/// 是否已设置上游追踪分支
bool get hasUpstream => upstream != null && upstream!.isNotEmpty;
/// 本地与远端均已产生新提交(需要先 pull 再 push)
bool get isDiverged => ahead > 0 && behind > 0;
/// 是否需要推送到远端
bool get needsPush => hasUpstream && ahead > 0;
/// 是否需要从远端拉取
bool get needsPull => hasUpstream && behind > 0;
/// 是否与上游完全同步
bool get isUpToDate => hasUpstream && ahead == 0 && behind == 0;
/// 上游对应的远程仓库名(如 `origin`)
String? get remoteName {
if (!hasUpstream) return null;
final idx = upstream!.indexOf('/');
return idx > 0 ? upstream!.substring(0, idx) : null;
}
/// 上游对应的分支名(如 `main`)
String? get upstreamBranch {
if (!hasUpstream) return null;
final idx = upstream!.indexOf('/');
return idx >= 0 && idx + 1 < upstream!.length ? upstream!.substring(idx + 1) : null;
}
/// 未设置上游时的初始状态
factory GitBranchTracking.noUpstream(String branch) =>
GitBranchTracking(branch: branch);
@override
String toString() =>
'GitBranchTracking($branch -> ${upstream ?? "none"}, +$ahead/-$behind)';
}
/// 云端操作的实时进度(驱动面板内联进度条)
class GitOperationProgress {
/// 操作类型标识:fetch / pull / push / clone
final String phase;
/// 0~100 的百分比;无法解析时为 null(UI 应展示不确定态进度条)
final int? percent;
/// 原始输出行,供"查看详情"展开
final String raw;
const GitOperationProgress({
required this.phase,
this.percent,
this.raw = '',
});
@override
String toString() => 'GitOperationProgress($phase, ${percent ?? "-"}%)';
}
/// Git 仓库信息(支持根仓库及子模块/嵌套仓库)
class GitRepositoryInfo {
final String rootPath;
@@ -45,11 +298,19 @@ class GitRepositoryInfo {
final bool isRoot;
final String? currentBranch;
/// 已配置的远程仓库列表
final List<GitRemote> remotes;
/// 当前分支的上游追踪状态
final GitBranchTracking? tracking;
const GitRepositoryInfo({
required this.rootPath,
required this.name,
this.isRoot = false,
this.currentBranch,
this.remotes = const [],
this.tracking,
});
GitRepositoryInfo copyWith({
@@ -57,15 +318,31 @@ class GitRepositoryInfo {
String? name,
bool? isRoot,
String? currentBranch,
List<GitRemote>? remotes,
GitBranchTracking? tracking,
}) {
return GitRepositoryInfo(
rootPath: rootPath ?? this.rootPath,
name: name ?? this.name,
isRoot: isRoot ?? this.isRoot,
currentBranch: currentBranch ?? this.currentBranch,
remotes: remotes ?? this.remotes,
tracking: tracking ?? this.tracking,
);
}
/// 是否已配置任何远程仓库
bool get hasRemote => remotes.isNotEmpty;
/// 默认远程仓库(优先 origin,否则取第一个)
GitRemote? get defaultRemote {
if (remotes.isEmpty) return null;
for (final r in remotes) {
if (r.name == 'origin') return r;
}
return remotes.first;
}
@override
bool operator ==(Object other) =>
identical(this, other) ||