2024-02-05 23:08:37 -08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-05-20 20:52:30 -07:00
|
|
|
|
using System.IO;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class EditRemote : Popup
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
[Required(ErrorMessage = "Remote name is required!!!")]
|
|
|
|
|
[RegularExpression(@"^[\w\-\.]+$", ErrorMessage = "Bad remote name format!!!")]
|
|
|
|
|
[CustomValidation(typeof(EditRemote), nameof(ValidateRemoteName))]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _name;
|
|
|
|
|
set => SetProperty(ref _name, value, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Remote URL is required!!!")]
|
|
|
|
|
[CustomValidation(typeof(EditRemote), nameof(ValidateRemoteURL))]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Url
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _url;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (SetProperty(ref _url, 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;
|
2024-05-20 20:52:30 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _useSSH, value))
|
|
|
|
|
ValidateProperty(_sshkey, nameof(SSHKey));
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 20:52:30 -07:00
|
|
|
|
[CustomValidation(typeof(EditRemote), nameof(ValidateSSHKey))]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string SSHKey
|
|
|
|
|
{
|
2024-05-20 20:52:30 -07:00
|
|
|
|
get => _sshkey;
|
|
|
|
|
set => SetProperty(ref _sshkey, value, true);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public EditRemote(Repository repo, Models.Remote remote)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
_remote = remote;
|
|
|
|
|
_name = remote.Name;
|
|
|
|
|
_url = remote.URL;
|
|
|
|
|
_useSSH = Models.Remote.IsSSH(remote.URL);
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_useSSH)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SSHKey = new Commands.Config(repo.FullPath).Get($"remote.{remote.Name}.sshkey");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
View = new Views.EditRemote() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateRemoteName(string name, ValidationContext ctx)
|
|
|
|
|
{
|
|
|
|
|
if (ctx.ObjectInstance is EditRemote edit)
|
|
|
|
|
{
|
|
|
|
|
foreach (var remote in edit._repo.Remotes)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (remote != edit._remote && name == remote.Name)
|
2024-07-14 09:30:31 -07:00
|
|
|
|
return new ValidationResult("A remote with given name already exists!!!");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateRemoteURL(string url, ValidationContext ctx)
|
|
|
|
|
{
|
|
|
|
|
if (ctx.ObjectInstance is EditRemote edit)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!Models.Remote.IsValidURL(url))
|
|
|
|
|
return new ValidationResult("Bad remote URL format!!!");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var remote in edit._repo.Remotes)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (remote != edit._remote && url == remote.URL)
|
2024-07-14 09:30:31 -07:00
|
|
|
|
return new ValidationResult("A remote with the same url already exists!!!");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 20:52:30 -07:00
|
|
|
|
public static ValidationResult ValidateSSHKey(string sshkey, ValidationContext ctx)
|
|
|
|
|
{
|
2024-08-12 04:28:01 -07:00
|
|
|
|
if (ctx.ObjectInstance is EditRemote { _useSSH: true } && !string.IsNullOrEmpty(sshkey))
|
2024-05-20 20:52:30 -07:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(sshkey))
|
|
|
|
|
return new ValidationResult("Given SSH private key can NOT be found!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo.SetWatcherEnabled(false);
|
2024-02-25 19:29:57 -08:00
|
|
|
|
ProgressDescription = $"Editing remote '{_remote.Name}' ...";
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
if (_remote.Name != _name)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var succ = new Commands.Remote(_repo.FullPath).Rename(_remote.Name, _name);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (succ)
|
|
|
|
|
_remote.Name = _name;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_remote.URL != _url)
|
|
|
|
|
{
|
2024-10-10 19:03:42 -07:00
|
|
|
|
var succ = new Commands.Remote(_repo.FullPath).SetURL(_name, _url, false);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (succ)
|
|
|
|
|
_remote.URL = _url;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 19:03:42 -07:00
|
|
|
|
var pushURL = new Commands.Remote(_repo.FullPath).GetURL(_name, true);
|
|
|
|
|
if (pushURL != _url)
|
|
|
|
|
new Commands.Remote(_repo.FullPath).SetURL(_name, _url, true);
|
|
|
|
|
|
2024-05-20 20:52:30 -07:00
|
|
|
|
SetProgressDescription("Post processing ...");
|
|
|
|
|
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Repository _repo = null;
|
|
|
|
|
private readonly Models.Remote _remote = null;
|
2024-07-14 09:30:31 -07:00
|
|
|
|
private string _name = null;
|
|
|
|
|
private string _url = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private bool _useSSH = false;
|
2024-05-20 20:52:30 -07:00
|
|
|
|
private string _sshkey = string.Empty;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|