import 'package:path/path.dart' as p; /// Git 文件状态类别 enum GitFileStatusType { untracked, // 未跟踪 (??) modified, // 已修改 ( M / M ) added, // 新增 (A ) deleted, // 已删除 ( D / D ) renamed, // 重命名 (R ) copied, // 复制 (C ) typeChange,// 类型变更 (T ) unmerged, // 冲突未合并 (UU / AA / DD 等) ignored, // 忽略 (!!) unknown, // 未知 } /// Git 文件变更条目 class GitFileStatus { final String path; final String relativePath; final GitFileStatusType statusType; final bool isStaged; final String? originalPath; // 用于重命名/复制的源路径 const GitFileStatus({ required this.path, required this.relativePath, required this.statusType, this.isStaged = false, this.originalPath, }); String get fileName => p.basename(relativePath); String get directoryPath { final dir = p.dirname(relativePath); return dir == '.' ? '' : dir; } } /// 推送目标远程的解析来源 /// /// 用于在 UI 上解释「为什么推送会发到这个远程」,避免多远程或 fork 场景下 /// 用户对推送去向了哪个仓库产生误解。 enum GitRemoteSource { /// 无可用远程 none, /// 分支级配置 `branch..pushRemote` pushRemote, /// 仓库级配置 `remote.pushDefault` pushDefault, /// 当前分支的上游远程(`git status` 的 branch.upstream) upstream, /// 分支配置 `branch..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 missing; /// 损坏的对象或无法归类的错误行 final List corrupt; /// 悬空对象(正常现象,不影响使用) final List 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; final String name; final bool isRoot; final String? currentBranch; /// 已配置的远程仓库列表 final List remotes; /// 当前分支的上游追踪状态 final GitBranchTracking? tracking; const GitRepositoryInfo({ required this.rootPath, required this.name, this.isRoot = false, this.currentBranch, this.remotes = const [], this.tracking, }); GitRepositoryInfo copyWith({ String? rootPath, String? name, bool? isRoot, String? currentBranch, List? 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) || other is GitRepositoryInfo && runtimeType == other.runtimeType && rootPath == other.rootPath; @override int get hashCode => rootPath.hashCode; } /// Git 命令行执行结果 class GitCommandResult { final bool success; final int exitCode; final String stdout; final String stderr; const GitCommandResult({ required this.success, required this.exitCode, required this.stdout, required this.stderr, }); factory GitCommandResult.error(String message, {int exitCode = -1}) { return GitCommandResult( success: false, exitCode: exitCode, stdout: '', stderr: message, ); } } /// Git 提交记录与图表节点模型 class GitCommit { final String hash; final String shortHash; final List parentHashes; final String authorName; final String authorEmail; final DateTime authorDate; final String relativeDate; final String subject; final List refs; // e.g. ["HEAD -> main", "origin/main", "tag: v1.0.0"] /// 线性图表拓扑布局字段 final int lane; // 列通道索引 (0, 1, 2...) final List activeLanes; // 经过当前行的活跃通道列表 final List outgoingLanes; // 当前节点连接向父节点的通道列表 const GitCommit({ required this.hash, required this.shortHash, required this.parentHashes, required this.authorName, required this.authorEmail, required this.authorDate, required this.relativeDate, required this.subject, this.refs = const [], this.lane = 0, this.activeLanes = const [], this.outgoingLanes = const [], }); bool get isHead => refs.any((r) => r.startsWith('HEAD')); GitCommit copyWith({ String? hash, String? shortHash, List? parentHashes, String? authorName, String? authorEmail, DateTime? authorDate, String? relativeDate, String? subject, List? refs, int? lane, List? activeLanes, List? outgoingLanes, }) { return GitCommit( hash: hash ?? this.hash, shortHash: shortHash ?? this.shortHash, parentHashes: parentHashes ?? this.parentHashes, authorName: authorName ?? this.authorName, authorEmail: authorEmail ?? this.authorEmail, authorDate: authorDate ?? this.authorDate, relativeDate: relativeDate ?? this.relativeDate, subject: subject ?? this.subject, refs: refs ?? this.refs, lane: lane ?? this.lane, activeLanes: activeLanes ?? this.activeLanes, outgoingLanes: outgoingLanes ?? this.outgoingLanes, ); } }