forked from gdevic/GitForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassRemotes.cs
163 lines (143 loc) · 5.37 KB
/
ClassRemotes.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GitForce
{
/// <summary>
/// Class containing a set of remotes for a given repository
/// This class mainly manages names, command alias and passwords - things that
/// are not kept natively with a git repo
/// </summary>
[Serializable]
public class ClassRemotes
{
/// <summary>
/// Structure describing a remote repo. Only the name, push cmd and password are
/// to be used across the sessions, while the URL fields gets rewritten
/// every time the list of remotes is updated from git
/// </summary>
[Serializable]
public struct Remote
{
public string Name;
public string UrlFetch;
public string UrlPush;
public string PushCmd;
public string Password;
}
/// <summary>
/// Current (default) remote name
/// </summary>
public string Current = "";
/// <summary>
/// Stores the current list of remotes and serves as a
/// lookup dictionary of passwords for a given remote name
/// </summary>
private Dictionary<string, Remote> _remotes = new Dictionary<string, Remote>();
/// <summary>
/// Return the list of names of remote repos
/// </summary>
public List<string> GetListNames()
{
List<string> list = _remotes.Select(kvp => kvp.Key).ToList();
return list;
}
/// <summary>
/// Return the remote structure associated with a given name
/// </summary>
public Remote Get(string name)
{
return _remotes[name];
}
/// <summary>
/// Refresh the list of remotes for the given repo while keeping the
/// existing passwords and push commands
/// </summary>
public void Refresh(ClassRepo repo)
{
// Build the new list while picking up password fields from the existing list
Dictionary<string, Remote> newlist = new Dictionary<string, Remote>();
string[] response = new[] {string.Empty};
ExecResult result = repo.Run("remote -v");
if (result.Success())
{
response = result.stdout.Split((Environment.NewLine).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string s in response)
{
Remote r = new Remote();
// Split the resulting remote repo name/url into separate strings
string[] url = s.Split("\t ".ToCharArray());
string name = url[0];
// Find if the name exists in the main list and save off the password from it
if (newlist.ContainsKey(name))
r = newlist[name];
if (_remotes.ContainsKey(name))
{
r.Password = _remotes[name].Password;
r.PushCmd = _remotes[name].PushCmd;
}
// Set all other fields that we refresh every time
r.Name = name;
if (url[2] == "(fetch)") r.UrlFetch = url[1];
if (url[2] == "(push)") r.UrlPush = url[1];
// Add it to the new list
newlist[name] = r;
}
}
// Set the newly built list to be the master list
_remotes = newlist;
// Fixup the new current string name
if (!_remotes.ContainsKey(Current))
Current = _remotes.Count > 0 ? _remotes.ElementAt(0).Key : "";
}
/// <summary>
/// Sets the password field for the given remote name or
/// creates a new remote if the named one does not exist
/// </summary>
public void SetPassword(string name, string password)
{
Remote r;
if (!_remotes.TryGetValue(name, out r))
r.Name = name;
r.Password = password;
_remotes[name] = r;
}
/// <summary>
/// Return the password for a given remote by name or
/// the current remote (if name is empty string)
/// </summary>
public string GetPassword(string name)
{
Remote r;
r.Password = "";
if (name == "") name = Current;
_remotes.TryGetValue(name, out r);
return r.Password;
}
/// <summary>
/// Sets the push command field for the given remote name or
/// creates a new remote if the named one does not exist
/// </summary>
public void SetPushCmd(string name, string cmd)
{
Remote r;
if (!_remotes.TryGetValue(name, out r))
r.Name = name;
r.PushCmd = cmd;
_remotes[name] = r;
}
/// <summary>
/// Return the push cmd for a given remote by name or
/// the current remote (if name is empty string)
/// </summary>
public string GetPushCmd(string name)
{
Remote r;
r.PushCmd = "";
if (name == "") name = Current;
_remotes.TryGetValue(name, out r);
return r.PushCmd;
}
}
}