-
Notifications
You must be signed in to change notification settings - Fork 4
/
FindForm.cs
405 lines (370 loc) · 15.5 KB
/
FindForm.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
namespace DRP.Find_Changeset_By_Comment
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using EnvDTE80;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
public partial class FindForm : Form
{
/// <summary>
/// The search criteria
/// </summary>
private SearchCriteria searchCriteria = new SearchCriteria();
/// <summary>
/// The version control server which will be used to find history
/// </summary>
private VersionControlServer versionControlServer = null;
/// <summary>
/// The version control ext
/// </summary>
private VersionControlExt versionControlExt = null;
/// <summary>
/// The list of changesets returned from the search
/// </summary>
private List<Changeset> changes = new List<Changeset>();
/// <summary>
/// The list of affected files
/// </summary>
private List<string> files = new List<string>();
/// <summary>
/// The UI shell interface
/// </summary>
public IVsUIShell UiShell = null;
/// <summary>
/// The main automation object
/// </summary>
public DTE2 AutomationObject = null;
/// <summary>
/// Initializes a new instance of the <see cref="FindForm"/> class.
/// </summary>
public FindForm()
{
this.InitializeComponent();
}
/// <summary>
/// Initializes a new instance of the <see cref="FindForm"/> class.
/// </summary>
/// <param name="automationObject">The automation object.</param>
public FindForm(DTE2 automationObject) : this()
{
this.AutomationObject = automationObject;
this.versionControlExt = (VersionControlExt)this.AutomationObject.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt");
this.versionControlServer = this.versionControlExt.Explorer.Workspace.VersionControlServer;
this.ContainingFileText.Text = this.versionControlExt.Explorer.CurrentFolderItem.SourceServerPath;
this.FromDatePicker.Value = DateTime.Now - new TimeSpan(28, 0, 0, 0);
this.ToDatePicker.Value = DateTime.Now;
}
/// <summary>
/// Handles the Click event of the FindButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void FindButton_Click(object sender, EventArgs e)
{
// Check that we have a folder to search
if (string.IsNullOrWhiteSpace(this.ContainingFileText.Text))
{
MessageBox.Show("Please specify a source control folder to search", "Find Changeset By Comment", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
// Check that we have a valid regular expression
if (this.useRegularExpressions.Checked && !string.IsNullOrWhiteSpace(this.ContainingCommentText.Text))
{
try
{
Regex rx = new Regex(this.ContainingCommentText.Text);
}
catch (ArgumentException)
{
MessageBox.Show("Please specify a valid regular expression", "Find Changeset By Comment", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
this.searchCriteria.SearchFile = this.ContainingFileText.Text.Trim();
this.searchCriteria.SearchUser = this.ByUserText.Text;
this.searchCriteria.FromDateVersion = this.FromDatePicker.Checked ? new DateVersionSpec(this.FromDatePicker.Value) : null;
this.searchCriteria.ToDateVersion = this.ToDatePicker.Checked ? new DateVersionSpec(this.ToDatePicker.Value) : null;
this.searchCriteria.SearchComment = this.ContainingCommentText.Text;
this.searchCriteria.UseRegex = this.useRegularExpressions.Checked;
this.ResultsList.Items.Clear();
this.FilesList.Items.Clear();
this.FindButton.Enabled = false;
this.FindChangesetsWorker.RunWorkerAsync();
}
/// <summary>
/// Handles the Click event of the CloseButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void CloseButton_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Handles the DoubleClick event of the ResultsList control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void ResultsList_DoubleClick(object sender, EventArgs e)
{
if (this.ResultsList.SelectedItems.Count == 0)
{
return;
}
foreach (Changeset changeset in this.changes)
{
if (changeset.ChangesetId.ToString() == this.ResultsList.SelectedItems[0].Text)
{
this.versionControlExt.ViewChangesetDetails(changeset.ChangesetId);
break;
}
}
}
/// <summary>
/// Handles the Load event of the FindForm control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void FindForm_Load(object sender, EventArgs e)
{
// Do any initiailization here
}
/// <summary>
/// Handles the Click event of the CopyButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void CopyButton_Click(object sender, EventArgs e)
{
StringBuilder clipboardText = new StringBuilder();
if (this.ChangesetsAndFiles.SelectedTab == this.ChangesetList)
{
// Copy list of changesets
foreach (Changeset changeset in this.changes)
{
clipboardText.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}", changeset.ChangesetId, changeset.Owner, changeset.CreationDate, changeset.Comment));
}
}
else
{
// Copy list of files
foreach (string listItem in this.FilesList.Items)
{
clipboardText.AppendLine(listItem);
}
}
if (clipboardText != null)
{
Clipboard.SetText(clipboardText.ToString());
}
else
{
MessageBox.Show("No results to copy.", "Find Changeset By Comment", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// Handles the DoWork event of the FindChangesetsWorker control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
private void FindChangesetsWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// Do not access the form's BackgroundWorker reference directly.
// Instead, use the reference provided by the sender parameter.
BackgroundWorker bw = sender as BackgroundWorker;
// Start looking for changesets
e.Result = this.FindChangesetsByComment(bw, this.searchCriteria);
// If the operation was canceled by the user,
// set the DoWorkEventArgs.Cancel property to true.
if (bw.CancellationPending)
{
e.Cancel = true;
}
}
/// <summary>
/// Handles the RunWorkerCompleted event of the FindChangesetsWorker control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
private void FindChangesetsWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
// The user canceled the operation.
MessageBox.Show("Operation was canceled", "Find Changeset By Comment", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (e.Error != null)
{
// There was an error during the operation.
string msg = String.Format("An error occurred: {0}", e.Error.Message);
MessageBox.Show(msg);
}
else
{
// The operation completed normally.
// Check for zero things found
if (this.changes.Count == 0)
{
MessageBox.Show("No changesets found matching the search criteria", "Find Changeset By Comment", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
this.FindButton.Enabled = true;
}
/// <summary>
/// Finds the changesets by comment.
/// </summary>
/// <param name="bw">The background worker.</param>
/// <param name="sc">The search criteria.</param>
/// <returns>An error code</returns>
private int FindChangesetsByComment(BackgroundWorker bw, SearchCriteria sc)
{
this.changes.Clear();
this.files.Clear();
try
{
IEnumerable changesets = this.versionControlServer.QueryHistory(
sc.SearchFile,
VersionSpec.Latest,
0,
RecursionType.Full,
string.IsNullOrWhiteSpace(sc.SearchUser) ? null : sc.SearchUser,
sc.FromDateVersion,
sc.ToDateVersion,
Int32.MaxValue,
true,
false);
foreach (Changeset changeset in changesets)
{
if (bw.CancellationPending)
{
break;
}
if (string.IsNullOrWhiteSpace(sc.SearchComment) ||
(!sc.UseRegex && changeset.Comment.IndexOf(sc.SearchComment.Trim(), StringComparison.CurrentCultureIgnoreCase) != -1) ||
(sc.UseRegex && Regex.IsMatch(changeset.Comment, sc.SearchComment)))
{
this.changes.Add(changeset);
bw.ReportProgress(0, changeset);
foreach (Change change in changeset.Changes)
{
if (!this.files.Contains(change.Item.ServerItem))
{
this.files.Add(change.Item.ServerItem);
bw.ReportProgress(0, change.Item.ServerItem);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Find Changeset By Comment", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return 0;
}
return 0;
}
/// <summary>
/// Handles the Click event of the CancelButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void CancelFindButton_Click(object sender, EventArgs e)
{
if (this.FindChangesetsWorker.IsBusy)
{
this.FindChangesetsWorker.CancelAsync();
}
}
/// <summary>
/// Handles the ProgressChanged event of the FindChangesetsWorker control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.ComponentModel.ProgressChangedEventArgs"/> instance containing the event data.</param>
private void FindChangesetsWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.UserState is Changeset)
{
Changeset changeset = e.UserState as Changeset;
this.ResultsList.Items.Add(new ListViewItem(new string[] { changeset.ChangesetId.ToString(), changeset.Owner, changeset.CreationDate.ToString(), changeset.Comment }));
}
else
{
string file = e.UserState as string;
this.FilesList.Items.Add(file);
}
}
/// <summary>
/// The search criteria for finding changesets
/// </summary>
private class SearchCriteria
{
/// <summary>
/// Gets or sets the file/folder to search
/// </summary>
public string SearchFile
{
get;
set;
}
/// <summary>
/// Gets or sets the user who made the change
/// </summary>
public string SearchUser
{
get;
set;
}
/// <summary>
/// Gets or sets the start date
/// </summary>
public DateVersionSpec FromDateVersion
{
get;
set;
}
/// <summary>
/// Gets or sets the end date
/// </summary>
public DateVersionSpec ToDateVersion
{
get;
set;
}
/// <summary>
/// Gets or sets the comment that we're searching for
/// </summary>
public string SearchComment
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether we are using regular expressions
/// </summary>
public bool UseRegex
{
get;
set;
}
}
/// <summary>
/// Handles the KeyUp event of the ResultsList control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
private void ResultsList_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space)
{
this.ResultsList_DoubleClick(sender, null);
}
}
}
}