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

Commit 4efffc9

Browse files
Moving head processing back to RepositoryManager
1 parent ff3c8bf commit 4efffc9

File tree

3 files changed

+67
-63
lines changed

3 files changed

+67
-63
lines changed

src/GitHub.Api/Git/Repository.cs

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Repository : IRepository, IEquatable<Repository>
1414
private ConfigBranch? currentBranch;
1515
private ConfigRemote? currentRemote;
1616
private GitStatus currentStatus;
17-
private string head;
1817
private Dictionary<string, ConfigBranch> localBranches = new Dictionary<string, ConfigBranch>();
1918
private IEnumerable<GitLock> locks;
2019
private Dictionary<string, Dictionary<string, ConfigBranch>> remoteBranches = new Dictionary<string, Dictionary<string, ConfigBranch>>();
@@ -58,7 +57,8 @@ public void Initialize(IRepositoryManager repositoryManager)
5857

5958
this.repositoryManager = repositoryManager;
6059

61-
repositoryManager.OnHeadUpdated += RepositoryManager_OnHeadUpdated;
60+
repositoryManager.OnCurrentBranchUpdated += RepositoryManager_OnCurrentBranchUpdated;
61+
repositoryManager.OnCurrentRemoteUpdated += RepositoryManager_OnCurrentRemoteUpdated;
6262
repositoryManager.OnStatusUpdated += RepositoryManager_OnStatusUpdated;
6363
repositoryManager.OnLocksUpdated += RepositoryManager_OnLocksUpdated;
6464
repositoryManager.OnLocalBranchListUpdated += RepositoryManager_OnLocalBranchListUpdated;
@@ -174,51 +174,13 @@ private void RepositoryManager_OnLocksUpdated(IEnumerable<GitLock> locks)
174174
OnLocksChanged?.Invoke(CurrentLocks);
175175
}
176176

177-
private void RepositoryManager_OnHeadUpdated(string h)
177+
private void RepositoryManager_OnCurrentBranchUpdated(ConfigBranch? branch)
178178
{
179-
Logger.Trace("HeadUpdated");
180-
181-
if (head != h)
182-
{
183-
head = h;
184-
UpdateCurrentBranchAndRemote();
185-
}
179+
CurrentBranch = branch;
186180
}
187181

188-
private void UpdateCurrentBranchAndRemote()
182+
private void RepositoryManager_OnCurrentRemoteUpdated(ConfigRemote? remote)
189183
{
190-
ConfigBranch? branch = null;
191-
192-
if (head.StartsWith("ref:"))
193-
{
194-
var branchName = head.Substring(head.IndexOf("refs/heads/") + "refs/heads/".Length);
195-
branch = GetBranch(branchName);
196-
}
197-
198-
CurrentBranch = branch;
199-
200-
var defaultRemote = "origin";
201-
ConfigRemote? remote = null;
202-
203-
if (currentBranch.HasValue && currentBranch.Value.IsTracking)
204-
{
205-
remote = currentBranch.Value.Remote;
206-
}
207-
208-
if (!remote.HasValue)
209-
{
210-
remote = repositoryManager.Config.GetRemote(defaultRemote);
211-
}
212-
213-
if (!remote.HasValue)
214-
{
215-
var configRemotes = repositoryManager.Config.GetRemotes().ToArray();
216-
if (configRemotes.Any())
217-
{
218-
remote = configRemotes.FirstOrDefault();
219-
}
220-
}
221-
222184
CurrentRemote = remote;
223185
}
224186

@@ -324,16 +286,6 @@ private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
324286
Logger.Warning("Remote {0} is not found", remote);
325287
}
326288
}
327-
328-
private ConfigBranch? GetBranch(string name)
329-
{
330-
if (localBranches.ContainsKey(name))
331-
{
332-
return localBranches[name];
333-
}
334-
335-
return null;
336-
}
337289

