forked from so0-biin/Welcome-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
479 lines (392 loc) · 15.9 KB
/
Form1.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using FileManager.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace FileManager
{
public partial class Form1 : Form
{
private Explorer FilesListView;
public Form1()
{
InitializeComponent();
FilesListView = new Explorer();
FilesListView.Initialize();
splitContainer1.Panel1.Controls.Add(FilesListView);
InitializeViewOptions();
textBox1.ReadOnly = true;
button1.Text = "Create git repository";
button2.Text = "Commit";
button3.Text = "Refresh";
button4.Text = "Clone git repository";
button5.Text = "History";
}
private void NavigationPanel_AfterSelect(object sender, TreeViewEventArgs e)
{
FilesListView.Items.Clear();
FilesListView.LargeImageList.Images.Clear();
FilesListView.SmallImageList.Images.Clear();
textBox1.Text = string.Empty;
string path = (string)e.Node.Tag;
FilesListView.SelectedNavigationNode = e.Node;
this.CurrentDirectory.Text = path;
try
{
if (Directory.Exists(this.CurrentDirectory.Text + "\\.git"))
{
button1.Enabled = false;
button5.Enabled = true;
}
else
{
button1.Enabled = true;
button5.Enabled = false;
}
FilesListView.ShowFiles(path);
FilesListView.ShowDirectories(path);
}
catch
{
}
}
private void InitializeViewOptions()
{
ViewDetails.Tag = View.Details;
ViewLargeIcons.Tag = View.LargeIcon;
ViewList.Tag = View.List;
ViewSmallIcons.Tag = View.SmallIcon;
ViewTiles.Tag = View.Tile;
EventHandler viewOptionClickHandler = new EventHandler(ViewOptions_Click);
ViewDetails.Click += viewOptionClickHandler;
ViewLargeIcons.Click += viewOptionClickHandler;
ViewList.Click += viewOptionClickHandler;
ViewSmallIcons.Click += viewOptionClickHandler;
ViewTiles.Click += viewOptionClickHandler;
}
private void ViewOptions_Click(object sender, EventArgs e)
{
ToolStripMenuItem ViewOption = (sender as ToolStripMenuItem);
if (ViewOption.Checked == true)
return;
foreach (ToolStripMenuItem item in this.ViewMenuItem.DropDownItems)
{
if (item.Checked == true)
{
item.Checked = false;
}
}
ViewOption.Checked = true;
switch ((View)ViewOption.Tag)
{
case View.Details:
FilesListView.View = View.Details;
break;
case View.LargeIcon:
FilesListView.View = View.LargeIcon;
if (FilesListView.LargeImageList.ImageSize != FilesListView.LargeIconSize)
{
FilesListView.LargeImageList.ImageSize = FilesListView.LargeIconSize;
// change in Imagesize removes elements of LargeImageList.Images, thus we need the following
FilesListView.UpdateIconImages();
}
break;
case View.List:
FilesListView.View = View.List;
break;
case View.SmallIcon:
FilesListView.View = View.SmallIcon;
break;
case View.Tile:
FilesListView.View = View.Tile;
if (FilesListView.LargeImageList.ImageSize != FilesListView.TileIconSize)
{
FilesListView.LargeImageList.ImageSize = FilesListView.TileIconSize;
// change in Imagesize removes elements of LargeImageList.Images, thus we need the following
FilesListView.UpdateIconImages();
}
break;
}
}
private void SearchMenuItem_Click(object sender, EventArgs e)
{
SearchDialog searchDialog = new SearchDialog();
searchDialog.CurrentDirectory = this.CurrentDirectory.Text;
searchDialog.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
//http://redreans.tistory.com/58
// cmd를 사용하기 위한 준비
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
String directoryPath;
cmd.FileName = @"cmd";
cmd.WindowStyle = ProcessWindowStyle.Hidden; // cmd창이 숨겨지도록 하기
cmd.CreateNoWindow = true; // cmd창을 띄우지 안도록 하기
cmd.UseShellExecute = false;
cmd.RedirectStandardOutput = true; // cmd창에서 데이터를 가져오기
cmd.RedirectStandardInput = true; // cmd창으로 데이터 보내기
cmd.RedirectStandardError = true; // cmd창에서 오류 내용 가져오기
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
// cmd 다루기
StringBuilder sb = new StringBuilder();
sb.Append(this.CurrentDirectory.Text);
directoryPath = sb.ToString();
process.Start(); // cmd 명령 입히는거 시작
process.StandardInput.Write(@"cd " + directoryPath + Environment.NewLine);
StringBuilder sb2 = new StringBuilder();
process.StandardInput.Write(@"git init" + Environment.NewLine);
if (directoryPath.Length >= 1)
{
textBox1.Text = "Initialized empty Git repository in " + directoryPath + "\\.git\\";
button1.Enabled = false;
button5.Enabled = true;
}
else
{
textBox1.Text = "Choose directory to initialize empty Git repository.";
}
process.StandardInput.Close(); // cmd 명령 입력 끝
process.WaitForExit();
process.Close(); // cmd 창을 닫음
// git init file Redirection
FilesListView.Items.Clear();
try
{
if (Directory.Exists(this.CurrentDirectory.Text + "\\.git"))
{
button1.Enabled = false;
button5.Enabled = true;
}
else
{
button1.Enabled = true;
button5.Enabled = false;
}
FilesListView.ShowFiles(directoryPath);
FilesListView.ShowDirectories(directoryPath);
}
catch
{
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string[] gitStatus;
string[] result;
gitStatus = GetStatus(this.CurrentDirectory.Text);
result = FindStatus(gitStatus);
CommitMenu commitMenu = new CommitMenu(this);
commitMenu.Show(); // commitmenu 닫기 전에는 form1 제어 불가
commitMenu.SetTextBeforeCommit(this.CurrentDirectory.Text, result);
}
private void Button1_MouseHover(object sender, EventArgs e)
{
this.toolTip1.ToolTipTitle = "Check please";
this.toolTip1.IsBalloon = true;
this.toolTip1.SetToolTip(this.button1, "Check exactly that the directory you want to initialize is correct.");
}
public string[] GetStatus(string path)
{
string[] gitStatus;
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
cmd.FileName = @"cmd";
cmd.WindowStyle = ProcessWindowStyle.Hidden; // cmd창이 숨겨지도록 하기
cmd.CreateNoWindow = true; // cmd창을 띄우지 안도록 하기
cmd.UseShellExecute = false;
cmd.RedirectStandardOutput = true; // cmd창에서 데이터를 가져오기
cmd.RedirectStandardInput = true; // cmd창으로 데이터 보내기
cmd.RedirectStandardError = true; // cmd창에서 오류 내용 가져오기
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
process.Start();
process.StandardInput.Write(@"cd " + path + Environment.NewLine);
process.StandardInput.Write(@"git status" + Environment.NewLine);
// 명령어를 보낼때는 꼭 마무리를 해줘야 한다. 그래서 마지막에 NewLine가 필요하다
process.StandardInput.Close();
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
gitStatus = output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
process.WaitForExit();
process.Close();
return gitStatus;
}
public string[] FindStatus(string[] gitStatus)
{
string[] result = new string[gitStatus.Length];
bool flag = false;
int i = 0;
foreach (string status in gitStatus)
{
if (status.Equals("Changes to be committed:"))
flag = true;
if (status.Equals("Changes not staged for commit:"))
flag = false;
if (status.Equals("Untracked files:"))
flag = false;
if (flag && status.Contains("modified: "))
{
result[i++] = status;
}
if (flag && status.Contains("new file: "))
{
result[i++] = status;
}
if (flag && status.Contains("deleted: "))
{
result[i++] = status;
}
if (flag && status.Contains("renamed: "))
{
result[i++] = status;
}
}
if (result == null) //-> return 커밋할 파일이 존재하지 않아용
return result;
return result;
}
public void setTextAfterCommit(string commitFail, string[] result, string commitMsg)
{
if(commitFail == null)
{
textBox1.Text = "";
textBox1.Text += "Successfully Committed!\r\n" + commitMsg;
string directoryPath = this.CurrentDirectory.Text;
FilesListView.Items.Clear();
try
{
FilesListView.ShowFiles(directoryPath);
FilesListView.ShowDirectories(directoryPath);
}
catch
{
}
}
else
{
textBox1.Text = "";
textBox1.Text += commitFail;
}
}
public void setTextAfterClone(bool success, string[] cloneMsg)
{
textBox1.Text = "";
if(success) // success clone,
{
textBox1.Text = "Successfully Cloned! Click the Refresh button.";
}
else
{
foreach(string msg in cloneMsg)
{
textBox1.Text += msg + "\r\n"; // fail 관련해서 정리해서 넘어온 msg 출력
}
}
}
private void SplitContainer_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private bool CheckBranch(string path)
{
bool branchFlag = false;
string[] gitBranch;
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
cmd.FileName = @"cmd";
cmd.WindowStyle = ProcessWindowStyle.Hidden; // cmd창이 숨겨지도록 하기
cmd.CreateNoWindow = true; // cmd창을 띄우지 안도록 하기
cmd.UseShellExecute = false;
cmd.RedirectStandardOutput = true; // cmd창에서 데이터를 가져오기
cmd.RedirectStandardInput = true; // cmd창으로 데이터 보내기
cmd.RedirectStandardError = true; // cmd창에서 오류 내용 가져오기
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
process.Start();
process.StandardInput.Write(@"cd " + path + Environment.NewLine);
process.StandardInput.Write(@"git branch" + Environment.NewLine);
// 명령어를 보낼때는 꼭 마무리를 해줘야 한다. 그래서 마지막에 NewLine가 필요하다
process.StandardInput.Close();
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
gitBranch = output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string result in gitBranch)
{
if (!result.Contains(@"\") && !result.Contains("Microsoft"))
{
branchFlag = true;
break;
}
}
process.WaitForExit();
process.Close();
return branchFlag;
}
private void button3_Click_1(object sender, EventArgs e)
{
string directoryPath = this.CurrentDirectory.Text;
FilesListView.Items.Clear();
try
{
if (Directory.Exists(directoryPath + "\\.git"))
{
button1.Enabled = false;
button5.Enabled = true;
}
else
{
button1.Enabled = true;
button5.Enabled = false;
}
FilesListView.ShowFiles(directoryPath);
FilesListView.ShowDirectories(directoryPath);
}
catch
{
}
}
private void button4_Click_1(Object sender, EventArgs e)
{
CloneMenu cloneMenu = new CloneMenu(this);
cloneMenu.Show();
cloneMenu.SetTextBeforeClone(this.CurrentDirectory.Text);
textBox1.Text = "";
}
private void button5_Click(object sender, EventArgs e)
{
string path = this.CurrentDirectory.Text;
if (CheckBranch(path))
{
HistoryMenu historyMenu = new HistoryMenu(path);
historyMenu.Show(); // historymenu 닫기 전에는 form1 제어 불가
}
else
{
textBox1.Clear();
textBox1.Text = "current directory not yet version controlled by git";
}
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}