-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
528 lines (436 loc) · 18.3 KB
/
Program.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Xml;
namespace ssl
{
class Program
{
private enum NodeFound { unknown, executable, parm, limit,
key, url,
mainColourFG, mainColourBG,
secondaryColourFG, secondaryColourBG,
currentColourFG, currentColourBG };
/// <summary>
/// Holds the Subversion command line to execute.
/// </summary>
private static string svnCommandLine;
/// <summary>
/// Holds the parameters to be passed to the subversion command
/// </summary>
private static string svnParms = " ";
/// <summary>
/// Revision to work with
/// </summary>
private static string revision = String.Empty;
/// <summary>
/// Search argument
/// </summary>
private static string search = string.Empty;
/// <summary>
/// Search argument
/// </summary>
private static string userFilter = string.Empty;
/// <summary>
/// Search argument
/// </summary>
private static DateTime dateFilter;
/// <summary>
/// The repositories which the client knows about
/// </summary>
private static Hashtable repositories;
/// <summary>
/// Main Colour (foreground) to show files in
/// </summary>
private static System.ConsoleColor mainColourFG = System.ConsoleColor.White;
/// <summary>
/// Main Colour (background) to show files in
/// </summary>
private static ConsoleColor mainColourBG = ConsoleColor.Black;
/// <summary>
/// Secondary Colour (foreground) to show files in
/// </summary>
private static ConsoleColor secondaryColourFG = ConsoleColor.Yellow;
/// <summary>
/// Secondary Colour (background) to show files in
/// </summary>
private static ConsoleColor secondaryColourBG = ConsoleColor.Black;
/// <summary>
/// Current foreground colour of console
/// </summary>
private static ConsoleColor currentColourFG = ConsoleColor.White;
/// <summary>
/// Current background colour of console
/// </summary>
private static ConsoleColor currentColourBG = ConsoleColor.Black;
///// <summary>
///// The number of entries searched
///// </summary>
//private static int totalEntries = 0;
///// <summary>
///// The number of entries matched
///// </summary>
//private static int matchedEntries = 0;
/// <summary>
/// Indicates whether the user has asked for the help text to be displayed
/// </summary>
private static bool helpRequested = false;
/// <summary>
/// Limit the number of entries returned by subversion
/// </summary>
private static int limit = 0;
/// <summary>
/// The subversion list entries found
/// </summary>
private static svnentries<svnentry> entries;
static void Main(string[] args)
{
Console.WriteLine("Subversion log viewer v 0.1");
if (args.Length == 0)
{
Console.WriteLine("Usage ssl [repository-letter] <search>");
return;
}
repositories = new Hashtable(4); //Create a hashtable which will initially expect 4 entries
entries = new svnentries<svnentry>();
//Load up the ini file
if (!readInifile())
{
Console.WriteLine("Cannot read from settings file ssl.xml");
return;
}
if (!parseArgs(args))
{
return;
}
//If the user has asked for help then display it and quit
if (helpRequested)
{
Console.WriteLine("Usage: ssl REPOS [options] [search]");
Console.WriteLine(" The following options are available:");
Console.WriteLine(" -uuser filter results by user (eg -umike)");
Console.WriteLine(" -lnn limits the result to the first nn (eg -l32)");
Console.WriteLine(" [search] is the search term to use (eg code or \"fixed bug\"");
Console.WriteLine(" ");
Console.WriteLine(" where REPOS is the code as defined in the ssl.xml file");
return;
}
//Now, see if the repository letter entered is available
string key = args[0];
if (!repositories.ContainsKey(key))
{
Console.WriteLine("Repository " + key + " is not defined");
Console.WriteLine("Available repositories are:");
foreach (string thisKey in repositories.Keys)
{
Console.WriteLine(thisKey + " = " + repositories[thisKey]);
}
return;
}
subversion svn = new subversion((string)repositories[key]);
string output = string.Empty;
string parms = svnParms;
if (revision.Length > 0) //If a specific revision has been requested then use it
{
parms += "-r " + revision + " ";
}
if (limit > 0) //If a limit is required then use it
{
parms += "--limit " + limit.ToString() + " ";
}
output = svn.RunSyncAndGetResults(svnCommandLine, parms);
Console.WriteLine("Executing command: " + svnCommandLine + parms + repositories[key]);
Console.WriteLine("");
entries.UserFilter = userFilter;
entries.SearchFilter = search;
entries.dateFilter = dateFilter;
if (!entries.Populate(output))
{
Console.WriteLine("Error reading data from subversion.");
return;
}
writeOutput();
Console.WriteLine("");
}
private static bool readInifile()
{
string iniFile = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\ssl.xml";
if (!File.Exists(iniFile))
{
return false;
}
XmlTextReader reader = new XmlTextReader(iniFile);
NodeFound nf = NodeFound.unknown;
try
{
string key = string.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "executable")
{
nf = NodeFound.executable;
}
if (reader.Name == "parm")
{
nf = NodeFound.parm;
}
if (reader.Name == "limit")
{
nf = NodeFound.limit;
}
if (reader.Name == "key")
{
nf = NodeFound.key;
}
if (reader.Name == "url")
{
nf = NodeFound.url;
}
if (reader.Name == "mainColourFG")
{
nf = NodeFound.mainColourFG;
}
if (reader.Name == "mainColourBG")
{
nf = NodeFound.mainColourBG;
}
if (reader.Name == "secondaryColourFG")
{
nf = NodeFound.secondaryColourFG;
}
if (reader.Name == "secondaryColourBG")
{
nf = NodeFound.secondaryColourBG;
}
if (reader.Name == "currentColourFG")
{
nf = NodeFound.currentColourFG;
}
if (reader.Name == "currentColourBG")
{
nf = NodeFound.currentColourBG;
}
}
if (reader.NodeType == XmlNodeType.Text)
{
switch (nf)
{
case NodeFound.executable:
svnCommandLine = reader.Value;
break;
case NodeFound.parm:
svnParms += reader.Value + " ";
break;
case NodeFound.limit:
try
{
int parmLimit = System.Convert.ToInt32(reader.Value);
limit = parmLimit;
}
#pragma warning disable 0168
catch (Exception excp)
{
//Do nothing, leave the limit at it's default
}
#pragma warning restore
break;
case NodeFound.key:
key = reader.Value;
break;
case NodeFound.url:
repositories.Add(key, reader.Value);
break;
case NodeFound.mainColourFG:
try
{
mainColourFG = (System.ConsoleColor)Enum.Parse(typeof(System.ConsoleColor), reader.Value);
}
catch (ArgumentException) { }; //Do nothing, we will use the default
break;
case NodeFound.mainColourBG:
try
{
mainColourBG = (System.ConsoleColor)Enum.Parse(typeof(System.ConsoleColor), reader.Value);
}
catch (ArgumentException) { }; //Do nothing, we will use the default
break;
case NodeFound.secondaryColourFG:
try
{
secondaryColourFG = (System.ConsoleColor)Enum.Parse(typeof(System.ConsoleColor), reader.Value);
}
catch (ArgumentException) { }; //Do nothing, we will use the default
break;
case NodeFound.secondaryColourBG:
try
{
secondaryColourBG = (System.ConsoleColor)Enum.Parse(typeof(System.ConsoleColor), reader.Value);
}
catch (ArgumentException) { }; //Do nothing, we will use the default
break;
case NodeFound.currentColourFG:
try
{
currentColourFG = (System.ConsoleColor)Enum.Parse(typeof(System.ConsoleColor), reader.Value);
}
catch (ArgumentException) { }; //Do nothing, we will use the default
break;
case NodeFound.currentColourBG:
try
{
currentColourBG = (System.ConsoleColor)Enum.Parse(typeof(System.ConsoleColor), reader.Value);
}
catch (ArgumentException) { }; //Do nothing, we will use the default
break;
}
}
}
}
catch (XmlException excp)
{
Console.WriteLine("Error reading ini file: " + excp.Message);
return false;
}
finally
{
reader.Close();
}
reader = null;
//If we are missing some key data then drop out
if (svnCommandLine.Length < 3 || svnParms.Length < 3 || repositories.Count == 0)
{
return false;
}
//If we get to here then everything is initialised correctly so return true.
return true;
}
/// <summary>
/// Parse the arguments passed in from the command line
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
private static bool parseArgs(string[] args)
{
Regex rxRepos = new Regex(@"^-r(\d*)([:, ]?\d*)?");
Regex rxUser = new Regex(@"\s*-u([a-zA-Z0-9]*)\s*");
Regex rxDate = new Regex(@"\s*-d(\d{2,4}[-\\]\d{1,2}[-\\]\d{1,2})\s*");
Regex rxLimit = new Regex(@"\s*-l(\d*)\s*");
foreach (string arg in args)
{
string ARG = arg.ToUpper();
//If user has requested help then set this option and drop out
if (ARG == "-H" || ARG == "--H" || ARG == "-HELP" || ARG == "--HELP")
{
helpRequested = true;
return true;
}
if (arg == args[0])
{
continue; //Drop out for the first argument which is the repository (if it wasn't a help request).
}
//Check if the argument is a revision
Match match = rxRepos.Match(arg);
if (match.Success)
{
if (match.Groups.Count == 1)
{
revision = match.Groups[1].Value;
}
else if (match.Groups.Count == 2)
{
revision = match.Groups[1].Value + match.Groups[2].Value;
}
else if (match.Groups.Count == 3)
{
revision = match.Groups[1].Value + match.Groups[2].Value + match.Groups[3].Value;
}
continue;
}
//Check if the argument is a user filter
match = rxUser.Match(arg);
if (match.Success && match.Groups.Count >= 1)
{
userFilter = match.Groups[1].Value;
continue;
}
//Check if the argument is a limit filter
match = rxLimit.Match(arg);
if (match.Success && match.Groups.Count >= 1)
{
try
{
int parmLimit = System.Convert.ToInt32(match.Groups[1].Value);
limit = parmLimit;
}
#pragma warning disable 0168
catch (Exception excp)
{
//Do nothing, leave the limit at it's default
}
#pragma warning restore
continue;
}
//Check if the argument is a date filter
match = rxDate.Match(arg);
if (match.Success && match.Groups.Count >= 1)
{
try
{
dateFilter = System.Convert.ToDateTime(match.Groups[1].Value + " 00:00:00");
}
#pragma warning disable 0168
catch (FormatException excp)
{
Console.WriteLine("Invalid date specified in date filter");
return false;
}
#pragma warning restore
continue;
}
search = arg;
}
return true;
}
/// <summary>
/// Write the output to the console
/// </summary>
private static void writeOutput()
{
int rows = Console.WindowHeight;
int columns = Console.WindowWidth;
bool useMainColour = true;
foreach (svnentry entry in entries)
{
if (useMainColour)
{
Console.ForegroundColor = mainColourFG;
Console.BackgroundColor = mainColourBG;
}
else
{
Console.ForegroundColor = secondaryColourFG;
Console.BackgroundColor = secondaryColourBG;
}
useMainColour = !useMainColour;
//Get the string to output and pad it with spaces as necessary
string outputLine = entry.Header + " " + entry.Detail;
int totalLength = 0;
while (totalLength < outputLine.Length)
{
totalLength += columns;
}
outputLine = outputLine.PadRight(totalLength - 1, ' ');
Console.WriteLine(outputLine);
}
Console.ForegroundColor = currentColourFG;
Console.BackgroundColor = currentColourBG;
Console.WriteLine("");
Console.WriteLine("Searched {0} entries, found {1} matches", entries.TotalEntries, entries.MatchedEntries);
}
}
}