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

Commit f81d00f

Browse files
Restoring RepositoryInfoCache
1 parent eb4c2d0 commit f81d00f

File tree

6 files changed

+170
-128
lines changed

6 files changed

+170
-128
lines changed

src/GitHub.Api/Cache/CacheInterfaces.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace GitHub.Unity
55
{
66
public enum CacheType
77
{
8+
RepositoryInfoCache,
89
BranchCache,
910
GitLogCache,
1011
GitStatusCache,
@@ -22,6 +23,7 @@ public interface ICacheContainer
2223
IGitStatusCache GitStatusCache { get; }
2324
IGitLocksCache GitLocksCache { get; }
2425
IGitUserCache GitUserCache { get; }
26+
IRepositoryInfoCache RepositoryInfoCache { get; }
2527
void Validate(CacheType cacheType);
2628
void ValidateAll();
2729
void Invalidate(CacheType cacheType);
@@ -72,8 +74,6 @@ public interface IConfigRemoteDictionary : IDictionary<string, ConfigRemote>
7274

7375
public interface IBranchCache : IManagedCache
7476
{
75-
GitRemote? CurrentGitRemote { get; set; }
76-
GitBranch? CurentGitBranch { get; set; }
7777
ConfigRemote? CurrentConfigRemote { get; set; }
7878
ConfigBranch? CurentConfigBranch { get; set; }
7979

@@ -93,6 +93,12 @@ public interface IBranchCache : IManagedCache
9393
void SetLocals(IDictionary<string, ConfigBranch> branchDictionary);
9494
}
9595

96+
public interface IRepositoryInfoCache : IManagedCache
97+
{
98+
GitRemote? CurrentGitRemote { get; set; }
99+
GitBranch? CurentGitBranch { get; set; }
100+
}
101+
96102
public interface IGitLogCache : IManagedCache
97103
{
98104
List<GitLogEntry> Log { get; set; }

src/GitHub.Api/Git/IRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public interface IRepository : IEquatable<IRepository>
1919
ITask RequestLock(string file);
2020
ITask ReleaseLock(string file, bool force);
2121

22+
void CheckRepositoryInfoCacheEvent(CacheUpdateEvent cacheUpdateEvent);
2223
void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent);
2324
void CheckGitStatusCacheEvent(CacheUpdateEvent cacheUpdateEvent);
2425
void CheckGitLogCacheEvent(CacheUpdateEvent cacheUpdateEvent);
@@ -65,5 +66,6 @@ public interface IRepository : IEquatable<IRepository>
6566
event Action<CacheUpdateEvent> GitLogCacheUpdated;
6667
event Action<CacheUpdateEvent> GitLockCacheUpdated;
6768
event Action<CacheUpdateEvent> BranchCacheUpdated;
69+
event Action<CacheUpdateEvent> RepositoryInfoCacheUpdated;
6870
}
6971
}

src/GitHub.Api/Git/Repository.cs

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Repository : IEquatable<Repository>, IRepository
1818
public event Action<CacheUpdateEvent> GitLogCacheUpdated;
1919
public event Action<CacheUpdateEvent> GitLockCacheUpdated;
2020
public event Action<CacheUpdateEvent> BranchCacheUpdated;
21+
public event Action<CacheUpdateEvent> RepositoryInfoCacheUpdated;
2122

2223
/// <summary>
2324
/// Initializes a new instance of the <see cref="Repository"/> class.
@@ -88,6 +89,10 @@ private void CacheContainer_OnCacheUpdated(CacheType cacheType, DateTimeOffset o
8889
case CacheType.GitUserCache:
8990
break;
9091

92+
case CacheType.RepositoryInfoCache:
93+
FireRepositoryInfoCacheUpdated(offset);
94+
break;
95+
9196
default:
9297
throw new ArgumentOutOfRangeException(nameof(cacheType), cacheType, null);
9398
}
@@ -117,6 +122,12 @@ private void FireGitLocksCacheUpdated(DateTimeOffset dateTimeOffset)
117122
GitLockCacheUpdated?.Invoke(new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() });
118123
}
119124

