-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathRenameBranch.cs
99 lines (85 loc) · 3.04 KB
/
RenameBranch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class RenameBranch : Popup
{
public Models.Branch Target
{
get;
}
[Required(ErrorMessage = "Branch name is required!!!")]
[RegularExpression(@"^[\w \-/\.#\+]+$", ErrorMessage = "Bad branch name format!")]
[CustomValidation(typeof(RenameBranch), nameof(ValidateBranchName))]
public string Name
{
get => _name;
set => SetProperty(ref _name, value, true);
}
public RenameBranch(Repository repo, Models.Branch target)
{
_repo = repo;
_name = target.Name;
Target = target;
}
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
{
if (ctx.ObjectInstance is RenameBranch rename)
{
var fixedName = rename.FixName(name);
foreach (var b in rename._repo.Branches)
{
if (b.IsLocal && b != rename.Target && b.Name == fixedName)
{
return new ValidationResult("A branch with same name already exists!!!");
}
}
}
return ValidationResult.Success;
}
public override Task<bool> Sure()
{
var fixedName = FixName(_name);
if (fixedName == Target.Name)
return null;
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Rename '{Target.Name}'";
var log = _repo.CreateLog($"Rename Branch '{Target.Name}'");
Use(log);
return Task.Run(() =>
{
var oldName = Target.FullName;
var succ = Commands.Branch.Rename(_repo.FullPath, Target.Name, fixedName, log);
log.Complete();
CallUIThread(() =>
{
if (succ)
{
foreach (var filter in _repo.Settings.HistoriesFilters)
{
if (filter.Type == Models.FilterType.LocalBranch &&
filter.Pattern.Equals(oldName, StringComparison.Ordinal))
{
filter.Pattern = $"refs/heads/{fixedName}";
break;
}
}
}
_repo.MarkBranchesDirtyManually();
_repo.SetWatcherEnabled(true);
});
return succ;
});
}
private string FixName(string name)
{
if (!name.Contains(' '))
return name;
var parts = name.Split(' ', StringSplitOptions.RemoveEmptyEntries);
return string.Join("-", parts);
}
private readonly Repository _repo;
private string _name;
}
}