Use idea from PUMA to configure global user name and email

This commit is contained in:
leo 2020-07-04 17:18:15 +08:00
parent 38227b1d57
commit d0f6077dc9
3 changed files with 90 additions and 9 deletions

View file

@ -2,6 +2,8 @@
开源的Git客户端仅用于Windows 10。单文件无需安装< 500KB
## 预览
* DarkTheme
![Preview_Dark](./Preview_Dark.png)
@ -10,3 +12,7 @@
![Preview_Light](./Preview_Light.png)
## Thanks
* [PUMA](https://gitee.com/whgfu) 配置默认User

View file

@ -8,7 +8,7 @@
xmlns:app="clr-namespace:SourceGit"
xmlns:git="clr-namespace:SourceGit.Git"
mc:Ignorable="d"
Height="362" Width="500">
Height="472" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="36"/>
@ -21,6 +21,10 @@
<RowDefinition Height="36"/>
<RowDefinition Height="28"/>
<RowDefinition Height="28"/>
<RowDefinition Height="18"/>
<RowDefinition Height="36"/>
<RowDefinition Height="28"/>
<RowDefinition Height="28"/>
<RowDefinition Height="28"/>
<RowDefinition Height="18"/>
<RowDefinition Height="32"/>
@ -76,10 +80,17 @@
</Button>
</Grid>
<!-- Global User -->
<Label Grid.Row="7" Grid.ColumnSpan="2" Content="Global User" FontSize="18"/>
<Label Grid.Row="8" Grid.Column="0" Content="Name :" HorizontalAlignment="Right"/>
<TextBox Grid.Row="8" Grid.Column="1" Height="24" helpers:TextBoxHelper.Placeholder="Global git user name" Text="{Binding ElementName=me, Path=GlobalUser, Mode=TwoWay}"/>
<Label Grid.Row="9" Grid.Column="0" Content="Email :" HorizontalAlignment="Right"/>
<TextBox Grid.Row="9" Grid.Column="1" Height="24" helpers:TextBoxHelper.Placeholder="Global git user email" Text="{Binding ElementName=me, Path=GlobalUserEmail, Mode=TwoWay}"/>
<!-- 合并工具配置 -->
<Label Grid.Row="7" Grid.ColumnSpan="2" Content="Merge Tool" FontSize="18"/>
<Label Grid.Row="8" Grid.Column="0" Content="Choose Merger :" HorizontalAlignment="Right"/>
<ComboBox Grid.Row="8" Grid.Column="1"
<Label Grid.Row="11" Grid.ColumnSpan="2" Content="Merge Tool" FontSize="18"/>
<Label Grid.Row="12" Grid.Column="0" Content="Choose Merger :" HorizontalAlignment="Right"/>
<ComboBox Grid.Row="12" Grid.Column="1"
Height="24"
Padding="2,0,0,0"
HorizontalContentAlignment="Left"
@ -88,8 +99,8 @@
ItemsSource="{Binding Source={x:Static git:MergeTool.Supported}}"
DisplayMemberPath="Name"
SelectionChanged="ChangeMergeTool"/>
<Label Grid.Row="9" Grid.Column="0" Content="Install Path :" HorizontalAlignment="Right"/>
<Grid Grid.Row="9" Grid.Column="1">
<Label Grid.Row="13" Grid.Column="0" Content="Install Path :" HorizontalAlignment="Right"/>
<Grid Grid.Row="13" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="28"/>
@ -104,13 +115,13 @@
<Path Width="14" Style="{StaticResource Style.Icon}" Data="{StaticResource Icon.Folder}"/>
</Button>
</Grid>
<Label Grid.Row="10" Grid.Column="0" Content="Command :" HorizontalAlignment="Right"/>
<TextBlock Grid.Row="10" Grid.Column="1"
<Label Grid.Row="14" Grid.Column="0" Content="Command :" HorizontalAlignment="Right"/>
<TextBlock Grid.Row="14" Grid.Column="1"
x:Name="txtMergeParam"
VerticalAlignment="Center"
Foreground="{StaticResource Brush.FG2}"/>
<Button Grid.Row="12" Grid.Column="1"
<Button Grid.Row="16" Grid.Column="1"
Content="CLOSE"
Click="Close"
Width="80"

View file

@ -1,5 +1,7 @@
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
@ -10,10 +12,29 @@ namespace SourceGit.UI {
/// </summary>
public partial class Preference : UserControl {
/// <summary>
/// Git global user name.
/// </summary>
public string GlobalUser {
get;
set;
}
/// <summary>
/// Git global user email.
/// </summary>
public string GlobalUserEmail {
get;
set;
}
/// <summary>
/// Constructor.
/// </summary>
public Preference() {
GlobalUser = GetConfig("user.name");
GlobalUserEmail = GetConfig("user.email");
InitializeComponent();
int mergeType = App.Preference.MergeTool;
@ -33,6 +54,12 @@ namespace SourceGit.UI {
/// Close this dialog
/// </summary>
private void Close(object sender, RoutedEventArgs e) {
var oldUser = GetConfig("user.name");
if (oldUser != GlobalUser) SetConfig("user.name", GlobalUser);
var oldEmail = GetConfig("user.email");
if (oldEmail != GlobalUserEmail) SetConfig("user.email", GlobalUserEmail);
PopupManager.Close();
}
@ -110,5 +137,42 @@ namespace SourceGit.UI {
App.Preference.MergeExecutable = dialog.FileName;
}
}
#region CONFIG
private string GetConfig(string key) {
if (!App.IsGitConfigured) return "";
var startInfo = new ProcessStartInfo();
startInfo.FileName = App.Preference.GitExecutable;
startInfo.Arguments = $"config --global {key}";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.StandardOutputEncoding = Encoding.UTF8;
var proc = new Process() { StartInfo = startInfo };
proc.Start();
var output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
proc.Close();
return output.Trim();
}
private void SetConfig(string key, string val) {
if (!App.IsGitConfigured) return;
var startInfo = new ProcessStartInfo();
startInfo.FileName = App.Preference.GitExecutable;
startInfo.Arguments = $"config --global {key} \"{val}\"";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
var proc = new Process() { StartInfo = startInfo };
proc.Start();
proc.WaitForExit();
proc.Close();
}
#endregion
}
}