feature<Preference>: query git version after selecting git path

This commit is contained in:
李通洲 2021-09-07 10:52:48 +08:00
parent 1a5fdc540c
commit ebc2fc6b91
6 changed files with 70 additions and 33 deletions

18
src/Commands/Version.cs Normal file
View file

@ -0,0 +1,18 @@
using System;
namespace SourceGit.Commands {
/// <summary>
/// 检测git是否可用并获取git版本信息
/// </summary>
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);
}
}
}

View file

@ -198,9 +198,7 @@ namespace SourceGit.Models {
/// </summary> /// </summary>
[JsonIgnore] [JsonIgnore]
public bool IsReady { public bool IsReady {
get { get => File.Exists(Git.Path) && new Commands.Version().Query() != null;
return !string.IsNullOrEmpty(Git.Path) && File.Exists(Git.Path);
}
} }
#region DATA #region DATA

View file

@ -364,6 +364,7 @@
<sys:String x:Key="Text.Preference.Git">GIT SETTING</sys:String> <sys:String x:Key="Text.Preference.Git">GIT SETTING</sys:String>
<sys:String x:Key="Text.Preference.Git.Path">Install Path</sys:String> <sys:String x:Key="Text.Preference.Git.Path">Install Path</sys:String>
<sys:String x:Key="Text.Preference.Git.Path.Placeholder">Input path for git.exe</sys:String> <sys:String x:Key="Text.Preference.Git.Path.Placeholder">Input path for git.exe</sys:String>
<sys:String x:Key="Text.Preference.Git.Version">Git version</sys:String>
<sys:String x:Key="Text.Preference.Git.Dir">Default Clone Dir</sys:String> <sys:String x:Key="Text.Preference.Git.Dir">Default Clone Dir</sys:String>
<sys:String x:Key="Text.Preference.Git.Dir.Placeholder">Default path to clone repo into</sys:String> <sys:String x:Key="Text.Preference.Git.Dir.Placeholder">Default path to clone repo into</sys:String>
<sys:String x:Key="Text.Preference.Git.User">User Name</sys:String> <sys:String x:Key="Text.Preference.Git.User">User Name</sys:String>

View file

@ -363,6 +363,7 @@
<sys:String x:Key="Text.Preference.Git">GIT配置</sys:String> <sys:String x:Key="Text.Preference.Git">GIT配置</sys:String>
<sys:String x:Key="Text.Preference.Git.Path">安装路径</sys:String> <sys:String x:Key="Text.Preference.Git.Path">安装路径</sys:String>
<sys:String x:Key="Text.Preference.Git.Path.Placeholder">填写git.exe所在位置</sys:String> <sys:String x:Key="Text.Preference.Git.Path.Placeholder">填写git.exe所在位置</sys:String>
<sys:String x:Key="Text.Preference.Git.Version">Git 版本</sys:String>
<sys:String x:Key="Text.Preference.Git.Dir">默认克隆路径</sys:String> <sys:String x:Key="Text.Preference.Git.Dir">默认克隆路径</sys:String>
<sys:String x:Key="Text.Preference.Git.Dir.Placeholder">默认的仓库本地存放位置</sys:String> <sys:String x:Key="Text.Preference.Git.Dir.Placeholder">默认的仓库本地存放位置</sys:String>
<sys:String x:Key="Text.Preference.Git.User">用户名</sys:String> <sys:String x:Key="Text.Preference.Git.User">用户名</sys:String>

View file

@ -73,6 +73,7 @@
<RowDefinition Height="28"/> <RowDefinition Height="28"/>
<RowDefinition Height="28"/> <RowDefinition Height="28"/>
<RowDefinition Height="28"/> <RowDefinition Height="28"/>
<RowDefinition Height="28"/>
<RowDefinition Height="8"/> <RowDefinition Height="8"/>
<RowDefinition Height="36"/> <RowDefinition Height="36"/>
<RowDefinition Height="28"/> <RowDefinition Height="28"/>
@ -230,13 +231,24 @@
Icon="{StaticResource Icon.Folder.Open}"/> Icon="{StaticResource Icon.Folder.Open}"/>
</Grid> </Grid>
<!-- Default Clone Dir --> <!-- Git Version -->
<TextBlock <TextBlock
Grid.Row="13" Grid.Column="0" Grid.Row="13" Grid.Column="0"
Text="{DynamicResource Text.Preference.Git.Version}"
HorizontalAlignment="Right"
Margin="0,0,8,0"/>
<TextBlock
Grid.Row="13" Grid.Column="1"
x:Name="textGitVersion"
Text="{Binding ElementName=me, Path=Version}"/>
<!-- Default Clone Dir -->
<TextBlock
Grid.Row="14" Grid.Column="0"
Text="{DynamicResource Text.Preference.Git.Dir}" Text="{DynamicResource Text.Preference.Git.Dir}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<Grid Grid.Row="13" Grid.Column="1"> <Grid Grid.Row="14" Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
@ -259,12 +271,12 @@
<!-- User --> <!-- User -->
<TextBlock <TextBlock
Grid.Row="14" Grid.Column="0" Grid.Row="15" Grid.Column="0"
Text="{DynamicResource Text.Preference.Git.User}" Text="{DynamicResource Text.Preference.Git.User}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<controls:TextEdit <controls:TextEdit
Grid.Row="14" Grid.Column="1" Grid.Row="15" Grid.Column="1"
x:Name="editGitUser" x:Name="editGitUser"
Height="24" Height="24"
Text="{Binding ElementName=me, Path=User, Mode=TwoWay}" Text="{Binding ElementName=me, Path=User, Mode=TwoWay}"
@ -272,12 +284,12 @@
<!-- Email --> <!-- Email -->
<TextBlock <TextBlock
Grid.Row="15" Grid.Column="0" Grid.Row="16" Grid.Column="0"
Text="{DynamicResource Text.Preference.Git.Email}" Text="{DynamicResource Text.Preference.Git.Email}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<controls:TextEdit <controls:TextEdit
Grid.Row="15" Grid.Column="1" Grid.Row="16" Grid.Column="1"
x:Name="editGitEmail" x:Name="editGitEmail"
Height="24" Height="24"
Text="{Binding ElementName=me, Path=Email, Mode=TwoWay}" Text="{Binding ElementName=me, Path=Email, Mode=TwoWay}"
@ -285,12 +297,12 @@
<!-- CRLF --> <!-- CRLF -->
<TextBlock <TextBlock
Grid.Row="16" Grid.Column="0" Grid.Row="17" Grid.Column="0"
Text="{DynamicResource Text.Preference.Git.CRLF}" Text="{DynamicResource Text.Preference.Git.CRLF}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<ComboBox <ComboBox
Grid.Row="16" Grid.Column="1" Grid.Row="17" Grid.Column="1"
x:Name="editGitCrlf" x:Name="editGitCrlf"
Height="24" Height="24"
ItemsSource="{Binding Source={x:Static models:CRLFOption.Supported}}" ItemsSource="{Binding Source={x:Static models:CRLFOption.Supported}}"
@ -308,19 +320,19 @@
<!-- Merge Tool Group --> <!-- Merge Tool Group -->
<TextBlock <TextBlock
Grid.Row="18" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="19" Grid.Column="0" Grid.ColumnSpan="2"
Text="{DynamicResource Text.Preference.Merger}" Text="{DynamicResource Text.Preference.Merger}"
FontSize="17" FontWeight="DemiBold" FontSize="17" FontWeight="DemiBold"
Foreground="{DynamicResource Brush.FG2}"/> Foreground="{DynamicResource Brush.FG2}"/>
<!-- Merge Tool Type --> <!-- Merge Tool Type -->
<TextBlock <TextBlock
Grid.Row="19" Grid.Column="0" Grid.Row="20" Grid.Column="0"
Text="{DynamicResource Text.Preference.Merger.Type}" Text="{DynamicResource Text.Preference.Merger.Type}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<ComboBox <ComboBox
Grid.Row="19" Grid.Column="1" Grid.Row="20" Grid.Column="1"
Height="24" Height="24"
ItemsSource="{Binding Source={x:Static models:MergeTool.Supported}}" ItemsSource="{Binding Source={x:Static models:MergeTool.Supported}}"
DisplayMemberPath="Name" DisplayMemberPath="Name"
@ -330,11 +342,11 @@
<!-- Merge Tool Executable Path --> <!-- Merge Tool Executable Path -->
<TextBlock <TextBlock
Grid.Row="20" Grid.Column="0" Grid.Row="21" Grid.Column="0"
Text="{DynamicResource Text.Preference.Merger.Path}" Text="{DynamicResource Text.Preference.Merger.Path}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,8,0"/> Margin="0,0,8,0"/>
<Grid Grid.Row="20" Grid.Column="1"> <Grid Grid.Row="21" Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>

View file

@ -15,6 +15,7 @@ namespace SourceGit.Views {
public string User { get; set; } public string User { get; set; }
public string Email { get; set; } public string Email { get; set; }
public string CRLF { 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://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathfindonpathw
// https://www.pinvoke.net/default.aspx/shlwapi.PathFindOnPath // 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 bool EnableWindowsTerminal { get; set; } = PathFindOnPath(new StringBuilder("wt.exe"), null);
public Preference() { public Preference() {
if (!UpdateGitInfoIfReady()) { UpdateGitInfo(false);
User = "";
Email = "";
CRLF = "false";
}
if (!EnableWindowsTerminal) { if (!EnableWindowsTerminal) {
Models.Preference.Instance.General.UseWindowsTerminal = false; Models.Preference.Instance.General.UseWindowsTerminal = false;
@ -37,13 +34,27 @@ namespace SourceGit.Views {
InitializeComponent(); InitializeComponent();
} }
private bool UpdateGitInfoIfReady() { private bool UpdateGitInfo(bool updateUi) {
if (!Models.Preference.Instance.IsReady) return false; var isReady = Models.Preference.Instance.IsReady;
User = new Commands.Config().Get("user.name"); if (isReady) {
Email = new Commands.Config().Get("user.email"); User = new Commands.Config().Get("user.name");
CRLF = new Commands.Config().Get("core.autocrlf"); Email = new Commands.Config().Get("user.email");
if (string.IsNullOrEmpty(CRLF)) CRLF = "false"; CRLF = new Commands.Config().Get("core.autocrlf");
return true; 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 #region EVENTS
@ -69,11 +80,7 @@ namespace SourceGit.Views {
if (dialog.ShowDialog() == true) { if (dialog.ShowDialog() == true) {
Models.Preference.Instance.Git.Path = dialog.FileName; Models.Preference.Instance.Git.Path = dialog.FileName;
editGitPath?.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); editGitPath?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
if (UpdateGitInfoIfReady()) { UpdateGitInfo(true);
editGitUser?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
editGitEmail?.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
editGitCrlf?.GetBindingExpression(ComboBox.SelectedValueProperty).UpdateTarget();
}
} }
} }