1+ using System ;
12using GitHub . Logging ;
23using System . Collections . Generic ;
34using System . Linq ;
45using UnityEditor ;
56using UnityEngine ;
7+ using Object = UnityEngine . Object ;
68
79namespace GitHub . Unity
810{
@@ -81,138 +83,96 @@ private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
8183 }
8284 }
8385
84- [ MenuItem ( AssetsMenuRequestLock , true ) ]
86+ [ MenuItem ( AssetsMenuRequestLock , true , 10000 ) ]
8587 private static bool ContextMenu_CanLock ( )
8688 {
8789 if ( ! EnsureInitialized ( ) )
8890 return false ;
8991 if ( isBusy )
9092 return false ;
91-
9293 return Selection . objects . Any ( IsObjectUnlocked ) ;
9394 }
9495
95- private static bool IsObjectUnlocked ( Object selected )
96+ [ MenuItem ( AssetsMenuReleaseLock , true , 10001 ) ]
97+ private static bool ContextMenu_CanUnlock ( )
9698 {
97- if ( selected == null )
99+ if ( ! EnsureInitialized ( ) )
98100 return false ;
99-
100- NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
101- NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
102-
103- var alreadyLocked = locks . Any ( x => repositoryPath == x . Path ) ;
104- GitFileStatus status = GitFileStatus . None ;
105- if ( entries != null )
106- {
107- status = entries . FirstOrDefault ( x => repositoryPath == x . Path . ToNPath ( ) ) . Status ;
108- }
109-
110- return ! alreadyLocked && status != GitFileStatus . Untracked && status != GitFileStatus . Ignored ;
111- }
112-
113- [ MenuItem ( AssetsMenuRequestLock ) ]
114- private static void ContextMenu_Lock ( )
115- {
116- isBusy = true ;
117-
118- var unlockedObjects = Selection . objects . Where ( IsObjectUnlocked ) . ToArray ( ) ;
119- var tasks = unlockedObjects . Select ( CreateLockObjectTask ) . ToArray ( ) ;
120-
121- var taskQueue = new TaskQueue ( ) ;
122- foreach ( var task in tasks )
123- {
124- taskQueue . Queue ( task ) ;
125- }
126-
127- taskQueue . FinallyInUI ( ( success , exception ) =>
128- {
129- if ( ! success )
130- {
131- var error = exception . Message ;
132- if ( error . Contains ( "exit status 255" ) )
133- error = "Failed to lock: no permissions" ;
134- EditorUtility . DisplayDialog ( Localization . RequestLockActionTitle ,
135- error ,
136- Localization . Ok ) ;
137- }
138-
139- isBusy = false ;
140- Selection . activeGameObject = null ;
141- } ) . Start ( ) ;
142- }
143-
144- private static ITask CreateLockObjectTask ( Object selected )
145- {
146- NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
147- NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
148-
149- var task = Repository . RequestLock ( repositoryPath ) ;
150- task . OnEnd += ( _ , s , ___ ) => { if ( s ) manager . TaskManager . Run ( manager . UsageTracker . IncrementUnityProjectViewContextLfsLock , null ) ; } ;
151- return task ;
101+ if ( isBusy )
102+ return false ;
103+ return Selection . objects . Any ( IsObjectLocked ) ;
152104 }
153105
154- [ MenuItem ( AssetsMenuReleaseLock , true , 1000 ) ]
155- private static bool ContextMenu_CanUnlock ( )
106+ [ MenuItem ( AssetsMenuReleaseLockForced , true , 10002 ) ]
107+ private static bool ContextMenu_CanUnlockForce ( )
156108 {
157109 if ( ! EnsureInitialized ( ) )
158110 return false ;
159111 if ( isBusy )
160112 return false ;
161-
162113 return Selection . objects . Any ( IsObjectLocked ) ;
163114 }
164115
165- private static bool IsObjectLocked ( Object selected )
116+ [ MenuItem ( AssetsMenuRequestLock , false , 10000 ) ]
117+ private static void ContextMenu_Lock ( )
166118 {
167- if ( selected == null )
168- return false ;
169-
170- NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
171- NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
172-
173- return locks . Any ( x => repositoryPath == x . Path ) ;
119+ RunLockUnlock ( IsObjectUnlocked , CreateLockObjectTask , Localization . RequestLockActionTitle , "Failed to lock: no permissions" ) ;
174120 }
175121
176- [ MenuItem ( AssetsMenuReleaseLock , false , 1000 ) ]
122+ [ MenuItem ( AssetsMenuReleaseLockForced , false , 10001 ) ]
177123 private static void ContextMenu_Unlock ( )
178124 {
179- isBusy = true ;
125+ RunLockUnlock ( IsObjectLocked , x => CreateUnlockObjectTask ( x , false ) , Localization . ReleaseLockActionTitle , "Failed to unlock: no permissions" ) ;
126+ }
180127
181- var lockedObjects = Selection . objects . Where ( IsObjectLocked ) . ToArray ( ) ;
182- var tasks = lockedObjects . Select ( o => CreateUnlockObjectTask ( o , false ) ) . ToArray ( ) ;
128+ [ MenuItem ( AssetsMenuReleaseLockForced , false , 10002 ) ]
129+ private static void ContextMenu_UnlockForce ( )
130+ {
131+ RunLockUnlock ( IsObjectLocked , x => CreateUnlockObjectTask ( x , true ) , Localization . ReleaseLockActionTitle , "Failed to unlock: no permissions" ) ;
132+ }
183133
134+ private static void RunLockUnlock ( Func < Object , bool > selector , Func < Object , ITask > creator , string title , string errorMessage )
135+ {
136+ isBusy = true ;
184137 var taskQueue = new TaskQueue ( ) ;
185- foreach ( var task in tasks )
138+ foreach ( var lockedObject in Selection . objects . Where ( selector ) )
186139 {
187- taskQueue . Queue ( task ) ;
140+ taskQueue . Queue ( creator ( lockedObject ) ) ;
188141 }
189-
190142 taskQueue . FinallyInUI ( ( success , exception ) =>
191143 {
192144 if ( ! success )
193145 {
194146 var error = exception . Message ;
195147 if ( error . Contains ( "exit status 255" ) )
196- error = "Failed to unlock: no permissions" ;
197- EditorUtility . DisplayDialog ( Localization . RequestLockActionTitle ,
198- error ,
199- Localization . Ok ) ;
148+ error = errorMessage ;
149+ EditorUtility . DisplayDialog ( title , error , Localization . Ok ) ;
200150 }
201-
202151 isBusy = false ;
203- Selection . activeGameObject = null ;
204- } ) . Start ( ) ;
152+ } ) ;
153+ taskQueue . Start ( ) ;
205154 }
206155
207- [ MenuItem ( AssetsMenuReleaseLockForced , true , 1000 ) ]
208- private static bool ContextMenu_CanUnlockForce ( )
156+ private static bool IsObjectUnlocked ( Object selected )
209157 {
210- if ( ! EnsureInitialized ( ) )
211- return false ;
212- if ( isBusy )
158+ if ( selected == null )
213159 return false ;
214160
215- var selected = Selection . activeObject ;
161+ NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
162+ NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
163+
164+ var alreadyLocked = locks . Any ( x => repositoryPath == x . Path ) ;
165+ GitFileStatus status = GitFileStatus . None ;
166+ if ( entries != null )
167+ {
168+ status = entries . FirstOrDefault ( x => repositoryPath == x . Path . ToNPath ( ) ) . Status ;
169+ }
170+
171+ return ! alreadyLocked && status != GitFileStatus . Untracked && status != GitFileStatus . Ignored ;
172+ }
173+
174+ private static bool IsObjectLocked ( Object selected )
175+ {
216176 if ( selected == null )
217177 return false ;
218178
@@ -222,40 +182,24 @@ private static bool ContextMenu_CanUnlockForce()
222182 return locks . Any ( x => repositoryPath == x . Path ) ;
223183 }
224184
225- [ MenuItem ( AssetsMenuReleaseLockForced , false , 1000 ) ]
226- private static void ContextMenu_UnlockForce ( )
185+ private static ITask CreateUnlockObjectTask ( Object selected , bool force )
227186 {
228- isBusy = true ;
229-
230- var lockedObjects = Selection . objects . Where ( IsObjectLocked ) . ToArray ( ) ;
231- var tasks = lockedObjects . Select ( o => CreateUnlockObjectTask ( o , true ) ) . ToArray ( ) ;
232-
233- var taskQueue = new TaskQueue ( ) ;
234- foreach ( var task in tasks )
235- {
236- taskQueue . Queue ( task ) ;
237- }
187+ NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
188+ NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
238189
239- taskQueue . FinallyInUI ( ( success , exception ) =>
240- {
241- isBusy = false ;
242- Selection . activeGameObject = null ;
243- } ) . Start ( ) ;
190+ var task = Repository . ReleaseLock ( repositoryPath , force ) ;
191+ task . OnEnd += ( _ , s , __ ) => { if ( s ) manager . TaskManager . Run ( manager . UsageTracker . IncrementUnityProjectViewContextLfsUnlock , null ) ; } ;
192+ return task ;
244193 }
245194
246- private static ITask CreateUnlockObjectTask ( Object selected , bool force )
195+ private static ITask CreateLockObjectTask ( Object selected )
247196 {
248197 NPath assetPath = AssetDatabase . GetAssetPath ( selected . GetInstanceID ( ) ) . ToNPath ( ) ;
249198 NPath repositoryPath = manager . Environment . GetRepositoryPath ( assetPath ) ;
250199
251- return Repository . ReleaseLock ( repositoryPath , force )
252- . FinallyInUI ( ( success , ex ) =>
253- {
254- if ( success )
255- {
256- manager . TaskManager . Run ( manager . UsageTracker . IncrementUnityProjectViewContextLfsUnlock , null ) ;
257- }
258- } ) ;
200+ var task = Repository . RequestLock ( repositoryPath ) ;
201+ task . OnEnd += ( _ , s , ___ ) => { if ( s ) manager . TaskManager . Run ( manager . UsageTracker . IncrementUnityProjectViewContextLfsLock , null ) ; } ;
202+ return task ;
259203 }
260204
261205 private static void OnLocksUpdate ( )
0 commit comments