125+
private void FireRepositoryInfoCacheUpdated(DateTimeOffset dateTimeOffset)
126+
{
127+
Logger.Trace("RepositoryInfoCacheUpdated {0}", dateTimeOffset);
128+
RepositoryInfoCacheUpdated?.Invoke(new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() });
129+
}
130+
120131
public void Initialize(IRepositoryManager initRepositoryManager)
121132
{
122133
Logger.Trace("Initialize");
@@ -138,8 +149,7 @@ public void Initialize(IRepositoryManager initRepositoryManager)
138149
UpdateGitStatus();
139150
UpdateGitLog();
140151

141-
new ActionTask(CancellationToken.None, UpdateLocks)
142-
{ Affinity = TaskAffinity.UI }.Start();
152+
new ActionTask(CancellationToken.None, UpdateLocks) { Affinity = TaskAffinity.UI }.Start();
143153
}
144154

145155
public ITask SetupRemote(string remote, string remoteUrl)
@@ -196,15 +206,27 @@ public ITask ReleaseLock(string file, bool force)
196206
return repositoryManager.UnlockFile(file, force).Then(UpdateLocks);
197207
}
198208

209+
public void CheckRepositoryInfoCacheEvent(CacheUpdateEvent cacheUpdateEvent)
210+
{
211+
var managedCache = cacheContainer.RepositoryInfoCache;
212+
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
213+
214+
Logger.Trace("CheckRepositoryInfoCacheEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
215+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
216+
217+
if (raiseEvent)
218+
{
219+
FireBranchCacheUpdated(managedCache.LastUpdatedAt);
220+
}
221+
}
222+
199223
public void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
200224
{
201225
var managedCache = cacheContainer.BranchCache;
202226
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
203227

204-
Logger.Trace("CheckBranchCacheEvent Current:{0} Check:{1} Result:{2}",
205-
managedCache.LastUpdatedAt,
206-
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]",
207-
raiseEvent);
228+
Logger.Trace("CheckBranchCacheEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
229+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
208230

209231
if (raiseEvent)
210232
{
@@ -217,10 +239,8 @@ public void CheckGitStatusCacheEvent(CacheUpdateEvent cacheUpdateEvent)
217239
var managedCache = cacheContainer.GitStatusCache;
218240
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
219241

220-
Logger.Trace("CheckGitStatusCacheEvent Current:{0} Check:{1} Result:{2}",
221-
managedCache.LastUpdatedAt,
222-
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]",
223-
raiseEvent);
242+
Logger.Trace("CheckGitStatusCacheEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
243+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
224244

225245
if (raiseEvent)
226246
{
@@ -233,10 +253,8 @@ public void CheckGitLogCacheEvent(CacheUpdateEvent cacheUpdateEvent)
233253
var managedCache = cacheContainer.GitLogCache;
234254
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
235255

236-
Logger.Trace("CheckGitLogCacheEvent Current:{0} Check:{1} Result:{2}",
237-
managedCache.LastUpdatedAt,
238-
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]",
239-
raiseEvent);
256+
Logger.Trace("CheckGitLogCacheEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
257+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
240258

241259
if (raiseEvent)
242260
{
@@ -249,10 +267,8 @@ public void CheckGitLocksCacheEvent(CacheUpdateEvent cacheUpdateEvent)
249267
var managedCache = cacheContainer.GitLocksCache;
250268
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
251269

252-
Logger.Trace("CheckGitLocksCacheEvent Current:{0} Check:{1} Result:{2}",
253-
managedCache.LastUpdatedAt,
254-
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]",
255-
raiseEvent);
270+
Logger.Trace("CheckGitLocksCacheEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
271+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
256272

257273
if (raiseEvent)
258274
{
@@ -288,6 +304,7 @@ public override bool Equals(object obj)
288304
{
289305
if (ReferenceEquals(this, obj))
290306
return true;
307+
291308
var other = obj as Repository;
292309
return Equals(other);
293310
}
@@ -301,8 +318,8 @@ public bool Equals(IRepository other)
301318
{
302319
if (ReferenceEquals(this, other))
303320
return true;
304-
return other != null &&
305-
object.Equals(LocalPath, other.LocalPath);
321+
322+
return other != null && object.Equals(LocalPath, other.LocalPath);
306323
}
307324

308325
private void RepositoryManager_OnCurrentRemoteUpdated(ConfigRemote? remote)
@@ -313,7 +330,7 @@ private void RepositoryManager_OnCurrentRemoteUpdated(ConfigRemote? remote)
313330
CurrentConfigRemote = remote;
314331
CurrentRemote = GetGitRemote(remote.Value);
315332
UpdateRepositoryInfo();
316-
}) {Affinity = TaskAffinity.UI}.Start();
333+
}) { Affinity = TaskAffinity.UI }.Start();
317334
}
318335
}
319336

