diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 97bc0cbfb..5dc121d6a 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -1059,6 +1059,7 @@ Template: ${0}$ WORKSPACE: Configure Workspaces... + Fetch All Repositories WORKTREE BRANCH Copy Path diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index ede43ac36..7392d8766 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -1063,6 +1063,7 @@ 模板:${0}$ 工作区: 配置工作区... + 拉取工作区所有仓库 本地工作树 連結分支 复制工作树路径 diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 20343afae..7982a279b 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -1039,6 +1039,7 @@ 範本: ${0}$ 工作區: 設定工作區... + 提取工作區所有存放庫 本機工作樹 分支 複製工作樹路徑 diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 1cf00a848..ccfcad5d1 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.IO; using System.Text; +using System.Threading.Tasks; using Avalonia.Collections; using Avalonia.Threading; @@ -122,6 +124,46 @@ public void CloseAll() _ignoreIndexChange = false; } + public async Task FetchAllRepositoriesAsync() + { + // avoid collection was modified while enumerating. + var pages = new List(); + pages.AddRange(Pages); + + var total = 0; + var succeeded = 0; + var noRemoteNames = new List(); + var failedNames = new List(); + + foreach (var page in pages) + { + if (page.Data is not Repository repo) + continue; + + if (repo.Remotes.Count == 0) + { + noRemoteNames.Add(page.Node.Name); + continue; + } + + total++; + if (await repo.FetchAllRemotesAsync()) + succeeded++; + else + failedNames.Add(page.Node.Name); + } + + var message = $"Fetched {succeeded}/{total} repositories"; + + if (noRemoteNames.Count > 0) + message += $"\n{noRemoteNames.Count} repositories skipped (no remote):\n {string.Join("\n ", noRemoteNames)}"; + + if (failedNames.Count > 0) + message += $"\n{failedNames.Count} repositories failed:\n {string.Join("\n ", failedNames)}"; + + Models.Notification.Send(null, message, succeeded < total); + } + public void SwitchWorkspace(Workspace to) { if (to == null || to.IsActive) diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 695b69632..9a5dad531 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -697,6 +697,46 @@ public async Task FetchAsync(bool autoStart) ShowPopup(new Fetch(this)); } + public async Task FetchAllRemotesAsync() + { + if (IsAutoFetching) + return false; + + CommandLog log = null; + var succeeded = true; + + try + { + var lockFile = Path.Combine(GitDir, "index.lock"); + if (File.Exists(lockFile)) + return false; + + if (_remotes.Count == 0) + return false; + + IsAutoFetching = true; + log = CreateLog("Fetch"); + + foreach (var remote in _remotes) + { + var succ = await new Commands.Fetch(FullPath, remote).Use(log).ExecAsync(); + if (!succ) + succeeded = false; + } + + _lastFetchTime = DateTime.Now; + } + catch + { + // Ignore all exceptions. + succeeded = false; + } + + IsAutoFetching = false; + log?.Complete(); + return succeeded; + } + public async Task PullAsync(bool autoStart) { if (IsBare || !CanCreatePopup()) diff --git a/src/Views/Launcher.axaml.cs b/src/Views/Launcher.axaml.cs index 0aedfbc72..e67eda1e3 100644 --- a/src/Views/Launcher.axaml.cs +++ b/src/Views/Launcher.axaml.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Avalonia; using Avalonia.Controls; @@ -409,6 +410,19 @@ private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e) menu.Items.Add(new MenuItem() { Header = "-" }); + var hasRepos = launcher.Pages.Any(p => p.Data is ViewModels.Repository); + + var fetchAll = new MenuItem(); + fetchAll.Header = App.Text("Workspace.FetchAllRepositories"); + fetchAll.Icon = this.CreateMenuIcon("Icons.Fetch"); + fetchAll.IsEnabled = hasRepos; + fetchAll.Click += async (_, ev) => + { + await launcher.FetchAllRepositoriesAsync(); + ev.Handled = true; + }; + menu.Items.Add(fetchAll); + var configure = new MenuItem(); configure.Header = App.Text("Workspace.Configure"); configure.Click += async (_, ev) =>