forked from trevorr/vss2git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
executable file
·261 lines (227 loc) · 9.56 KB
/
MainForm.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
/* Copyright 2009 HPDI, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Hpdi.VssLogicalLib;
namespace Hpdi.Vss2Git
{
/// <summary>
/// Main form for the application.
/// </summary>
/// <author>Trevor Robinson</author>
public partial class MainForm : Form
{
private readonly Dictionary<int, EncodingInfo> codePages = new Dictionary<int, EncodingInfo>();
private readonly WorkQueue workQueue = new WorkQueue(1);
private Logger logger = Logger.Null;
private RevisionAnalyzer revisionAnalyzer;
private ChangesetBuilder changesetBuilder;
public MainForm()
{
InitializeComponent();
}
private void OpenLog(string filename)
{
logger = string.IsNullOrEmpty(filename) ? Logger.Null : new Logger(filename);
}
private void goButton_Click(object sender, EventArgs e)
{
try
{
OpenLog(logTextBox.Text);
logger.WriteLine("VSS2Git version {0}", Assembly.GetExecutingAssembly().GetName().Version);
WriteSettings();
Encoding encoding = Encoding.Default;
EncodingInfo encodingInfo;
if (codePages.TryGetValue(encodingComboBox.SelectedIndex, out encodingInfo))
{
encoding = encodingInfo.GetEncoding();
}
logger.WriteLine("VSS encoding: {0} (CP: {1}, IANA: {2})",
encoding.EncodingName, encoding.CodePage, encoding.WebName);
logger.WriteLine("Comment transcoding: {0}",
transcodeCheckBox.Checked ? "enabled" : "disabled");
logger.WriteLine("Ignore errors: {0}",
ignoreErrorsCheckBox.Checked ? "enabled" : "disabled");
var df = new VssDatabaseFactory(vssDirTextBox.Text);
df.Encoding = encoding;
var db = df.Open();
var path = vssProjectTextBox.Text;
VssItem item;
try
{
item = db.GetItem(path);
}
catch (VssPathException ex)
{
MessageBox.Show(ex.Message, "Invalid project path",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var project = item as VssProject;
if (project == null)
{
MessageBox.Show(path + " is not a project", "Invalid project path",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
revisionAnalyzer = new RevisionAnalyzer(workQueue, logger, db);
if (!string.IsNullOrEmpty(excludeTextBox.Text))
{
revisionAnalyzer.ExcludeFiles = excludeTextBox.Text;
}
revisionAnalyzer.AddItem(project);
changesetBuilder = new ChangesetBuilder(workQueue, logger, revisionAnalyzer);
changesetBuilder.AnyCommentThreshold = TimeSpan.FromSeconds((double)anyCommentUpDown.Value);
changesetBuilder.SameCommentThreshold = TimeSpan.FromSeconds((double)sameCommentUpDown.Value);
changesetBuilder.BuildChangesets();
if (!string.IsNullOrEmpty(outDirTextBox.Text))
{
var gitExporter = new GitExporter(workQueue, logger,
revisionAnalyzer, changesetBuilder);
if (!string.IsNullOrEmpty(domainTextBox.Text))
{
gitExporter.EmailDomain = domainTextBox.Text;
}
if (!string.IsNullOrEmpty(commentTextBox.Text))
{
gitExporter.DefaultComment = commentTextBox.Text;
}
if (!transcodeCheckBox.Checked)
{
gitExporter.CommitEncoding = encoding;
}
gitExporter.IgnoreErrors = ignoreErrorsCheckBox.Checked;
gitExporter.ExportToGit(outDirTextBox.Text);
}
workQueue.Idle += delegate
{
logger.Dispose();
logger = Logger.Null;
};
statusTimer.Enabled = true;
goButton.Enabled = false;
}
catch (Exception ex)
{
logger.Dispose();
logger = Logger.Null;
ShowException(ex);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
workQueue.Abort();
}
private void statusTimer_Tick(object sender, EventArgs e)
{
statusLabel.Text = workQueue.LastStatus ?? "Idle";
timeLabel.Text = string.Format("Elapsed: {0:HH:mm:ss}",
new DateTime(workQueue.ActiveTime.Ticks));
if (revisionAnalyzer != null)
{
fileLabel.Text = "Files: " + revisionAnalyzer.FileCount;
revisionLabel.Text = "Revisions: " + revisionAnalyzer.RevisionCount;
}
if (changesetBuilder != null)
{
changeLabel.Text = "Changesets: " + changesetBuilder.Changesets.Count;
}
if (workQueue.IsIdle)
{
revisionAnalyzer = null;
changesetBuilder = null;
statusTimer.Enabled = false;
goButton.Enabled = true;
}
var exceptions = workQueue.FetchExceptions();
if (exceptions != null)
{
foreach (var exception in exceptions)
{
ShowException(exception);
}
}
}
private void ShowException(Exception exception)
{
var message = ExceptionFormatter.Format(exception);
logger.WriteLine("ERROR: {0}", message);
logger.WriteLine(exception);
MessageBox.Show(message, "Unhandled Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void MainForm_Load(object sender, EventArgs e)
{
this.Text += " " + Assembly.GetExecutingAssembly().GetName().Version;
var defaultCodePage = Encoding.Default.CodePage;
var description = string.Format("System default - {0}", Encoding.Default.EncodingName);
var defaultIndex = encodingComboBox.Items.Add(description);
encodingComboBox.SelectedIndex = defaultIndex;
var encodings = Encoding.GetEncodings();
foreach (var encoding in encodings)
{
var codePage = encoding.CodePage;
description = string.Format("CP{0} - {1}", codePage, encoding.DisplayName);
var index = encodingComboBox.Items.Add(description);
codePages[index] = encoding;
if (codePage == defaultCodePage)
{
codePages[defaultIndex] = encoding;
}
}
ReadSettings();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
WriteSettings();
workQueue.Abort();
workQueue.WaitIdle();
}
private void ReadSettings()
{
var settings = Properties.Settings.Default;
vssDirTextBox.Text = settings.VssDirectory;
vssProjectTextBox.Text = settings.VssProject;
excludeTextBox.Text = settings.VssExcludePaths;
outDirTextBox.Text = settings.GitDirectory;
domainTextBox.Text = settings.DefaultEmailDomain;
commentTextBox.Text = settings.DefaultComment;
logTextBox.Text = settings.LogFile;
transcodeCheckBox.Checked = settings.TranscodeComments;
forceAnnotatedCheckBox.Checked = settings.ForceAnnotatedTags;
anyCommentUpDown.Value = settings.AnyCommentSeconds;
sameCommentUpDown.Value = settings.SameCommentSeconds;
}
private void WriteSettings()
{
var settings = Properties.Settings.Default;
settings.VssDirectory = vssDirTextBox.Text;
settings.VssProject = vssProjectTextBox.Text;
settings.VssExcludePaths = excludeTextBox.Text;
settings.GitDirectory = outDirTextBox.Text;
settings.DefaultEmailDomain = domainTextBox.Text;
settings.LogFile = logTextBox.Text;
settings.TranscodeComments = transcodeCheckBox.Checked;
settings.ForceAnnotatedTags = forceAnnotatedCheckBox.Checked;
settings.AnyCommentSeconds = (int)anyCommentUpDown.Value;
settings.SameCommentSeconds = (int)sameCommentUpDown.Value;
settings.Save();
}
}
}