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

Commit b5351bd

Browse files
Exposing CurrentUser in GitClient
1 parent 7e0f82f commit b5351bd

File tree

14 files changed

+210
-153
lines changed

14 files changed

+210
-153
lines changed

src/GitHub.Api/Cache/CacheInterfaces.cs

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

5050
public interface IGitUserCache : IManagedCache
5151
{
52-
User User { get; }
52+
User User { get; set; }
5353
}
5454

5555
public interface IGitStatusCache : IManagedCache

src/GitHub.Api/Git/GitClient.cs

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace GitHub.Unity
88
{
99
public interface IGitClient
1010
{
11+
event Action<CacheUpdateEvent> CurrentUserChanged;
12+
1113
Task<NPath> FindGitInstallation();
1214
ITask<ValidateGitInstallResult> ValidateGitInstall(NPath path);
1315

@@ -23,8 +25,6 @@ ITask<string> GetConfig(string key, GitConfigSource configSource,
2325
ITask<string> SetConfig(string key, string value, GitConfigSource configSource,
2426
IOutputProcessor<string> processor = null);
2527

26-
ITask<User> GetConfigUserAndEmail();
27-
2828
ITask<List<GitLock>> ListLocks(bool local,
2929
BaseOutputListProcessor<GitLock> processor = null);
3030

@@ -84,7 +84,11 @@ 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+
void SetConfigUserAndEmail(string username, string email);
88+
89+
void CheckUserChangedEvent(CacheUpdateEvent gitLogCacheUpdateEvent);
90+
91+
User CurrentUser { get; }
8892
}
8993

9094
class GitClient : IGitClient
@@ -96,14 +100,69 @@ class GitClient : IGitClient
96100
private readonly ITaskManager taskManager;
97101
private readonly CancellationToken cancellationToken;
98102

103+
public event Action<CacheUpdateEvent> CurrentUserChanged;
104+
private bool cacheInitialized = false;
105+
99106
public GitClient(IEnvironment environment, IProcessManager processManager, ITaskManager taskManager)
100107
{
108+
Logger.Trace("Constructed");
109+
101110
this.environment = environment;
102111
this.processManager = processManager;
103112
this.taskManager = taskManager;
104113
this.cancellationToken = taskManager.Token;
105114
}
106115

116+
private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset)
117+
{
118+
HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent
119+
{
120+
UpdatedTimeString = timeOffset.ToString()
121+
});
122+
}
123+
124+
private void GitUserCacheOnCacheInvalidated()
125+
{
126+
127+
}
128+
129+
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent)
130+
{
131+
var managedCache = environment.CacheContainer.GitUserCache;
132+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
133+
134+
Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
135+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
136+
137+
if (raiseEvent)
138+
{
139+
var dateTimeOffset = managedCache.LastUpdatedAt;
140+
var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() };
141+
HandleGitLogCacheUpdatedEvent(updateEvent);
142+
}
143+
}
144+
private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
145+
{
146+
Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString);
147+
CurrentUserChanged?.Invoke(cacheUpdateEvent);
148+
}
149+
150+
public User CurrentUser
151+
{
152+
get
153+
{
154+
if (!cacheInitialized)
155+
{
156+
cacheInitialized = true;
157+
environment.CacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated;
158+
environment.CacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated;
159+
}
160+
161+
return environment.CacheContainer.GitUserCache.User;
162+
}
163+
private set { environment.CacheContainer.GitUserCache.User = value; }
164+
}
165+
107166
public async Task<NPath> FindGitInstallation()
108167
{
109168
if (!String.IsNullOrEmpty(environment.GitExecutablePath))
@@ -259,12 +318,12 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
259318
.Configure(processManager);
260319
}
261320

262-
public ITask<User> GetConfigUserAndEmail()
321+
private void UpdateUserAndEmail()
263322
{
264323
string username = null;
265324
string email = null;
266325

267-
return GetConfig(UserNameConfigKey, GitConfigSource.User)
326+
GetConfig(UserNameConfigKey, GitConfigSource.User)
268327
.Then((success, value) => {
269328
if (success)
270329
{
@@ -277,17 +336,20 @@ public ITask<User> GetConfigUserAndEmail()
277336
{
278337
email = value;
279338
}
280-
})).Then(success => {
281-
Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email);
282-
return new User { Name = username, Email = email };
283-
});
339+
})).ThenInUI(success => {
340+
environment.CacheContainer.GitUserCache.User= new User {
341+
Name = username,
342+
Email = email
343+
};
344+
}).Start();
284345
}
285346

286-
public ITask<User> SetConfigUserAndEmail(string username, string email)
347+
public void SetConfigUserAndEmail(string username, string email)
287348
{
288-
return SetConfig(UserNameConfigKey, username, GitConfigSource.User)
349+
SetConfig(UserNameConfigKey, username, GitConfigSource.User)
289350
.Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User))
290-
.Then(b => new User { Name = username, Email = email });
351+
.Then(UpdateUserAndEmail)
352+
.Start();
291353
}
292354

293355
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)

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 ShouldRaiseCacheEvent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent)
8+
{
9+
bool raiseEvent;
10+
if (cacheUpdateEvent.UpdatedTimeString == null)
11+
{
12+
raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue;
13+
}
14+
else
15+
{
16+
raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString;
17+
}
18+
return raiseEvent;
19+
}
20+
}
21+
}

