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

Commit 2585ec6

Browse files
Merge branch 'enhancements/repository-watcher-refactor-rollup' into enhancements/repository-watcher-refactor
2 parents 62e45fb + 8c37aff commit 2585ec6

File tree

14 files changed

+218
-150
lines changed

14 files changed

+218
-150
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; set; }
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: 47 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace GitHub.Unity
88
{
99
public interface IGitClient
1010
{
11-
event Action<CacheUpdateEvent> CurrentUserChanged;
12-
1311
Task<NPath> FindGitInstallation();
1412
ITask<ValidateGitInstallResult> ValidateGitInstall(NPath path);
1513

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

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

@@ -84,11 +84,7 @@ ITask<string> Unlock(string file, bool force,
8484

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

87-
void SetConfigUserAndEmail(string username, string email);
88-
89-
void CheckUserChangedEvent(CacheUpdateEvent gitLogCacheUpdateEvent);
90-
91-
User CurrentUser { get; }
87+
ITask<GitUser> SetConfigNameAndEmail(string username, string email);
9288
}
9389

9490
class GitClient : IGitClient
@@ -100,51 +96,14 @@ class GitClient : IGitClient
10096
private readonly ITaskManager taskManager;
10197
private readonly CancellationToken cancellationToken;
10298

103-
public event Action<CacheUpdateEvent> CurrentUserChanged;
104-
private bool cacheInitialized;
105-
10699
public GitClient(IEnvironment environment, IProcessManager processManager, ITaskManager taskManager)
107100
{
108-
Logger.Trace("Constructed");
109-
110101
this.environment = environment;
111102
this.processManager = processManager;
112103
this.taskManager = taskManager;
113104
this.cancellationToken = taskManager.Token;
114105
}
115106

116-
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent)
117-
{
118-
var managedCache = environment.CacheContainer.GitUserCache;
119-
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
120-
121-
Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
122-
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
123-
124-
if (raiseEvent)
125-
{
126-
var dateTimeOffset = managedCache.LastUpdatedAt;
127-
var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() };
128-
HandleGitLogCacheUpdatedEvent(updateEvent);
129-
}
130-
}
131-
132-
public User CurrentUser
133-
{
134-
get
135-
{
136-
if (!cacheInitialized)
137-
{
138-
cacheInitialized = true;
139-
environment.CacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated;
140-
environment.CacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated;
141-
}
142-
143-
return environment.CacheContainer.GitUserCache.User;
144-
}
145-
private set { environment.CacheContainer.GitUserCache.User = value; }
146-
}
147-
148107
public async Task<NPath> FindGitInstallation()
149108
{
150109
if (!String.IsNullOrEmpty(environment.GitExecutablePath))
@@ -300,12 +259,35 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
300259
.Configure(processManager);
301260
}
302261

303-
public void SetConfigUserAndEmail(string username, string email)
262+
public ITask<GitUser> GetConfigUserAndEmail()
263+
{
264+
string username = null;
265+
string email = null;
266+
267+
return GetConfig(UserNameConfigKey, GitConfigSource.User)
268+
.Then((success, value) => {
269+
if (success)
270+
{
271+
username = value;
272+
}
273+
})
274+
.Then(GetConfig(UserEmailConfigKey, GitConfigSource.User)
275+
.Then((success, value) => {
276+
if (success)
277+
{
278+
email = value;
279+
}
280+
})).Then(success => {
281+
Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email);
282+
return new GitUser(username, email);
283+
});
284+
}
285+
286+
public ITask<GitUser> SetConfigNameAndEmail(string username, string email)
304287
{
305-
SetConfig(UserNameConfigKey, username, GitConfigSource.User)
288+
return SetConfig(UserNameConfigKey, username, GitConfigSource.User)
306289
.Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User))
307-
.Then(UpdateUserAndEmail)
308-
.Start();
290+
.Then(b => new GitUser(username, email));
309291
}
310292

311293
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)
@@ -480,54 +462,28 @@ public ITask<string> Unlock(string file, bool force,
480462
.Configure(processManager);
481463
}
482464

483-
private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset)
484-
{
485-
HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent
486-
{
487-
UpdatedTimeString = timeOffset.ToString()
488-
});
489-
}
465+
protected static ILogging Logger { get; } = Logging.GetLogger<GitClient>();
466+
}
490467

491-
private void GitUserCacheOnCacheInvalidated()
492-
{
493-
Logger.Trace("GitUserCache Invalidated");
494-
UpdateUserAndEmail();
495-
}
496-
497-
private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
498-
{
499-
Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString);
500-
CurrentUserChanged?.Invoke(cacheUpdateEvent);
501-
}
468+
public struct GitUser
469+
{
470+
public static GitUser Default = new GitUser();
502471

503-
private void UpdateUserAndEmail()
504-
{
505-
Logger.Trace("UpdateUserAndEmail");
472+
public string name;
473+
public string email;
506474

507-
string username = null;
508-
string email = null;
475+
public string Name { get { return name; } }
476+
public string Email { get { return email; } }
509477

510-
GetConfig(UserNameConfigKey, GitConfigSource.User)
511-
.Then((success, value) => {
512-
if (success)
513-
{
514-
username = value;
515-
}
516-
})
517-
.Then(GetConfig(UserEmailConfigKey, GitConfigSource.User)
518-
.Then((success, value) => {
519-
if (success)
520-
{
521-
email = value;
522-
}
523-
})).ThenInUI(success => {
524-
CurrentUser = new User {
525-
Name = username,
526-
Email = email
527-
};
528-
}).Start();
478+
public GitUser(string name, string email)
479+
{
480+
this.name = name;
481+
this.email = email;
529482
}
530483

531-
protected static ILogging Logger { get; } = Logging.GetLogger<GitClient>();
484+
public override string ToString()
485+
{
486+
return $"Name:\"{Name}\" Email:\"{Email}\"";
487+
}
532488
}
533489
}

src/GitHub.Api/Git/ManagedCacheExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ namespace GitHub.Unity
44
{
55
static class ManagedCacheExtensions
66
{
7-
public static bool ShouldRaiseCacheEvent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent)
7+
public static bool IsLastUpdatedTimeDifferent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent)
88
{
9-
bool raiseEvent;
9+
bool isDifferent;
1010
if (cacheUpdateEvent.UpdatedTimeString == null)
1111
{
12-
raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue;
12+
isDifferent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue;
1313
}
1414
else
1515
{
16-
raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString;
16+
isDifferent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString;
1717
}
18-
return raiseEvent;
18+
return isDifferent;
1919
}
2020
}
2121
}

0 commit comments

Comments
 (0)