@@ -346,17 +363,13 @@ private void RepositoryManager_OnCurrentBranchUpdated(ConfigBranch? branch)
346363
{
347364
if (!Nullable.Equals(CurrentConfigBranch, branch))
348365
{
349-
new ActionTask(CancellationToken.None, () =>
350-
{
351-
var currentBranch = branch != null
352-
? (GitBranch?)GetLocalGitBranch(branch.Value)
353-
: null;
366+
new ActionTask(CancellationToken.None, () => {
367+
var currentBranch = branch != null ? (GitBranch?)GetLocalGitBranch(branch.Value) : null;
354368

355369
CurrentConfigBranch = branch;
356370
CurrentBranch = currentBranch;
357371
UpdateLocalBranches();
358-
})
359-
{ Affinity = TaskAffinity.UI }.Start();
372+
}) { Affinity = TaskAffinity.UI }.Start();
360373
}
361374
}
362375

@@ -369,41 +382,36 @@ private void RepositoryManager_OnLocalBranchUpdated(string name)
369382
}
370383
}
371384

372-
private void RepositoryManager_OnRemoteBranchListUpdated(IDictionary<string, ConfigRemote> remotes, IDictionary<string, IDictionary<string, ConfigBranch>> branches)
385+
private void RepositoryManager_OnRemoteBranchListUpdated(IDictionary<string, ConfigRemote> remotes,
386+
IDictionary<string, IDictionary<string, ConfigBranch>> branches)
373387
{
374-
new ActionTask(CancellationToken.None, () =>
375-
{
388+
new ActionTask(CancellationToken.None, () => {
376389
cacheContainer.BranchCache.SetRemotes(remotes, branches);
377390
UpdateRemoteAndRemoteBranches();
378-
})
379-
{ Affinity = TaskAffinity.UI }.Start();
391+
}) { Affinity = TaskAffinity.UI }.Start();
380392
}
381393

382394
private void UpdateRemoteAndRemoteBranches()
383395
{
384396
cacheContainer.BranchCache.Remotes =
385-
cacheContainer.BranchCache.ConfigRemotes.Values
386-
.Select(GetGitRemote)
387-
.ToArray();
388-
389-
cacheContainer.BranchCache.RemoteBranches =
390-
cacheContainer.BranchCache.RemoteConfigBranches.Values
391-
.SelectMany(x => x.Values).Select(GetRemoteGitBranch)
392-
.ToArray();
397+
cacheContainer.BranchCache.ConfigRemotes.Values.Select(GetGitRemote).ToArray();
398+
399+
cacheContainer.BranchCache.RemoteBranches = cacheContainer
400+
.BranchCache.RemoteConfigBranches.Values.SelectMany(x => x.Values).Select(GetRemoteGitBranch).ToArray();
393401
}
394402

395403
private void RepositoryManager_OnLocalBranchListUpdated(IDictionary<string, ConfigBranch> branches)
396404
{
397405
new ActionTask(CancellationToken.None, () => {
398-
cacheContainer.BranchCache.SetLocals(branches);
399-
UpdateLocalBranches();
400-
})
401-
{ Affinity = TaskAffinity.UI }.Start();
406+
cacheContainer.BranchCache.SetLocals(branches);
407+
UpdateLocalBranches();
408+
}) { Affinity = TaskAffinity.UI }.Start();
402409
}
403410

404411
private void UpdateLocalBranches()
405412
{
406-
cacheContainer.BranchCache.LocalBranches = cacheContainer.BranchCache.LocalConfigBranches.Values.Select(GetLocalGitBranch).ToArray();
413+
cacheContainer.BranchCache.LocalBranches = cacheContainer
414+
.BranchCache.LocalConfigBranches.Values.Select(GetLocalGitBranch).ToArray();
407415
}
408416

409417
private void UpdateRepositoryInfo()
@@ -424,42 +432,34 @@ private void UpdateRepositoryInfo()
424432

425433
private void RepositoryManager_OnLocalBranchRemoved(string name)
426434
{
427-
new ActionTask(CancellationToken.None, () =>
428-
{
435+
new ActionTask(CancellationToken.None, () => {
429436
cacheContainer.BranchCache.RemoveLocalBranch(name);
430437
UpdateLocalBranches();
431-
})
432-
{ Affinity = TaskAffinity.UI }.Start();
438+
}) { Affinity = TaskAffinity.UI }.Start();
433439
}
434440