338290
/// <summary>
339291
/// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ namespace GitHub.Unity
99
public interface IRepositoryManager : IDisposable
1010
{
1111
event Action<bool> OnIsBusyChanged;
12-
event Action<string> OnHeadUpdated;
1312
event Action<GitStatus> OnStatusUpdated;
1413
event Action<IEnumerable<GitLock>> OnLocksUpdated;
1514
event Action<Dictionary<string, ConfigBranch>> OnLocalBranchListUpdated;
1615
event Action<Dictionary<string, Dictionary<string, ConfigBranch>>> OnRemoteBranchListUpdated;
16+
event Action<ConfigBranch?> OnCurrentBranchUpdated;
17+
event Action<ConfigRemote?> OnCurrentRemoteUpdated;
1718
event Action<string> OnLocalBranchUpdated;
1819
event Action<string> OnLocalBranchAdded;
1920
event Action<string> OnLocalBranchRemoved;
@@ -111,13 +112,14 @@ class RepositoryManager : IRepositoryManager
111112
public event Action<Dictionary<string, Dictionary<string, ConfigBranch>>> OnRemoteBranchListUpdated;
112113
public event Action<IEnumerable<GitLock>> OnLocksUpdated;
113114
public event Action<GitStatus> OnStatusUpdated;
114-
public event Action<string> OnHeadUpdated;
115+
public event Action<ConfigBranch?> OnCurrentBranchUpdated;
116+
public event Action<ConfigRemote?> OnCurrentRemoteUpdated;
115117
public event Action<string> OnLocalBranchUpdated;
116118
public event Action<string> OnLocalBranchAdded;
117119
public event Action<string> OnLocalBranchRemoved;
118-
public event Action<IUser> OnGitUserLoaded;
119120
public event Action<string, string> OnRemoteBranchAdded;
120121
public event Action<string, string> OnRemoteBranchRemoved;
122+
public event Action<IUser> OnGitUserLoaded;
121123

122124
public static RepositoryManager CreateInstance(IPlatform platform, ITaskManager taskManager, IUsageTracker usageTracker,
123125
IGitClient gitClient, NPath repositoryRoot)
@@ -347,7 +349,7 @@ private void SetupWatcher()
347349
private void UpdateHead()
348350
{
349351
var head = repositoryPaths.DotGitHead.ReadAllLines().FirstOrDefault();
350-
OnHeadUpdated?.Invoke(head);
352+
UpdateCurrentBranchAndRemote(head);
351353
}
352354

353355
private ITask HookupHandlers(ITask task, bool disableWatcher = false)
@@ -387,6 +389,7 @@ private void Watcher_OnRemoteBranchCreated(string remote, string name)
387389

388390
private void Watcher_OnRepositoryChanged()
389391
{
392+
Logger.Trace("OnRepositoryChanged");
390393
UpdateGitStatus();
391394
}
392395

@@ -420,6 +423,42 @@ private void Watcher_OnHeadChanged()
420423
UpdateGitStatus();
421424
}
422425

426+
private void UpdateCurrentBranchAndRemote(string head)
427+
{
428+
ConfigBranch? branch = null;
429+
430+
if (head.StartsWith("ref:"))
431+
{
432+
var branchName = head.Substring(head.IndexOf("refs/heads/") + "refs/heads/".Length);
433+
branch = config.GetBranch(branchName);
434+
}
435+
436+
var defaultRemote = "origin";
437+
ConfigRemote? remote = null;
438+
439+
if (branch.HasValue && branch.Value.IsTracking)
440+
{
441+
remote = branch.Value.Remote;
442+
}
443+
444+
if (!remote.HasValue)
445+
{
446+
remote = config.GetRemote(defaultRemote);
447+
}
448+
449+
if (!remote.HasValue)
450+
{
451+
var configRemotes = config.GetRemotes().ToArray();
452+
if (configRemotes.Any())
453+
{
454+
remote = configRemotes.FirstOrDefault();
455+
}
456+
}
457+
458+
OnCurrentBranchUpdated?.Invoke(branch);
459+
OnCurrentRemoteUpdated?.Invoke(remote);
460+
}
461+
423462
private void Watcher_OnIndexChanged()
424463
{}
425464

src/tests/TestUtils/Events/IRepositoryManagerListener.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading;
@@ -11,7 +12,6 @@ interface IRepositoryManagerListener
1112
void OnIsBusyChanged(bool busy);
1213
void OnStatusUpdated(GitStatus status);
1314
void OnLocksUpdated(IEnumerable<GitLock> locks);
14-
void OnHeadUpdated(string head);
1515
void OnLocalBranchListUpdated(Dictionary<string, ConfigBranch> branchList);
1616
void OnRemoteBranchListUpdated(Dictionary<string, Dictionary<string, ConfigBranch>> remoteBranchList);
1717
void OnLocalBranchUpdated(string name);
@@ -20,6 +20,8 @@ interface IRepositoryManagerListener
2020
void OnRemoteBranchAdded(string origin, string name);
2121
void OnRemoteBranchRemoved(string origin, string name);
2222
void OnGitUserLoaded(IUser user);
23+
void OnCurrentBranchUpdated(ConfigBranch? configBranch);
24+
void OnCurrentRemoteUpdated(ConfigRemote? configRemote);
2325
}
2426

