@@ -15,6 +15,13 @@ class BranchesView : Subview
15
15
private const string ConfirmSwitchMessage = "Switch branch to {0}?" ;
16
16
private const string ConfirmSwitchOK = "Switch" ;
17
17
private const string ConfirmSwitchCancel = "Cancel" ;
18
+ private const string ConfirmCheckoutBranchTitle = "Confirm branch checkout" ;
19
+ private const string ConfirmCheckoutBranchMessage = "Checkout branch {0} from {1}?" ;
20
+ private const string ConfirmCheckoutBranchOK = "Checkout" ;
21
+ private const string ConfirmCheckoutBranchCancel = "Cancel" ;
22
+ private const string WarningCheckoutBranchExistsTitle = "Branch already exists" ;
23
+ private const string WarningCheckoutBranchExistsMessage = "Branch {0} already exists" ;
24
+ private const string WarningCheckoutBranchExistsOK = "Ok" ;
18
25
private const string NewBranchCancelButton = "x" ;
19
26
private const string NewBranchConfirmButton = "Create" ;
20
27
private const string FavoritesSetting = "Favorites" ;
@@ -23,6 +30,10 @@ class BranchesView : Subview
23
30
private const string LocalTitle = "Local branches" ;
24
31
private const string RemoteTitle = "Remote branches" ;
25
32
private const string CreateBranchButton = "New Branch" ;
33
+ private const string DeleteBranchMessageFormatString = "Are you sure you want to delete the branch: {0}?" ;
34
+ private const string DeleteBranchTitle = "Delete Branch?" ;
35
+ private const string DeleteBranchButton = "Delete" ;
36
+ private const string CancelButtonLabel = "Cancel" ;
26
37
27
38
private bool showLocalBranches = true ;
28
39
private bool showRemoteBranches = true ;
@@ -147,7 +158,7 @@ public void OnEmbeddedGUI()
147
158
148
159
GUILayout . BeginHorizontal ( ) ;
149
160
{
150
- OnCreateGUI ( ) ;
161
+ OnButtonBarGUI ( ) ;
151
162
}
152
163
GUILayout . EndHorizontal ( ) ;
153
164
@@ -308,6 +319,9 @@ private void OnRemoteBranchesUpdate(IEnumerable<GitBranch> list)
308
319
309
320
private void BuildTree ( IEnumerable < GitBranch > local , IEnumerable < GitBranch > remote )
310
321
{
322
+ //Clear the selected node
323
+ selectedNode = null ;
324
+
311
325
// Sort
312
326
var localBranches = new List < GitBranch > ( local ) ;
313
327
var remoteBranches = new List < GitBranch > ( remote ) ;
@@ -451,28 +465,28 @@ private void SetFavourite(BranchTreeNode branch, bool favourite)
451
465
}
452
466
}
453
467
454
- private void OnCreateGUI ( )
468
+ private void OnButtonBarGUI ( )
455
469
{
456
- // Create button
457
470
if ( mode == BranchesMode . Default )
458
471
{
472
+ // Delete button
459
473
// If the current branch is selected, then do not enable the Delete button
460
- var disableDelete = activeBranchNode == selectedNode ;
474
+ var disableDelete = selectedNode == null || selectedNode . Type == NodeType . Folder || activeBranchNode == selectedNode ;
461
475
EditorGUI . BeginDisabledGroup ( disableDelete ) ;
462
476
{
463
- if ( GUILayout . Button ( "Delete" , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
477
+ if ( GUILayout . Button ( DeleteBranchButton , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
464
478
{
465
479
var selectedBranchName = selectedNode . Name ;
466
- var dialogTitle = "Delete Branch: " + selectedBranchName ;
467
- var dialogMessage = "Are you sure you want to delete the branch: " + selectedBranchName + "?" ;
468
- if ( EditorUtility . DisplayDialog ( "Delete Branch?" , dialogMessage , "Delete" , "Cancel" ) )
480
+ var dialogMessage = string . Format ( DeleteBranchMessageFormatString , selectedBranchName ) ;
481
+ if ( EditorUtility . DisplayDialog ( DeleteBranchTitle , dialogMessage , DeleteBranchButton , CancelButtonLabel ) )
469
482
{
470
483
GitClient . DeleteBranch ( selectedBranchName , true ) . Start ( ) ;
471
484
}
472
485
}
473
486
}
474
487
EditorGUI . EndDisabledGroup ( ) ;
475
488
489
+ // Create button
476
490
GUILayout . FlexibleSpace ( ) ;
477
491
if ( GUILayout . Button ( CreateBranchButton , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
478
492
{
@@ -649,37 +663,62 @@ private void OnTreeNodeGUI(BranchTreeNode node)
649
663
650
664
if ( Event . current . clickCount > 1 && mode == BranchesMode . Default )
651
665
{
652
- if ( node . Type == NodeType . LocalBranch &&
653
- EditorUtility . DisplayDialog ( ConfirmSwitchTitle , String . Format ( ConfirmSwitchMessage , node . Name ) , ConfirmSwitchOK ,
654
- ConfirmSwitchCancel ) )
666
+ if ( node . Type == NodeType . LocalBranch )
655
667
{
656
- GitClient . SwitchBranch ( node . Name )
657
- . FinallyInUI ( ( success , e ) =>
658
- {
659
- if ( success )
660
- Refresh ( ) ;
661
- else
668
+ if ( EditorUtility . DisplayDialog ( ConfirmSwitchTitle , String . Format ( ConfirmSwitchMessage , node . Name ) , ConfirmSwitchOK , ConfirmSwitchCancel ) )
669
+ {
670
+ GitClient . SwitchBranch ( node . Name )
671
+ . FinallyInUI ( ( success , e ) =>
662
672
{
663
- EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
664
- String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
665
- Localization . Ok ) ;
666
- }
667
- } ) . Start ( ) ;
673
+ if ( success )
674
+ {
675
+ Refresh ( ) ;
676
+ }
677
+ else
678
+ {
679
+ EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
680
+ String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
681
+ Localization . Ok ) ;
682
+ }
683
+ } ) . Start ( ) ;
684
+ }
668
685
}
669
686
else if ( node . Type == NodeType . RemoteBranch )
670
687
{
671
- GitClient . CreateBranch ( selectedNode . Name . Substring ( selectedNode . Name . IndexOf ( '/' ) + 1 ) , selectedNode . Name )
672
- . FinallyInUI ( ( success , e ) =>
688
+ var indexOfFirstSlash = selectedNode . Name . IndexOf ( '/' ) ;
689
+ var originName = selectedNode . Name . Substring ( 0 , indexOfFirstSlash ) ;
690
+ var branchName = selectedNode . Name . Substring ( indexOfFirstSlash + 1 ) ;
691
+
692
+ if ( Repository . LocalBranches . Any ( localBranch => localBranch . Name == branchName ) )
693
+ {
694
+ EditorUtility . DisplayDialog ( WarningCheckoutBranchExistsTitle ,
695
+ String . Format ( WarningCheckoutBranchExistsMessage , branchName ) ,
696
+ WarningCheckoutBranchExistsOK ) ;
697
+ }
698
+ else
699
+ {
700
+ var confirmCheckout = EditorUtility . DisplayDialog ( ConfirmCheckoutBranchTitle ,
701
+ String . Format ( ConfirmCheckoutBranchMessage , node . Name , originName ) ,
702
+ ConfirmCheckoutBranchOK , ConfirmCheckoutBranchCancel ) ;
703
+
704
+ if ( confirmCheckout )
673
705
{
674
- if ( success )
675
- Refresh ( ) ;
676
- else
677
- {
678
- EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
679
- String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
680
- Localization . Ok ) ;
681
- }
682
- } ) . Start ( ) ;
706
+ GitClient . CreateBranch ( branchName , selectedNode . Name )
707
+ . FinallyInUI ( ( success , e ) =>
708
+ {
709
+ if ( success )
710
+ {
711
+ Refresh ( ) ;
712
+ }
713
+ else
714
+ {
715
+ EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
716
+ String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
717
+ Localization . Ok ) ;
718
+ }
719
+ } ) . Start ( ) ;
720
+ }
721
+ }
683
722
}
684
723
}
685
724
}
0 commit comments