Skip to content

Commit 9840491

Browse files
committed
Added dropdown for changing branches.
1 parent e8d959d commit 9840491

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/Git.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@ public static string Branch()
2929

3030
}
3131

32+
public static string[] Branches()
33+
{
34+
35+
var process = Process.Start(new ProcessStartInfo
36+
{
37+
FileName = "/usr/local/bin/git",
38+
Arguments = "for-each-ref --format='%(refname:short)' refs/heads",
39+
UseShellExecute = false,
40+
RedirectStandardOutput = true,
41+
RedirectStandardError = true,
42+
CreateNoWindow = true
43+
});
44+
45+
return process?.StandardOutput
46+
.ReadToEnd()
47+
.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
48+
.ToArray();
49+
50+
}
51+
52+
public static void CheckoutBranch(string branch)
53+
{
54+
55+
Process.Start(new ProcessStartInfo
56+
{
57+
FileName = "/usr/local/bin/git",
58+
Arguments = $"checkout {branch}",
59+
UseShellExecute = false,
60+
RedirectStandardOutput = true,
61+
RedirectStandardError = true,
62+
CreateNoWindow = true
63+
});
64+
65+
}
66+
3267
public static string[] AllChanges()
3368
{
3469

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitStatusPanel.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class GitStatusPanel : EditorWindow
1818

1919
private string _branch;
2020

21+
private string[] _branches;
22+
2123
private string[] _changedFiles;
2224

2325
private string[] _untrackedFiles;
@@ -53,7 +55,36 @@ private void Update()
5355
private void OnGUI()
5456
{
5557

56-
GUILayout.Label($"Current Branch: {_branch}");
58+
GUILayout.Space(5);
59+
60+
var selectedBranch = Array.IndexOf(_branches, _branch);
61+
62+
selectedBranch = EditorGUILayout.Popup("Branch:", selectedBranch, _branches);
63+
64+
if (!_branches[selectedBranch].Equals(_branch))
65+
{
66+
67+
if (_changedFiles?.Length > 0)
68+
{
69+
70+
EditorUtility.DisplayDialog(
71+
$"Unable to checkout branch",
72+
$"Unable to checkout {_branches[selectedBranch]} as with {_changedFiles?.Length} changes. " +
73+
"Commit, discard or stash before checking out a different branch.",
74+
"Ok");
75+
76+
}
77+
else
78+
{
79+
80+
Git.CheckoutBranch(_branches[selectedBranch]);
81+
82+
_branch = _branches[selectedBranch];
83+
84+
}
85+
86+
}
87+
5788
GUILayout.Label($"Number of Changes: {_changedFiles?.Length}");
5889
GUILayout.Label($"Untracked Files: {_untrackedFiles?.Length}");
5990
GUILayout.Label($"Last Updated: {_lastUpdated}");
@@ -72,6 +103,8 @@ private void UpdateData()
72103

73104
_branch = Git.Branch();
74105

106+
_branches = Git.Branches();
107+
75108
_changedFiles = Git.ChangedFiles();
76109

77110
_untrackedFiles = Git.UntrackedFiles();

0 commit comments

Comments
 (0)