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

Commit a54f8d9

Browse files
Starting to take away responsibility from Repository
Listening to new events and calling functionality from RepositoryManager
1 parent 62c3d14 commit a54f8d9

File tree

2 files changed

+107
-87
lines changed

2 files changed

+107
-87
lines changed

src/GitHub.Api/Git/Repository.cs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public void Initialize(IRepositoryManager initRepositoryManager)
5050
repositoryManager.OnCurrentBranchAndRemoteUpdated += RepositoryManager_OnCurrentBranchAndRemoteUpdated;
5151
repositoryManager.OnLocalBranchListUpdated += RepositoryManager_OnLocalBranchListUpdated;
5252
repositoryManager.OnRemoteBranchListUpdated += RepositoryManager_OnRemoteBranchListUpdated;
53-
54-
UpdateGitStatus();
55-
UpdateGitLog();
56-
57-
new ActionTask(CancellationToken.None, UpdateLocks) { Affinity = TaskAffinity.UI }.Start();
5853
}
5954

6055
public ITask SetupRemote(string remote, string remoteUrl)
@@ -88,8 +83,7 @@ public ITask Pull()
8883

8984
public ITask Push()
9085
{
91-
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch?.Name)
92-
.Then(UpdateGitStatus);
86+
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch?.Name);
9387
}
9488

9589
public ITask Fetch()
@@ -104,14 +98,12 @@ public ITask Revert(string changeset)
10498

10599
public ITask RequestLock(string file)
106100
{
107-
return repositoryManager.LockFile(file)
108-
.Then(UpdateLocks);
101+
return repositoryManager.LockFile(file);
109102
}
110103

111104
public ITask ReleaseLock(string file, bool force)
112105
{
113-
return repositoryManager.UnlockFile(file, force)
114-
.Then(UpdateLocks);
106+
return repositoryManager.UnlockFile(file, force);
115107
}
116108

117109
public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
@@ -351,30 +343,6 @@ private void HandleBranchCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
351343
LocalAndRemoteBranchListChanged?.Invoke(cacheUpdateEvent);
352344
}
353345

