From b5351bd30e962af05e7db8ec09e6428d8fafcde4 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 13 Nov 2017 15:06:26 -0500 Subject: [PATCH 01/13] Exposing CurrentUser in GitClient --- src/GitHub.Api/Cache/CacheInterfaces.cs | 2 +- src/GitHub.Api/Git/GitClient.cs | 86 +++++++++++++--- src/GitHub.Api/Git/IRepository.cs | 1 - src/GitHub.Api/Git/ManagedCacheExtensions.cs | 21 ++++ src/GitHub.Api/Git/Repository.cs | 28 +----- src/GitHub.Api/Git/RepositoryManager.cs | 12 --- src/GitHub.Api/GitHub.Api.csproj | 1 + src/GitHub.Api/Platform/DefaultEnvironment.cs | 6 +- src/GitHub.Api/Platform/IEnvironment.cs | 1 + .../Editor/GitHub.Unity/ApplicationCache.cs | 31 +++--- .../Editor/GitHub.Unity/UI/InitProjectView.cs | 63 ++++++++---- .../GitHub.Unity/UI/UserSettingsView.cs | 97 ++++++++----------- .../Git/IntegrationTestEnvironment.cs | 5 + .../Events/IRepositoryManagerListener.cs | 9 -- 14 files changed, 210 insertions(+), 153 deletions(-) create mode 100644 src/GitHub.Api/Git/ManagedCacheExtensions.cs diff --git a/src/GitHub.Api/Cache/CacheInterfaces.cs b/src/GitHub.Api/Cache/CacheInterfaces.cs index a303520fa..95cfd811a 100644 --- a/src/GitHub.Api/Cache/CacheInterfaces.cs +++ b/src/GitHub.Api/Cache/CacheInterfaces.cs @@ -49,7 +49,7 @@ public interface IGitLocksCache : IManagedCache public interface IGitUserCache : IManagedCache { - User User { get; } + User User { get; set; } } public interface IGitStatusCache : IManagedCache diff --git a/src/GitHub.Api/Git/GitClient.cs b/src/GitHub.Api/Git/GitClient.cs index a9f5c7e4c..ca3918d77 100644 --- a/src/GitHub.Api/Git/GitClient.cs +++ b/src/GitHub.Api/Git/GitClient.cs @@ -8,6 +8,8 @@ namespace GitHub.Unity { public interface IGitClient { + event Action CurrentUserChanged; + Task FindGitInstallation(); ITask ValidateGitInstall(NPath path); @@ -23,8 +25,6 @@ ITask GetConfig(string key, GitConfigSource configSource, ITask SetConfig(string key, string value, GitConfigSource configSource, IOutputProcessor processor = null); - ITask GetConfigUserAndEmail(); - ITask> ListLocks(bool local, BaseOutputListProcessor processor = null); @@ -84,7 +84,11 @@ ITask Unlock(string file, bool force, ITask LfsVersion(IOutputProcessor processor = null); - ITask SetConfigUserAndEmail(string username, string email); + void SetConfigUserAndEmail(string username, string email); + + void CheckUserChangedEvent(CacheUpdateEvent gitLogCacheUpdateEvent); + + User CurrentUser { get; } } class GitClient : IGitClient @@ -96,14 +100,69 @@ class GitClient : IGitClient private readonly ITaskManager taskManager; private readonly CancellationToken cancellationToken; + public event Action CurrentUserChanged; + private bool cacheInitialized = false; + public GitClient(IEnvironment environment, IProcessManager processManager, ITaskManager taskManager) { + Logger.Trace("Constructed"); + this.environment = environment; this.processManager = processManager; this.taskManager = taskManager; this.cancellationToken = taskManager.Token; } + private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) + { + HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent + { + UpdatedTimeString = timeOffset.ToString() + }); + } + + private void GitUserCacheOnCacheInvalidated() + { + + } + + public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) + { + var managedCache = environment.CacheContainer.GitUserCache; + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + + Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, + cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); + + if (raiseEvent) + { + var dateTimeOffset = managedCache.LastUpdatedAt; + var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() }; + HandleGitLogCacheUpdatedEvent(updateEvent); + } + } + private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) + { + Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); + CurrentUserChanged?.Invoke(cacheUpdateEvent); + } + + public User CurrentUser + { + get + { + if (!cacheInitialized) + { + cacheInitialized = true; + environment.CacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated; + environment.CacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated; + } + + return environment.CacheContainer.GitUserCache.User; + } + private set { environment.CacheContainer.GitUserCache.User = value; } + } + public async Task FindGitInstallation() { if (!String.IsNullOrEmpty(environment.GitExecutablePath)) @@ -259,12 +318,12 @@ public ITask SetConfig(string key, string value, GitConfigSource configS .Configure(processManager); } - public ITask GetConfigUserAndEmail() + private void UpdateUserAndEmail() { string username = null; string email = null; - return GetConfig(UserNameConfigKey, GitConfigSource.User) + GetConfig(UserNameConfigKey, GitConfigSource.User) .Then((success, value) => { if (success) { @@ -277,17 +336,20 @@ public ITask GetConfigUserAndEmail() { email = value; } - })).Then(success => { - Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email); - return new User { Name = username, Email = email }; - }); + })).ThenInUI(success => { + environment.CacheContainer.GitUserCache.User= new User { + Name = username, + Email = email + }; + }).Start(); } - public ITask SetConfigUserAndEmail(string username, string email) + public void SetConfigUserAndEmail(string username, string email) { - return SetConfig(UserNameConfigKey, username, GitConfigSource.User) + SetConfig(UserNameConfigKey, username, GitConfigSource.User) .Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User)) - .Then(b => new User { Name = username, Email = email }); + .Then(UpdateUserAndEmail) + .Start(); } public ITask> ListLocks(bool local, BaseOutputListProcessor processor = null) diff --git a/src/GitHub.Api/Git/IRepository.cs b/src/GitHub.Api/Git/IRepository.cs index 8ed108a22..cede4be77 100644 --- a/src/GitHub.Api/Git/IRepository.cs +++ b/src/GitHub.Api/Git/IRepository.cs @@ -64,7 +64,6 @@ public interface IRepository : IEquatable GitRemote[] Remotes { get; } GitBranch[] LocalBranches { get; } GitBranch[] RemoteBranches { get; } - IUser User { get; set; } List CurrentLocks { get; } string CurrentBranchName { get; } List CurrentLog { get; } diff --git a/src/GitHub.Api/Git/ManagedCacheExtensions.cs b/src/GitHub.Api/Git/ManagedCacheExtensions.cs new file mode 100644 index 000000000..760f4ef53 --- /dev/null +++ b/src/GitHub.Api/Git/ManagedCacheExtensions.cs @@ -0,0 +1,21 @@ +using System; + +namespace GitHub.Unity +{ + static class ManagedCacheExtensions + { + public static bool ShouldRaiseCacheEvent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent) + { + bool raiseEvent; + if (cacheUpdateEvent.UpdatedTimeString == null) + { + raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue; + } + else + { + raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString; + } + return raiseEvent; + } + } +} \ No newline at end of file diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index daadaacda..8a3a9e33f 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -35,7 +35,6 @@ public Repository(NPath localPath, ICacheContainer container) Guard.ArgumentNotNull(localPath, nameof(localPath)); LocalPath = localPath; - User = new User(); cacheContainer = container; cacheContainer.CacheInvalidated += CacheContainer_OnCacheInvalidated; @@ -57,7 +56,6 @@ public void Initialize(IRepositoryManager initRepositoryManager) repositoryManager.OnLocalBranchRemoved += RepositoryManager_OnLocalBranchRemoved; repositoryManager.OnRemoteBranchAdded += RepositoryManager_OnRemoteBranchAdded; repositoryManager.OnRemoteBranchRemoved += RepositoryManager_OnRemoteBranchRemoved; - repositoryManager.OnGitUserLoaded += user => User = user; UpdateGitStatus(); UpdateGitLog(); @@ -145,7 +143,7 @@ public void RefreshStatus() public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.GitLogCache; - var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache); + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); Logger.Trace("Check GitLogCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -161,7 +159,7 @@ public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent) public void CheckStatusChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.GitStatusCache; - var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache); + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); Logger.Trace("Check GitStatusCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -192,7 +190,7 @@ public void CheckCurrentBranchAndRemoteChangedEvent(CacheUpdateEvent cacheUpdate private void CheckRepositoryInfoCacheEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.RepositoryInfoCache; - var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache); + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); Logger.Trace("Check RepositoryInfoCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -209,7 +207,7 @@ public void CheckLocksChangedEvent(CacheUpdateEvent cacheUpdateEvent) { CacheUpdateEvent cacheUpdateEvent1 = cacheUpdateEvent; var managedCache = cacheContainer.GitLocksCache; - var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent1, managedCache); + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent1); Logger.Trace("Check GitLocksCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent1.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -272,7 +270,7 @@ public bool Equals(IRepository other) private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.BranchCache; - var raiseEvent = ShouldRaiseCacheEvent(cacheUpdateEvent, managedCache); + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); Logger.Trace("Check BranchCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -285,20 +283,6 @@ private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent) } } - private static bool ShouldRaiseCacheEvent(CacheUpdateEvent cacheUpdateEvent, IManagedCache managedCache) - { - bool raiseEvent; - if (cacheUpdateEvent.UpdatedTimeString == null) - { - raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue; - } - else - { - raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString; - } - return raiseEvent; - } - private void CacheContainer_OnCacheInvalidated(CacheType cacheType) { switch (cacheType) @@ -671,8 +655,6 @@ public bool IsGitHub "{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}", GetHashCode(), Owner, Name, CloneUrl, LocalPath, CurrentBranch, CurrentRemote); - public IUser User { get; set; } - protected static ILogging Logger { get; } = Logging.GetLogger(); } diff --git a/src/GitHub.Api/Git/RepositoryManager.cs b/src/GitHub.Api/Git/RepositoryManager.cs index fa458c184..dcb3ea1d6 100644 --- a/src/GitHub.Api/Git/RepositoryManager.cs +++ b/src/GitHub.Api/Git/RepositoryManager.cs @@ -8,7 +8,6 @@ namespace GitHub.Unity public interface IRepositoryManager : IDisposable { event Action OnCurrentBranchAndRemoteUpdated; - event Action OnGitUserLoaded; event Action OnIsBusyChanged; event Action OnLocalBranchAdded; event Action> OnLocalBranchListUpdated; @@ -101,7 +100,6 @@ class RepositoryManager : IRepositoryManager private bool isBusy; public event Action OnCurrentBranchAndRemoteUpdated; - public event Action OnGitUserLoaded; public event Action OnIsBusyChanged; public event Action OnLocalBranchAdded; public event Action> OnLocalBranchListUpdated; @@ -150,7 +148,6 @@ public void Start() Logger.Trace("Start"); UpdateConfigData(); - LoadGitUser(); watcher.Start(); } @@ -298,15 +295,6 @@ public ITask UnlockFile(string file, bool force) return HookupHandlers(task); } - private void LoadGitUser() - { - GitClient.GetConfigUserAndEmail() - .Then((success, user) => { - Logger.Trace("OnGitUserLoaded: {0}", user); - OnGitUserLoaded?.Invoke(user); - }).Start(); - } - private void SetupWatcher() { watcher.HeadChanged += Watcher_OnHeadChanged; diff --git a/src/GitHub.Api/GitHub.Api.csproj b/src/GitHub.Api/GitHub.Api.csproj index 2816e0140..7185423ec 100644 --- a/src/GitHub.Api/GitHub.Api.csproj +++ b/src/GitHub.Api/GitHub.Api.csproj @@ -103,6 +103,7 @@ + diff --git a/src/GitHub.Api/Platform/DefaultEnvironment.cs b/src/GitHub.Api/Platform/DefaultEnvironment.cs index 7301e3875..f7e4615fc 100644 --- a/src/GitHub.Api/Platform/DefaultEnvironment.cs +++ b/src/GitHub.Api/Platform/DefaultEnvironment.cs @@ -7,7 +7,6 @@ namespace GitHub.Unity public class DefaultEnvironment : IEnvironment { private const string logFile = "github-unity.log"; - private ICacheContainer cacheContainer; public NPath LogPath { get; } public DefaultEnvironment() @@ -39,7 +38,7 @@ public DefaultEnvironment() public DefaultEnvironment(ICacheContainer cacheContainer) : this() { - this.cacheContainer = cacheContainer; + this.CacheContainer = cacheContainer; } public void Initialize(string unityVersion, NPath extensionInstallPath, NPath unityPath, NPath assetsPath) @@ -86,7 +85,7 @@ public void InitializeRepository(NPath expectedRepositoryPath = null) { Logger.Trace("Determined expectedRepositoryPath:{0}", expectedRepositoryPath); RepositoryPath = expectedRepositoryPath; - Repository = new Repository(RepositoryPath, cacheContainer); + Repository = new Repository(RepositoryPath, CacheContainer); } } @@ -133,6 +132,7 @@ public NPath GitExecutablePath public NPath GitInstallPath { get; private set; } public NPath RepositoryPath { get; private set; } + public ICacheContainer CacheContainer { get; private set; } public IRepository Repository { get; set; } public bool IsWindows { get { return OnWindows; } } diff --git a/src/GitHub.Api/Platform/IEnvironment.cs b/src/GitHub.Api/Platform/IEnvironment.cs index 1c42158ad..04177c406 100644 --- a/src/GitHub.Api/Platform/IEnvironment.cs +++ b/src/GitHub.Api/Platform/IEnvironment.cs @@ -29,5 +29,6 @@ public interface IEnvironment IFileSystem FileSystem { get; set; } IRepository Repository { get; set; } string ExecutableExtension { get; } + ICacheContainer CacheContainer { get; } } } \ No newline at end of file diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs index 55c9ffd38..2ac007371 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs @@ -915,22 +915,6 @@ sealed class GitUserCache : ManagedCacheBase, IGitUserCache [SerializeField] private string lastVerifiedAtString = DateTimeOffset.MinValue.ToString(); [SerializeField] private User user; - public void UpdateData(User userUpdate) - { - var now = DateTimeOffset.Now; - var isUpdated = false; - - Logger.Trace("Processing Update: {0}", now); - - if (user != userUpdate) - { - user = userUpdate; - isUpdated = true; - } - - SaveData(now, isUpdated); - } - public User User { get @@ -938,6 +922,21 @@ public User User ValidateData(); return user; } + set + { + var now = DateTimeOffset.Now; + var isUpdated = false; + + Logger.Trace("Processing Update: {0}", now); + + if (user != value) + { + user = value; + isUpdated = true; + } + + SaveData(now, isUpdated); + } } public override string LastUpdatedAtString diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs index 9a195b510..6f30ce228 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs @@ -13,12 +13,25 @@ class InitProjectView : Subview [NonSerialized] private bool isBusy; [NonSerialized] private bool isUserDataPresent; [NonSerialized] private bool hasCompletedInitialCheck; - [NonSerialized] private bool userDataHasChanged; + + [SerializeField] private CacheUpdateEvent lastCheckUserChangedEvent; + [NonSerialized] private bool userHasChanges; public override void OnEnable() { base.OnEnable(); - userDataHasChanged = Environment.GitExecutablePath != null; + AttachHandlers(); + + if (GitClient != null) + { + GitClient.CheckUserChangedEvent(lastCheckUserChangedEvent); + } + } + + public override void OnDisable() + { + base.OnDisable(); + DetachHandlers(); } public override void OnGUI() @@ -76,35 +89,45 @@ public override void OnDataUpdate() MaybeUpdateData(); } - private void MaybeUpdateData() + private void AttachHandlers() { - if (userDataHasChanged) + if (GitClient != null) { - userDataHasChanged = false; - CheckForUser(); + GitClient.CurrentUserChanged+=GitClientOnCurrentUserChanged; } } - private void CheckForUser() + private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) { - if (string.IsNullOrEmpty(Environment.GitExecutablePath)) + if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { - Logger.Warning("No git exec cannot check for user"); - return; + new ActionTask(TaskManager.Token, () => + { + lastCheckUserChangedEvent = cacheUpdateEvent; + userHasChanges = true; + Redraw(); + }) + { Affinity = TaskAffinity.UI }.Start(); } + } - Logger.Trace("Checking for user"); - isBusy = true; + private void DetachHandlers() + { + if (GitClient != null) + { + GitClient.CurrentUserChanged -= GitClientOnCurrentUserChanged; + } + } - GitClient.GetConfigUserAndEmail().FinallyInUI((success, ex, user) => { - isBusy = false; - isUserDataPresent = success && !String.IsNullOrEmpty(user.Name) && !String.IsNullOrEmpty(user.Email); + private void MaybeUpdateData() + { + if (userHasChanges) + { + userHasChanges = false; hasCompletedInitialCheck = true; - - Logger.Trace("User Present: {0}", isUserDataPresent); - - Redraw(); - }).Start(); + isUserDataPresent = !string.IsNullOrEmpty(GitClient.CurrentUser.Name) + && !string.IsNullOrEmpty(GitClient.CurrentUser.Email); + } } public override bool IsBusy diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index 6bf1945a0..33653544f 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -16,7 +16,6 @@ class UserSettingsView : Subview private const string GitConfigUserSave = "Save User"; [NonSerialized] private bool isBusy; - [NonSerialized] private bool userDataHasChanged; [SerializeField] private string gitName; [SerializeField] private string gitEmail; @@ -24,6 +23,9 @@ class UserSettingsView : Subview [SerializeField] private string newGitEmail; [SerializeField] private bool needsSaving; + [SerializeField] private CacheUpdateEvent lastCheckUserChangedEvent; + [NonSerialized] private bool userHasChanges; + public override void InitializeView(IView parent) { base.InitializeView(parent); @@ -62,29 +64,7 @@ public override void OnGUI() GUI.FocusControl(null); isBusy = true; - GitClient.SetConfigUserAndEmail(newGitName, newGitEmail) - .FinallyInUI((success, exception, user) => { - isBusy = false; - if (success) - { - if (Repository != null) - { - Repository.User.Name = gitName = newGitName; - Repository.User.Email = gitEmail = newGitEmail; - } - else - { - gitName = newGitName; - gitEmail = newGitEmail; - } - - needsSaving = false; - - Redraw(); - Finish(true); - } - }) - .Start(); + GitClient.SetConfigUserAndEmail(newGitName, newGitEmail); } } EditorGUI.EndDisabledGroup(); @@ -95,52 +75,57 @@ public override void OnGUI() public override void OnEnable() { base.OnEnable(); - userDataHasChanged = true; + AttachHandlers(); + + if (GitClient != null) + { + GitClient.CheckUserChangedEvent(lastCheckUserChangedEvent); + } } - private void MaybeUpdateData() + public override void OnDisable() + { + base.OnDisable(); + DetachHandlers(); + } + private void AttachHandlers() { - if (userDataHasChanged) + if (GitClient != null) { - userDataHasChanged = false; - - if (Repository == null) - { - UpdateUserDataFromClient(); - } - else - { - newGitName = gitName = Repository.User.Name; - newGitEmail = gitEmail = Repository.User.Email; - needsSaving = false; - } + GitClient.CurrentUserChanged += GitClientOnCurrentUserChanged; } } - private void UpdateUserDataFromClient() + private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) { - if (String.IsNullOrEmpty(EntryPoint.Environment.GitExecutablePath)) + if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { - return; + new ActionTask(TaskManager.Token, () => + { + lastCheckUserChangedEvent = cacheUpdateEvent; + userHasChanges = true; + Redraw(); + }) + { Affinity = TaskAffinity.UI }.Start(); } + } - if (GitClient == null) + private void DetachHandlers() + { + if (GitClient != null) { - return; + GitClient.CurrentUserChanged -= GitClientOnCurrentUserChanged; } + } - Logger.Trace("Update user data from GitClient"); - - GitClient.GetConfigUserAndEmail() - .ThenInUI((success, user) => { - if (success && !String.IsNullOrEmpty(user.Name) && !String.IsNullOrEmpty(user.Email)) - { - newGitName = gitName = user.Name; - newGitEmail = gitEmail = user.Email; - needsSaving = false; - Redraw(); - } - }).Start(); + private void MaybeUpdateData() + { + if (userHasChanges) + { + userHasChanges = false; + gitName = newGitName = GitClient.CurrentUser.Name; + gitEmail = newGitEmail = GitClient.CurrentUser.Email; + } } public override bool IsBusy diff --git a/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs b/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs index 62158eac2..cc5a37099 100644 --- a/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs +++ b/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs @@ -120,5 +120,10 @@ public NPath GitExecutablePath public IRepository Repository { get; set; } public IFileSystem FileSystem { get { return defaultEnvironment.FileSystem; } set { defaultEnvironment.FileSystem = value; } } public string ExecutableExtension { get { return defaultEnvironment.ExecutableExtension; } } + + public ICacheContainer CacheContainer + { + get { throw new NotImplementedException(); } + } } } diff --git a/src/tests/TestUtils/Events/IRepositoryManagerListener.cs b/src/tests/TestUtils/Events/IRepositoryManagerListener.cs index 2d43f6304..ec7a10537 100644 --- a/src/tests/TestUtils/Events/IRepositoryManagerListener.cs +++ b/src/tests/TestUtils/Events/IRepositoryManagerListener.cs @@ -19,7 +19,6 @@ interface IRepositoryManagerListener void OnLocalBranchRemoved(string name); void OnRemoteBranchAdded(string origin, string name); void OnRemoteBranchRemoved(string origin, string name); - void OnGitUserLoaded(IUser user); void OnCurrentBranchAndRemoteUpdated(ConfigBranch? configBranch, ConfigRemote? configRemote); } @@ -38,7 +37,6 @@ class RepositoryManagerEvents public EventWaitHandle OnLocalBranchRemoved { get; } = new AutoResetEvent(false); public EventWaitHandle OnRemoteBranchAdded { get; } = new AutoResetEvent(false); public EventWaitHandle OnRemoteBranchRemoved { get; } = new AutoResetEvent(false); - public EventWaitHandle OnGitUserLoaded { get; } = new AutoResetEvent(false); public void Reset() { @@ -55,7 +53,6 @@ public void Reset() OnLocalBranchRemoved.Reset(); OnRemoteBranchAdded.Reset(); OnRemoteBranchRemoved.Reset(); - OnGitUserLoaded.Reset(); } public void WaitForNotBusy(int seconds = 1) @@ -138,12 +135,6 @@ public static void AttachListener(this IRepositoryManagerListener listener, listener.OnRemoteBranchRemoved(origin, name); managerEvents?.OnRemoteBranchRemoved.Set(); }; - - repositoryManager.OnGitUserLoaded += user => { - logger?.Trace("OnGitUserLoaded Name:{0}", user); - listener.OnGitUserLoaded(user); - managerEvents?.OnGitUserLoaded.Set(); - }; } public static void AssertDidNotReceiveAnyCalls(this IRepositoryManagerListener repositoryManagerListener) From bf459e23fdef47e5a12e07508e18f22fea80b664 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 13 Nov 2017 17:34:20 -0500 Subject: [PATCH 02/13] Utilizing the git user cache better --- src/GitHub.Api/Git/GitClient.cs | 5 ++++- src/GitHub.Api/Git/Repository.cs | 16 ++++++++++++++-- .../Editor/GitHub.Unity/UI/InitProjectView.cs | 7 ++++--- .../Editor/GitHub.Unity/UI/UserSettingsView.cs | 11 +++++++---- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/GitHub.Api/Git/GitClient.cs b/src/GitHub.Api/Git/GitClient.cs index ca3918d77..8db6947be 100644 --- a/src/GitHub.Api/Git/GitClient.cs +++ b/src/GitHub.Api/Git/GitClient.cs @@ -123,7 +123,8 @@ private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) private void GitUserCacheOnCacheInvalidated() { - + Logger.Trace("GitUserCache Invalidated"); + UpdateUserAndEmail(); } public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) @@ -320,6 +321,8 @@ public ITask SetConfig(string key, string value, GitConfigSource configS private void UpdateUserAndEmail() { + Logger.Trace("UpdateUserAndEmail"); + string username = null; string email = null; diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index 05e70b524..1d7ebe49d 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -665,13 +665,25 @@ public interface IUser [Serializable] public class User : IUser { + public string name; + public string email; + public override string ToString() { return String.Format("Name: {0} Email: {1}", Name, Email); } - public string Name { get; set; } - public string Email { get; set; } + public string Name + { + get { return name; } + set { name = value; } + } + + public string Email + { + get { return email; } + set { email = value; } + } } [Serializable] diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs index 6f30ce228..4e64bd38f 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs @@ -93,7 +93,7 @@ private void AttachHandlers() { if (GitClient != null) { - GitClient.CurrentUserChanged+=GitClientOnCurrentUserChanged; + GitClient.CurrentUserChanged += GitClientOnCurrentUserChanged; } } @@ -124,9 +124,10 @@ private void MaybeUpdateData() if (userHasChanges) { userHasChanges = false; + var currentUser = GitClient.CurrentUser; + isUserDataPresent = !string.IsNullOrEmpty(currentUser.Name) + && !string.IsNullOrEmpty(currentUser.Email); hasCompletedInitialCheck = true; - isUserDataPresent = !string.IsNullOrEmpty(GitClient.CurrentUser.Name) - && !string.IsNullOrEmpty(GitClient.CurrentUser.Email); } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index 33653544f..4fd7a76ef 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -98,12 +98,14 @@ private void AttachHandlers() private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) { + Logger.Trace("GitClientOnCurrentUserChanged"); + if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { - new ActionTask(TaskManager.Token, () => - { + new ActionTask(TaskManager.Token, () => { lastCheckUserChangedEvent = cacheUpdateEvent; userHasChanges = true; + isBusy = false; Redraw(); }) { Affinity = TaskAffinity.UI }.Start(); @@ -123,8 +125,9 @@ private void MaybeUpdateData() if (userHasChanges) { userHasChanges = false; - gitName = newGitName = GitClient.CurrentUser.Name; - gitEmail = newGitEmail = GitClient.CurrentUser.Email; + var currentUser = GitClient.CurrentUser; + gitName = newGitName = currentUser.Name; + gitEmail = newGitEmail = currentUser.Email; } } From 46c8ddee15a43822728211db0dcfd6f0f1d916ba Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 13 Nov 2017 18:09:46 -0500 Subject: [PATCH 03/13] Reordering functions --- src/GitHub.Api/Git/GitClient.cs | 97 +++++++++++++++++---------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/src/GitHub.Api/Git/GitClient.cs b/src/GitHub.Api/Git/GitClient.cs index 8db6947be..a0e39f1a5 100644 --- a/src/GitHub.Api/Git/GitClient.cs +++ b/src/GitHub.Api/Git/GitClient.cs @@ -101,7 +101,7 @@ class GitClient : IGitClient private readonly CancellationToken cancellationToken; public event Action CurrentUserChanged; - private bool cacheInitialized = false; + private bool cacheInitialized; public GitClient(IEnvironment environment, IProcessManager processManager, ITaskManager taskManager) { @@ -113,20 +113,6 @@ public GitClient(IEnvironment environment, IProcessManager processManager, ITask this.cancellationToken = taskManager.Token; } - private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) - { - HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent - { - UpdatedTimeString = timeOffset.ToString() - }); - } - - private void GitUserCacheOnCacheInvalidated() - { - Logger.Trace("GitUserCache Invalidated"); - UpdateUserAndEmail(); - } - public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = environment.CacheContainer.GitUserCache; @@ -142,11 +128,6 @@ public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) HandleGitLogCacheUpdatedEvent(updateEvent); } } - private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) - { - Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); - CurrentUserChanged?.Invoke(cacheUpdateEvent); - } public User CurrentUser { @@ -319,34 +300,6 @@ public ITask SetConfig(string key, string value, GitConfigSource configS .Configure(processManager); } - private void UpdateUserAndEmail() - { - Logger.Trace("UpdateUserAndEmail"); - - string username = null; - string email = null; - - GetConfig(UserNameConfigKey, GitConfigSource.User) - .Then((success, value) => { - if (success) - { - username = value; - } - }) - .Then(GetConfig(UserEmailConfigKey, GitConfigSource.User) - .Then((success, value) => { - if (success) - { - email = value; - } - })).ThenInUI(success => { - environment.CacheContainer.GitUserCache.User= new User { - Name = username, - Email = email - }; - }).Start(); - } - public void SetConfigUserAndEmail(string username, string email) { SetConfig(UserNameConfigKey, username, GitConfigSource.User) @@ -527,6 +480,54 @@ public ITask Unlock(string file, bool force, .Configure(processManager); } + private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) + { + HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent + { + UpdatedTimeString = timeOffset.ToString() + }); + } + + private void GitUserCacheOnCacheInvalidated() + { + Logger.Trace("GitUserCache Invalidated"); + UpdateUserAndEmail(); + } + + private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) + { + Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); + CurrentUserChanged?.Invoke(cacheUpdateEvent); + } + + private void UpdateUserAndEmail() + { + Logger.Trace("UpdateUserAndEmail"); + + string username = null; + string email = null; + + GetConfig(UserNameConfigKey, GitConfigSource.User) + .Then((success, value) => { + if (success) + { + username = value; + } + }) + .Then(GetConfig(UserEmailConfigKey, GitConfigSource.User) + .Then((success, value) => { + if (success) + { + email = value; + } + })).ThenInUI(success => { + CurrentUser = new User { + Name = username, + Email = email + }; + }).Start(); + } + protected static ILogging Logger { get; } = Logging.GetLogger(); } } From 92cea975bd9af79bb00bbf1415d9a982a67edf3d Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 13 Nov 2017 18:12:00 -0500 Subject: [PATCH 04/13] More code formatting --- .../Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index 4fd7a76ef..35edc084b 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -15,15 +15,14 @@ class UserSettingsView : Subview private const string GitConfigEmailLabel = "Email"; private const string GitConfigUserSave = "Save User"; - [NonSerialized] private bool isBusy; - [SerializeField] private string gitName; [SerializeField] private string gitEmail; [SerializeField] private string newGitName; [SerializeField] private string newGitEmail; [SerializeField] private bool needsSaving; - [SerializeField] private CacheUpdateEvent lastCheckUserChangedEvent; + + [NonSerialized] private bool isBusy; [NonSerialized] private bool userHasChanges; public override void InitializeView(IView parent) @@ -88,6 +87,7 @@ public override void OnDisable() base.OnDisable(); DetachHandlers(); } + private void AttachHandlers() { if (GitClient != null) From 60312fd87052262d1a8c367b1df74f0e5149119f Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 12:14:45 -0500 Subject: [PATCH 05/13] Migrating Caching of user data from GitClient to User Misunderstood the initial requirements. I believed the User object was just for currying data. So I created GitUser to represent the user data returned from GitClient and then moved the caching functionality over to User. --- src/GitHub.Api/Git/GitClient.cs | 138 ++++++------------ src/GitHub.Api/Git/Repository.cs | 83 ++++++++++- src/GitHub.Api/Platform/DefaultEnvironment.cs | 2 + src/GitHub.Api/Platform/IEnvironment.cs | 1 + .../Editor/GitHub.Unity/UI/BaseWindow.cs | 4 + .../Assets/Editor/GitHub.Unity/UI/IView.cs | 2 + .../Editor/GitHub.Unity/UI/InitProjectView.cs | 22 +-- .../Assets/Editor/GitHub.Unity/UI/Subview.cs | 2 + .../GitHub.Unity/UI/UserSettingsView.cs | 22 +-- .../Git/IntegrationTestEnvironment.cs | 1 + 10 files changed, 146 insertions(+), 131 deletions(-) diff --git a/src/GitHub.Api/Git/GitClient.cs b/src/GitHub.Api/Git/GitClient.cs index a0e39f1a5..96bcd0702 100644 --- a/src/GitHub.Api/Git/GitClient.cs +++ b/src/GitHub.Api/Git/GitClient.cs @@ -8,8 +8,6 @@ namespace GitHub.Unity { public interface IGitClient { - event Action CurrentUserChanged; - Task FindGitInstallation(); ITask ValidateGitInstall(NPath path); @@ -25,6 +23,8 @@ ITask GetConfig(string key, GitConfigSource configSource, ITask SetConfig(string key, string value, GitConfigSource configSource, IOutputProcessor processor = null); + ITask GetConfigUserAndEmail(); + ITask> ListLocks(bool local, BaseOutputListProcessor processor = null); @@ -84,11 +84,7 @@ ITask Unlock(string file, bool force, ITask LfsVersion(IOutputProcessor processor = null); - void SetConfigUserAndEmail(string username, string email); - - void CheckUserChangedEvent(CacheUpdateEvent gitLogCacheUpdateEvent); - - User CurrentUser { get; } + ITask SetConfigUserAndEmail(string username, string email); } class GitClient : IGitClient @@ -100,51 +96,14 @@ class GitClient : IGitClient private readonly ITaskManager taskManager; private readonly CancellationToken cancellationToken; - public event Action CurrentUserChanged; - private bool cacheInitialized; - public GitClient(IEnvironment environment, IProcessManager processManager, ITaskManager taskManager) { - Logger.Trace("Constructed"); - this.environment = environment; this.processManager = processManager; this.taskManager = taskManager; this.cancellationToken = taskManager.Token; } - public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) - { - var managedCache = environment.CacheContainer.GitUserCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); - - Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, - cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); - - if (raiseEvent) - { - var dateTimeOffset = managedCache.LastUpdatedAt; - var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() }; - HandleGitLogCacheUpdatedEvent(updateEvent); - } - } - - public User CurrentUser - { - get - { - if (!cacheInitialized) - { - cacheInitialized = true; - environment.CacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated; - environment.CacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated; - } - - return environment.CacheContainer.GitUserCache.User; - } - private set { environment.CacheContainer.GitUserCache.User = value; } - } - public async Task FindGitInstallation() { if (!String.IsNullOrEmpty(environment.GitExecutablePath)) @@ -300,12 +259,35 @@ public ITask SetConfig(string key, string value, GitConfigSource configS .Configure(processManager); } - public void SetConfigUserAndEmail(string username, string email) + public ITask GetConfigUserAndEmail() + { + string username = null; + string email = null; + + return GetConfig(UserNameConfigKey, GitConfigSource.User) + .Then((success, value) => { + if (success) + { + username = value; + } + }) + .Then(GetConfig(UserEmailConfigKey, GitConfigSource.User) + .Then((success, value) => { + if (success) + { + email = value; + } + })).Then(success => { + Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email); + return new GitUser(username, email); + }); + } + + public ITask SetConfigUserAndEmail(string username, string email) { - SetConfig(UserNameConfigKey, username, GitConfigSource.User) + return SetConfig(UserNameConfigKey, username, GitConfigSource.User) .Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User)) - .Then(UpdateUserAndEmail) - .Start(); + .Then(b => new GitUser(username, email)); } public ITask> ListLocks(bool local, BaseOutputListProcessor processor = null) @@ -480,54 +462,28 @@ public ITask Unlock(string file, bool force, .Configure(processManager); } - private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) - { - HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent - { - UpdatedTimeString = timeOffset.ToString() - }); - } + protected static ILogging Logger { get; } = Logging.GetLogger(); + } - private void GitUserCacheOnCacheInvalidated() - { - Logger.Trace("GitUserCache Invalidated"); - UpdateUserAndEmail(); - } - - private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) - { - Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); - CurrentUserChanged?.Invoke(cacheUpdateEvent); - } + public struct GitUser + { + public static GitUser Default = new GitUser(); - private void UpdateUserAndEmail() - { - Logger.Trace("UpdateUserAndEmail"); + public string name; + public string email; - string username = null; - string email = null; + public string Name { get { return name; } } + public string Email { get { return email; } } - GetConfig(UserNameConfigKey, GitConfigSource.User) - .Then((success, value) => { - if (success) - { - username = value; - } - }) - .Then(GetConfig(UserEmailConfigKey, GitConfigSource.User) - .Then((success, value) => { - if (success) - { - email = value; - } - })).ThenInUI(success => { - CurrentUser = new User { - Name = username, - Email = email - }; - }).Start(); + public GitUser(string name, string email) + { + this.name = name; + this.email = email; } - protected static ILogging Logger { get; } = Logging.GetLogger(); + public override string ToString() + { + return $"Name:\"{Name}\" Email:\"{Email}\""; + } } } diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index 8cd981f1b..164e5fbde 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -668,15 +668,43 @@ public bool IsGitHub public interface IUser { - string Name { get; set; } - string Email { get; set; } + string Name { get; } + string Email { get; } + event Action UserChanged; + void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent); } [Serializable] public class User : IUser { - public string name; - public string email; + private ICacheContainer cacheContainer; + private IGitClient gitClient; + + public event Action UserChanged; + + public User(ICacheContainer cacheContainer) + { + this.cacheContainer = cacheContainer; + + cacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated; + cacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated; + } + + public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) + { + var managedCache = cacheContainer.GitUserCache; + var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + + Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, + cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); + + if (raiseEvent) + { + var dateTimeOffset = managedCache.LastUpdatedAt; + var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() }; + HandleGitLogCacheUpdatedEvent(updateEvent); + } + } public override string ToString() { @@ -685,15 +713,54 @@ public override string ToString() public string Name { - get { return name; } - set { name = value; } + get { return cacheContainer.GitUserCache.User.Name; } + private set { cacheContainer.GitUserCache.User.Name = value; } } public string Email { - get { return email; } - set { email = value; } + get { return cacheContainer.GitUserCache.User.Email; } + private set { cacheContainer.GitUserCache.User.Email = value; } } + + private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) + { + HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent + { + UpdatedTimeString = timeOffset.ToString() + }); + } + + private void GitUserCacheOnCacheInvalidated() + { + Logger.Trace("GitUserCache Invalidated"); + UpdateUserAndEmail(); + } + + private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) + { + Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); + UserChanged?.Invoke(cacheUpdateEvent); + } + + private void UpdateUserAndEmail() + { + Logger.Trace("UpdateUserAndEmail"); + + string username = null; + string email = null; + + gitClient.GetConfigUserAndEmail() + .Then((success, value) => { + if (success) + { + username = value.Name; + email = value.Email; + } + }).Start(); + } + + protected static ILogging Logger { get; } = Logging.GetLogger(); } [Serializable] diff --git a/src/GitHub.Api/Platform/DefaultEnvironment.cs b/src/GitHub.Api/Platform/DefaultEnvironment.cs index f7e4615fc..b568804fd 100644 --- a/src/GitHub.Api/Platform/DefaultEnvironment.cs +++ b/src/GitHub.Api/Platform/DefaultEnvironment.cs @@ -48,6 +48,7 @@ public void Initialize(string unityVersion, NPath extensionInstallPath, NPath un UnityAssetsPath = assetsPath; UnityProjectPath = assetsPath.Parent; UnityVersion = unityVersion; + User = new User(CacheContainer); } public void InitializeRepository(NPath expectedRepositoryPath = null) @@ -134,6 +135,7 @@ public NPath GitExecutablePath public NPath RepositoryPath { get; private set; } public ICacheContainer CacheContainer { get; private set; } public IRepository Repository { get; set; } + public IUser User { get; set; } public bool IsWindows { get { return OnWindows; } } public bool IsLinux { get { return OnLinux; } } diff --git a/src/GitHub.Api/Platform/IEnvironment.cs b/src/GitHub.Api/Platform/IEnvironment.cs index 04177c406..d37c89ec9 100644 --- a/src/GitHub.Api/Platform/IEnvironment.cs +++ b/src/GitHub.Api/Platform/IEnvironment.cs @@ -27,6 +27,7 @@ public interface IEnvironment NPath SystemCachePath { get; set; } NPath LogPath { get; } IFileSystem FileSystem { get; set; } + IUser User { get; set; } IRepository Repository { get; set; } string ExecutableExtension { get; } ICacheContainer CacheContainer { get; } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs index c864652c5..3cc00a234 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs @@ -7,6 +7,7 @@ namespace GitHub.Unity abstract class BaseWindow : EditorWindow, IView { [NonSerialized] private bool initialized = false; + [NonSerialized] private IUser cachedUser; [NonSerialized] private IRepository cachedRepository; [NonSerialized] private bool initializeWasCalled; [NonSerialized] private bool inLayout; @@ -21,6 +22,7 @@ public void InitializeWindow(IApplicationManager applicationManager, bool requir initialized = true; initializeWasCalled = true; Manager = applicationManager; + cachedUser = Environment.User; cachedRepository = Environment.Repository; Initialize(applicationManager); if (requiresRedraw) @@ -103,6 +105,8 @@ public virtual void OnSelectionChange() public abstract bool IsBusy { get; } public IRepository Repository { get { return inLayout ? cachedRepository : Environment.Repository; } } public bool HasRepository { get { return Repository != null; } } + public IUser User { get { return cachedUser; } } + public bool HasUser { get { return User != null; } } protected ITaskManager TaskManager { get { return Manager.TaskManager; } } protected IGitClient GitClient { get { return Manager.GitClient; } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs index 7afe70af4..52cc69c2c 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs @@ -14,6 +14,8 @@ interface IView void Finish(bool result); IRepository Repository { get; } bool HasRepository { get; } + IUser User { get; } + bool HasUser { get; } IApplicationManager Manager { get; } bool IsBusy { get; } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs index 4e64bd38f..869c75b9b 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs @@ -22,10 +22,7 @@ public override void OnEnable() base.OnEnable(); AttachHandlers(); - if (GitClient != null) - { - GitClient.CheckUserChangedEvent(lastCheckUserChangedEvent); - } + User.CheckUserChangedEvent(lastCheckUserChangedEvent); } public override void OnDisable() @@ -91,13 +88,10 @@ public override void OnDataUpdate() private void AttachHandlers() { - if (GitClient != null) - { - GitClient.CurrentUserChanged += GitClientOnCurrentUserChanged; - } + User.UserChanged += UserOnUserChanged; } - private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) + private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) { if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { @@ -113,10 +107,7 @@ private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) private void DetachHandlers() { - if (GitClient != null) - { - GitClient.CurrentUserChanged -= GitClientOnCurrentUserChanged; - } + User.UserChanged -= UserOnUserChanged; } private void MaybeUpdateData() @@ -124,9 +115,8 @@ private void MaybeUpdateData() if (userHasChanges) { userHasChanges = false; - var currentUser = GitClient.CurrentUser; - isUserDataPresent = !string.IsNullOrEmpty(currentUser.Name) - && !string.IsNullOrEmpty(currentUser.Email); + isUserDataPresent = !string.IsNullOrEmpty(User.Name) + && !string.IsNullOrEmpty(User.Email); hasCompletedInitialCheck = true; } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs index b6a4d84f7..e144f48be 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs @@ -52,6 +52,8 @@ public virtual void Finish(bool result) public IApplicationManager Manager { get { return Parent.Manager; } } public IRepository Repository { get { return Parent.Repository; } } public bool HasRepository { get { return Parent.HasRepository; } } + public IUser User { get { return Parent.User; } } + public bool HasUser { get { return Parent.HasUser; } } public abstract bool IsBusy { get; } protected ITaskManager TaskManager { get { return Manager.TaskManager; } } protected IGitClient GitClient { get { return Manager.GitClient; } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index 35edc084b..e675fc0de 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -76,10 +76,7 @@ public override void OnEnable() base.OnEnable(); AttachHandlers(); - if (GitClient != null) - { - GitClient.CheckUserChangedEvent(lastCheckUserChangedEvent); - } + User.CheckUserChangedEvent(lastCheckUserChangedEvent); } public override void OnDisable() @@ -90,13 +87,10 @@ public override void OnDisable() private void AttachHandlers() { - if (GitClient != null) - { - GitClient.CurrentUserChanged += GitClientOnCurrentUserChanged; - } + User.UserChanged += UserOnUserChanged; } - private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) + private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) { Logger.Trace("GitClientOnCurrentUserChanged"); @@ -114,10 +108,7 @@ private void GitClientOnCurrentUserChanged(CacheUpdateEvent cacheUpdateEvent) private void DetachHandlers() { - if (GitClient != null) - { - GitClient.CurrentUserChanged -= GitClientOnCurrentUserChanged; - } + User.UserChanged -= UserOnUserChanged; } private void MaybeUpdateData() @@ -125,9 +116,8 @@ private void MaybeUpdateData() if (userHasChanges) { userHasChanges = false; - var currentUser = GitClient.CurrentUser; - gitName = newGitName = currentUser.Name; - gitEmail = newGitEmail = currentUser.Email; + gitName = newGitName = User.Name; + gitEmail = newGitEmail = User.Email; } } diff --git a/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs b/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs index cc5a37099..3951f9537 100644 --- a/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs +++ b/src/tests/IntegrationTests/Git/IntegrationTestEnvironment.cs @@ -118,6 +118,7 @@ public NPath GitExecutablePath public NPath GitInstallPath => defaultEnvironment.GitInstallPath; public IRepository Repository { get; set; } + public IUser User { get; set; } public IFileSystem FileSystem { get { return defaultEnvironment.FileSystem; } set { defaultEnvironment.FileSystem = value; } } public string ExecutableExtension { get { return defaultEnvironment.ExecutableExtension; } } From 9643fb803e1b870f654b93d7763883fa4a8a408c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 12:27:38 -0500 Subject: [PATCH 06/13] Serialize the name and email directly More rippling changes from the previous implementation attempt --- src/GitHub.Api/Cache/CacheInterfaces.cs | 3 +- src/GitHub.Api/Git/Repository.cs | 17 ++++----- .../Editor/GitHub.Unity/ApplicationCache.cs | 37 ++++++++++++++++--- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/GitHub.Api/Cache/CacheInterfaces.cs b/src/GitHub.Api/Cache/CacheInterfaces.cs index 95cfd811a..d91fb60d1 100644 --- a/src/GitHub.Api/Cache/CacheInterfaces.cs +++ b/src/GitHub.Api/Cache/CacheInterfaces.cs @@ -49,7 +49,8 @@ public interface IGitLocksCache : IManagedCache public interface IGitUserCache : IManagedCache { - User User { get; set; } + string Name { get; set; } + string Email { get; set; } } public interface IGitStatusCache : IManagedCache diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index 164e5fbde..47ab50dcc 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -713,14 +713,14 @@ public override string ToString() public string Name { - get { return cacheContainer.GitUserCache.User.Name; } - private set { cacheContainer.GitUserCache.User.Name = value; } + get { return cacheContainer.GitUserCache.Name; } + private set { cacheContainer.GitUserCache.Name = value; } } public string Email { - get { return cacheContainer.GitUserCache.User.Email; } - private set { cacheContainer.GitUserCache.User.Email = value; } + get { return cacheContainer.GitUserCache.Email; } + private set { cacheContainer.GitUserCache.Email = value; } } private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) @@ -747,15 +747,12 @@ private void UpdateUserAndEmail() { Logger.Trace("UpdateUserAndEmail"); - string username = null; - string email = null; - gitClient.GetConfigUserAndEmail() - .Then((success, value) => { + .ThenInUI((success, value) => { if (success) { - username = value.Name; - email = value.Email; + Name = value.Name; + Email = value.Email; } }).Start(); } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs index ede850d31..b3b91ce18 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs @@ -1008,28 +1008,53 @@ sealed class GitUserCache : ManagedCacheBase, IGitUserCache [SerializeField] private string lastUpdatedAtString = DateTimeOffset.MinValue.ToString(); [SerializeField] private string lastVerifiedAtString = DateTimeOffset.MinValue.ToString(); [SerializeField] private string initializedAtString = DateTimeOffset.MinValue.ToString(); - [SerializeField] private User user; + [SerializeField] private string gitName; + [SerializeField] private string gitEmail; public GitUserCache() : base(true) { } - public User User + public string Name { get { ValidateData(); - return user; + return gitName; } set { var now = DateTimeOffset.Now; var isUpdated = false; - Logger.Trace("Updating: {0} user:{1}", now, value); + Logger.Trace("Updating: {0} Name:{1}", now, value); - if (!user.Equals(value)) + if (!gitName.Equals(value)) { - user = value; + gitName = value; + isUpdated = true; + } + + SaveData(now, isUpdated); + } + } + + public string Email + { + get + { + ValidateData(); + return gitEmail; + } + set + { + var now = DateTimeOffset.Now; + var isUpdated = false; + + Logger.Trace("Updating: {0} Email:{1}", now, value); + + if (!gitEmail.Equals(value)) + { + gitEmail = value; isUpdated = true; } From e54fef8c950fb79088c6aa0d88da4c02518f9e53 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 13:36:05 -0500 Subject: [PATCH 07/13] GitName and GitEmail might be null themselves --- .../Assets/Editor/GitHub.Unity/ApplicationCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs index 2ab59a429..7899f27c0 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs @@ -1058,7 +1058,7 @@ public string Name Logger.Trace("Updating: {0} Name:{1}", now, value); - if (!gitName.Equals(value)) + if (gitName != value) { gitName = value; isUpdated = true; @@ -1082,7 +1082,7 @@ public string Email Logger.Trace("Updating: {0} Email:{1}", now, value); - if (!gitEmail.Equals(value)) + if (gitEmail != value) { gitEmail = value; isUpdated = true; From 7ac73ec47057eb750db6dae00a06debc4d1d436a Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 13:45:10 -0500 Subject: [PATCH 08/13] Moving functionality to set name and email Moving the functionality to set the name an email to User and changing UserSettingsView to use that method. Also did some minor method renames. --- .../Application/ApplicationManagerBase.cs | 1 + src/GitHub.Api/Git/GitClient.cs | 4 ++-- src/GitHub.Api/Git/Repository.cs | 24 +++++++++++++++++++ .../GitHub.Unity/UI/UserSettingsView.cs | 4 ++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/GitHub.Api/Application/ApplicationManagerBase.cs b/src/GitHub.Api/Application/ApplicationManagerBase.cs index ffa1bf77d..63c4d67bc 100644 --- a/src/GitHub.Api/Application/ApplicationManagerBase.cs +++ b/src/GitHub.Api/Application/ApplicationManagerBase.cs @@ -78,6 +78,7 @@ private async Task SetupGit() } } + Environment.User.Initialize(GitClient); } public ITask InitializeRepository() diff --git a/src/GitHub.Api/Git/GitClient.cs b/src/GitHub.Api/Git/GitClient.cs index 96bcd0702..9c3643a58 100644 --- a/src/GitHub.Api/Git/GitClient.cs +++ b/src/GitHub.Api/Git/GitClient.cs @@ -84,7 +84,7 @@ ITask Unlock(string file, bool force, ITask LfsVersion(IOutputProcessor processor = null); - ITask SetConfigUserAndEmail(string username, string email); + ITask SetConfigNameAndEmail(string username, string email); } class GitClient : IGitClient @@ -283,7 +283,7 @@ public ITask GetConfigUserAndEmail() }); } - public ITask SetConfigUserAndEmail(string username, string email) + public ITask SetConfigNameAndEmail(string username, string email) { return SetConfig(UserNameConfigKey, username, GitConfigSource.User) .Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User)) diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index 47ab50dcc..db33ef6fc 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -672,6 +672,8 @@ public interface IUser string Email { get; } event Action UserChanged; void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent); + void Initialize(IGitClient client); + void SetNameAndEmail(string name, string email); } [Serializable] @@ -706,6 +708,28 @@ public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) } } + public void Initialize(IGitClient client) + { + Guard.ArgumentNotNull(client, nameof(client)); + + Logger.Trace("Initialize"); + + gitClient = client; + UpdateUserAndEmail(); + } + + public void SetNameAndEmail(string name, string email) + { + gitClient.SetConfigNameAndEmail(name, email) + .ThenInUI((success, value) => { + if (success) + { + Name = value.Name; + Email = value.Email; + } + }).Start(); + } + public override string ToString() { return String.Format("Name: {0} Email: {1}", Name, Email); diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index e675fc0de..0ae75dd72 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -63,7 +63,7 @@ public override void OnGUI() GUI.FocusControl(null); isBusy = true; - GitClient.SetConfigUserAndEmail(newGitName, newGitEmail); + User.SetNameAndEmail(newGitName, newGitEmail); } } EditorGUI.EndDisabledGroup(); @@ -92,7 +92,7 @@ private void AttachHandlers() private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) { - Logger.Trace("GitClientOnCurrentUserChanged"); + Logger.Trace("UserOnUserChanged"); if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { From 03e1f7df813b08ff72fac360bbe7d3c1094c6e55 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 13:51:13 -0500 Subject: [PATCH 09/13] Checking if gitClient is null --- src/GitHub.Api/Git/Repository.cs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index db33ef6fc..ad2255812 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -769,16 +769,20 @@ private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) private void UpdateUserAndEmail() { - Logger.Trace("UpdateUserAndEmail"); + if (gitClient != null) + { + Logger.Trace("UpdateUserAndEmail"); - gitClient.GetConfigUserAndEmail() - .ThenInUI((success, value) => { - if (success) + gitClient.GetConfigUserAndEmail() + .ThenInUI((success, value) => { - Name = value.Name; - Email = value.Email; - } - }).Start(); + if (success) + { + Name = value.Name; + Email = value.Email; + } + }).Start(); + } } protected static ILogging Logger { get; } = Logging.GetLogger(); From 1b89aee7d7b3b455d8fe85b22bb009e8cbf740a7 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 13:59:23 -0500 Subject: [PATCH 10/13] Renaming event --- src/GitHub.Api/Git/Repository.cs | 8 ++++---- .../Assets/Editor/GitHub.Unity/UI/InitProjectView.cs | 6 +++--- .../Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index ad2255812..15d7dbc28 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -670,7 +670,7 @@ public interface IUser { string Name { get; } string Email { get; } - event Action UserChanged; + event Action Changed; void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent); void Initialize(IGitClient client); void SetNameAndEmail(string name, string email); @@ -682,7 +682,7 @@ public class User : IUser private ICacheContainer cacheContainer; private IGitClient gitClient; - public event Action UserChanged; + public event Action Changed; public User(ICacheContainer cacheContainer) { @@ -764,7 +764,7 @@ private void GitUserCacheOnCacheInvalidated() private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) { Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); - UserChanged?.Invoke(cacheUpdateEvent); + Changed?.Invoke(cacheUpdateEvent); } private void UpdateUserAndEmail() @@ -785,7 +785,7 @@ private void UpdateUserAndEmail() } } - protected static ILogging Logger { get; } = Logging.GetLogger(); + protected static ILogging Logger { get; } = Logging.GetLogger(); } [Serializable] diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs index 869c75b9b..b6175d6de 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/InitProjectView.cs @@ -88,10 +88,10 @@ public override void OnDataUpdate() private void AttachHandlers() { - User.UserChanged += UserOnUserChanged; + User.Changed += UserOnChanged; } - private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) + private void UserOnChanged(CacheUpdateEvent cacheUpdateEvent) { if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { @@ -107,7 +107,7 @@ private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) private void DetachHandlers() { - User.UserChanged -= UserOnUserChanged; + User.Changed -= UserOnChanged; } private void MaybeUpdateData() diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index 0ae75dd72..a411a7de0 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -87,12 +87,12 @@ public override void OnDisable() private void AttachHandlers() { - User.UserChanged += UserOnUserChanged; + User.Changed += UserOnChanged; } - private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) + private void UserOnChanged(CacheUpdateEvent cacheUpdateEvent) { - Logger.Trace("UserOnUserChanged"); + Logger.Trace("UserOnChanged"); if (!lastCheckUserChangedEvent.Equals(cacheUpdateEvent)) { @@ -108,7 +108,7 @@ private void UserOnUserChanged(CacheUpdateEvent cacheUpdateEvent) private void DetachHandlers() { - User.UserChanged -= UserOnUserChanged; + User.Changed -= UserOnChanged; } private void MaybeUpdateData() From c74b1ea5795e12ce84e75362836f5b04c7390521 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 14:08:32 -0500 Subject: [PATCH 11/13] Renaming method --- src/GitHub.Api/Git/Repository.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index 15d7dbc28..a00b7ce37 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -704,7 +704,7 @@ public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var dateTimeOffset = managedCache.LastUpdatedAt; var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() }; - HandleGitLogCacheUpdatedEvent(updateEvent); + HandleUserCacheUpdatedEvent(updateEvent); } } @@ -749,7 +749,7 @@ public string Email private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset) { - HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent + HandleUserCacheUpdatedEvent(new CacheUpdateEvent { UpdatedTimeString = timeOffset.ToString() }); @@ -761,7 +761,7 @@ private void GitUserCacheOnCacheInvalidated() UpdateUserAndEmail(); } - private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) + private void HandleUserCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent) { Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString); Changed?.Invoke(cacheUpdateEvent); From 293f1509f93ff5d8136af4c727909793ade551f8 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 20 Nov 2017 15:37:08 -0500 Subject: [PATCH 12/13] Updating needsSaving after we get an update from the cache --- .../Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs index a411a7de0..f280b5eda 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/UserSettingsView.cs @@ -118,6 +118,7 @@ private void MaybeUpdateData() userHasChanges = false; gitName = newGitName = User.Name; gitEmail = newGitEmail = User.Email; + needsSaving = false; } } From 0d2f8a06d588f4ef53cb40c065e6390953075725 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 21 Nov 2017 07:57:03 -0500 Subject: [PATCH 13/13] Renaming ShouldRaiseCacheEvent to IsLastUpdatedTimeDifferent The new method name is clearer as to what the method is checking for --- src/GitHub.Api/Git/ManagedCacheExtensions.cs | 10 +++++----- src/GitHub.Api/Git/Repository.cs | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/GitHub.Api/Git/ManagedCacheExtensions.cs b/src/GitHub.Api/Git/ManagedCacheExtensions.cs index 760f4ef53..4590b911f 100644 --- a/src/GitHub.Api/Git/ManagedCacheExtensions.cs +++ b/src/GitHub.Api/Git/ManagedCacheExtensions.cs @@ -4,18 +4,18 @@ namespace GitHub.Unity { static class ManagedCacheExtensions { - public static bool ShouldRaiseCacheEvent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent) + public static bool IsLastUpdatedTimeDifferent(this IManagedCache managedCache, CacheUpdateEvent cacheUpdateEvent) { - bool raiseEvent; + bool isDifferent; if (cacheUpdateEvent.UpdatedTimeString == null) { - raiseEvent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue; + isDifferent = managedCache.LastUpdatedAt != DateTimeOffset.MinValue; } else { - raiseEvent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString; + isDifferent = managedCache.LastUpdatedAt.ToString() != cacheUpdateEvent.UpdatedTimeString; } - return raiseEvent; + return isDifferent; } } } \ No newline at end of file diff --git a/src/GitHub.Api/Git/Repository.cs b/src/GitHub.Api/Git/Repository.cs index 8794bdec7..2aabfd7f3 100644 --- a/src/GitHub.Api/Git/Repository.cs +++ b/src/GitHub.Api/Git/Repository.cs @@ -148,7 +148,7 @@ public void UpdateConfigData() public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.GitLogCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent); Logger.Trace("Check GitLogCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -164,7 +164,7 @@ public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent) public void CheckStatusChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.GitStatusCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent); Logger.Trace("Check GitStatusCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -195,7 +195,7 @@ public void CheckCurrentBranchAndRemoteChangedEvent(CacheUpdateEvent cacheUpdate private void CheckRepositoryInfoCacheEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.RepositoryInfoCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent); Logger.Trace("Check RepositoryInfoCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -212,7 +212,7 @@ public void CheckLocksChangedEvent(CacheUpdateEvent cacheUpdateEvent) { CacheUpdateEvent cacheUpdateEvent1 = cacheUpdateEvent; var managedCache = cacheContainer.GitLocksCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent1); + var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent1); Logger.Trace("Check GitLocksCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent1.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -275,7 +275,7 @@ public bool Equals(IRepository other) private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.BranchCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent); Logger.Trace("Check BranchCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent); @@ -685,7 +685,7 @@ public User(ICacheContainer cacheContainer) public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) { var managedCache = cacheContainer.GitUserCache; - var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent); + var raiseEvent = managedCache.IsLastUpdatedTimeDifferent(cacheUpdateEvent); Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt, cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);