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 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-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 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);
|
|
|
|
|
if (exists != null) return 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 AddRemote add)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
if (!Models.Remote.IsValidURL(url)) return new ValidationResult("Bad remote URL format!!!");
|
|
|
|
|
|
|
|
|
|
var exists = add._repo.Remotes.Find(x => x.URL == url);
|
|
|
|
|
if (exists != null) return 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 = "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 ...");
|
|
|
|
|
new Commands.Fetch(_repo.FullPath, _name, true, SetProgressDescription).Exec();
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
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-03-17 18:37:06 -07:00
|
|
|
|
}
|