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

Commit 61771b0

Browse files
Merge pull request #427 from github-for-unity/enhancements/git-client-cache
Utilizing a cache for user data
2 parents ede34b5 + 1db3448 commit 61771b0

File tree

18 files changed

+294
-148
lines changed

18 files changed

+294
-148
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private async Task SetupGit()
7878
}
7979
}
8080

81+
Environment.User.Initialize(GitClient);
8182
}
8283

8384
public ITask InitializeRepository()

src/GitHub.Api/Cache/CacheInterfaces.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public interface IGitLocksCache : IManagedCache
4949

5050
public interface IGitUserCache : IManagedCache
5151
{
52-
User User { get; }
52+
string Name { get; set; }
53+
string Email { get; set; }
5354
}
5455

5556
public interface IGitStatusCache : IManagedCache

src/GitHub.Api/Git/GitClient.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ITask<string> GetConfig(string key, GitConfigSource configSource,
2323
ITask<string> SetConfig(string key, string value, GitConfigSource configSource,
2424
IOutputProcessor<string> processor = null);
2525

26-
ITask<User> GetConfigUserAndEmail();
26+
ITask<GitUser> GetConfigUserAndEmail();
2727

2828
ITask<List<GitLock>> ListLocks(bool local,
2929
BaseOutputListProcessor<GitLock> processor = null);
@@ -84,7 +84,7 @@ ITask<string> Unlock(string file, bool force,
8484

8585
ITask<Version> LfsVersion(IOutputProcessor<Version> processor = null);
8686

87-
ITask<User> SetConfigUserAndEmail(string username, string email);
87+
ITask<GitUser> SetConfigNameAndEmail(string username, string email);
8888
}
8989

9090
class GitClient : IGitClient
@@ -259,7 +259,7 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
259259
.Configure(processManager);
260260
}
261261

262-
public ITask<User> GetConfigUserAndEmail()
262+
public ITask<GitUser> GetConfigUserAndEmail()
263263
{
264264
string username = null;
265265
string email = null;
@@ -279,15 +279,15 @@ public ITask<User> GetConfigUserAndEmail()
279279
}
280280
})).Then(success => {
281281
Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email);
282-
return new User { Name = username, Email = email };
282+
return new GitUser(username, email);
283283
});
284284
}
285285

286-
public ITask<User> SetConfigUserAndEmail(string username, string email)
286+
public ITask<GitUser> SetConfigNameAndEmail(string username, string email)
287287
{
288288
return SetConfig(UserNameConfigKey, username, GitConfigSource.User)
289289
.Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User))
290-
.Then(b => new User { Name = username, Email = email });
290+
.Then(b => new GitUser(username, email));
291291
}
292292

293293
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)
@@ -464,4 +464,26 @@ public ITask<string> Unlock(string file, bool force,
464464

465465
protected static ILogging Logger { get; } = Logging.GetLogger<GitClient>();
466466
}
467+
468+
public struct GitUser
469+
{
470+
public static GitUser Default = new GitUser();
471+
472+
public string name;
473+
public string email;
474+
475+
public string Name { get { return name; } }
476+
public string Email { get { return email; } }
477+
478+
public GitUser(string name, string email)
479+
{
480+
this.name = name;
481+
this.email = email;
482+
}
483+
484+
public override string ToString()
485+
{
486+
return $"Name:\"{Name}\" Email:\"{Email}\"";
487+
}
488+
}
467489
}

src/GitHub.Api/Git/IRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public interface IRepository : IEquatable<IRepository>
6464
GitRemote[] Remotes { get; }
6565
GitBranch[] LocalBranches { get; }
6666
GitBranch[] RemoteBranches { get; }
67-
IUser User { get; set; }
6867
List<GitLock> CurrentLocks { get; }
6968
string CurrentBranchName { get; }
7069
List<GitLogEntry> CurrentLog { get; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
static class ManagedCacheExtensions
6+
{
7+
public static bool IsLastUpdatedTimeDifferent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent)
8+
{
9+
bool isDifferent;
10+
if (cacheUpdateEvent.UpdatedTimeString == null)
11+
{
12+
isDifferent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue;
13+
}
14+
else
15+
{
16+
isDifferent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString;
17+
}
18+
return isDifferent;
19+
}
20+
}
21+
}

src/GitHub.Api/Git/Repository.cs

Lines changed: 113 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public Repository(NPath localPath, ICacheContainer container)
3535
Guard.ArgumentNotNull(localPath, nameof(localPath));
3636

3737
LocalPath = localPath;
38-
User = new User();
3938

