2024-02-05 23:08:37 -08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
if (SetProperty(ref _url, value, true)) UseSSH = Models.Remote.IsSSH(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
if (remote != edit._remote && name == remote.Name) new ValidationResult("A remote with given name already exists!!!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
if (!Models.Remote.IsValidURL(url)) return new ValidationResult("Bad remote URL format!!!");
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var remote in edit._repo.Remotes)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
if (remote != edit._remote && url == remote.URL) new ValidationResult("A remote with the same url already exists!!!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
if (succ) _remote.Name = _name;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_remote.URL != _url)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var succ = new Commands.Remote(_repo.FullPath).SetURL(_name, _url);
|
|
|
|
|
if (succ) _remote.URL = _url;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_useSSH)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Post processing ...");
|
|
|
|
|
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", SSHKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
private string _name = string.Empty;
|
|
|
|
|
private string _url = string.Empty;
|
|
|
|
|
private bool _useSSH = false;
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|