-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathRenameBranch.cs
More file actions
72 lines (59 loc) · 2.11 KB
/
RenameBranch.cs
File metadata and controls
72 lines (59 loc) · 2.11 KB
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
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)
{
foreach (var b in rename._repo.Branches)
{
if (b.IsLocal && b != rename.Target && b.Name.Equals(name, StringComparison.Ordinal))
return new ValidationResult("A branch with same name already exists!!!");
}
}
return ValidationResult.Success;
}
public override async Task<bool> Sure()
{
if (Target.Name.Equals(_name, StringComparison.Ordinal))
return true;
using var lockWatcher = _repo.LockWatcher();
ProgressDescription = $"Rename '{Target.Name}'";
var log = _repo.CreateLog($"Rename Branch '{Target.Name}'");
Use(log);
var isCurrent = Target.IsCurrent;
var oldName = Target.FullName;
var succ = await new Commands.Branch(_repo.FullPath, Target.Name)
.Use(log)
.RenameAsync(_name);
if (succ)
_repo.RefreshAfterRenameBranch(Target, _name);
log.Complete();
return succ;
}
private readonly Repository _repo;
private string _name;
}
}