@@ -17,6 +17,7 @@ public interface IRepositoryManager : IDisposable
1717 event Action < List < GitLogEntry > > GitLogUpdated ;
1818 event Action < Dictionary < string , ConfigBranch > > LocalBranchesUpdated ;
1919 event Action < Dictionary < string , ConfigRemote > , Dictionary < string , Dictionary < string , ConfigBranch > > > RemoteBranchesUpdated ;
20+ event Action < GitAheadBehindStatus > GitAheadBehindStatusUpdated ;
2021
2122 void Initialize ( ) ;
2223 void Start ( ) ;
@@ -35,6 +36,7 @@ public interface IRepositoryManager : IDisposable
3536 ITask CreateBranch ( string branch , string baseBranch ) ;
3637 ITask LockFile ( string file ) ;
3738 ITask UnlockFile ( string file , bool force ) ;
39+ ITask DiscardChanges ( GitStatusEntry [ ] gitStatusEntries ) ;
3840 void UpdateGitLog ( ) ;
3941 void UpdateGitStatus ( ) ;
4042 void UpdateGitAheadBehindStatus ( ) ;
@@ -44,7 +46,6 @@ public interface IRepositoryManager : IDisposable
4446 IGitConfig Config { get ; }
4547 IGitClient GitClient { get ; }
4648 bool IsBusy { get ; }
47- event Action < GitAheadBehindStatus > GitAheadBehindStatusUpdated ;
4849 }
4950
5051 interface IRepositoryPathConfiguration
@@ -96,7 +97,10 @@ class RepositoryManager : IRepositoryManager
9697 {
9798 private readonly IGitConfig config ;
9899 private readonly IGitClient gitClient ;
100+ private readonly IProcessManager processManager ;
99101 private readonly IRepositoryPathConfiguration repositoryPaths ;
102+ private readonly IFileSystem fileSystem ;
103+ private readonly CancellationToken token ;
100104 private readonly IRepositoryWatcher watcher ;
101105
102106 private bool isBusy ;
@@ -112,18 +116,24 @@ class RepositoryManager : IRepositoryManager
112116
113117 public RepositoryManager ( IGitConfig gitConfig ,
114118 IRepositoryWatcher repositoryWatcher , IGitClient gitClient ,
119+ IProcessManager processManager ,
120+ IFileSystem fileSystem ,
121+ CancellationToken token ,
115122 IRepositoryPathConfiguration repositoryPaths )
116123 {
117124 this . repositoryPaths = repositoryPaths ;
125+ this . fileSystem = fileSystem ;
126+ this . token = token ;
118127 this . gitClient = gitClient ;
128+ this . processManager = processManager ;
119129 this . watcher = repositoryWatcher ;
120130 this . config = gitConfig ;
121131
122132 SetupWatcher ( ) ;
123133 }
124134
125- public static RepositoryManager CreateInstance ( IPlatform platform , ITaskManager taskManager ,
126- IGitClient gitClient , NPath repositoryRoot )
135+ public static RepositoryManager CreateInstance ( IPlatform platform , ITaskManager taskManager , IGitClient gitClient ,
136+ IProcessManager processManager , IFileSystem fileSystem , NPath repositoryRoot )
127137 {
128138 var repositoryPathConfiguration = new RepositoryPathConfiguration ( repositoryRoot ) ;
129139 string filePath = repositoryPathConfiguration . DotGitConfig ;
@@ -132,7 +142,8 @@ public static RepositoryManager CreateInstance(IPlatform platform, ITaskManager
132142 var repositoryWatcher = new RepositoryWatcher ( platform , repositoryPathConfiguration , taskManager . Token ) ;
133143
134144 return new RepositoryManager ( gitConfig , repositoryWatcher ,
135- gitClient , repositoryPathConfiguration ) ;
145+ gitClient , processManager , fileSystem ,
146+ taskManager . Token , repositoryPathConfiguration ) ;
136147 }
137148
138149 public void Initialize ( )
@@ -211,15 +222,13 @@ public ITask Revert(string changeset)
211222 public ITask RemoteAdd ( string remote , string url )
212223 {
213224 var task = GitClient . RemoteAdd ( remote , url ) ;
214- task = HookupHandlers ( task , true , false ) ;
215- return task ;
225+ return HookupHandlers ( task , true , false ) ;
216226 }
217227
218228 public ITask RemoteRemove ( string remote )
219229 {
220230 var task = GitClient . RemoteRemove ( remote ) ;
221- task = HookupHandlers ( task , true , false ) ;
222- return task ;
231+ return HookupHandlers ( task , true , false ) ;
223232 }
224233
225234 public ITask RemoteChange ( string remote , string url )
@@ -260,28 +269,75 @@ public ITask UnlockFile(string file, bool force)
260269
261270 public void UpdateGitLog ( )
262271 {
263- var task = GitClient . Log ( ) ;
264- task = HookupHandlers ( task , false , false ) ;
265- task . Then ( ( success , logEntries ) =>
266- {
267- if ( success )
272+ var task = GitClient
273+ . Log ( )
274+ . Then ( ( success , logEntries ) =>
268275 {
269- GitLogUpdated ? . Invoke ( logEntries ) ;
270- }
271- } ) . Start ( ) ;
276+ if ( success )
277+ {
278+ GitLogUpdated ? . Invoke ( logEntries ) ;
279+ }
280+ } ) ;
281+ task = HookupHandlers ( task , false , false ) ;
282+ task . Start ( ) ;
272283 }
273284
274285 public void UpdateGitStatus ( )
275286 {
276- var task = GitClient . Status ( ) ;
287+ var task = GitClient
288+ . Status ( )
289+ . Then ( ( success , status ) =>
290+ {
291+ if ( success )
292+ {
293+ GitStatusUpdated ? . Invoke ( status ) ;
294+ }
295+ } ) ;
277296 task = HookupHandlers ( task , true , false ) ;
278- task . Then ( ( success , status ) =>
279- {
280- if ( success )
297+ task . Start ( ) ;
298+ }
299+
300+ public ITask DiscardChanges ( GitStatusEntry [ ] gitStatusEntries )
301+ {
302+ Guard . ArgumentNotNullOrEmpty ( gitStatusEntries , "gitStatusEntries" ) ;
303+
304+ ActionTask < GitStatusEntry [ ] > task = null ;
305+ task = new ActionTask < GitStatusEntry [ ] > ( token , ( _ , entries ) =>
281306 {
282- GitStatusUpdated ? . Invoke ( status ) ;
307+ var itemsToDelete = new List < string > ( ) ;
308+ var itemsToRevert = new List < string > ( ) ;
309+
310+ foreach ( var gitStatusEntry in gitStatusEntries )
311+ {
312+ if ( gitStatusEntry . status == GitFileStatus . Added || gitStatusEntry . status == GitFileStatus . Untracked )
313+ {
314+ itemsToDelete . Add ( gitStatusEntry . path ) ;
315+ }
316+ else
317+ {
318+ itemsToRevert . Add ( gitStatusEntry . path ) ;
319+ }
320+ }
321+
322+ if ( itemsToDelete . Any ( ) )
323+ {
324+ foreach ( var itemToDelete in itemsToDelete )
325+ {
326+ fileSystem . FileDelete ( itemToDelete ) ;
327+ }
328+ }
329+
330+ ITask < string > gitDiscardTask = null ;
331+ if ( itemsToRevert . Any ( ) )
332+ {
333+ gitDiscardTask = GitClient . Discard ( itemsToRevert ) ;
334+ task . Then ( gitDiscardTask ) ;
335+ }
283336 }
284- } ) . Start ( ) ;
337+ , ( ) => gitStatusEntries ) ;
338+
339+
340+ return HookupHandlers ( task , true , true ) ;
285341 }
286342
287343 public void UpdateGitAheadBehindStatus ( )
@@ -295,15 +351,17 @@ public void UpdateGitAheadBehindStatus()
295351 var name = configBranch . Value . Name ;
296352 var trackingName = configBranch . Value . IsTracking ? configBranch . Value . Remote . Value . Name + "/" + name : "[None]" ;
297353
298- var task = GitClient . AheadBehindStatus ( name , trackingName ) ;
299- task = HookupHandlers ( task , true , false ) ;
300- task . Then ( ( success , status ) =>
301- {
302- if ( success )
354+ var task = GitClient
355+ . AheadBehindStatus ( name , trackingName )
356+ . Then ( ( success , status ) =>
303357 {
304- GitAheadBehindStatusUpdated ? . Invoke ( status ) ;
305- }
306- } ) . Start ( ) ;
358+ if ( success )
359+ {
360+ GitAheadBehindStatusUpdated ? . Invoke ( status ) ;
361+ }
362+ } ) ;
363+ task = HookupHandlers ( task , true , false ) ;
364+ task . Start ( ) ;
307365 }
308366 else
309367 {
@@ -324,9 +382,9 @@ public void UpdateLocks()
324382 } ) . Start ( ) ;
325383 }
326384
327- private ITask < T > HookupHandlers < T > ( ITask < T > task , bool isExclusive , bool filesystemChangesExpected )
385+ private ITask HookupHandlers ( ITask task , bool isExclusive , bool filesystemChangesExpected )
328386 {
329- return new ActionTask ( TaskManager . Instance . Token , ( ) => {
387+ return new ActionTask ( token , ( ) => {
330388 if ( isExclusive )
331389 {
332390 Logger . Trace ( "Starting Operation - Setting Busy Flag" ) ;
@@ -340,7 +398,7 @@ private ITask<T> HookupHandlers<T>(ITask<T> task, bool isExclusive, bool filesys
340398 }
341399 } )
342400 . Then ( task )
343- . Finally ( ( success , exception , result ) => {
401+ . Finally ( ( success , exception ) => {
344402 if ( filesystemChangesExpected )
345403 {
346404 Logger . Trace ( "Ended Operation - Enable Watcher" ) ;
@@ -353,12 +411,10 @@ private ITask<T> HookupHandlers<T>(ITask<T> task, bool isExclusive, bool filesys
353411 IsBusy = false ;
354412 }
355413
356- if ( success )
414+ if ( ! success )
357415 {
358- return result ;
416+ throw exception ;
359417 }
360-
361- throw exception ;
362418 } ) ;
363419 }
364420
0 commit comments