4039
cacheContainer = container;
4140
cacheContainer.CacheInvalidated += CacheContainer_OnCacheInvalidated;
@@ -57,7 +56,6 @@ public void Initialize(IRepositoryManager initRepositoryManager)
5756
repositoryManager.OnLocalBranchRemoved += RepositoryManager_OnLocalBranchRemoved;
5857
repositoryManager.OnRemoteBranchAdded += RepositoryManager_OnRemoteBranchAdded;
5958
repositoryManager.OnRemoteBranchRemoved += RepositoryManager_OnRemoteBranchRemoved;
60-
repositoryManager.OnGitUserLoaded += user => User = user;
6159

6260
UpdateGitStatus();
6361
UpdateGitLog();
@@ -150,7 +148,7 @@ public void UpdateConfigData()
150148
public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
151149
{
152150
var managedCache = cacheContainer.GitLogCache;
153-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
151+
var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent);
154152

155153
Logger.Trace("Check GitLogCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
156154
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -166,7 +164,7 @@ public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
166164
public void CheckStatusChangedEvent(CacheUpdateEvent cacheUpdateEvent)
167165
{
168166
var managedCache = cacheContainer.GitStatusCache;
169-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
167+
var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent);
170168

171169
Logger.Trace("Check GitStatusCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
172170
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -197,7 +195,7 @@ public void CheckCurrentBranchAndRemoteChangedEvent(CacheUpdateEvent cacheUpdate
197195
private void CheckRepositoryInfoCacheEvent(CacheUpdateEvent cacheUpdateEvent)
198196
{
199197
var managedCache = cacheContainer.RepositoryInfoCache;
200-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
198+
var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent);
201199

202200
Logger.Trace("Check RepositoryInfoCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
203201
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -214,7 +212,7 @@ public void CheckLocksChangedEvent(CacheUpdateEvent cacheUpdateEvent)
214212
{
215213
CacheUpdateEvent cacheUpdateEvent1 = cacheUpdateEvent;
216214
var managedCache = cacheContainer.GitLocksCache;
217-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent1, managedCache);
215+
var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent1);
218216

219217
Logger.Trace("Check GitLocksCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
220218
cacheUpdateEvent1.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -277,7 +275,7 @@ public bool Equals(IRepository other)
277275
private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
278276
{
279277
var managedCache = cacheContainer.BranchCache;
280-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
278+
var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent);
281279

282280
Logger.Trace("Check BranchCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
283281
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -290,20 +288,6 @@ private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
290288
}
291289
}
292290

293-
private static bool ShouldRaiseCacheEvent(CacheUpdateEvent cacheUpdateEvent, IManagedCache managedCache)
294-
{
295-
bool raiseEvent;
296-
if (cacheUpdateEvent.UpdatedTimeString == null)
297-
{
298-
raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue;
299-
}
300-
else
301-
{
302-
raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString;
303-
}
304-
return raiseEvent;
305-
}
306-
307291
private void CacheContainer_OnCacheInvalidated(CacheType cacheType)
308292
{
309293
switch (cacheType)
@@ -669,27 +653,129 @@ public bool IsGitHub
669653
"{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}", GetHashCode(), Owner, Name,
670654
CloneUrl, LocalPath, CurrentBranch, CurrentRemote);
671655

672-
public IUser User { get; set; }
673-
674656
protected static ILogging Logger { get; } = Logging.GetLogger<Repository>();
675657
}
676658

677659
public interface IUser
678660
{
679-
string Name { get; set; }
680-
string Email { get; set; }
661+
string Name { get; }
662+
string Email { get; }
663+
event Action<CacheUpdateEvent> Changed;
664+
void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent);
665+
void Initialize(IGitClient client);
666+
void SetNameAndEmail(string name, string email);
681667
}
682668

683669
[Serializable]
684670
public class User : IUser
685671
{
672+
private ICacheContainer cacheContainer;
673+
private IGitClient gitClient;
674+
675+
public event Action<CacheUpdateEvent> Changed;
676+
677+
public User(ICacheContainer cacheContainer)
678+
{
679+
this.cacheContainer = cacheContainer;
680+
681+
cacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated;
682+
cacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated;
683+
}
684+
685+
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent)
686+
{
687+
var managedCache = cacheContainer.GitUserCache;
688+
var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent);
689+
690+
Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
691+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
692+
693+
if (raiseEvent)
694+
{
695+
var dateTimeOffset = managedCache.LastUpdatedAt;
696+
var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() };
697+
HandleUserCacheUpdatedEvent(updateEvent);
698+
}
699+
}
700+
701+
public void Initialize(IGitClient client)
702+
{
703+
Guard.ArgumentNotNull(client, nameof(client));
704+
705+
Logger.Trace("Initialize");
706+
707+
gitClient = client;
708+
UpdateUserAndEmail();
709+
}
710+
711+
public void SetNameAndEmail(string name, string email)
712+
{
713+
gitClient.SetConfigNameAndEmail(name, email)
714+
.ThenInUI((success, value) => {
715+
if (success)
716+
{
717+
Name = value.Name;
718+
Email = value.Email;
719+
}
720+
}).Start();
721+
}
722+
686723
public override string ToString()
687724
{
688725
return String.Format("Name: {0} Email: {1}", Name, Email);
689726
}
690727