2527
class RepositoryManagerEvents
@@ -28,6 +30,8 @@ class RepositoryManagerEvents
2830
public EventWaitHandle OnIsNotBusy { get; } = new AutoResetEvent(false);
2931
public EventWaitHandle OnStatusUpdated { get; } = new AutoResetEvent(false);
3032
public EventWaitHandle OnLocksUpdated { get; } = new AutoResetEvent(false);
33+
public EventWaitHandle OnCurrentBranchUpdated { get; } = new AutoResetEvent(false);
34+
public EventWaitHandle OnCurrentRemoteUpdated { get; } = new AutoResetEvent(false);
3135
public EventWaitHandle OnHeadUpdated { get; } = new AutoResetEvent(false);
3236
public EventWaitHandle OnLocalBranchListUpdated { get; } = new AutoResetEvent(false);
3337
public EventWaitHandle OnRemoteBranchListUpdated { get; } = new AutoResetEvent(false);
@@ -44,6 +48,8 @@ public void Reset()
4448
OnIsNotBusy.Reset();
4549
OnStatusUpdated.Reset();
4650
OnLocksUpdated.Reset();
51+
OnCurrentBranchUpdated.Reset();
52+
OnCurrentRemoteUpdated.Reset();
4753
OnHeadUpdated.Reset();
4854
OnLocalBranchListUpdated.Reset();
4955
OnRemoteBranchListUpdated.Reset();
@@ -85,10 +91,16 @@ public static void AttachListener(this IRepositoryManagerListener listener,
8591
managerEvents?.OnLocksUpdated.Set();
8692
};
8793

88-
repositoryManager.OnHeadUpdated += head => {
89-
logger?.Trace("OnHeadUpdated");
90-
listener.OnHeadUpdated(head);
91-
managerEvents?.OnHeadUpdated.Set();
94+
repositoryManager.OnCurrentBranchUpdated += configBranch => {
95+
logger?.Trace("OnCurrentBranchUpdated");
96+
listener.OnCurrentBranchUpdated(configBranch);
97+
managerEvents?.OnCurrentBranchUpdated.Set();
98+
};
99+
100+
repositoryManager.OnCurrentRemoteUpdated += configRemote => {
101+
logger?.Trace("OnCurrentRemoteUpdated");
102+
listener.OnCurrentRemoteUpdated(configRemote);
103+
managerEvents?.OnCurrentRemoteUpdated.Set();
92104
};
93105

94106
repositoryManager.OnLocalBranchListUpdated += branchList => {
@@ -145,7 +157,8 @@ public static void AssertDidNotReceiveAnyCalls(this IRepositoryManagerListener r
145157
repositoryManagerListener.DidNotReceive().OnIsBusyChanged(Args.Bool);
146158
repositoryManagerListener.DidNotReceive().OnStatusUpdated(Args.GitStatus);
147159
repositoryManagerListener.DidNotReceive().OnLocksUpdated(Args.EnumerableGitLock);
148-
repositoryManagerListener.DidNotReceive().OnHeadUpdated(Args.String);
160+
repositoryManagerListener.DidNotReceive().OnCurrentBranchUpdated(Arg.Any<ConfigBranch?>());
161+
repositoryManagerListener.DidNotReceive().OnCurrentRemoteUpdated(Arg.Any<ConfigRemote?>());
149162
repositoryManagerListener.DidNotReceive().OnLocalBranchListUpdated(Arg.Any<Dictionary<string, ConfigBranch>>());
150163
repositoryManagerListener.DidNotReceive().OnRemoteBranchListUpdated(Arg.Any<Dictionary<string, Dictionary<string, ConfigBranch>>>());
151164
repositoryManagerListener.DidNotReceive().OnLocalBranchUpdated(Args.String);

0 commit comments

Comments
 (0)