@@ -18,14 +18,15 @@ class ProjectWindowInterface : AssetPostprocessor
1818 private static List < GitLock > locks = new List < GitLock > ( ) ;
1919 private static List < string > guids = new List < string > ( ) ;
2020 private static List < string > guidsLocks = new List < string > ( ) ;
21- private static string loggedInUser ;
21+ private static string currentUsername ;
2222
2323 private static IApplicationManager manager ;
2424 private static bool isBusy = false ;
2525 private static ILogging logger ;
2626 private static ILogging Logger { get { return logger = logger ?? LogHelper . GetLogger < ProjectWindowInterface > ( ) ; } }
2727 private static CacheUpdateEvent lastRepositoryStatusChangedEvent ;
2828 private static CacheUpdateEvent lastLocksChangedEvent ;
29+ private static CacheUpdateEvent lastCurrentRemoteChangedEvent ;
2930 private static IRepository Repository { get { return manager != null ? manager . Environment . Repository : null ; } }
3031 private static IPlatform Platform { get { return manager != null ? manager . Platform : null ; } }
3132 private static bool IsInitialized { get { return Repository != null ; } }
@@ -37,13 +38,14 @@ public static void Initialize(IApplicationManager theManager)
3738
3839 manager = theManager ;
3940
40- Platform . Keychain . ConnectionsChanged += KeychainMayHaveChanged ;
41- KeychainMayHaveChanged ( ) ;
41+ Platform . Keychain . ConnectionsChanged += UpdateCurrentUsername ;
42+ UpdateCurrentUsername ( ) ;
4243
4344 if ( IsInitialized )
4445 {
4546 Repository . StatusEntriesChanged += RepositoryOnStatusEntriesChanged ;
4647 Repository . LocksChanged += RepositoryOnLocksChanged ;
48+ Repository . CurrentRemoteChanged += RepositoryOnCurrentRemoteChanged ;
4749 ValidateCachedData ( ) ;
4850 }
4951 }
@@ -88,9 +90,39 @@ private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
8890 }
8991 }
9092
91- private static void KeychainMayHaveChanged ( )
93+ private static void RepositoryOnCurrentRemoteChanged ( CacheUpdateEvent cacheUpdateEvent )
9294 {
93- loggedInUser = Platform . Keychain . Connections . Select ( x => x . Username ) . FirstOrDefault ( ) ;
95+ if ( ! lastCurrentRemoteChangedEvent . Equals ( cacheUpdateEvent ) )
96+ {
97+ lastCurrentRemoteChangedEvent = cacheUpdateEvent ;
98+ }
99+ }
100+
101+ private static void UpdateCurrentUsername ( )
102+ {
103+ var username = String . Empty ;
104+ if ( Repository != null )
105+ {
106+ Connection [ ] connections ;
107+ if ( ! string . IsNullOrEmpty ( Repository . CloneUrl ) )
108+ {
109+ var host = Repository . CloneUrl . ToRepositoryUri ( )
110+ . GetComponents ( UriComponents . Host , UriFormat . SafeUnescaped ) ;
111+
112+ connections = Platform . Keychain . Connections . OrderByDescending ( x => x . Host == host ) . ToArray ( ) ;
113+ }
114+ else
115+ {
116+ connections = Platform . Keychain . Connections . OrderByDescending ( HostAddress . IsGitHubDotCom ) . ToArray ( ) ;
117+ }
118+
119+ if ( connections . Any ( ) )
120+ {
121+ username = connections . First ( ) . Username ;
122+ }
123+ }
124+
125+ currentUsername = username ;
94126 }
95127
96128 [ MenuItem ( AssetsMenuRequestLock , true , 10000 ) ]
@@ -202,7 +234,7 @@ private static bool IsObjectLocked(Object selected, bool isLockedByCurrentUser)
202234 NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
203235 NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
204236
205- return locks . Any ( x => repositoryPath == x . Path && ( ! isLockedByCurrentUser || x . Owner . Name == loggedInUser ) ) ;
237+ return locks . Any ( x => repositoryPath == x . Path && ( ! isLockedByCurrentUser || x . Owner . Name == currentUsername ) ) ;
206238 }
207239
208240 private static ITask CreateUnlockObjectTask ( Object selected , bool force )
0 commit comments