691-
public string Name { get; set; }
692-
public string Email { get; set; }
728+
public string Name
729+
{
730+
get { return cacheContainer.GitUserCache.Name; }
731+
private set { cacheContainer.GitUserCache.Name = value; }
732+
}
733+
734+
public string Email
735+
{
736+
get { return cacheContainer.GitUserCache.Email; }
737+
private set { cacheContainer.GitUserCache.Email = value; }
738+
}
739+
740+
private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset)
741+
{
742+
HandleUserCacheUpdatedEvent(new CacheUpdateEvent
743+
{
744+
UpdatedTimeString = timeOffset.ToString()
745+
});
746+
}
747+
748+
private void GitUserCacheOnCacheInvalidated()
749+
{
750+
Logger.Trace("GitUserCache Invalidated");
751+
UpdateUserAndEmail();
752+
}
753+
754+
private void HandleUserCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
755+
{
756+
Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString);
757+
Changed?.Invoke(cacheUpdateEvent);
758+
}
759+
760+
private void UpdateUserAndEmail()
761+
{
762+
if (gitClient != null)
763+
{
764+
Logger.Trace("UpdateUserAndEmail");
765+
766+
gitClient.GetConfigUserAndEmail()
767+
.ThenInUI((success, value) =>
768+
{
769+
if (success)
770+
{
771+
Name = value.Name;
772+
Email = value.Email;
773+
}
774+
}).Start();
775+
}
776+
}
777+
778+
protected static ILogging Logger { get; } = Logging.GetLogger<User>();
693779
}
694780

695781
[Serializable]

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace GitHub.Unity
88
public interface IRepositoryManager : IDisposable
99
{
1010
event Action<ConfigBranch?, ConfigRemote?> OnCurrentBranchAndRemoteUpdated;
11-
event Action<IUser> OnGitUserLoaded;
1211
event Action<bool> OnIsBusyChanged;
1312
event Action<string> OnLocalBranchAdded;
1413
event Action<Dictionary<string, ConfigBranch>> OnLocalBranchListUpdated;
@@ -101,7 +100,6 @@ class RepositoryManager : IRepositoryManager
101100
private bool isBusy;
102101

103102
public event Action<ConfigBranch?, ConfigRemote?> OnCurrentBranchAndRemoteUpdated;
104-
public event Action<IUser> OnGitUserLoaded;
105103
public event Action<bool> OnIsBusyChanged;
106104
public event Action<string> OnLocalBranchAdded;
107105
public event Action<Dictionary<string, ConfigBranch>> OnLocalBranchListUpdated;
@@ -149,7 +147,6 @@ public void Start()
149147
Logger.Trace("Start");
150148

151149
UpdateConfigData();
152-
LoadGitUser();
153150
watcher.Start();
154151
}
155152

@@ -300,15 +297,6 @@ public void UpdateConfigData()
300297
UpdateConfigData(false);
301298
}
302299

303-
private void LoadGitUser()
304-
{
305-
GitClient.GetConfigUserAndEmail()
306-
.Then((success, user) => {
307-
Logger.Trace("OnGitUserLoaded: {0}", user);
308-
OnGitUserLoaded?.Invoke(user);
309-
}).Start();
310-
}
311-
312300
private void SetupWatcher()
313301
{
314302
watcher.HeadChanged += Watcher_OnHeadChanged;

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="Application\Organization.cs" />
104104
<Compile Include="Cache\CacheInterfaces.cs" />
105105
<Compile Include="Extensions\ListExtensions.cs" />
106+
<Compile Include="Git\ManagedCacheExtensions.cs" />
106107
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
107108
<Compile Include="Git\Tasks\GitVersionTask.cs" />
108109
<Compile Include="Git\ValidateGitInstallResult.cs" />

0 commit comments

Comments
 (0)