From 245084fd349a230f1e9528b0a42955ab37429176 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 8 Apr 2021 10:57:28 +0800 Subject: [PATCH] feature: add settings for fetching remotes automatically --- src/App.preference.cs | 4 ++ src/Git/Repository.cs | 32 ++++++++++---- src/Resources/Locales/en_US.xaml | 9 ++-- src/Resources/Locales/zh_CN.xaml | 7 ++- src/UI/PopupManager.xaml | 4 +- src/UI/SettingDialog.xaml | 76 ++++++++++++++++---------------- 6 files changed, 75 insertions(+), 57 deletions(-) diff --git a/src/App.preference.cs b/src/App.preference.cs index 86d541d2..a30204a1 100644 --- a/src/App.preference.cs +++ b/src/App.preference.cs @@ -118,6 +118,10 @@ namespace SourceGit { /// public int LastCheckUpdate { get; set; } = 0; /// + /// Fetch remotes automatically? + /// + public bool AutoFetchRemotes { get; set; } = true; + /// /// Settings for executables. /// public ToolSetting Tools { get; set; } = new ToolSetting(); diff --git a/src/Git/Repository.cs b/src/Git/Repository.cs index e3609b5c..52df196f 100644 --- a/src/Git/Repository.cs +++ b/src/Git/Repository.cs @@ -5,6 +5,7 @@ using System.IO; using System.Text; using System.Text.Json.Serialization; using System.Text.RegularExpressions; +using System.Threading; using System.Windows.Threading; namespace SourceGit.Git { @@ -65,12 +66,13 @@ namespace SourceGit.Git { private List cachedTags = new List(); private FileSystemWatcher gitDirWatcher = null; private FileSystemWatcher workingCopyWatcher = null; - private DispatcherTimer timer = null; + private Timer timer = null; private bool isWatcherDisabled = false; private long nextUpdateTags = 0; private long nextUpdateLocalChanges = 0; private long nextUpdateStashes = 0; private long nextUpdateTree = 0; + private long nextFetchingRemotes = 0; private string featurePrefix = null; private string releasePrefix = null; @@ -197,6 +199,9 @@ namespace SourceGit.Git { OnWorkingCopyChanged?.Invoke(); OnTagChanged?.Invoke(); + nextUpdateLocalChanges = 0; + nextUpdateTags = 0; + nextUpdateTree = 0; isWatcherDisabled = false; } #endregion @@ -250,6 +255,7 @@ namespace SourceGit.Git { /// public void Open() { isWatcherDisabled = false; + nextFetchingRemotes = DateTime.Now.AddMinutes(10).ToFileTime(); GitDir = ".git"; RunCommand("rev-parse --git-dir", line => { @@ -287,10 +293,7 @@ namespace SourceGit.Git { workingCopyWatcher.Deleted += OnWorkingCopyFSChanged; workingCopyWatcher.EnableRaisingEvents = true; - timer = new DispatcherTimer(); - timer.Tick += Tick; - timer.Interval = TimeSpan.FromSeconds(.1); - timer.Start(); + timer = new Timer(Tick, null, 100, 100); featurePrefix = GetConfig("gitflow.prefix.feature"); releasePrefix = GetConfig("gitflow.prefix.release"); @@ -320,7 +323,7 @@ namespace SourceGit.Git { workingCopyWatcher.EnableRaisingEvents = false; gitDirWatcher.Dispose(); workingCopyWatcher.Dispose(); - timer.Stop(); + timer.Dispose(); gitDirWatcher = null; workingCopyWatcher = null; @@ -338,7 +341,13 @@ namespace SourceGit.Git { isWatcherDisabled = !enabled; } - private void Tick(object sender, EventArgs e) { + private void Tick(object sender) { + var now = DateTime.Now.ToFileTime(); + if (now >= nextFetchingRemotes) { + Fetch(null, true, null, false); + return; + } + if (isWatcherDisabled) { nextUpdateLocalChanges = 0; nextUpdateStashes = 0; @@ -347,7 +356,6 @@ namespace SourceGit.Git { return; } - var now = DateTime.Now.ToFileTime(); if (nextUpdateLocalChanges > 0 && now >= nextUpdateLocalChanges) { nextUpdateLocalChanges = 0; OnWorkingCopyChanged?.Invoke(); @@ -443,8 +451,10 @@ namespace SourceGit.Git { /// /// /// - public void Fetch(Remote remote, bool prune, Action onProgress) { + /// + public void Fetch(Remote remote, bool prune, Action onProgress, bool raiseError = true) { isWatcherDisabled = true; + nextFetchingRemotes = DateTime.Now.AddMinutes(10).ToFileTime(); var args = "-c credential.helper=manager fetch --progress --verbose "; @@ -461,6 +471,9 @@ namespace SourceGit.Git { }, true); OnSubmoduleChanged?.Invoke(); + if (!raiseError) errs = null; + + nextFetchingRemotes = DateTime.Now.AddMinutes(10).ToFileTime(); AssertCommand(errs); } @@ -475,6 +488,7 @@ namespace SourceGit.Git { /// Progress message handler. public void Pull(string remote, string branch, Action onProgress, bool rebase = false, bool autostash = false) { isWatcherDisabled = true; + nextFetchingRemotes = DateTime.Now.AddMinutes(10).ToFileTime(); var args = "-c credential.helper=manager pull --verbose --progress "; var needPopStash = false; diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index cdc851ea..763e5d49 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -87,8 +87,6 @@ User name for this repository Email : Email address - COMMIT TEMPLATE - Template : Create Branch Create Local Branch @@ -345,10 +343,11 @@ Preference GENERAL SETTING RESTART REQUIRED - Display Language : - Light Theme : - Check for Update : + Language : Avatar Server : + Use light theme + Check for update + Fetch remotes automatically GIT INSTANCE Install Path : Input path for git.exe diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index bc2f6903..dbe1bc3e 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -87,8 +87,6 @@ 应用于本仓库的用户名 邮箱 : 邮箱地址 - 提交模板 - 模板内容 : 新建分支 创建本地分支 @@ -346,9 +344,10 @@ 通用配置 需要重启软件 显示语言 : - 启用浅色主题 : - 检测更新 : 头像服务 : + 启用浅色主题 + 启用检测更新 + 启用定时自动拉取远程更新 GIT配置 安装路径 : 填写git.exe所在位置 diff --git a/src/UI/PopupManager.xaml b/src/UI/PopupManager.xaml index ec5f17ca..4a8b4aec 100644 --- a/src/UI/PopupManager.xaml +++ b/src/UI/PopupManager.xaml @@ -9,8 +9,8 @@ - - + + diff --git a/src/UI/SettingDialog.xaml b/src/UI/SettingDialog.xaml index fbae2ac4..d1e26649 100644 --- a/src/UI/SettingDialog.xaml +++ b/src/UI/SettingDialog.xaml @@ -8,7 +8,7 @@ xmlns:app="clr-namespace:SourceGit" xmlns:git="clr-namespace:SourceGit.Git" mc:Ignorable="d" - Height="588" Width="500" + Height="616" Width="500" Title="{StaticResource Text.Preference}" WindowStartupLocation="CenterOwner" ResizeMode="NoResize"> @@ -64,6 +64,7 @@ + @@ -81,7 +82,7 @@ - + @@ -99,22 +100,8 @@ VerticalContentAlignment="Center" DisplayMemberPath="Desc" SelectionChanged="ChangeLanguage"/> - -