From ebc2fc6b91d9162bca425f0089c6c201733c3ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 7 Sep 2021 10:52:48 +0800 Subject: [PATCH] feature: query git version after selecting git path --- src/Commands/Version.cs | 18 ++++++++++++++ src/Models/Preference.cs | 4 +--- src/Resources/Locales/en_US.xaml | 1 + src/Resources/Locales/zh_CN.xaml | 1 + src/Views/Preference.xaml | 38 +++++++++++++++++++---------- src/Views/Preference.xaml.cs | 41 +++++++++++++++++++------------- 6 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 src/Commands/Version.cs diff --git a/src/Commands/Version.cs b/src/Commands/Version.cs new file mode 100644 index 00000000..1911f34a --- /dev/null +++ b/src/Commands/Version.cs @@ -0,0 +1,18 @@ +using System; + +namespace SourceGit.Commands { + /// + /// 检测git是否可用,并获取git版本信息 + /// + public class Version : Command { + const string GitVersionPrefix = "git version "; + public string Query() { + Args = $"--version"; + var result = ReadToEnd(); + if (!result.IsSuccess || string.IsNullOrEmpty(result.Output)) return null; + var version = result.Output.Trim(); + if (!version.StartsWith(GitVersionPrefix, StringComparison.Ordinal)) return null; + return version.Substring(GitVersionPrefix.Length); + } + } +} diff --git a/src/Models/Preference.cs b/src/Models/Preference.cs index 3c1c6837..66071940 100644 --- a/src/Models/Preference.cs +++ b/src/Models/Preference.cs @@ -198,9 +198,7 @@ namespace SourceGit.Models { /// [JsonIgnore] public bool IsReady { - get { - return !string.IsNullOrEmpty(Git.Path) && File.Exists(Git.Path); - } + get => File.Exists(Git.Path) && new Commands.Version().Query() != null; } #region DATA diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index 58713562..d332a421 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -364,6 +364,7 @@ GIT SETTING Install Path Input path for git.exe + Git version Default Clone Dir Default path to clone repo into User Name diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index 69212190..663db92e 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -363,6 +363,7 @@ GIT配置 安装路径 填写git.exe所在位置 + Git 版本 默认克隆路径 默认的仓库本地存放位置 用户名 diff --git a/src/Views/Preference.xaml b/src/Views/Preference.xaml index 6b661f50..0fa7a85d 100644 --- a/src/Views/Preference.xaml +++ b/src/Views/Preference.xaml @@ -73,6 +73,7 @@ + @@ -230,13 +231,24 @@ Icon="{StaticResource Icon.Folder.Open}"/> - + + + + + - + @@ -259,12 +271,12 @@ - + diff --git a/src/Views/Preference.xaml.cs b/src/Views/Preference.xaml.cs index 6580e377..5cab241d 100644 --- a/src/Views/Preference.xaml.cs +++ b/src/Views/Preference.xaml.cs @@ -15,6 +15,7 @@ namespace SourceGit.Views { public string User { get; set; } public string Email { get; set; } public string CRLF { get; set; } + public string Version { get; set; } // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathfindonpathw // https://www.pinvoke.net/default.aspx/shlwapi.PathFindOnPath @@ -24,11 +25,7 @@ namespace SourceGit.Views { public bool EnableWindowsTerminal { get; set; } = PathFindOnPath(new StringBuilder("wt.exe"), null); public Preference() { - if (!UpdateGitInfoIfReady()) { - User = ""; - Email = ""; - CRLF = "false"; - } + UpdateGitInfo(false); if (!EnableWindowsTerminal) { Models.Preference.Instance.General.UseWindowsTerminal = false; @@ -37,13 +34,27 @@ namespace SourceGit.Views { InitializeComponent(); } - private bool UpdateGitInfoIfReady() { - if (!Models.Preference.Instance.IsReady) return false; - User = new Commands.Config().Get("user.name"); - Email = new Commands.Config().Get("user.email"); - CRLF = new Commands.Config().Get("core.autocrlf"); - if (string.IsNullOrEmpty(CRLF)) CRLF = "false"; - return true; + private bool UpdateGitInfo(bool updateUi) { + var isReady = Models.Preference.Instance.IsReady; + if (isReady) { + User = new Commands.Config().Get("user.name"); + Email = new Commands.Config().Get("user.email"); + CRLF = new Commands.Config().Get("core.autocrlf"); + Version = new Commands.Version().Query(); + if (string.IsNullOrEmpty(CRLF)) CRLF = "false"; + } else { + User = ""; + Email = ""; + CRLF = "false"; + Version = "Unknown"; + } + if (updateUi) { + editGitUser?.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); + editGitEmail?.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); + editGitCrlf?.GetBindingExpression(ComboBox.SelectedValueProperty).UpdateTarget(); + textGitVersion?.GetBindingExpression(TextBlock.TextProperty).UpdateTarget(); + } + return isReady; } #region EVENTS @@ -69,11 +80,7 @@ namespace SourceGit.Views { if (dialog.ShowDialog() == true) { Models.Preference.Instance.Git.Path = dialog.FileName; editGitPath?.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); - if (UpdateGitInfoIfReady()) { - editGitUser?.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); - editGitEmail?.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); - editGitCrlf?.GetBindingExpression(ComboBox.SelectedValueProperty).UpdateTarget(); - } + UpdateGitInfo(true); } }