From 697879b6a504df4e5799910a71681b4235276182 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 23 Aug 2023 20:48:29 +0800 Subject: [PATCH] feature: supports for providing user on the HTTP/HTTPS git URL --- src/Views/Clone.xaml.cs | 21 ++++++--------------- src/Views/Popups/Remote.xaml.cs | 6 +++--- src/Views/Validations/GitURL.cs | 12 +++++++++++- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/Views/Clone.xaml.cs b/src/Views/Clone.xaml.cs index bac20bfa..4161fbf9 100644 --- a/src/Views/Clone.xaml.cs +++ b/src/Views/Clone.xaml.cs @@ -1,8 +1,6 @@ using Microsoft.Win32; -using SourceGit.Views.Validations; using System; using System.IO; -using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -13,11 +11,6 @@ namespace SourceGit.Views { /// 克隆 /// public partial class Clone : Controls.Window { - private static readonly Regex[] SSH_PROTOCOAL = new Regex[] { - new Regex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-]+/[\w\-]+\.git$"), - new Regex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-]+\.git$"), - }; - public string Uri { get; set; } public string Folder { get; set; } public string LocalName { get; set; } @@ -118,14 +111,12 @@ namespace SourceGit.Views { } private void OnUrlChanged(object sender, TextChangedEventArgs e) { - foreach (var check in SSH_PROTOCOAL) { - if (check.IsMatch(txtUrl.Text)) { - rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel); - return; - } - } - - rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel); + var isSSHProtocal = Validations.GitURL.IsSSH(txtUrl.Text); + if (isSSHProtocal) { + rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel); + } else { + rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel); + } } private void OnCloseException(object s, RoutedEventArgs e) { diff --git a/src/Views/Popups/Remote.xaml.cs b/src/Views/Popups/Remote.xaml.cs index 24a2762d..6054d2a2 100644 --- a/src/Views/Popups/Remote.xaml.cs +++ b/src/Views/Popups/Remote.xaml.cs @@ -28,7 +28,7 @@ namespace SourceGit.Views.Popups { InitializeComponent(); ruleName.Repo = repo; - if (!string.IsNullOrEmpty(RemoteURL) && RemoteURL.StartsWith("git@")) { + if (Validations.GitURL.IsSSH(RemoteURL)) { txtSSHKey.Text = new Commands.Config(repo.Path).Get($"remote.{remote.Name}.sshkey"); } else { txtSSHKey.Text = ""; @@ -94,8 +94,8 @@ namespace SourceGit.Views.Popups { } private void OnUrlChanged(object sender, TextChangedEventArgs e) { - if (!string.IsNullOrEmpty(txtUrl.Text)) { - rowSSHKey.Height = new GridLength(txtUrl.Text.StartsWith("git@") ? 32 : 0, GridUnitType.Pixel); + if (Validations.GitURL.IsSSH(txtUrl.Text)) { + rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel); } else { rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel); } diff --git a/src/Views/Validations/GitURL.cs b/src/Views/Validations/GitURL.cs index b726265b..ae34da1e 100644 --- a/src/Views/Validations/GitURL.cs +++ b/src/Views/Validations/GitURL.cs @@ -6,11 +6,21 @@ namespace SourceGit.Views.Validations { public class GitURL : ValidationRule { private static readonly Regex[] VALID_FORMATS = new Regex[] { - new Regex(@"^http[s]?://[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-]+\.git$"), + new Regex(@"^http[s]?://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-]+\.git$"), new Regex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-]+/[\w\-]+\.git$"), new Regex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-]+\.git$"), }; + public static bool IsSSH(string url) { + if (string.IsNullOrEmpty(url)) return false; + + for (int i = 1; i < VALID_FORMATS.Length; i++) { + if (VALID_FORMATS[i].IsMatch(url)) return true; + } + + return false; + } + public override ValidationResult Validate(object value, CultureInfo cultureInfo) { string url = value as string; if (!string.IsNullOrEmpty(url)) {