@@ -33,7 +33,6 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
3333 {
3434 readonly IRepositoryHost repositoryHost ;
3535 readonly ILocalRepositoryModel repository ;
36- readonly IGitService gitService ;
3736 readonly IPullRequestService pullRequestsService ;
3837 readonly IAvatarProvider avatarProvider ;
3938 PullRequestState state ;
@@ -47,29 +46,26 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
4746 int changeCount ;
4847 ChangedFilesView changedFilesView ;
4948 OpenChangedFileAction openChangedFileAction ;
50- bool uncommittedChanges ;
5149 CheckoutMode checkoutMode ;
5250 string checkoutError ;
5351 int commitsBehind ;
52+ string checkoutDisabledMessage ;
5453
5554 /// <summary>
5655 /// Initializes a new instance of the <see cref="PullRequestDetailViewModel"/> class.
5756 /// </summary>
5857 /// <param name="connectionRepositoryHostMap">The connection repository host map.</param>
5958 /// <param name="teservice">The team explorer service.</param>
60- /// <param name="gitService">The git service.</param>
6159 /// <param name="pullRequestsService">The pull requests service.</param>
6260 /// <param name="avatarProvider">The avatar provider.</param>
6361 [ ImportingConstructor ]
6462 PullRequestDetailViewModel (
6563 IConnectionRepositoryHostMap connectionRepositoryHostMap ,
6664 ITeamExplorerServiceHolder teservice ,
67- IGitService gitService ,
6865 IPullRequestService pullRequestsService ,
6966 IAvatarProvider avatarProvider )
7067 : this ( connectionRepositoryHostMap . CurrentRepositoryHost ,
7168 teservice . ActiveRepo ,
72- gitService ,
7369 pullRequestsService ,
7470 avatarProvider )
7571 {
@@ -80,25 +76,24 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
8076 /// </summary>
8177 /// <param name="repositoryHost">The repository host.</param>
8278 /// <param name="teservice">The team explorer service.</param>
83- /// <param name="gitService">The git service.</param>
8479 /// <param name="pullRequestsService">The pull requests service.</param>
8580 /// <param name="avatarProvider">The avatar provider.</param>
8681 public PullRequestDetailViewModel (
8782 IRepositoryHost repositoryHost ,
8883 ILocalRepositoryModel repository ,
89- IGitService gitService ,
9084 IPullRequestService pullRequestsService ,
9185 IAvatarProvider avatarProvider )
9286 {
9387 this . repositoryHost = repositoryHost ;
9488 this . repository = repository ;
95- this . gitService = gitService ;
9689 this . pullRequestsService = pullRequestsService ;
9790 this . avatarProvider = avatarProvider ;
9891
99- Checkout = ReactiveCommand . CreateAsyncObservable (
100- this . WhenAnyValue ( x => x . UncommittedChanges , x => ! x ) ,
101- DoCheckout ) ;
92+ var canCheckout = this . WhenAnyValue (
93+ x => x . CheckoutMode ,
94+ x => x . CheckoutDisabledMessage ,
95+ ( mode , disabled ) => mode != CheckoutMode . UpToDate && disabled == null ) ;
96+ Checkout = ReactiveCommand . CreateAsyncObservable ( canCheckout , DoCheckout ) ;
10297
10398 OpenOnGitHub = ReactiveCommand . Create ( ) ;
10499
@@ -216,15 +211,6 @@ public OpenChangedFileAction OpenChangedFileAction
216211 set { this . RaiseAndSetIfChanged ( ref openChangedFileAction , value ) ; }
217212 }
218213
219- /// <summary>
220- /// Gets a value indicating whether there are uncommitted changes blocking a checkout.
221- /// </summary>
222- public bool UncommittedChanges
223- {
224- get { return uncommittedChanges ; }
225- private set { this . RaiseAndSetIfChanged ( ref uncommittedChanges , value ) ; }
226- }
227-
228214 /// <summary>
229215 /// Gets the checkout mode for the pull request.
230216 /// </summary>
@@ -253,6 +239,15 @@ public int CommitsBehind
253239 private set { this . RaiseAndSetIfChanged ( ref commitsBehind , value ) ; }
254240 }
255241
242+ /// <summary>
243+ /// Gets a message indicating the why the <see cref="Checkout"/> command is disabled.
244+ /// </summary>
245+ public string CheckoutDisabledMessage
246+ {
247+ get { return checkoutDisabledMessage ; }
248+ private set { this . RaiseAndSetIfChanged ( ref checkoutDisabledMessage , value ) ; }
249+ }
250+
256251 /// <summary>
257252 /// Gets the changed files as a tree.
258253 /// </summary>
@@ -334,14 +329,6 @@ public async Task Load(PullRequest pullRequest, IList<PullRequestFile> files)
334329 ChangedFilesTree . Add ( change ) ;
335330 }
336331
337- var repo = gitService . GetRepository ( repository . LocalPath ) ;
338- UncommittedChanges = repo . RetrieveStatus ( ) . IsDirty ;
339-
340- if ( UncommittedChanges )
341- {
342- CheckoutError = "Cannot check out: you have uncommitted changes" ;
343- }
344-
345332 var localBranches = await pullRequestsService . GetLocalBranches ( repository , Number ) . ToList ( ) ;
346333
347334 if ( localBranches . Contains ( repository . CurrentBranch ) )
@@ -371,6 +358,12 @@ public async Task Load(PullRequest pullRequest, IList<PullRequestFile> files)
371358 CheckoutMode = CheckoutMode . Fetch ;
372359 }
373360
361+ var clean = await pullRequestsService . CleanForCheckout ( repository ) ;
362+
363+ CheckoutDisabledMessage = ( ! clean && CheckoutMode != CheckoutMode . UpToDate ) ?
364+ $ "Cannot { GetCheckoutModeDescription ( CheckoutMode ) } as your working directory has uncommitted changes." :
365+ null ;
366+
374367 IsBusy = false ;
375368 }
376369
@@ -411,6 +404,22 @@ static IPullRequestDirectoryViewModel CreateChangedFilesTree(IEnumerable<IPullRe
411404 return dirs [ string . Empty ] ;
412405 }
413406
407+ static string GetCheckoutModeDescription ( CheckoutMode checkoutMode )
408+ {
409+ switch ( checkoutMode )
410+ {
411+ case CheckoutMode . NeedsPull :
412+ return "update branch" ;
413+ case CheckoutMode . Switch :
414+ return "switch branches" ;
415+ case CheckoutMode . Fetch :
416+ return "checkout pull request" ;
417+ default :
418+ Debug . Fail ( "Invalid CheckoutMode in GetCheckoutModeDescription" ) ;
419+ return null ;
420+ }
421+ }
422+
414423 static PullRequestDirectoryViewModel GetDirectory ( string path , Dictionary < string , PullRequestDirectoryViewModel > dirs )
415424 {
416425 PullRequestDirectoryViewModel dir ;
0 commit comments