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 RenameBranch : Popup
|
|
|
|
|
{
|
|
|
|
|
public Models.Branch Target
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Branch name is required!!!")]
|
|
|
|
|
[RegularExpression(@"^[\w\-/\.]+$", ErrorMessage = "Bad branch name format!")]
|
|
|
|
|
[CustomValidation(typeof(RenameBranch), nameof(ValidateBranchName))]
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public RenameBranch(Repository repo, Models.Branch target)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
_name = target.Name;
|
|
|
|
|
Target = target;
|
|
|
|
|
View = new Views.RenameBranch() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
|
|
|
|
|
{
|
|
|
|
|
if (ctx.ObjectInstance is RenameBranch rename)
|
|
|
|
|
{
|
|
|
|
|
foreach (var b in rename._repo.Branches)
|
|
|
|
|
{
|
|
|
|
|
if (b != rename.Target && b.Name == name)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return new ValidationResult("A branch with same name already exists!!!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_name == Target.Name)
|
|
|
|
|
return null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
_repo.SetWatcherEnabled(false);
|
2024-02-25 19:29:57 -08:00
|
|
|
|
ProgressDescription = $"Rename '{Target.Name}'";
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var succ = Commands.Branch.Rename(_repo.FullPath, Target.Name, _name);
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|