Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit bf8a2ae

Browse files
Adding warning message if branch already exists
1 parent 089d010 commit bf8a2ae

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ class BranchesView : Subview
1616
private const string ConfirmSwitchOK = "Switch";
1717
private const string ConfirmSwitchCancel = "Cancel";
1818
private const string ConfirmCheckoutBranchTitle = "Confirm branch checkout";
19-
private const string ConfirmCheckoutBranchMessage = "Checkout branch {0}?";
19+
private const string ConfirmCheckoutBranchMessage = "Checkout branch {0} from {1}?";
2020
private const string ConfirmCheckoutBranchOK = "Checkout";
2121
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";
2225
private const string NewBranchCancelButton = "x";
2326
private const string NewBranchConfirmButton = "Create";
2427
private const string FavoritesSetting = "Favorites";
@@ -668,32 +671,53 @@ private void OnTreeNodeGUI(BranchTreeNode node)
668671
.FinallyInUI((success, e) =>
669672
{
670673
if (success)
674+
{
671675
Refresh();
676+
}
672677
else
673678
{
674679
EditorUtility.DisplayDialog(Localization.SwitchBranchTitle,
675680
String.Format(Localization.SwitchBranchFailedDescription, node.Name),
676-
Localization.Ok);
681+
Localization.Ok);
677682
}
678683
}).Start();
679684
}
680685
}
681686
else if (node.Type == NodeType.RemoteBranch)
682687
{
683-
if (EditorUtility.DisplayDialog(ConfirmCheckoutBranchTitle, String.Format(ConfirmCheckoutBranchMessage, node.Name), ConfirmCheckoutBranchOK, ConfirmCheckoutBranchCancel))
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))
684693
{
685-
GitClient.CreateBranch(selectedNode.Name.Substring(selectedNode.Name.IndexOf('/') + 1), selectedNode.Name)
686-
.FinallyInUI((success, e) =>
687-
{
688-
if (success)
689-
Refresh();
690-
else
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)
705+
{
706+
GitClient.CreateBranch(branchName, selectedNode.Name)
707+
.FinallyInUI((success, e) =>
691708
{
692-
EditorUtility.DisplayDialog(Localization.SwitchBranchTitle,
693-
String.Format(Localization.SwitchBranchFailedDescription, node.Name),
694-
Localization.Ok);
695-
}
696-
}).Start();
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+
}
697721
}
698722
}
699723
}

0 commit comments

Comments
 (0)