354-
private void UpdateGitStatus()
355-
{
356-
repositoryManager?.Status()
357-
.ThenInUI((b, status) => { CurrentStatus = status; })
358-
.Start();
359-
}
360-
361-
private void UpdateGitLog()
362-
{
363-
repositoryManager?.Log()
364-
.ThenInUI((b, log) => { CurrentLog = log; })
365-
.Start();
366-
}
367-
368-
private void UpdateLocks()
369-
{
370-
if (CurrentRemote.HasValue)
371-
{
372-
repositoryManager?.ListLocks(false)
373-
.ThenInUI((b, locks) => { CurrentLocks = locks; })
374-
.Start();
375-
}
376-
}
377-
378346
private void RepositoryManager_OnCurrentBranchAndRemoteUpdated(ConfigBranch? branch, ConfigRemote? remote)
379347
{
380348
new ActionTask(CancellationToken.None, () => {

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 104 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public interface IRepositoryManager : IDisposable
99
{
1010
event Action<ConfigBranch?, ConfigRemote?> OnCurrentBranchAndRemoteUpdated;
1111
event Action<bool> OnIsBusyChanged;
12+
event Action<GitStatus> GitStatusUpdated;
13+
event Action<List<GitLogEntry>> GitLogUpdated;
1214
event Action<Dictionary<string, ConfigBranch>> OnLocalBranchListUpdated;
1315
event Action<Dictionary<string, ConfigRemote>, Dictionary<string, Dictionary<string, ConfigBranch>>> OnRemoteBranchListUpdated;
1416

@@ -96,6 +98,8 @@ class RepositoryManager : IRepositoryManager
9698

9799
public event Action<ConfigBranch?, ConfigRemote?> OnCurrentBranchAndRemoteUpdated;
98100
public event Action<bool> OnIsBusyChanged;
101+
public event Action<GitStatus> GitStatusUpdated;
102+
public event Action<List<GitLogEntry>> GitLogUpdated;
99103
public event Action<Dictionary<string, ConfigBranch>> OnLocalBranchListUpdated;
100104
public event Action<Dictionary<string, ConfigRemote>, Dictionary<string, Dictionary<string, ConfigBranch>>> OnRemoteBranchListUpdated;
101105

@@ -283,10 +287,13 @@ public ITask UnlockFile(string file, bool force)
283287

284288
private void SetupWatcher()
285289
{
286-
watcher.HeadChanged += Watcher_OnHeadChanged;
287-
watcher.IndexChanged += Watcher_OnIndexChanged;
288-
watcher.ConfigChanged += Watcher_OnConfigChanged;
289-
watcher.RepositoryChanged += Watcher_OnRepositoryChanged;
290+
watcher.HeadChanged += WatcherOnHeadChanged;
291+
watcher.IndexChanged += WatcherOnIndexChanged;
292+
watcher.ConfigChanged += WatcherOnConfigChanged;
293+
watcher.RepositoryCommitted += WatcherOnRepositoryCommitted;
294+
watcher.RepositoryChanged += WatcherOnRepositoryChanged;
295+
watcher.LocalBranchesChanged += WatcherOnLocalBranchesChanged;
296+
watcher.RemoteBranchesChanged += WatcherOnRemoteBranchesChanged;
290297
}
291298

292299
private void UpdateHead()
@@ -296,6 +303,48 @@ private void UpdateHead()
296303
UpdateCurrentBranchAndRemote(head);
297304
}
298305

306+
private void UpdateCurrentBranchAndRemote(string head)
307+
{
308+
ConfigBranch? branch = null;
309+
310+
if (head.StartsWith("ref:"))
311+
{
312+
var branchName = head.Substring(head.IndexOf("refs/heads/") + "refs/heads/".Length);
313+
branch = config.GetBranch(branchName);
314+
315+
if (!branch.HasValue)
316+
{
317+
branch = new ConfigBranch { Name = branchName };
318+
}
319+
}
320+
321+
var defaultRemote = "origin";
322+
ConfigRemote? remote = null;
323+
324+
if (branch.HasValue && branch.Value.IsTracking)
325+
{
326+
remote = branch.Value.Remote;
327+
}
328+
329+
if (!remote.HasValue)
330+
{
331+
remote = config.GetRemote(defaultRemote);
332+
}
333+
334+
if (!remote.HasValue)
335+
{
336+
var configRemotes = config.GetRemotes().ToArray();
337+
if (configRemotes.Any())
338+
{
339+
remote = configRemotes.FirstOrDefault();
340+
}
341+
}
342+
343+
Logger.Trace("CurrentBranch: {0}", branch.HasValue ? branch.Value.ToString() : "[NULL]");
344+
Logger.Trace("CurrentRemote: {0}", remote.HasValue ? remote.Value.ToString() : "[NULL]");
345+
OnCurrentBranchAndRemoteUpdated?.Invoke(branch, remote);
346+
}
347+
299348
private ITask HookupHandlers(ITask task, bool disableWatcher = false)
300349
{
301350
task.OnStart += t => {
@@ -321,67 +370,70 @@ private ITask HookupHandlers(ITask task, bool disableWatcher = false)
321370
return task;
322371
}
323372

324-
private void Watcher_OnRepositoryChanged()
373+
private void WatcherOnRemoteBranchesChanged()
325374
{
326-
Logger.Trace("OnRepositoryChanged");
375+
Logger.Trace("WatcherOnRemoteBranchesChanged");
376+
UpdateRemoteBranches();
327377
}
328378

329-
private void Watcher_OnConfigChanged()
379+
private void WatcherOnLocalBranchesChanged()
330380
{
381+
Logger.Trace("WatcherOnLocalBranchesChanged");
382+
UpdateLocalBranches();
383+
}
384+
385+
private void WatcherOnRepositoryCommitted()
386+
{
387+
Logger.Trace("WatcherOnRepositoryCommitted");
388+
UpdateLog();
389+
}
390+
391+
private void WatcherOnRepositoryChanged()
392+
{
393+
Logger.Trace("WatcherOnRepositoryChanged");
394+
UpdateStatus();
395+
}
396+
397+
private void WatcherOnConfigChanged()
398+
{
399+
Logger.Trace("WatcherOnConfigChanged");
331400
UpdateConfigData(true);
332401
}
333402

334-
private void Watcher_OnHeadChanged()
403+
private void WatcherOnHeadChanged()
335404
{
336-
Logger.Trace("Watcher_OnHeadChanged");
405+
Logger.Trace("WatcherOnHeadChanged");
337406
UpdateHead();
338407
}
339408

340-
private void UpdateCurrentBranchAndRemote(string head)
409+
private void WatcherOnIndexChanged()
341410
{
342-
ConfigBranch? branch = null;
411+
Logger.Trace("WatcherOnIndexChanged");
412+
UpdateStatus();
413+
}
343414

344-
if (head.StartsWith("ref:"))
415+
private void UpdateLog()
416+
{
417+
Log().Then((success, logEntries) =>
345418
{
346-
var branchName = head.Substring(head.IndexOf("refs/heads/") + "refs/heads/".Length);
347-
branch = config.GetBranch(branchName);
348-
349-
if (!branch.HasValue)
419+
if (success)
350420
{
351-
branch = new ConfigBranch { Name = branchName };
421+
GitLogUpdated?.Invoke(logEntries);
352422
}
353-
}
354-
355-
var defaultRemote = "origin";
356-
ConfigRemote? remote = null;
357-
358-
if (branch.HasValue && branch.Value.IsTracking)
359-
{
360-
remote = branch.Value.Remote;
361-
}
362-
363-
if (!remote.HasValue)
364-
{
365-
remote = config.GetRemote(defaultRemote);
366-
}
423+
}).Start();
424+
}
367425

368-
if (!remote.HasValue)
426+
private void UpdateStatus()
427+
{
428+
Status().Then((success, status) =>
369429
{
370-
var configRemotes = config.GetRemotes().ToArray();
371-
if (configRemotes.Any())
430+
if (success)
372431
{
373-
remote = configRemotes.FirstOrDefault();
432+
GitStatusUpdated?.Invoke(status);
374433
}
375-
}
376-
377-
Logger.Trace("OnCurrentBranchUpdated: {0}", branch.HasValue ? branch.Value.ToString() : "[NULL]");
378-
Logger.Trace("OnCurrentRemoteUpdated: {0}", remote.HasValue ? remote.Value.ToString() : "[NULL]");
379-
OnCurrentBranchAndRemoteUpdated?.Invoke(branch, remote);
434+
}).Start();
380435
}
381436

382-
private void Watcher_OnIndexChanged()
383-
{}
384-
385437
private void UpdateConfigData(bool resetConfig = false)
386438
{
387439
Logger.Trace("UpdateConfigData reset:{0}", resetConfig);
@@ -391,23 +443,23 @@ private void UpdateConfigData(bool resetConfig = false)
391443
config.Reset();
392444
}
393445

394-
LoadBranchesFromConfig();
395-
LoadRemotesFromConfig();
446+
UpdateLocalBranches();
447+
UpdateRemoteBranches();
396448
UpdateHead();
397449
}
398450

399-
private void LoadBranchesFromConfig()
451+
private void UpdateLocalBranches()
400452
{
401-
Logger.Trace("LoadBranchesFromConfig");
453+
Logger.Trace("UpdateLocalBranches");
402454

403455
var branches = new Dictionary<string, ConfigBranch>();
404-
LoadBranchesFromConfig(branches, repositoryPaths.BranchesPath, config.GetBranches().Where(x => x.IsTracking), "");
456+
UpdateLocalBranches(branches, repositoryPaths.BranchesPath, config.GetBranches().Where(x => x.IsTracking), "");
405457

406458
Logger.Trace("OnLocalBranchListUpdated {0} branches", branches.Count);
407459
OnLocalBranchListUpdated?.Invoke(branches);
408460
}
409461

410-
private void LoadBranchesFromConfig(Dictionary<string, ConfigBranch> branches, NPath path, IEnumerable<ConfigBranch> configBranches, string prefix)
462+
private void UpdateLocalBranches(Dictionary<string, ConfigBranch> branches, NPath path, IEnumerable<ConfigBranch> configBranches, string prefix)
411463
{
412464
foreach (var file in path.Files())
413465
{
@@ -423,13 +475,13 @@ private void LoadBranchesFromConfig(Dictionary<string, ConfigBranch> branches, N
423475

424476
foreach (var dir in path.Directories())
425477
{
426-
LoadBranchesFromConfig(branches, dir, configBranches, prefix + dir.FileName + "/");
478+
UpdateLocalBranches(branches, dir, configBranches, prefix + dir.FileName + "/");
427479
}
428480
}
429481

430-
private void LoadRemotesFromConfig()
482+
private void UpdateRemoteBranches()
431483
{
432-
Logger.Trace("LoadRemotesFromConfig");
484+
Logger.Trace("UpdateRemoteBranches");
433485

434486
var remotes = config.GetRemotes().ToArray().ToDictionary(x => x.Name, x => x);
435487
var remoteBranches = new Dictionary<string, Dictionary<string, ConfigBranch>>();

0 commit comments

Comments
 (0)