feature<GitURL>: supports for providing user on the HTTP/HTTPS git URL

This commit is contained in:
leo 2023-08-23 20:48:29 +08:00
parent 1c10d9a286
commit 697879b6a5
3 changed files with 20 additions and 19 deletions

View file

@ -1,8 +1,6 @@
using Microsoft.Win32; using Microsoft.Win32;
using SourceGit.Views.Validations;
using System; using System;
using System.IO; using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -13,11 +11,6 @@ namespace SourceGit.Views {
/// 克隆 /// 克隆
/// </summary> /// </summary>
public partial class Clone : Controls.Window { 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 Uri { get; set; }
public string Folder { get; set; } public string Folder { get; set; }
public string LocalName { get; set; } public string LocalName { get; set; }
@ -118,15 +111,13 @@ namespace SourceGit.Views {
} }
private void OnUrlChanged(object sender, TextChangedEventArgs e) { private void OnUrlChanged(object sender, TextChangedEventArgs e) {
foreach (var check in SSH_PROTOCOAL) { var isSSHProtocal = Validations.GitURL.IsSSH(txtUrl.Text);
if (check.IsMatch(txtUrl.Text)) { if (isSSHProtocal) {
rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel); rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel);
return; } else {
}
}
rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel); rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel);
} }
}
private void OnCloseException(object s, RoutedEventArgs e) { private void OnCloseException(object s, RoutedEventArgs e) {
exception.Visibility = Visibility.Collapsed; exception.Visibility = Visibility.Collapsed;

View file

@ -28,7 +28,7 @@ namespace SourceGit.Views.Popups {
InitializeComponent(); InitializeComponent();
ruleName.Repo = repo; 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"); txtSSHKey.Text = new Commands.Config(repo.Path).Get($"remote.{remote.Name}.sshkey");
} else { } else {
txtSSHKey.Text = ""; txtSSHKey.Text = "";
@ -94,8 +94,8 @@ namespace SourceGit.Views.Popups {
} }
private void OnUrlChanged(object sender, TextChangedEventArgs e) { private void OnUrlChanged(object sender, TextChangedEventArgs e) {
if (!string.IsNullOrEmpty(txtUrl.Text)) { if (Validations.GitURL.IsSSH(txtUrl.Text)) {
rowSSHKey.Height = new GridLength(txtUrl.Text.StartsWith("git@") ? 32 : 0, GridUnitType.Pixel); rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel);
} else { } else {
rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel); rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel);
} }

View file

@ -6,11 +6,21 @@ namespace SourceGit.Views.Validations {
public class GitURL : ValidationRule { public class GitURL : ValidationRule {
private static readonly Regex[] VALID_FORMATS = new Regex[] { 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(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-]+/[\w\-]+\.git$"),
new Regex(@"^ssh://([\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) { public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
string url = value as string; string url = value as string;
if (!string.IsNullOrEmpty(url)) { if (!string.IsNullOrEmpty(url)) {