435441
private void RepositoryManager_OnLocalBranchAdded(string name)
436442
{
437-
new ActionTask(CancellationToken.None, () =>
438-
{
443+
new ActionTask(CancellationToken.None, () => {
439444
cacheContainer.BranchCache.AddLocalBranch(name);
440445
UpdateLocalBranches();
441-
})
442-
{ Affinity = TaskAffinity.UI }.Start();
446+
}) { Affinity = TaskAffinity.UI }.Start();
443447
}
444448

445449
private void RepositoryManager_OnRemoteBranchAdded(string remote, string name)
446450
{
447-
new ActionTask(CancellationToken.None, () =>
448-
{
451+
new ActionTask(CancellationToken.None, () => {
449452
cacheContainer.BranchCache.AddRemoteBranch(remote, name);
450453
UpdateRemoteAndRemoteBranches();
451-
})
452-
{ Affinity = TaskAffinity.UI }.Start();
454+
}) { Affinity = TaskAffinity.UI }.Start();
453455
}
454456

455457
private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
456458
{
457-
new ActionTask(CancellationToken.None, () =>
458-
{
459+
new ActionTask(CancellationToken.None, () => {
459460
cacheContainer.BranchCache.RemoveRemoteBranch(remote, name);
460461
UpdateRemoteAndRemoteBranches();
461-
})
462-
{ Affinity = TaskAffinity.UI }.Start();
462+
}) { Affinity = TaskAffinity.UI }.Start();
463463
}
464464

465465
private GitBranch GetLocalGitBranch(ConfigBranch x)
@@ -468,14 +468,14 @@ private GitBranch GetLocalGitBranch(ConfigBranch x)
468468
var trackingName = x.IsTracking ? x.Remote.Value.Name + "/" + name : "[None]";
469469
var isActive = name == CurrentBranchName;
470470

471-
return new GitBranch {Name= name, Tracking = trackingName, IsActive = isActive};
471+
return new GitBranch { Name = name, Tracking = trackingName, IsActive = isActive };
472472
}
473473

474474
private static GitBranch GetRemoteGitBranch(ConfigBranch x)
475475
{
476476
var name = x.Remote.Value.Name + "/" + x.Name;
477477

478-
return new GitBranch {Name= name};
478+
return new GitBranch { Name = name };
479479
}
480480

481481
private static GitRemote GetGitRemote(ConfigRemote configRemote)
@@ -509,16 +509,16 @@ public GitStatus CurrentStatus
509509

510510
public GitBranch? CurrentBranch
511511
{
512-
get { return cacheContainer.BranchCache.CurentGitBranch; }
513-
set { cacheContainer.BranchCache.CurentGitBranch = value; }
512+
get { return cacheContainer.RepositoryInfoCache.CurentGitBranch; }
513+
set { cacheContainer.RepositoryInfoCache.CurentGitBranch = value; }
514514
}
515515

516516
public string CurrentBranchName => CurrentConfigBranch?.Name;
517517

518518
public GitRemote? CurrentRemote
519519
{
520-
get { return cacheContainer.BranchCache.CurrentGitRemote; }
521-
set { cacheContainer.BranchCache.CurrentGitRemote = value; }
520+
get { return cacheContainer.RepositoryInfoCache.CurrentGitRemote; }
521+
set { cacheContainer.RepositoryInfoCache.CurrentGitRemote = value; }
522522
}
523523

524524
public List<GitLogEntry> CurrentLog
@@ -541,18 +541,14 @@ public List<GitLock> CurrentLocks
541541

542542
public string Owner => CloneUrl?.Owner ?? null;
543543

544-
public bool IsGitHub { get { return HostAddress.IsGitHubDotCom(CloneUrl); } }
544+
public bool IsGitHub
545+
{
546+
get { return HostAddress.IsGitHubDotCom(CloneUrl); }
547+
}
545548

546-
internal string DebuggerDisplay => String.Format(
547-
CultureInfo.InvariantCulture,
548-
"{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}",
549-
GetHashCode(),
550-
Owner,
551-
Name,
552-
CloneUrl,
553-
LocalPath,
554-
CurrentBranch,
555-
CurrentRemote);
549+
internal string DebuggerDisplay => String.Format(CultureInfo.InvariantCulture,
550+
"{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}", GetHashCode(), Owner, Name,
551+
CloneUrl, LocalPath, CurrentBranch, CurrentRemote);
556552

557553
public IUser User { get; set; }
558554

@@ -582,4 +578,4 @@ public struct CacheUpdateEvent
582578
{
583579
public string UpdatedTimeString;
584580
}
585-
}
581+
}

0 commit comments

Comments
 (0)