2024-11-13 05:45:28 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
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 RenameBranch : Popup
|
|
|
|
|
{
|
|
|
|
|
public Models.Branch Target
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Branch name is required!!!")]
|
2024-07-21 02:43:23 -07:00
|
|
|
|
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
|
2024-02-05 23:08:37 -08:00
|
|
|
|
[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)
|
|
|
|
|
{
|
2024-04-25 23:25:14 -07:00
|
|
|
|
if (b.IsLocal && b != rename.Target && b.Name == name)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
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-10-02 18:25:56 -07:00
|
|
|
|
var oldName = Target.FullName;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var succ = Commands.Branch.Rename(_repo.FullPath, Target.Name, _name);
|
2024-10-02 18:25:56 -07:00
|
|
|
|
CallUIThread(() =>
|
|
|
|
|
{
|
2024-11-13 05:45:28 -08:00
|
|
|
|
if (succ)
|
2024-10-02 18:25:56 -07:00
|
|
|
|
{
|
2024-11-13 05:45:28 -08:00
|
|
|
|
foreach (var filter in _repo.Settings.HistoriesFilters)
|
|
|
|
|
{
|
|
|
|
|
if (filter.Type == Models.FilterType.LocalBranch &&
|
|
|
|
|
filter.Pattern.Equals(oldName, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
filter.Pattern = $"refs/heads/{_name}";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-02 18:25:56 -07:00
|
|
|
|
}
|
2024-10-14 18:39:01 -07:00
|
|
|
|
|
2024-10-02 18:25:56 -07:00
|
|
|
|
_repo.MarkBranchesDirtyManually();
|
|
|
|
|
_repo.SetWatcherEnabled(true);
|
|
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return succ;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 09:30:31 -07:00
|
|
|
|
private readonly Repository _repo;
|
|
|
|
|
private string _name;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|