forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditRemote.cs
More file actions
142 lines (122 loc) · 4.63 KB
/
Copy pathEditRemote.cs
File metadata and controls
142 lines (122 loc) · 4.63 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class EditRemote : Popup
{
[Required(ErrorMessage = "Remote name is required!!!")]
[RegularExpression(@"^[\w\-\.]+$", ErrorMessage = "Bad remote name format!!!")]
[CustomValidation(typeof(EditRemote), nameof(ValidateRemoteName))]
public string Name
{
get => _name;
set => SetProperty(ref _name, value, true);
}
[Required(ErrorMessage = "Remote URL is required!!!")]
[CustomValidation(typeof(EditRemote), nameof(ValidateRemoteURL))]
public string Url
{
get => _url;
set
{
if (SetProperty(ref _url, value, true))
UseSSH = Models.Remote.IsSSH(value);
}
}
public bool UseSSH
{
get => _useSSH;
set
{
if (SetProperty(ref _useSSH, value))
ValidateProperty(_sshkey, nameof(SSHKey));
}
}
[CustomValidation(typeof(EditRemote), nameof(ValidateSSHKey))]
public string SSHKey
{
get => _sshkey;
set => SetProperty(ref _sshkey, value, true);
}
public EditRemote(Repository repo, Models.Remote remote)
{
_repo = repo;
_remote = remote;
_name = remote.Name;
_url = remote.URL;
_useSSH = Models.Remote.IsSSH(remote.URL);
if (_useSSH)
{
SSHKey = new Commands.Config(repo.FullPath).Get($"remote.{remote.Name}.sshkey");
}
}
public static ValidationResult ValidateRemoteName(string name, ValidationContext ctx)
{
if (ctx.ObjectInstance is EditRemote edit)
{
foreach (var remote in edit._repo.Remotes)
{
if (remote != edit._remote && name == remote.Name)
return new ValidationResult("A remote with given name already exists!!!");
}
}
return ValidationResult.Success;
}
public static ValidationResult ValidateRemoteURL(string url, ValidationContext ctx)
{
if (ctx.ObjectInstance is EditRemote edit)
{
if (!Models.Remote.IsValidURL(url))
return new ValidationResult("Bad remote URL format!!!");
foreach (var remote in edit._repo.Remotes)
{
if (remote != edit._remote && url == remote.URL)
return new ValidationResult("A remote with the same url already exists!!!");
}
}
return ValidationResult.Success;
}
public static ValidationResult ValidateSSHKey(string sshkey, ValidationContext ctx)
{
if (ctx.ObjectInstance is EditRemote { _useSSH: true } && !string.IsNullOrEmpty(sshkey))
{
if (!File.Exists(sshkey))
return new ValidationResult("Given SSH private key can NOT be found!");
}
return ValidationResult.Success;
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Editing remote '{_remote.Name}' ...";
return Task.Run(() =>
{
if (_remote.Name != _name)
{
var succ = new Commands.Remote(_repo.FullPath).Rename(_remote.Name, _name);
if (succ)
_remote.Name = _name;
}
if (_remote.URL != _url)
{
var succ = new Commands.Remote(_repo.FullPath).SetURL(_name, _url, false);
if (succ)
_remote.URL = _url;
}
var pushURL = new Commands.Remote(_repo.FullPath).GetURL(_name, true);
if (pushURL != _url)
new Commands.Remote(_repo.FullPath).SetURL(_name, _url, true);
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return true;
});
}
private readonly Repository _repo = null;
private readonly Models.Remote _remote = null;
private string _name = null;
private string _url = null;
private bool _useSSH = false;
private string _sshkey = string.Empty;
}
}