2024-05-07 00:49:57 -07:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class Clone : Popup
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
[Required(ErrorMessage = "Remote URL is required")]
|
|
|
|
|
[CustomValidation(typeof(Clone), nameof(ValidateRemote))]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Remote
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _remote;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (SetProperty(ref _remote, value, true))
|
|
|
|
|
UseSSH = Models.Remote.IsSSH(value);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool UseSSH
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _useSSH;
|
|
|
|
|
set => SetProperty(ref _useSSH, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string SSHKey
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _sshKey;
|
|
|
|
|
set => SetProperty(ref _sshKey, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Parent folder is required")]
|
|
|
|
|
[CustomValidation(typeof(Clone), nameof(ValidateParentFolder))]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string ParentFolder
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _parentFolder;
|
|
|
|
|
set => SetProperty(ref _parentFolder, value, true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Local
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _local;
|
|
|
|
|
set => SetProperty(ref _local, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string ExtraArgs
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _extraArgs;
|
|
|
|
|
set => SetProperty(ref _extraArgs, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:13:59 -07:00
|
|
|
|
public Clone(Launcher launcher)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-21 19:05:20 -08:00
|
|
|
|
_launcher = launcher;
|
2024-05-30 00:13:59 -07:00
|
|
|
|
_page = launcher.ActivePage;
|
2024-02-21 19:05:20 -08:00
|
|
|
|
|
|
|
|
|
View = new Views.Clone() { DataContext = this };
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateRemote(string remote, ValidationContext _)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!Models.Remote.IsValidURL(remote))
|
|
|
|
|
return new ValidationResult("Invalid remote repository URL format");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateParentFolder(string folder, ValidationContext _)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!Directory.Exists(folder))
|
|
|
|
|
return new ValidationResult("Given path can NOT be found");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-02-25 19:29:57 -08:00
|
|
|
|
ProgressDescription = "Clone ...";
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var cmd = new Commands.Clone(HostPageId, _parentFolder, _remote, _local, _useSSH ? _sshKey : "", _extraArgs, SetProgressDescription);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!cmd.Exec())
|
|
|
|
|
return false;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
var path = _parentFolder;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_local))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
path = Path.GetFullPath(Path.Combine(path, _local));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var name = Path.GetFileName(_remote);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (name.EndsWith(".git"))
|
|
|
|
|
name = name.Substring(0, name.Length - 4);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
path = Path.GetFullPath(Path.Combine(path, name));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
CallUIThread(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
App.RaiseException(HostPageId, $"Folder '{path}' can NOT be found");
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_useSSH && !string.IsNullOrEmpty(_sshKey))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var config = new Commands.Config(path);
|
|
|
|
|
config.Set("remote.origin.sshkey", _sshKey);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
CallUIThread(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var repo = Preference.AddRepository(path, Path.Combine(path, ".git"));
|
2024-05-20 02:36:43 -07:00
|
|
|
|
var node = Preference.FindOrAddNodeByRepositoryPath(repo.FullPath, null, true);
|
2024-02-21 19:05:20 -08:00
|
|
|
|
_launcher.OpenRepositoryInTab(node, _page);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Launcher _launcher = null;
|
|
|
|
|
private readonly LauncherPage _page = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private string _remote = string.Empty;
|
|
|
|
|
private bool _useSSH = false;
|
|
|
|
|
private string _sshKey = string.Empty;
|
|
|
|
|
private string _parentFolder = Preference.Instance.GitDefaultCloneDir;
|
|
|
|
|
private string _local = string.Empty;
|
|
|
|
|
private string _extraArgs = string.Empty;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|