1
This commit is contained in:
1 parent
4614825b7f
commit
6f5d1ed570
19 files changed
+9864
-99
No files matched your search
@@ -1,8 +1,11 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import '../models/git_model.dart';
|
||||
import '../services/distro_manager.dart';
|
||||
import '../services/git_account_service.dart';
|
||||
import '../services/git_service.dart';
|
||||
import '../utils/git_decoration_utils.dart';
|
||||
import '../utils/git_error_mapper.dart';
|
||||
|
||||
/// Git 状态管理 Provider
|
||||
class GitProvider extends ChangeNotifier {
|
||||
@@ -31,6 +34,46 @@ class GitProvider extends ChangeNotifier {
|
||||
int _commitsLimit = 50;
|
||||
bool _isLoadingMoreCommits = false;
|
||||
|
||||
// ---------------- 云端(远程仓库)状态 ----------------
|
||||
|
||||
/// 当前仓库已配置的远程仓库列表
|
||||
List<GitRemote> _remotes = [];
|
||||
|
||||
/// 当前分支的上游追踪状态(ahead/behind)
|
||||
GitBranchTracking? _tracking;
|
||||
|
||||
/// 远端追踪分支列表(`origin/xxx`)
|
||||
List<GitRemoteBranch> _remoteBranches = [];
|
||||
|
||||
/// 最近一次仓库自检结果
|
||||
GitFsckReport? _fsckReport;
|
||||
|
||||
bool _isFsckRunning = false;
|
||||
|
||||
/// 克隆进行中的进度
|
||||
GitOperationProgress? _cloneProgress;
|
||||
|
||||
bool _isCloning = false;
|
||||
|
||||
bool _isFetching = false;
|
||||
bool _isPulling = false;
|
||||
bool _isPushing = false;
|
||||
|
||||
/// 最近一次云端操作的实时进度(驱动内联进度条)
|
||||
GitOperationProgress? _remoteProgress;
|
||||
|
||||
/// 最近一次云端操作的原始输出,供"查看详情"展开
|
||||
String _remoteLog = '';
|
||||
|
||||
/// 最近一次云端操作的结构化错误
|
||||
GitErrorInfo? _lastRemoteError;
|
||||
|
||||
/// 进行中的云端操作取消令牌
|
||||
HeadlessCancelToken? _remoteCancelToken;
|
||||
|
||||
/// 最近一次 fetch 完成时间(用于决定是否提示用户刷新)
|
||||
DateTime? _lastFetchAt;
|
||||
|
||||
// UI 内存状态缓存(保留折叠状态、输入框草稿与滚动位置)
|
||||
bool _stagedExpanded = true;
|
||||
bool _unstagedExpanded = true;
|
||||
@@ -63,6 +106,155 @@ class GitProvider extends ChangeNotifier {
|
||||
bool get isLoadingMoreCommits => _isLoadingMoreCommits;
|
||||
bool get hasMoreCommits => _totalCommitsCount > 0 ? _commits.length < _totalCommitsCount : false;
|
||||
|
||||
// ---------------- 云端状态 Getters ----------------
|
||||
|
||||
List<GitRemote> get remotes => List.unmodifiable(_remotes);
|
||||
GitBranchTracking? get tracking => _tracking;
|
||||
|
||||
/// 远端追踪分支(用于分支选择器分区展示与检出)
|
||||
List<GitRemoteBranch> get remoteBranches => List.unmodifiable(_remoteBranches);
|
||||
GitFsckReport? get fsckReport => _fsckReport;
|
||||
bool get isFsckRunning => _isFsckRunning;
|
||||
bool get isCloning => _isCloning;
|
||||
GitOperationProgress? get cloneProgress => _cloneProgress;
|
||||
bool get isFetching => _isFetching;
|
||||
bool get isPulling => _isPulling;
|
||||
bool get isPushing => _isPushing;
|
||||
GitOperationProgress? get remoteProgress => _remoteProgress;
|
||||
String get remoteLog => _remoteLog;
|
||||
GitErrorInfo? get lastRemoteError => _lastRemoteError;
|
||||
DateTime? get lastFetchAt => _lastFetchAt;
|
||||
|
||||
/// 是否有任何云端操作正在进行(用于禁用并发按钮)
|
||||
bool get isRemoteBusy => _isFetching || _isPulling || _isPushing;
|
||||
|
||||
/// 是否已配置远程仓库
|
||||
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;
|
||||
}
|
||||
|
||||
// ---------------- 远程目标解析 ----------------
|
||||
//
|
||||
// 「上游」与「推送目标」是两个概念:单人项目里通常都是 origin,但 fork 协作
|
||||
// 场景必须分开(从 upstream 拉、往自己的 fork 推)。这里遵循 git 自身的优先级,
|
||||
// 使用户在命令行里配过的 pushRemote / remote.pushDefault 同样在面板生效。
|
||||
|
||||
/// 缓存的分支级推送远程(`branch.<name>.pushRemote`)
|
||||
String? _branchPushRemote;
|
||||
|
||||
/// 缓存的全局推送默认远程(`remote.pushDefault`)
|
||||
String? _remotePushDefault;
|
||||
|
||||
/// 缓存的分支级抓取远程(`branch.<name>.remote`)
|
||||
String? _branchRemote;
|
||||
|
||||
/// 解析推送目标:pushRemote → remote.pushDefault → 上游远程 → origin/第一个
|
||||
({GitRemote? remote, GitRemoteSource source}) get pushTarget =>
|
||||
_resolvePushTarget();
|
||||
|
||||
/// 本会话内用户手动指定的推送目标(覆盖 git 配置,不落盘)
|
||||
///
|
||||
/// 做成会话级覆盖而不是写 git config:切换仓库即失效,
|
||||
/// 避免一次临时选择被永久写进仓库配置。
|
||||
String? _pushTargetOverride;
|
||||
|
||||
bool get hasPushTargetOverride => _pushTargetOverride != null;
|
||||
|
||||
/// 手动指定推送目标;传 null 恢复按 git 配置自动解析
|
||||
void setPushTargetOverride(String? remoteName) {
|
||||
if (_pushTargetOverride == remoteName) return;
|
||||
_pushTargetOverride = remoteName;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
({GitRemote? remote, GitRemoteSource source}) _resolvePushTarget() {
|
||||
if (_remotes.isEmpty) {
|
||||
return (remote: null, source: GitRemoteSource.none);
|
||||
}
|
||||
|
||||
// 用户在本会话中的显式选择优先级最高
|
||||
if (_pushTargetOverride != null) {
|
||||
final override = _remoteByName(_pushTargetOverride);
|
||||
if (override != null) {
|
||||
return (remote: override, source: GitRemoteSource.manualOverride);
|
||||
}
|
||||
// 兜底:正常情况下覆盖会在远程列表变化时被即时清除,
|
||||
// 这里再拦一次,避免静默回落到别的远程却不告诉用户。
|
||||
_pushTargetOverride = null;
|
||||
}
|
||||
|
||||
final upstreamName = _tracking?.remoteName;
|
||||
final upstream = _remoteByName(upstreamName);
|
||||
// 分支尚未建立追踪时,其「上游」等价于 git 的 branch.<name>.remote
|
||||
final branchRemote = _remoteByName(_branchRemote);
|
||||
|
||||
// 1. branch.<name>.pushRemote
|
||||
final pushRemote = _remoteByName(_branchPushRemote);
|
||||
if (pushRemote != null) {
|
||||
return (remote: pushRemote, source: GitRemoteSource.pushRemote);
|
||||
}
|
||||
// 2. remote.pushDefault
|
||||
final pushDefault = _remoteByName(_remotePushDefault);
|
||||
if (pushDefault != null) {
|
||||
return (remote: pushDefault, source: GitRemoteSource.pushDefault);
|
||||
}
|
||||
// 3. 上游 / 分支配置的远程
|
||||
if (upstream != null) {
|
||||
return (remote: upstream, source: GitRemoteSource.upstream);
|
||||
}
|
||||
if (branchRemote != null) {
|
||||
return (remote: branchRemote, source: GitRemoteSource.branchRemote);
|
||||
}
|
||||
// 4. origin / 第一个
|
||||
return (remote: defaultRemote, source: GitRemoteSource.fallback);
|
||||
}
|
||||
|
||||
/// 解析抓取目标:`branch.<name>.remote` → 上游远程 → origin/第一个
|
||||
///
|
||||
/// 必须优先于 defaultRemote:fork 场景下分支追踪的是 upstream,
|
||||
/// 若抓取固定走 origin,则 upstream/* 永不更新,ahead/behind 会一直算错。
|
||||
GitRemote? get fetchTarget {
|
||||
if (_remotes.isEmpty) return null;
|
||||
return _remoteByName(_branchRemote) ??
|
||||
_remoteByName(_tracking?.remoteName) ??
|
||||
defaultRemote;
|
||||
}
|
||||
|
||||
/// 解析拉取目标:上游远程 → origin/第一个
|
||||
GitRemote? get pullTarget {
|
||||
if (_remotes.isEmpty) return null;
|
||||
return _remoteByName(_tracking?.remoteName) ?? defaultRemote;
|
||||
}
|
||||
|
||||
GitRemote? _remoteByName(String? name) {
|
||||
if (name == null || name.isEmpty) return null;
|
||||
for (final r in _remotes) {
|
||||
if (r.name == name) return r;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 待推送的提交数(无上游时为 0,UI 应改为展示"发布分支")
|
||||
int get aheadCount => _tracking?.ahead ?? 0;
|
||||
|
||||
/// 待拉取的提交数
|
||||
int get behindCount => _tracking?.behind ?? 0;
|
||||
|
||||
/// 当前分支是否需要建立上游追踪关系
|
||||
bool get needsPublishBranch {
|
||||
final branch = _currentBranch;
|
||||
if (branch == null || branch.isEmpty) return false;
|
||||
if (_tracking == null) return false;
|
||||
return !_tracking!.hasUpstream || _tracking!.isGone;
|
||||
}
|
||||
|
||||
bool get stagedExpanded => _stagedExpanded;
|
||||
bool get unstagedExpanded => _unstagedExpanded;
|
||||
bool get graphExpanded => _graphExpanded;
|
||||
@@ -131,6 +323,7 @@ class GitProvider extends ChangeNotifier {
|
||||
_commits = [];
|
||||
_stashCount = 0;
|
||||
_errorMessage = null;
|
||||
_clearRemoteState();
|
||||
|
||||
if (_rootPath != null) {
|
||||
refresh();
|
||||
@@ -143,6 +336,8 @@ class GitProvider extends ChangeNotifier {
|
||||
/// 响应底层运行容器被销毁重建:立即失效环境缓存,清空内存仓库与分支状态,恢复未安装引导
|
||||
void onContainerReset() {
|
||||
_gitService.invalidateEnvStatus();
|
||||
// 容器 rootfs 被重建后 ~/.git-credentials 一并丢失,需重新灌注
|
||||
GitAccountService.instance.invalidateCredentialSync();
|
||||
_gitInstalled = false;
|
||||
_gitVersion = null;
|
||||
_repositories = [];
|
||||
@@ -156,9 +351,49 @@ class GitProvider extends ChangeNotifier {
|
||||
_stashCount = 0;
|
||||
_errorMessage = '底层容器已重置,需重新安装 Git 工具链';
|
||||
_isLoading = false;
|
||||
_clearRemoteState();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 清空云端相关状态(切换项目 / 容器重置 / 切换仓库时调用)
|
||||
void _clearRemoteState() {
|
||||
_remotes = [];
|
||||
_tracking = null;
|
||||
_remoteBranches = [];
|
||||
_fsckReport = null;
|
||||
_isFsckRunning = false;
|
||||
_cloneProgress = null;
|
||||
_isCloning = false;
|
||||
_isFetching = false;
|
||||
_isPulling = false;
|
||||
_isPushing = false;
|
||||
_remoteProgress = null;
|
||||
_remoteLog = '';
|
||||
_lastRemoteError = null;
|
||||
_lastFetchAt = null;
|
||||
_remoteCancelToken = null;
|
||||
_branchPushRemote = null;
|
||||
_remotePushDefault = null;
|
||||
_branchRemote = null;
|
||||
_pushTargetOverride = null;
|
||||
}
|
||||
|
||||
/// 读取与远程目标解析相关的 git 配置
|
||||
///
|
||||
/// 这些值决定 push/fetch 走哪个远程;必须与仓库同步刷新,
|
||||
/// 否则用户在命令行改过 pushRemote 后面板不会跟随。
|
||||
Future<void> _loadRemoteTargetConfig() async {
|
||||
if (_currentRepoPath == null) return;
|
||||
final branch = _currentBranch ?? '';
|
||||
try {
|
||||
_branchPushRemote = await _gitService.getBranchPushRemote(_currentRepoPath!, branch);
|
||||
_remotePushDefault = await _gitService.getRemotePushDefault(_currentRepoPath!);
|
||||
_branchRemote = await _gitService.getBranchRemote(_currentRepoPath!, branch);
|
||||
} catch (e) {
|
||||
debugPrint('[GitProvider] 读取远程目标配置异常: $e');
|
||||
}
|
||||
}
|
||||
|
||||
int _refreshSeq = 0;
|
||||
|
||||
/// 刷新 Git 状态与仓库信息
|
||||
@@ -176,6 +411,7 @@ class GitProvider extends ChangeNotifier {
|
||||
_commits = [];
|
||||
_stashCount = 0;
|
||||
_isLoading = false;
|
||||
_clearRemoteState();
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
@@ -220,6 +456,7 @@ class GitProvider extends ChangeNotifier {
|
||||
_changedFiles = [];
|
||||
_rebuildStatusMaps();
|
||||
_currentBranch = null;
|
||||
_clearRemoteState();
|
||||
}
|
||||
} catch (e) {
|
||||
if (seq == _refreshSeq) {
|
||||
@@ -411,6 +648,9 @@ class GitProvider extends ChangeNotifier {
|
||||
if (res.success) {
|
||||
_currentBranch = branchName;
|
||||
_isCurrentRefTag = false;
|
||||
// 推送目标是按分支解析的:换了分支就必须重新解析,
|
||||
// 否则在 main 上选的远程会被带到 feature 分支上(可能推错仓库)。
|
||||
_pushTargetOverride = null;
|
||||
await _loadCurrentRepoDetails();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -422,6 +662,7 @@ class GitProvider extends ChangeNotifier {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.createBranch(_currentRepoPath!, branchName, checkout: checkout);
|
||||
if (res.success) {
|
||||
if (checkout) _pushTargetOverride = null;
|
||||
await _loadCurrentRepoDetails();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -512,6 +753,8 @@ class GitProvider extends ChangeNotifier {
|
||||
if (res.success) {
|
||||
_currentBranch = tagName;
|
||||
_isCurrentRefTag = true;
|
||||
// 标签检出后处于 detached HEAD,同样需要重新解析推送目标
|
||||
_pushTargetOverride = null;
|
||||
await _loadCurrentRepoDetails();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -574,16 +817,505 @@ class GitProvider extends ChangeNotifier {
|
||||
}
|
||||
_stashCount = await _gitService.getStashCount(_currentRepoPath!);
|
||||
|
||||
// 加载远程仓库列表与上游追踪状态(ahead/behind)
|
||||
_remotes = await _gitService.getRemotes(_currentRepoPath!);
|
||||
_tracking = await _gitService.getBranchTracking(_currentRepoPath!);
|
||||
_remoteBranches = await _gitService.getRemoteBranches(_currentRepoPath!);
|
||||
// 远程目标解析依赖 branch.<name>.* 与 remote.pushDefault
|
||||
await _loadRemoteTargetConfig();
|
||||
|
||||
// 同步更新 repositories 列表中当前项的分支信息
|
||||
final idx = _repositories.indexWhere((r) => r.rootPath == _currentRepoPath);
|
||||
if (idx != -1) {
|
||||
_repositories[idx] = _repositories[idx].copyWith(currentBranch: _currentBranch);
|
||||
_repositories[idx] = _repositories[idx].copyWith(
|
||||
currentBranch: _currentBranch,
|
||||
remotes: _remotes,
|
||||
tracking: _tracking,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[GitProvider] 加载仓库详情异常: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// 云端(远程仓库)操作
|
||||
// ==========================================================================
|
||||
|
||||
/// 仅刷新远端相关状态(不重跑提交历史等重活),用于网络操作完成后的即时反馈
|
||||
Future<void> _loadRemoteStateOnly() async {
|
||||
if (_currentRepoPath == null) return;
|
||||
try {
|
||||
_remotes = await _gitService.getRemotes(_currentRepoPath!);
|
||||
_tracking = await _gitService.getBranchTracking(_currentRepoPath!);
|
||||
// fetch/push 都可能带来新的远端分支,必须一并刷新
|
||||
_remoteBranches = await _gitService.getRemoteBranches(_currentRepoPath!);
|
||||
// 首次 push --set-upstream 会写入 branch.<name>.remote,需重新读取
|
||||
await _loadRemoteTargetConfig();
|
||||
} catch (e) {
|
||||
debugPrint('[GitProvider] 刷新远端状态异常: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 把一次云端命令的执行结果转换为结构化错误并记录原始输出
|
||||
void _recordRemoteResult(GitRemoteCommandResult res) {
|
||||
_remoteLog = res.combinedOutput.trim();
|
||||
if (res.success || res.cancelled) {
|
||||
_lastRemoteError = null;
|
||||
return;
|
||||
}
|
||||
_lastRemoteError = GitErrorMapper.describe(res.stderr, stdout: res.stdout);
|
||||
}
|
||||
|
||||
void _beginRemoteOperation() {
|
||||
_remoteProgress = null;
|
||||
_remoteLog = '';
|
||||
_lastRemoteError = null;
|
||||
_remoteCancelToken = HeadlessCancelToken();
|
||||
}
|
||||
|
||||
/// 云端网络操作前的准备:确保容器内凭据文件是最新的
|
||||
///
|
||||
/// 容器重建会连同 rootfs 一起丢掉 ~/.git-credentials,若不重写就会出现
|
||||
/// "凭据明明配好了却报认证失败" 的迷惑现象。
|
||||
Future<void> _prepareRemoteOperation() async {
|
||||
try {
|
||||
await GitAccountService.instance.ensureContainerCredentials();
|
||||
} catch (e) {
|
||||
debugPrint('[GitProvider] 凭据灌注异常: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _endRemoteOperation() {
|
||||
_remoteCancelToken = null;
|
||||
_remoteProgress = null;
|
||||
}
|
||||
|
||||
void _onProgress(GitOperationProgress progress) {
|
||||
_remoteProgress = progress;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 请求取消当前进行中的云端操作
|
||||
void cancelRemoteOperation() {
|
||||
_remoteCancelToken?.cancel();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 抓取远端更新
|
||||
Future<GitRemoteCommandResult> fetchRemote({String? remote}) async {
|
||||
if (_currentRepoPath == null) {
|
||||
return GitRemoteCommandResult.error('未选择仓库');
|
||||
}
|
||||
if (isRemoteBusy) {
|
||||
return GitRemoteCommandResult.error('已有云端操作正在进行,请稍候');
|
||||
}
|
||||
|
||||
_isFetching = true;
|
||||
_beginRemoteOperation();
|
||||
await _prepareRemoteOperation();
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final res = await _gitService.fetch(
|
||||
_currentRepoPath!,
|
||||
rootPath: _rootPath,
|
||||
// 修复:原先直接取 defaultRemote,fork 场景下会永远抓 origin
|
||||
// 而漏掉上游,导致 upstream/* 过期、ahead/behind 长期算错。
|
||||
remote: remote ?? fetchTarget?.name,
|
||||
onProgress: _onProgress,
|
||||
cancelToken: _remoteCancelToken,
|
||||
);
|
||||
_recordRemoteResult(res);
|
||||
if (res.success) _lastFetchAt = DateTime.now();
|
||||
// fetch 会改变 ahead/behind,必须即时刷新远端状态
|
||||
await _loadRemoteStateOnly();
|
||||
return res;
|
||||
} finally {
|
||||
_isFetching = false;
|
||||
_endRemoteOperation();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 拉取远端改动(rebase 方式)
|
||||
///
|
||||
/// [remote]/[branch] 缺省时使用当前分支的上游。
|
||||
Future<GitRemoteCommandResult> pull({
|
||||
String? remote,
|
||||
String? branch,
|
||||
}) async {
|
||||
if (_currentRepoPath == null) {
|
||||
return GitRemoteCommandResult.error('未选择仓库');
|
||||
}
|
||||
if (isRemoteBusy) {
|
||||
return GitRemoteCommandResult.error('已有云端操作正在进行,请稍候');
|
||||
}
|
||||
|
||||
final targetRemote = remote ?? pullTarget?.name;
|
||||
final targetBranch = branch ?? _tracking?.upstreamBranch;
|
||||
|
||||
_isPulling = true;
|
||||
_beginRemoteOperation();
|
||||
await _prepareRemoteOperation();
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final res = await _gitService.pullRebase(
|
||||
_currentRepoPath!,
|
||||
rootPath: _rootPath,
|
||||
remote: targetRemote,
|
||||
branch: targetBranch,
|
||||
onProgress: _onProgress,
|
||||
cancelToken: _remoteCancelToken,
|
||||
);
|
||||
_recordRemoteResult(res);
|
||||
if (res.success) {
|
||||
// pull 会改动工作区与提交历史,需完整刷新
|
||||
await _loadCurrentRepoDetails();
|
||||
notifyListeners();
|
||||
} else {
|
||||
await _loadRemoteStateOnly();
|
||||
}
|
||||
return res;
|
||||
} finally {
|
||||
_isPulling = false;
|
||||
_endRemoteOperation();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 推送当前分支到远端
|
||||
///
|
||||
/// [setUpstream] 用于首次发布分支;[forceWithLease] 为唯一允许的强制方式。
|
||||
Future<GitRemoteCommandResult> push({
|
||||
String? remote,
|
||||
String? branch,
|
||||
bool? setUpstream,
|
||||
bool forceWithLease = false,
|
||||
}) async {
|
||||
if (_currentRepoPath == null) {
|
||||
return GitRemoteCommandResult.error('未选择仓库');
|
||||
}
|
||||
if (isRemoteBusy) {
|
||||
return GitRemoteCommandResult.error('已有云端操作正在进行,请稍候');
|
||||
}
|
||||
|
||||
// 推送目标遵循 git 自身优先级:pushRemote → pushDefault → 上游 → origin
|
||||
final targetRemote = remote ?? pushTarget.remote?.name;
|
||||
final targetBranch = branch ?? _currentBranch;
|
||||
final shouldSetUpstream = setUpstream ?? needsPublishBranch;
|
||||
|
||||
_isPushing = true;
|
||||
_beginRemoteOperation();
|
||||
await _prepareRemoteOperation();
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final res = await _gitService.push(
|
||||
_currentRepoPath!,
|
||||
rootPath: _rootPath,
|
||||
remote: targetRemote,
|
||||
branch: targetBranch,
|
||||
setUpstream: shouldSetUpstream,
|
||||
forceWithLease: forceWithLease,
|
||||
onProgress: _onProgress,
|
||||
cancelToken: _remoteCancelToken,
|
||||
);
|
||||
_recordRemoteResult(res);
|
||||
if (res.success) {
|
||||
await _loadRemoteStateOnly();
|
||||
}
|
||||
return res;
|
||||
} finally {
|
||||
_isPushing = false;
|
||||
_endRemoteOperation();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 推送被拒后的一键修复:先 pull(rebase) 再 push
|
||||
Future<({bool success, GitRemoteCommandResult? pullResult, GitRemoteCommandResult? pushResult})>
|
||||
pullThenPush() async {
|
||||
if (_currentRepoPath == null) {
|
||||
return (success: false, pullResult: null, pushResult: null);
|
||||
}
|
||||
|
||||
final pullRes = await pull();
|
||||
if (!pullRes.success) {
|
||||
return (success: false, pullResult: pullRes, pushResult: null);
|
||||
}
|
||||
|
||||
final pushRes = await push();
|
||||
return (success: pushRes.success, pullResult: pullRes, pushResult: pushRes);
|
||||
}
|
||||
|
||||
// ------------------------------ 远端仓库管理 ------------------------------
|
||||
|
||||
/// 重新加载远程仓库列表
|
||||
Future<void> reloadRemotes() async {
|
||||
if (_currentRepoPath == null) return;
|
||||
_remotes = await _gitService.getRemotes(_currentRepoPath!);
|
||||
// 远程集合变化可能使会话级选择失效(被删除/改名),
|
||||
// 这里即时清除而不是等到读取时才发现,避免状态长期不一致。
|
||||
if (_pushTargetOverride != null && _remoteByName(_pushTargetOverride) == null) {
|
||||
_pushTargetOverride = null;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 添加远程仓库
|
||||
Future<GitCommandResult> addRemote(
|
||||
String name,
|
||||
String url, {
|
||||
String? pushUrl,
|
||||
}) async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.addRemote(
|
||||
_currentRepoPath!,
|
||||
name,
|
||||
url,
|
||||
pushUrl: pushUrl,
|
||||
);
|
||||
if (res.success) await reloadRemotes();
|
||||
return res;
|
||||
}
|
||||
|
||||
/// 移除远程仓库
|
||||
Future<GitCommandResult> removeRemote(String name) async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.removeRemote(_currentRepoPath!, name);
|
||||
if (res.success) {
|
||||
await reloadRemotes();
|
||||
// 移除远程仓库会影响上游追踪关系
|
||||
_tracking = await _gitService.getBranchTracking(_currentRepoPath!);
|
||||
notifyListeners();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/// 重命名远程仓库
|
||||
Future<GitCommandResult> renameRemote(String oldName, String newName) async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.renameRemote(_currentRepoPath!, oldName, newName);
|
||||
if (res.success) {
|
||||
await reloadRemotes();
|
||||
_tracking = await _gitService.getBranchTracking(_currentRepoPath!);
|
||||
notifyListeners();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/// 修改远程仓库地址
|
||||
Future<GitCommandResult> setRemoteUrl(
|
||||
String name,
|
||||
String url, {
|
||||
bool push = false,
|
||||
}) async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.setRemoteUrl(
|
||||
_currentRepoPath!,
|
||||
name,
|
||||
url,
|
||||
push: push,
|
||||
);
|
||||
if (res.success) await reloadRemotes();
|
||||
return res;
|
||||
}
|
||||
|
||||
/// 清理远端已删除的追踪分支
|
||||
Future<GitCommandResult> pruneRemote(String name) async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.pruneRemote(_currentRepoPath!, name);
|
||||
if (res.success) await reloadRemotes();
|
||||
return res;
|
||||
}
|
||||
|
||||
/// 自检:验证远端地址的连通性与认证是否可用
|
||||
Future<GitRemoteCommandResult> checkRemoteAuth(String url) async {
|
||||
if (_currentRepoPath == null) {
|
||||
return GitRemoteCommandResult.error('未选择仓库');
|
||||
}
|
||||
_beginRemoteOperation();
|
||||
await _prepareRemoteOperation();
|
||||
notifyListeners();
|
||||
try {
|
||||
final res = await _gitService.checkRemoteAuth(
|
||||
_currentRepoPath!,
|
||||
url,
|
||||
rootPath: _rootPath,
|
||||
cancelToken: _remoteCancelToken,
|
||||
);
|
||||
_recordRemoteResult(res);
|
||||
return res;
|
||||
} finally {
|
||||
_endRemoteOperation();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 清除最近一次云端错误提示
|
||||
void clearRemoteError() {
|
||||
_lastRemoteError = null;
|
||||
_remoteLog = '';
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// ---------------------------- 远端分支检出 ----------------------------
|
||||
|
||||
/// 基于远端分支检出(或切换到已有的)本地分支
|
||||
///
|
||||
/// 本地已有同名分支时直接切换;否则以远端分支为起点创建并建立追踪关系。
|
||||
Future<GitCommandResult> checkoutRemoteBranch(
|
||||
GitRemoteBranch branch, {
|
||||
bool forceCreate = false,
|
||||
}) async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.checkoutRemoteBranch(
|
||||
_currentRepoPath!,
|
||||
branch.remoteName,
|
||||
branch.branchName,
|
||||
forceCreate: forceCreate,
|
||||
);
|
||||
if (res.success) {
|
||||
_pushTargetOverride = null;
|
||||
await _loadCurrentRepoDetails();
|
||||
notifyListeners();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// ---------------------------- 仓库自检 ----------------------------
|
||||
|
||||
/// 执行仓库完整性自检
|
||||
Future<GitFsckReport?> runFsck() async {
|
||||
if (_currentRepoPath == null) return null;
|
||||
if (_isFsckRunning) return _fsckReport;
|
||||
|
||||
_isFsckRunning = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
_fsckReport = await _gitService.fsck(_currentRepoPath!);
|
||||
return _fsckReport;
|
||||
} catch (e) {
|
||||
debugPrint('[GitProvider] 仓库自检异常: $e');
|
||||
return null;
|
||||
} finally {
|
||||
_isFsckRunning = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 清除自检结果
|
||||
void clearFsckReport() {
|
||||
_fsckReport = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// ---------------------------- 克隆 ----------------------------
|
||||
|
||||
/// 克隆远端仓库到指定项目的内部目录
|
||||
///
|
||||
/// [parentDir] 为克隆父目录(通常是项目根),克隆完成后目标目录为
|
||||
/// `parentDir/<targetDirName>`。
|
||||
Future<GitRemoteCommandResult> cloneRepository({
|
||||
required String parentDir,
|
||||
required String url,
|
||||
required String targetDirName,
|
||||
String? branch,
|
||||
int? depth,
|
||||
HeadlessCancelToken? cancelToken,
|
||||
}) async {
|
||||
if (_isCloning) {
|
||||
return GitRemoteCommandResult.error('已有克隆任务正在进行');
|
||||
}
|
||||
|
||||
_isCloning = true;
|
||||
_cloneProgress = null;
|
||||
_lastRemoteError = null;
|
||||
_remoteLog = '';
|
||||
_remoteCancelToken = cancelToken ?? HeadlessCancelToken();
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await _prepareRemoteOperation();
|
||||
final res = await _gitService.clone(
|
||||
parentDir,
|
||||
url,
|
||||
targetDirName,
|
||||
rootPath: parentDir,
|
||||
branch: branch,
|
||||
depth: depth,
|
||||
onProgress: _onCloneProgress,
|
||||
cancelToken: _remoteCancelToken,
|
||||
);
|
||||
_remoteLog = res.combinedOutput.trim();
|
||||
if (!res.success && !res.cancelled) {
|
||||
_lastRemoteError = GitErrorMapper.describe(res.stderr, stdout: res.stdout);
|
||||
}
|
||||
return res;
|
||||
} finally {
|
||||
_isCloning = false;
|
||||
_cloneProgress = null;
|
||||
_remoteCancelToken = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void _onCloneProgress(GitOperationProgress progress) {
|
||||
_cloneProgress = progress;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 取消进行中的克隆
|
||||
void cancelClone() {
|
||||
if (!_isCloning) return;
|
||||
_remoteCancelToken?.cancel();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 放弃进行中的 rebase(冲突后的退出路径)
|
||||
Future<GitCommandResult> abortRebase() async {
|
||||
if (_currentRepoPath == null) return GitCommandResult.error('未选择仓库');
|
||||
final res = await _gitService.abortRebase(_currentRepoPath!);
|
||||
if (res.success) {
|
||||
await _loadCurrentRepoDetails();
|
||||
notifyListeners();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/// 仓库是否正处于 rebase 中间状态
|
||||
bool get isRebaseInProgress =>
|
||||
_currentRepoPath != null && _gitService.isRebaseInProgress(_currentRepoPath!);
|
||||
|
||||
/// 贮藏本地改动后拉取(不自动 pop,避免冲突被静默带入工作区)
|
||||
///
|
||||
/// 拉取成功后本地改动仍在 stash 栈中,由用户在需要时手动恢复,
|
||||
/// 这样"贮藏"这个动作始终对用户可见、可回退。
|
||||
Future<({bool success, GitRemoteCommandResult? pullResult, GitCommandResult? stashResult})>
|
||||
stashAndPull() async {
|
||||
if (_currentRepoPath == null) {
|
||||
return (success: false, pullResult: null, stashResult: null);
|
||||
}
|
||||
|
||||
final stashRes = await _gitService.stash(
|
||||
_currentRepoPath!,
|
||||
message: 'Auto-stash before pull',
|
||||
);
|
||||
if (!stashRes.success) {
|
||||
return (success: false, pullResult: null, stashResult: stashRes);
|
||||
}
|
||||
await _loadCurrentRepoDetails();
|
||||
|
||||
final pullRes = await pull();
|
||||
return (
|
||||
success: pullRes.success,
|
||||
pullResult: pullRes,
|
||||
stashResult: stashRes,
|
||||
);
|
||||
}
|
||||
|
||||
/// 预先计算文件和目录的 Git 状态映射,将文件树每帧查询时间复杂度从 O(N*M) 降至 O(1)
|
||||
void _rebuildStatusMaps() {
|
||||
final fileMap = <String, GitFileStatus>{};
|
||||
@@ -651,6 +1383,15 @@ class GitProvider extends ChangeNotifier {
|
||||
bool? showTagsInSelector,
|
||||
String? commitMessage,
|
||||
double? scrollOffset,
|
||||
List<GitRemote>? remotes,
|
||||
GitBranchTracking? tracking,
|
||||
bool? isFetching,
|
||||
bool? isPulling,
|
||||
bool? isPushing,
|
||||
GitOperationProgress? remoteProgress,
|
||||
String? remoteLog,
|
||||
GitErrorInfo? lastRemoteError,
|
||||
DateTime? lastFetchAt,
|
||||
}) {
|
||||
if (rootPath != null) _rootPath = rootPath;
|
||||
if (repositories != null) _repositories = repositories;
|
||||
@@ -682,6 +1423,15 @@ class GitProvider extends ChangeNotifier {
|
||||
if (showTagsInSelector != null) _showTagsInSelector = showTagsInSelector;
|
||||
if (commitMessage != null) _commitMessage = commitMessage;
|
||||
if (scrollOffset != null) _scrollOffset = scrollOffset;
|
||||
if (remotes != null) _remotes = remotes;
|
||||
if (tracking != null) _tracking = tracking;
|
||||
if (isFetching != null) _isFetching = isFetching;
|
||||
if (isPulling != null) _isPulling = isPulling;
|
||||
if (isPushing != null) _isPushing = isPushing;
|
||||
if (remoteProgress != null) _remoteProgress = remoteProgress;
|
||||
if (remoteLog != null) _remoteLog = remoteLog;
|
||||
if (lastRemoteError != null) _lastRemoteError = lastRemoteError;
|
||||
if (lastFetchAt != null) _lastFetchAt = lastFetchAt;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user