@@ -44,9 +44,7 @@ internal static class GitHelpers
44
44
? CLIENT_ID_SECRET
45
45
: string . Empty ;
46
46
47
- private static ThreadWithMessageQueue ? _owningThread ;
48
-
49
- private static int _activeOperationsCount = 0 ;
47
+ private static readonly SemaphoreSlim GitOperationSemaphore = new SemaphoreSlim ( 1 , 1 ) ;
50
48
51
49
private static bool _IsExecutingGitAction ;
52
50
public static bool IsExecutingGitAction
@@ -66,14 +64,6 @@ private set
66
64
67
65
public static event EventHandler ? GitFetchCompleted ;
68
66
69
- public static void TryDispose ( )
70
- {
71
- var threadToDispose = _owningThread ;
72
- _owningThread = null ;
73
- Interlocked . Exchange ( ref _activeOperationsCount , 0 ) ;
74
- threadToDispose ? . Dispose ( ) ;
75
- }
76
-
77
67
public static string ? GetGitRepositoryPath ( string ? path , string root )
78
68
{
79
69
if ( string . IsNullOrEmpty ( root ) )
@@ -125,7 +115,7 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
125
115
if ( string . IsNullOrWhiteSpace ( path ) || ! Repository . IsValid ( path ) )
126
116
return Array . Empty < BranchItem > ( ) ;
127
117
128
- var ( result , returnValue ) = await PostMethodToThreadWithMessageQueueAsync < ( GitOperationResult , BranchItem [ ] ) > ( ( ) =>
118
+ var ( result , returnValue ) = await DoGitOperationAsync < ( GitOperationResult , BranchItem [ ] ) > ( ( ) =>
129
119
{
130
120
var branches = Array . Empty < BranchItem > ( ) ;
131
121
var result = GitOperationResult . Success ;
@@ -157,7 +147,7 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
157
147
if ( string . IsNullOrWhiteSpace ( path ) || ! Repository . IsValid ( path ) )
158
148
return null ;
159
149
160
- var ( _, returnValue ) = await PostMethodToThreadWithMessageQueueAsync < ( GitOperationResult , BranchItem ? ) > ( ( ) =>
150
+ var ( _, returnValue ) = await DoGitOperationAsync < ( GitOperationResult , BranchItem ? ) > ( ( ) =>
161
151
{
162
152
BranchItem ? head = null ;
163
153
try
@@ -179,7 +169,7 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
179
169
}
180
170
181
171
return ( GitOperationResult . Success , head ) ;
182
- } ) ;
172
+ } , true ) ;
183
173
184
174
return returnValue ;
185
175
}
@@ -228,7 +218,7 @@ public static async Task<bool> Checkout(string? repositoryPath, string? branch)
228
218
}
229
219
}
230
220
231
- var result = await PostMethodToThreadWithMessageQueueAsync < GitOperationResult > ( ( ) =>
221
+ var result = await DoGitOperationAsync < GitOperationResult > ( ( ) =>
232
222
{
233
223
try
234
224
{
@@ -306,7 +296,7 @@ public static async Task DeleteBranchAsync(string? repositoryPath, string? activ
306
296
307
297
IsExecutingGitAction = true ;
308
298
309
- await PostMethodToThreadWithMessageQueueAsync < GitOperationResult > ( ( ) =>
299
+ await DoGitOperationAsync < GitOperationResult > ( ( ) =>
310
300
{
311
301
try
312
302
{
@@ -363,7 +353,7 @@ public static async void FetchOrigin(string? repositoryPath)
363
353
IsExecutingGitAction = true ;
364
354
} ) ;
365
355
366
- await PostMethodToThreadWithMessageQueueAsync < GitOperationResult > ( ( ) =>
356
+ await DoGitOperationAsync < GitOperationResult > ( ( ) =>
367
357
{
368
358
var result = GitOperationResult . Success ;
369
359
try
@@ -422,7 +412,7 @@ public static async Task PullOriginAsync(string? repositoryPath)
422
412
IsExecutingGitAction = true ;
423
413
} ) ;
424
414
425
- var result = await PostMethodToThreadWithMessageQueueAsync < GitOperationResult > ( ( ) =>
415
+ var result = await DoGitOperationAsync < GitOperationResult > ( ( ) =>
426
416
{
427
417
try
428
418
{
@@ -508,7 +498,7 @@ public static async Task PushToOriginAsync(string? repositoryPath, string? branc
508
498
b => b . UpstreamBranch = branch . CanonicalName ) ;
509
499
}
510
500
511
- var result = await PostMethodToThreadWithMessageQueueAsync < GitOperationResult > ( ( ) =>
501
+ var result = await DoGitOperationAsync < GitOperationResult > ( ( ) =>
512
502
{
513
503
try
514
504
{
@@ -830,29 +820,22 @@ private static bool IsAuthorizationException(Exception ex)
830
820
ex . Message . Contains ( "authentication replays" , StringComparison . OrdinalIgnoreCase ) ;
831
821
}
832
822
833
- private static void DisposeIfFinished ( )
823
+ private static async Task < T ? > DoGitOperationAsync < T > ( Func < object > payload , bool useSemaphore = false )
834
824
{
835
- if ( Interlocked . Decrement ( ref _activeOperationsCount ) == 0 )
836
- TryDispose ( ) ;
837
- }
838
-
839
- private static async Task < T ? > PostMethodToThreadWithMessageQueueAsync < T > ( Func < object > payload )
840
- {
841
- T ? returnValue = default ;
842
-
843
- Interlocked . Increment ( ref _activeOperationsCount ) ;
844
- _owningThread ??= new ThreadWithMessageQueue ( ) ;
825
+ if ( useSemaphore )
826
+ await GitOperationSemaphore . WaitAsync ( ) ;
827
+ else
828
+ await Task . Yield ( ) ;
845
829
846
830
try
847
831
{
848
- returnValue = await _owningThread . PostMethod < T > ( payload ) ;
832
+ return ( T ) payload ( ) ;
849
833
}
850
834
finally
851
835
{
852
- DisposeIfFinished ( ) ;
836
+ if ( useSemaphore )
837
+ GitOperationSemaphore . Release ( ) ;
853
838
}
854
-
855
- return returnValue ;
856
839
}
857
840
}
858
841
}
0 commit comments