-
Notifications
You must be signed in to change notification settings - Fork 20
/
FormNewBranch.cs
157 lines (145 loc) · 5.66 KB
/
FormNewBranch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Form to create a new branch from a list of options.
/// </summary>
public partial class FormNewBranch : Form
{
/// <summary>
/// Shortcut variable to the main app's current repo
/// </summary>
private readonly ClassBranches branches;
/// <summary>
/// Singular branch origin selected among various options with a radio button
/// </summary>
private string origin = "";
public FormNewBranch()
{
InitializeComponent();
ClassWinGeometry.Restore(this);
branches = App.Repos.Current.Branches;
}
/// <summary>
/// Form is closing.
/// </summary>
private void FormNewBranchFormClosing(object sender, FormClosingEventArgs e)
{
ClassWinGeometry.Save(this);
}
/// <summary>
/// Called on a change of radio button selection for the branch origin
/// </summary>
private void RadioBranchSourceCheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked == false)
{
switch (rb.Tag.ToString())
{
case "SHA1":
textSHA1.Enabled = false;
break;
default:
listBranches.Enabled = false;
listBranches.BackColor = SystemColors.Control;
break;
}
origin = null;
}
else
{
switch (rb.Tag.ToString())
{
case "Head":
break;
case "SHA1":
textSHA1.Enabled = true;
break;
case "Local":
listBranches.Items.Clear();
foreach (var branch in branches.Local)
listBranches.Items.Add(branch);
listBranches.Enabled = true;
listBranches.BackColor = SystemColors.Window;
break;
case "Remote":
listBranches.Items.Clear();
foreach (var branch in branches.Remote)
listBranches.Items.Add(branch);
listBranches.Enabled = true;
listBranches.BackColor = SystemColors.Window;
break;
case "Tag":
listBranches.Items.Clear();
ExecResult result = App.Repos.Current.Run("tag");
if (result.Success())
{
string[] tags = result.stdout.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (var tag in tags)
listBranches.Items.Add(tag);
listBranches.Enabled = true;
}
listBranches.BackColor = SystemColors.Window;
break;
default:
break;
}
}
}
/// <summary>
/// Button clicked to create a new branch and exit the dialog
/// </summary>
private void BtCreateClick(object sender, EventArgs e)
{
string name = textBranchName.Text.Trim();
string cmd = String.Format("branch {0} {1}", name, origin);
ExecResult result = App.Repos.Current.RunCmd(cmd);
if (result.Success())
{
// Check out the branch if needed
if (checkCheckOut.Checked)
{
result = App.Repos.Current.RunCmd("checkout " + name);
if (result.Success() == false)
MessageBox.Show(result.stderr, "Error checking out a branch", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
else
MessageBox.Show(result.stderr, "Error creating a branch", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// Store the selected item name of a local, remote branch or tag into the origin
/// tracking variable.
/// </summary>
private void ListBoxSelectedIndexChanged(object sender, EventArgs e)
{
origin = listBranches.SelectedItem.ToString();
}
/// <summary>
/// Store the SHA1 textual value as it is being typed, into the origin
/// tracking variable
/// </summary>
private void TextSha1TextChanged(object sender, EventArgs e)
{
origin = textSHA1.Text;
}
/// <summary>
/// Limit the character set that can be used to specify the branch name
/// or SHA1 key (somewhat loosely in order to reuse the function)
/// </summary>
private void TextBranchNameKeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsLetterOrDigit(e.KeyChar) && e.KeyChar != '/' && e.KeyChar != '_' && e.KeyChar != '-' && !char.IsControl(e.KeyChar))
e.Handled = true;
}
private void TextBranchNameKeyUp(object sender, KeyEventArgs e)
{
// Enable the Create button if we have the branch name
btCreate.Enabled = textBranchName.Text.Length > 0;
}
}
}