From 5e706588a0e54b9be600d7f5b3d778a745a4c281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 3 Sep 2021 19:27:19 +0800 Subject: [PATCH 1/4] feature: support Windows Terminal --- src/Models/Preference.cs | 5 + src/Resources/Locales/en_US.xaml | 1 + src/Resources/Locales/zh_CN.xaml | 1 + src/Views/Preference.xaml | 170 +++++++++++++++------------- src/Views/Preference.xaml.cs | 13 +++ src/Views/Widgets/Dashboard.xaml.cs | 19 +++- 6 files changed, 123 insertions(+), 86 deletions(-) diff --git a/src/Models/Preference.cs b/src/Models/Preference.cs index 3224a329..04362cc7 100644 --- a/src/Models/Preference.cs +++ b/src/Models/Preference.cs @@ -77,6 +77,11 @@ namespace SourceGit.Models { /// 是否启用崩溃上报 /// public bool EnableCrashReport { get; set; } = false; + + /// + /// 是否尝试使用 Windows Terminal 打开终端 + /// + public bool UseWindowsTerminal { get; set; } = false; } /// diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index b70851d3..58713562 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -360,6 +360,7 @@ Fetch remotes automatically (need restart) Restore windows Enable crash report (maybe include related path) + Use Windows Terminal to open Git terminal GIT SETTING Install Path Input path for git.exe diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index ae84d628..69212190 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -359,6 +359,7 @@ 启用定时自动拉取远程更新(重启生效) 启动时恢复上次打开的仓库 开启崩溃上报(可能涉及上报相关路径) + 使用 Windows Terminal 打开 Git 终端 GIT配置 安装路径 填写git.exe所在位置 diff --git a/src/Views/Preference.xaml b/src/Views/Preference.xaml index 62415a8d..92c24416 100644 --- a/src/Views/Preference.xaml +++ b/src/Views/Preference.xaml @@ -1,4 +1,4 @@ - - + @@ -31,24 +31,24 @@ - + - + - + - - @@ -65,6 +65,7 @@ + @@ -87,33 +88,33 @@ - + - - - + - - - - - - - + - + + + + - + - - + @@ -221,14 +229,14 @@ BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}" Icon="{StaticResource Icon.Folder.Open}"/> - + - - + @@ -248,39 +256,39 @@ BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}" Icon="{StaticResource Icon.Folder.Open}"/> - + - - - + - - - + - - - - + diff --git a/src/Views/Preference.xaml.cs b/src/Views/Preference.xaml.cs index 6a91aacc..1ec52efe 100644 --- a/src/Views/Preference.xaml.cs +++ b/src/Views/Preference.xaml.cs @@ -1,5 +1,7 @@ using Microsoft.Win32; using System; +using System.Runtime.InteropServices; +using System.Text; using System.Windows; using System.Windows.Controls; @@ -14,6 +16,13 @@ namespace SourceGit.Views { public string Email { get; set; } public string CRLF { get; set; } + // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathfindonpathw + // https://www.pinvoke.net/default.aspx/shlwapi.PathFindOnPath + [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)] + private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs); + + public bool EnableWindowsTerminal { get; set; } = PathFindOnPath(new StringBuilder("wt.exe"), null); + public Preference() { if (Models.Preference.Instance.IsReady) { User = new Commands.Config().Get("user.name"); @@ -26,6 +35,10 @@ namespace SourceGit.Views { CRLF = "false"; } + if (!EnableWindowsTerminal) { + Models.Preference.Instance.General.UseWindowsTerminal = false; + } + InitializeComponent(); } diff --git a/src/Views/Widgets/Dashboard.xaml.cs b/src/Views/Widgets/Dashboard.xaml.cs index 4963a54b..64f07b9f 100644 --- a/src/Views/Widgets/Dashboard.xaml.cs +++ b/src/Views/Widgets/Dashboard.xaml.cs @@ -315,11 +315,20 @@ namespace SourceGit.Views.Widgets { Models.Exception.Raise(App.Text("MissingBash")); return; } - - var start = new ProcessStartInfo(); - start.WorkingDirectory = repo.Path; - start.FileName = bash; - Process.Start(start); + if (Models.Preference.Instance.General.UseWindowsTerminal) { + Process.Start(new ProcessStartInfo { + WorkingDirectory = repo.Path, + FileName = "wt", + Arguments = bash, + UseShellExecute = false, + }); + } else { + Process.Start(new ProcessStartInfo { + WorkingDirectory = repo.Path, + FileName = bash, + UseShellExecute = true, + }); + } e.Handled = true; } From adce866716a95029bb164eb4b27f6b52802c9f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 3 Sep 2021 22:22:53 +0800 Subject: [PATCH 2/4] feature: try finding `git.exe` automatically --- src/Views/Preference.xaml.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Views/Preference.xaml.cs b/src/Views/Preference.xaml.cs index 1ec52efe..bc5b4d4c 100644 --- a/src/Views/Preference.xaml.cs +++ b/src/Views/Preference.xaml.cs @@ -49,12 +49,18 @@ namespace SourceGit.Views { } private void SelectGitPath(object sender, RoutedEventArgs e) { - var dialog = new OpenFileDialog(); - dialog.Filter = "Git Executable|git.exe"; - dialog.FileName = "git.exe"; - dialog.Title = App.Text("Preference.Dialog.GitExe"); - dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); - dialog.CheckFileExists = true; + var sb = new StringBuilder("git.exe"); + string dir = PathFindOnPath(sb, null) + ? sb.ToString() + : Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); + + var dialog = new OpenFileDialog { + Filter = "Git Executable|git.exe", + FileName = "git.exe", + Title = App.Text("Preference.Dialog.GitExe"), + InitialDirectory = dir, + CheckFileExists = true, + }; if (dialog.ShowDialog() == true) { Models.Preference.Instance.Git.Path = dialog.FileName; From 1a5fdc540c5c7612fe1585e52ef13f42369fe845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 6 Sep 2021 19:58:41 +0800 Subject: [PATCH 3/4] feature: query git config after selecting git path --- src/Models/Preference.cs | 2 +- src/Views/Preference.xaml | 3 +++ src/Views/Preference.xaml.cs | 21 +++++++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Models/Preference.cs b/src/Models/Preference.cs index 04362cc7..3c1c6837 100644 --- a/src/Models/Preference.cs +++ b/src/Models/Preference.cs @@ -194,7 +194,7 @@ namespace SourceGit.Models { } /// - /// 检测配置是否 + /// 检测配置是否正常 /// [JsonIgnore] public bool IsReady { diff --git a/src/Views/Preference.xaml b/src/Views/Preference.xaml index 92c24416..6b661f50 100644 --- a/src/Views/Preference.xaml +++ b/src/Views/Preference.xaml @@ -265,6 +265,7 @@ Margin="0,0,8,0"/> @@ -277,6 +278,7 @@ Margin="0,0,8,0"/> @@ -289,6 +291,7 @@ Margin="0,0,8,0"/> Date: Tue, 7 Sep 2021 10:52:48 +0800 Subject: [PATCH 4/4] 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); } }