src/GitHub.Api/Git/Repository.cs

Lines changed: 5 additions & 23 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();
@@ -145,7 +143,7 @@ public void RefreshStatus()
145143
public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
146144
{
147145
var managedCache = cacheContainer.GitLogCache;
148-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
146+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
149147

150148
Logger.Trace("Check GitLogCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
151149
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -161,7 +159,7 @@ public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
161159
public void CheckStatusChangedEvent(CacheUpdateEvent cacheUpdateEvent)
162160
{
163161
var managedCache = cacheContainer.GitStatusCache;
164-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
162+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
165163

166164
Logger.Trace("Check GitStatusCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
167165
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -192,7 +190,7 @@ public void CheckCurrentBranchAndRemoteChangedEvent(CacheUpdateEvent cacheUpdate
192190
private void CheckRepositoryInfoCacheEvent(CacheUpdateEvent cacheUpdateEvent)
193191
{
194192
var managedCache = cacheContainer.RepositoryInfoCache;
195-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
193+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
196194

197195
Logger.Trace("Check RepositoryInfoCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
198196
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -209,7 +207,7 @@ public void CheckLocksChangedEvent(CacheUpdateEvent cacheUpdateEvent)
209207
{
210208
CacheUpdateEvent cacheUpdateEvent1 = cacheUpdateEvent;
211209
var managedCache = cacheContainer.GitLocksCache;
212-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent1, managedCache);
210+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent1);
213211

214212
Logger.Trace("Check GitLocksCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
215213
cacheUpdateEvent1.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -272,7 +270,7 @@ public bool Equals(IRepository other)
272270
private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
273271
{
274272
var managedCache = cacheContainer.BranchCache;
275-
var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache);
273+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
276274

277275
Logger.Trace("Check BranchCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
278276
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
@@ -285,20 +283,6 @@ private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
285283
}
286284
}
287285

288-
private static bool ShouldRaiseCacheEvent(CacheUpdateEvent cacheUpdateEvent, IManagedCache managedCache)
289-
{
290-
bool raiseEvent;
291-
if (cacheUpdateEvent.UpdatedTimeString == null)
292-
{
293-
raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue;
294-
}
295-
else
296-
{
297-
raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString;
298-
}
299-
return raiseEvent;
300-
}
301-
302286
private void CacheContainer_OnCacheInvalidated(CacheType cacheType)
303287
{
304288
switch (cacheType)
@@ -671,8 +655,6 @@ public bool IsGitHub
671655
"{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}", GetHashCode(), Owner, Name,
672656
CloneUrl, LocalPath, CurrentBranch, CurrentRemote);
673657

674-
public IUser User { get; set; }
675-
676658
protected static ILogging Logger { get; } = Logging.GetLogger<Repository>();
677659
}
678660

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;
@@ -150,7 +148,6 @@ public void Start()
150148
Logger.Trace("Start");
151149

152150
UpdateConfigData();
153-
LoadGitUser();
154151
watcher.Start();
155152
}
156153

@@ -298,15 +295,6 @@ public ITask UnlockFile(string file, bool force)
298295
return HookupHandlers(task);
299296
}
300297

301-
private void LoadGitUser()
302-
{
303-
GitClient.GetConfigUserAndEmail()
304-
.Then((success, user) => {
305-
Logger.Trace("OnGitUserLoaded: {0}", user);
306-
OnGitUserLoaded?.Invoke(user);
307-
}).Start();
308-
}
309-
310298
private void SetupWatcher()
311299
{
312300
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" />

src/GitHub.Api/Platform/DefaultEnvironment.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace GitHub.Unity
77
public class DefaultEnvironment : IEnvironment
88
{
99
private const string logFile = "github-unity.log";
10-
private ICacheContainer cacheContainer;
1110

1211
public NPath LogPath { get; }
1312
public DefaultEnvironment()
@@ -39,7 +38,7 @@ public DefaultEnvironment()
3938
public DefaultEnvironment(ICacheContainer cacheContainer)
4039
: this()
4140
{
42-
this.cacheContainer = cacheContainer;
41+
this.CacheContainer = cacheContainer;
4342
}
4443

4544
public void Initialize(string unityVersion, NPath extensionInstallPath, NPath unityPath, NPath assetsPath)
@@ -86,7 +85,7 @@ public void InitializeRepository(NPath expectedRepositoryPath = null)
8685
{
8786
Logger.Trace("Determined expectedRepositoryPath:{0}", expectedRepositoryPath);
8887
RepositoryPath = expectedRepositoryPath;
89-
Repository = new Repository(RepositoryPath, cacheContainer);
88+
Repository = new Repository(RepositoryPath, CacheContainer);
9089
}
9190
}
9291

@@ -133,6 +132,7 @@ public NPath GitExecutablePath
133132
public NPath GitInstallPath { get; private set; }
134133

135134
public NPath RepositoryPath { get; private set; }
135+
public ICacheContainer CacheContainer { get; private set; }
136136
public IRepository Repository { get; set; }
137137

138138
public bool IsWindows { get { return OnWindows; } }

src/GitHub.Api/Platform/IEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ public interface IEnvironment
2929
IFileSystem FileSystem { get; set; }
3030
IRepository Repository { get; set; }
3131
string ExecutableExtension { get; }
32+
ICacheContainer CacheContainer { get; }
3233
}
3334
}

0 commit comments

Comments
 (0)