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 AddRemote : Popup
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
[Required(ErrorMessage = "Remote name is required!!!")]
|
|
|
|
|
[RegularExpression(@"^[\w\-\.]+$", ErrorMessage = "Bad remote name format!!!")]
|
|
|
|
|
[CustomValidation(typeof(AddRemote), 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(AddRemote), 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(AddRemote), 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 AddRemote(Repository repo)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
View = new Views.AddRemote() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateRemoteName(string name, ValidationContext ctx)
|
|
|
|
|
{
|
|
|
|
|
if (ctx.ObjectInstance is AddRemote add)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var exists = add._repo.Remotes.Find(x => x.Name == name);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (exists != null)
|
|
|
|
|
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 AddRemote add)
|
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
var exists = add._repo.Remotes.Find(x => x.URL == url);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (exists != null)
|
|
|
|
|
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-06-05 02:32:19 -07:00
|
|
|
|
if (ctx.ObjectInstance is AddRemote { _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 = "Adding remote ...";
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var succ = new Commands.Remote(_repo.FullPath).Add(_name, _url);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (succ)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Fetching from added remote ...");
|
2024-05-20 20:52:30 -07:00
|
|
|
|
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null);
|
2024-06-05 02:32:19 -07:00
|
|
|
|
new Commands.Fetch(_repo.FullPath, _name, true, SetProgressDescription).Exec();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
CallUIThread(() =>
|
|
|
|
|
{
|
2024-03-07 17:57:29 -08:00
|
|
|
|
_repo.MarkBranchesDirtyManually();
|
|
|
|
|
_repo.SetWatcherEnabled(true);
|
|
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return succ;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Repository _repo = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private string _name = string.Empty;
|
|
|
|
|
private string _url = string.Empty;
|
|
|
|
|
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
|
|
|
|
}
|