Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit c497641

Browse files
Attempting to add a remotes list; Reordered file
1 parent 7a5abbd commit c497641

File tree

10 files changed

+165
-135
lines changed

10 files changed

+165
-135
lines changed

src/GitHub.Api/Git/GitRemote.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace GitHub.Unity
55
{
6-
enum GitRemoteFunction
6+
public enum GitRemoteFunction
77
{
88
Unknown,
99
Fetch,
@@ -12,7 +12,7 @@ enum GitRemoteFunction
1212
}
1313

1414
[Serializable]
15-
struct GitRemote
15+
public struct GitRemote
1616
{
1717
public string Name;
1818
public string Url;

src/GitHub.Api/Git/IRepository.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ public interface IRepository : IEquatable<IRepository>
5151
/// </summary>
5252
ConfigBranch? CurrentBranch { get; set; }
5353
GitStatus CurrentStatus { get; set; }
54-
IEnumerable<GitBranch> LocalBranches { get; }
55-
IEnumerable<GitBranch> RemoteBranches { get; }
54+
IList<GitRemote> Remotes { get; }
55+
IList<GitBranch> LocalBranches { get; }
56+
IList<GitBranch> RemoteBranches { get; }
5657
IUser User { get; set; }
57-
IEnumerable<GitLock> CurrentLocks { get; }
58+
IList<GitLock> CurrentLocks { get; }
5859
string CurrentBranchName { get; }
5960

6061
event Action<GitStatus> OnStatusChanged;
6162
event Action<string> OnCurrentBranchChanged;
6263
event Action<string> OnCurrentRemoteChanged;
6364
event Action OnLocalBranchListChanged;
64-
event Action OnHeadChanged;
65+
event Action OnLocalBranchChanged;
6566
event Action<IEnumerable<GitLock>> OnLocksChanged;
6667
event Action OnRepositoryInfoChanged;
6768
event Action OnRemoteBranchListChanged;

src/GitHub.Api/Git/Repository.cs

Lines changed: 112 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,30 @@
88
namespace GitHub.Unity
99
{
1010
[DebuggerDisplay("{DebuggerDisplay,nq}")]
11-
class Repository : IRepository, IEquatable<Repository>
11+
class Repository : IEquatable<Repository>, IRepository
1212
{
13-
private IRepositoryManager repositoryManager;
1413
private ConfigBranch? currentBranch;
14+
private IList<GitLock> currentLocks;
1515
private ConfigRemote? currentRemote;
1616
private GitStatus currentStatus;
1717
private Dictionary<string, ConfigBranch> localBranches = new Dictionary<string, ConfigBranch>();
18-
private IEnumerable<GitLock> locks;
1918
private Dictionary<string, Dictionary<string, ConfigBranch>> remoteBranches = new Dictionary<string, Dictionary<string, ConfigBranch>>();
2019
private Dictionary<string, ConfigRemote> remotes;
21-
22-
public event Action<GitStatus> OnStatusChanged;
20+
private IRepositoryManager repositoryManager;
2321
public event Action<string> OnCurrentBranchChanged;
2422
public event Action<string> OnCurrentRemoteChanged;
23+
public event Action OnLocalBranchChanged;
2524
public event Action OnLocalBranchListChanged;
26-
public event Action OnRemoteBranchListChanged;
27-
public event Action OnHeadChanged;
2825
public event Action<IEnumerable<GitLock>> OnLocksChanged;
26+
public event Action OnRemoteBranchListChanged;
2927
public event Action OnRepositoryInfoChanged;
3028

31-
public IEnumerable<GitBranch> LocalBranches => localBranches.Values.Select(
32-
x => new GitBranch(x.Name, (x.IsTracking ? (x.Remote.Value.Name + "/" + x.Name) : "[None]"), x.Name == CurrentBranch?.Name));
33-
34-
public IEnumerable<GitBranch> RemoteBranches => remoteBranches.Values.SelectMany(
35-
x => x.Values).Select(x => new GitBranch(x.Remote.Value.Name + "/" + x.Name, "[None]", false));
29+
public event Action<GitStatus> OnStatusChanged;
3630

3731
/// <summary>
3832
/// Initializes a new instance of the <see cref="Repository"/> class.
3933
/// </summary>
40-
/// <param name="repositoryManager"></param>
4134
/// <param name="name">The repository name.</param>
42-
/// <param name="cloneUrl">The repository's clone URL.</param>
4335
/// <param name="localPath"></param>
4436
public Repository(string name, NPath localPath)
4537
{
@@ -57,13 +49,13 @@ public void Initialize(IRepositoryManager repositoryManager)
5749

5850
this.repositoryManager = repositoryManager;
5951

60-
repositoryManager.OnCurrentBranchUpdated += RepositoryManager_OnCurrentBranchUpdated;
61-
repositoryManager.OnCurrentRemoteUpdated += RepositoryManager_OnCurrentRemoteUpdated;
62-
repositoryManager.OnStatusUpdated += RepositoryManager_OnStatusUpdated;
63-
repositoryManager.OnLocksUpdated += RepositoryManager_OnLocksUpdated;
64-
repositoryManager.OnLocalBranchListUpdated += RepositoryManager_OnLocalBranchListUpdated;
65-
repositoryManager.OnRemoteBranchListUpdated += RepositoryManager_OnRemoteBranchListUpdated;
66-
repositoryManager.OnLocalBranchUpdated += RepositoryManager_OnLocalBranchUpdated;
52+
repositoryManager.OnCurrentBranchUpdated += branch => CurrentBranch = branch;
53+
repositoryManager.OnCurrentRemoteUpdated += remote => CurrentRemote = remote;
54+
repositoryManager.OnStatusUpdated += status => CurrentStatus = status;
55+
repositoryManager.OnLocksUpdated += locks => CurrentLocks = locks;
56+
repositoryManager.OnLocalBranchListUpdated += OnRepositoryManager_OnLocalBranchListUpdated;
57+
repositoryManager.OnRemoteBranchListUpdated += OnRepositoryManager_OnRemoteBranchListUpdated;
58+
repositoryManager.OnLocalBranchUpdated += OnRepositoryManager_OnLocalBranchUpdated;
6759
repositoryManager.OnLocalBranchAdded += RepositoryManager_OnLocalBranchAdded;
6860
repositoryManager.OnLocalBranchRemoved += RepositoryManager_OnLocalBranchRemoved;
6961
repositoryManager.OnRemoteBranchAdded += RepositoryManager_OnRemoteBranchAdded;
@@ -122,7 +114,7 @@ public ITask Fetch()
122114
{
123115
return repositoryManager.Fetch(CurrentRemote.Value.Name);
124116
}
125-
117+
126118
public ITask Revert(string changeset)
127119
{
128120
return repositoryManager.Revert(changeset);
@@ -145,65 +137,109 @@ public ITask ReleaseLock(string file, bool force)
145137
return repositoryManager.UnlockFile(file, force);
146138
}
147139

148-
private void UpdateRepositoryInfo()
140+
/// <summary>
141+
/// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime
142+
/// of a repository. Equals takes care of any hash collisions because of this
143+
/// </summary>
144+
/// <returns></returns>
145+
public override int GetHashCode()
149146
{
150-
if (CurrentRemote.HasValue)
151-
{
152-
CloneUrl = new UriString(CurrentRemote.Value.Url);
153-
Name = CloneUrl.RepositoryName;
154-
Logger.Trace("CloneUrl: {0}", CloneUrl.ToString());
155-
}
156-
else
157-
{
158-
CloneUrl = null;
159-
Name = LocalPath.FileName;
160-
Logger.Trace("CloneUrl: [NULL]");
161-
}
162-
163-
OnRepositoryInfoChanged?.Invoke();
147+
return LocalPath.GetHashCode();
164148
}
165149

166-
private void RepositoryManager_OnStatusUpdated(GitStatus status)
150+
public override bool Equals(object obj)
167151
{
168-
CurrentStatus = status;
152+
if (ReferenceEquals(this, obj))
153+
return true;
154+
var other = obj as Repository;
155+
return Equals(other);
169156
}
170157

171-
private void RepositoryManager_OnLocksUpdated(IEnumerable<GitLock> locks)
158+
public bool Equals(Repository other)
172159
{
173-
CurrentLocks = locks;
174-
OnLocksChanged?.Invoke(CurrentLocks);
160+
return Equals((IRepository)other);
175161
}
176162

177-
private void RepositoryManager_OnCurrentBranchUpdated(ConfigBranch? branch)
163+
public bool Equals(IRepository other)
178164
{
179-
CurrentBranch = branch;
165+
if (ReferenceEquals(this, other))
166+
return true;
167+
return other != null &&
168+
object.Equals(LocalPath, other.LocalPath);
180169
}
181170

182-
private void RepositoryManager_OnCurrentRemoteUpdated(ConfigRemote? remote)
171+
private void OnRepositoryManager_OnLocalBranchUpdated(string name)
183172
{
184-
CurrentRemote = remote;
173+
if (name == currentBranch?.Name)
174+
{
175+
OnLocalBranchChanged?.Invoke();
176+
Refresh();
177+
}
185178
}
186179

187-
private void RepositoryManager_OnRemoteBranchListUpdated(Dictionary<string, Dictionary<string, ConfigBranch>> branches)
180+
private void OnRepositoryManager_OnRemoteBranchListUpdated(Dictionary<string, ConfigRemote> updatedRemotes, Dictionary<string, Dictionary<string, ConfigBranch>> branches)
188181
{
189-
Logger.Trace("RemoteBranchListUpdated");
182+
remotes = updatedRemotes;
183+
184+
Remotes = remotes.Select(pair => new GitRemote {
185+
Name = pair.Value.Name,
186+
Url = pair.Value.Url
187+
}).ToArray();
190188

191189
remoteBranches = branches;
192-
remotes = branches
193-
.Where(pair => pair.Value.Any())
194-
.ToDictionary(pair => pair.Key, pair => pair.Value.First().Value.Remote.Value);
195190

191+
RemoteBranches = remoteBranches.Values
192+
.SelectMany(x => x.Values)
193+
.Select(x =>
194+
{
195+
var name = x.Remote.Value.Name + "/" + x.Name;
196+
return new GitBranch(name, "[None]", false);
197+
}).ToArray();
198+
199+
200+
Logger.Trace("OnRemoteBranchListChanged");
196201
OnRemoteBranchListChanged?.Invoke();
197202
}
198203

199-
private void RepositoryManager_OnLocalBranchListUpdated(Dictionary<string, ConfigBranch> branches)
204+
private void OnRepositoryManager_OnLocalBranchListUpdated(Dictionary<string, ConfigBranch> branches)
200205
{
201-
Logger.Trace("LocalBranchListUpdated");
202-
203206
localBranches = branches;
207+
208+
LocalBranches = localBranches.Values.Select(x =>
209+
{
210+
var argName = x.Name;
211+
212+
var tracking = x.IsTracking
213+
? x.Remote.Value.Name + "/" + argName
214+
: "[None]";
215+
216+
var active = argName == CurrentBranch?.Name;
217+
218+
return new GitBranch(argName, tracking, active);
219+
}).ToArray();
220+
221+
Logger.Trace("OnLocalBranchListChanged");
204222
OnLocalBranchListChanged?.Invoke();
205223
}
206224

225+
private void UpdateRepositoryInfo()
226+
{
227+
if (CurrentRemote.HasValue)
228+
{
229+
CloneUrl = new UriString(CurrentRemote.Value.Url);
230+
Name = CloneUrl.RepositoryName;
231+
Logger.Trace("CloneUrl: {0}", CloneUrl.ToString());
232+
}
233+
else
234+
{
235+
CloneUrl = null;
236+
Name = LocalPath.FileName;
237+
Logger.Trace("CloneUrl: [NULL]");
238+
}
239+
240+
OnRepositoryInfoChanged?.Invoke();
241+
}
242+
207243
private void RepositoryManager_OnLocalBranchRemoved(string name)
208244
{
209245
if (localBranches.ContainsKey(name))
@@ -235,16 +271,6 @@ private void RepositoryManager_OnLocalBranchAdded(string name)
235271
}
236272
}
237273

238-
private void RepositoryManager_OnLocalBranchUpdated(string name)
239-
{
240-
if (name == currentBranch?.Name)
241-
{
242-
// commit of current branch changed, trigger OnHeadChanged
243-
OnHeadChanged?.Invoke();
244-
repositoryManager.Refresh();
245-
}
246-
}
247-
248274
private void RepositoryManager_OnRemoteBranchAdded(string remote, string name)
249275
{
250276
Dictionary<string, ConfigBranch> branchList;
@@ -265,7 +291,7 @@ private void RepositoryManager_OnRemoteBranchAdded(string remote, string name)
265291
Logger.Warning("Remote {0} is not found", remote);
266292
}
267293
}
268-
294+
269295
private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
270296
{
271297
Dictionary<string, ConfigBranch> branchList;
@@ -287,36 +313,11 @@ private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
287313
}
288314
}
289315

290-
/// <summary>
291-
/// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime
292-
/// of a repository. Equals takes care of any hash collisions because of this
293-
/// </summary>
294-
/// <returns></returns>
295-
public override int GetHashCode()
296-
{
297-
return LocalPath.GetHashCode();
298-
}
316+
public IList<GitRemote> Remotes { get; private set; }
299317

300-
public override bool Equals(object obj)
301-
{
302-
if (ReferenceEquals(this, obj))
303-
return true;
304-
var other = obj as Repository;
305-
return Equals(other);
306-
}
318+
public IList<GitBranch> LocalBranches { get; private set; }
307319

308-
public bool Equals(Repository other)
309-
{
310-
return (Equals((IRepository)other));
311-
}
312-
313-
public bool Equals(IRepository other)
314-
{
315-
if (ReferenceEquals(this, other))
316-
return true;
317-
return other != null &&
318-
object.Equals(LocalPath, other.LocalPath);
319-
}
320+
public IList<GitBranch> RemoteBranches { get; private set; }
320321

321322
public ConfigBranch? CurrentBranch
322323
{
@@ -331,6 +332,7 @@ public ConfigBranch? CurrentBranch
331332
}
332333
}
333334
}
335+
334336
/// <summary>
335337
/// Gets the current branch of the repository.
336338
/// </summary>
@@ -381,14 +383,25 @@ public GitStatus CurrentStatus
381383
get { return currentStatus; }
382384
set
383385
{
384-
Logger.Trace("OnStatusUpdated: {0}", value.ToString());
385386
currentStatus = value;
386-
OnStatusChanged?.Invoke(CurrentStatus);
387+
Logger.Trace("OnStatusChanged: {0}", value.ToString());
388+
OnStatusChanged?.Invoke(value);
387389
}
388390
}
389391

390392
public IUser User { get; set; }
391-
public IEnumerable<GitLock> CurrentLocks { get; private set; }
393+
394+
public IList<GitLock> CurrentLocks
395+
{
396+
get { return currentLocks; }
397+
private set
398+
{
399+
Logger.Trace("OnLocksChanged: {0}", value.ToString());
400+
currentLocks = value;
401+
OnLocksChanged?.Invoke(value);
402+
}
403+
}
404+
392405
protected static ILogging Logger { get; } = Logging.GetLogger<Repository>();
393406
}
394407

@@ -401,12 +414,12 @@ public interface IUser
401414
[Serializable]
402415
class User : IUser
403416
{
404-
public string Name { get; set; }
405-
public string Email { get; set; }
406-
407417
public override string ToString()
408418
{
409419
return String.Format("Name: {0} Email: {1}", Name, Email);
410420
}
421+
422+
public string Name { get; set; }
423+
public string Email { get; set; }
411424
}
412425
}

0 commit comments

Comments
 (0)