2022-10-19 00:20:58 -07:00
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using SourceGit.Views.Validations;
|
2021-10-12 02:14:48 -07:00
|
|
|
|
using System;
|
2021-08-04 04:11:22 -07:00
|
|
|
|
using System.IO;
|
2023-06-01 19:29:43 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2021-10-12 02:14:48 -07:00
|
|
|
|
using System.Windows;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
2022-10-19 00:20:58 -07:00
|
|
|
|
namespace SourceGit.Views {
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 克隆
|
|
|
|
|
/// </summary>
|
2022-10-19 00:20:58 -07:00
|
|
|
|
public partial class Clone : Controls.Window {
|
2023-06-01 19:29:43 -07:00
|
|
|
|
private static readonly Regex[] SSH_PROTOCOAL = new Regex[] {
|
2023-08-23 01:45:45 -07:00
|
|
|
|
new Regex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-]+/[\w\-]+\.git$"),
|
2023-06-01 19:29:43 -07:00
|
|
|
|
new Regex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-]+/[\w\-]+\.git$"),
|
|
|
|
|
};
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
public string Uri { get; set; }
|
|
|
|
|
public string Folder { get; set; }
|
|
|
|
|
public string LocalName { get; set; }
|
|
|
|
|
public string RemoteName { get; set; }
|
|
|
|
|
public string ExtraArgs { get; set; }
|
|
|
|
|
|
|
|
|
|
public Clone() {
|
|
|
|
|
Folder = Models.Preference.Instance.Git.DefaultCloneDir;
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
ruleRemote.IsOptional = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 00:20:58 -07:00
|
|
|
|
#region EVENTS
|
|
|
|
|
private void OnQuit(object sender, RoutedEventArgs e) {
|
|
|
|
|
Close();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 00:20:58 -07:00
|
|
|
|
private async void OnStart(object s, RoutedEventArgs e) {
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var checks = new Controls.TextEdit[] { txtUrl, txtFolder, txtLocal, txtRemote };
|
|
|
|
|
foreach (var edit in checks) {
|
|
|
|
|
edit.GetBindingExpression(TextBox.TextProperty).UpdateSource();
|
2022-10-19 00:20:58 -07:00
|
|
|
|
if (Validation.GetHasError(edit)) return;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 00:20:58 -07:00
|
|
|
|
progress.Visibility = Visibility.Visible;
|
|
|
|
|
processing.IsAnimating = true;
|
2021-10-12 02:14:48 -07:00
|
|
|
|
|
2022-10-19 00:20:58 -07:00
|
|
|
|
var sshKey = txtSSHKey.Text;
|
|
|
|
|
var succ = await Task.Run(() => {
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var extras = string.IsNullOrEmpty(ExtraArgs) ? "" : ExtraArgs;
|
|
|
|
|
if (!string.IsNullOrEmpty(RemoteName)) extras += $" --origin {RemoteName}";
|
|
|
|
|
|
2022-10-25 19:32:42 -07:00
|
|
|
|
var cloneRs = new Commands.Clone(Folder, Uri, LocalName, sshKey, extras, msg => {
|
2022-10-19 00:20:58 -07:00
|
|
|
|
Dispatcher.Invoke(() => txtProgress.Text = msg);
|
|
|
|
|
}, err => {
|
|
|
|
|
Dispatcher.Invoke(() => txtError.Text = err);
|
|
|
|
|
}).Exec();
|
|
|
|
|
|
2022-10-25 19:32:42 -07:00
|
|
|
|
if (!cloneRs) return false;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
var path = Folder;
|
|
|
|
|
if (!string.IsNullOrEmpty(LocalName)) {
|
2021-08-04 04:11:22 -07:00
|
|
|
|
path = Path.GetFullPath(Path.Combine(path, LocalName));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
} else {
|
2021-08-04 04:11:22 -07:00
|
|
|
|
var name = Path.GetFileName(Uri);
|
|
|
|
|
if (name.EndsWith(".git")) name = name.Substring(0, name.Length - 4);
|
|
|
|
|
path = Path.GetFullPath(Path.Combine(path, name));
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 04:11:22 -07:00
|
|
|
|
if (!Directory.Exists(path)) {
|
|
|
|
|
Models.Exception.Raise($"Folder {path} not found!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 02:14:48 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(sshKey)) {
|
|
|
|
|
var config = new Commands.Config(path);
|
|
|
|
|
var remote = "origin";
|
|
|
|
|
if (!string.IsNullOrEmpty(RemoteName)) remote = RemoteName;
|
|
|
|
|
config.Set($"remote.{remote}.sshkey", sshKey);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 04:11:22 -07:00
|
|
|
|
var gitDir = new Commands.QueryGitDir(path).Result();
|
2022-10-14 05:38:53 -07:00
|
|
|
|
var repo = Models.Preference.Instance.AddRepository(path, gitDir);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
if (repo != null) Dispatcher.Invoke(() => Models.Watcher.Open(repo));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2022-10-19 00:20:58 -07:00
|
|
|
|
|
|
|
|
|
progress.Visibility = Visibility.Collapsed;
|
|
|
|
|
processing.IsAnimating = false;
|
|
|
|
|
if (succ) {
|
|
|
|
|
Close();
|
|
|
|
|
} else {
|
|
|
|
|
exception.Visibility = Visibility.Visible;
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 02:14:48 -07:00
|
|
|
|
private void OnFolderSelectorClick(object sender, RoutedEventArgs e) {
|
2023-08-17 22:28:55 -07:00
|
|
|
|
var dialog = new System.Windows.Forms.FolderBrowserDialog();
|
|
|
|
|
dialog.ShowNewFolderButton = true;
|
|
|
|
|
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
|
2021-05-25 20:08:31 -07:00
|
|
|
|
Folder = dialog.SelectedPath;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
txtFolder.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
|
2021-05-25 20:08:31 -07:00
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2021-10-12 02:14:48 -07:00
|
|
|
|
|
|
|
|
|
private void OnSelectSSHKey(object sender, RoutedEventArgs e) {
|
|
|
|
|
var initPath = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "..", ".ssh"));
|
|
|
|
|
if (!Directory.Exists(initPath)) Directory.CreateDirectory(initPath);
|
|
|
|
|
|
|
|
|
|
var dialog = new OpenFileDialog();
|
|
|
|
|
dialog.Filter = $"SSH Private Key|*";
|
|
|
|
|
dialog.Title = App.Text("SSHKey");
|
|
|
|
|
dialog.InitialDirectory = initPath;
|
|
|
|
|
dialog.CheckFileExists = true;
|
|
|
|
|
dialog.Multiselect = false;
|
|
|
|
|
|
|
|
|
|
if (dialog.ShowDialog() == true) txtSSHKey.Text = dialog.FileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUrlChanged(object sender, TextChangedEventArgs e) {
|
2023-06-01 19:29:43 -07:00
|
|
|
|
foreach (var check in SSH_PROTOCOAL) {
|
|
|
|
|
if (check.IsMatch(txtUrl.Text)) {
|
|
|
|
|
rowSSHKey.Height = new GridLength(32, GridUnitType.Pixel);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-12 02:14:48 -07:00
|
|
|
|
}
|
2023-06-01 19:29:43 -07:00
|
|
|
|
|
|
|
|
|
rowSSHKey.Height = new GridLength(0, GridUnitType.Pixel);
|
2021-10-12 02:14:48 -07:00
|
|
|
|
}
|
2022-10-19 00:20:58 -07:00
|
|
|
|
|
|
|
|
|
private void OnCloseException(object s, RoutedEventArgs e) {
|
|
|
|
|
exception.Visibility = Visibility.Collapsed;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|