forked from rlaneve/accurev2git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
387 lines (356 loc) · 14.7 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using CommandLine;
using CommandLine.Text;
using Mono.Unix;
namespace AccuRev2Git
{
class Options
{
[Option("d", "depot", HelpText = "Name of the AccuRev depot.", Required = true)]
public string DepotName { get; set; }
[Option("s", "stream", HelpText = "Name of the AccuRev stream.", DefaultValue = "")]
public string StreamName { get; set; }
[Option("w", "workingDir", HelpText = "Working directory of [new] git repo.", Required = true)]
public string WorkingDir { get; set; }
[Option("t", "startingTran", HelpText = "AccuRev transaction to start from.", DefaultValue = null)]
public int? StartingTransaction { get; set; }
[Option("r", "resume", HelpText = "Resume from last transaction completed.", DefaultValue = false)]
public bool Resume { get; set; }
[Option("u", "username", HelpText = "AccuRev username.", DefaultValue = null)]
public string AccuRevUserName { get; set; }
[Option("p", "password", HelpText = "AccuRev password.", DefaultValue = null)]
public string AccuRevPassword { get; set; }
}
class Program
{
private static Options _options = new Options();
private static string _accurevUsername;
private static List<GitUser> _gitUsers;
public static void Main(string[] args)
{
if (args == null || !CommandLineParser.Default.ParseArguments(args, _options))
{
Console.WriteLine(HelpText.AutoBuild(_options));
return;
}
var depotName = _options.DepotName;
var streamName = _options.StreamName;
var workingDir = _options.WorkingDir;
var startingTran = _options.StartingTransaction;
var resume = _options.Resume;
accurevLogin(_options);
loadUsers();
loadDepotFromScratch(depotName, streamName, workingDir, startingTran, resume);
}
private static void accurevLogin(Options options)
{
Console.WriteLine("AccuRev Login...");
_accurevUsername = options.AccuRevUserName;
if (string.IsNullOrEmpty(_accurevUsername))
{
Console.Write("Username: ");
_accurevUsername = Console.ReadLine();
}
var password = options.AccuRevPassword;
if (string.IsNullOrEmpty(password))
{
Console.Write("Password: ");
password = Console.ReadLine();
}
execAccuRev(string.Format("login -n {0} \"{1}\"", _accurevUsername, password), string.Empty);
}
private static void loadUsers()
{
if (!File.Exists("users.txt"))
throw new ApplicationException("No \"users.txt\" file found!");
var usersRaw = File.ReadAllLines("users.txt");
_gitUsers = new List<GitUser>(usersRaw.Length);
foreach (var parts in usersRaw.Where(userRaw => !string.IsNullOrEmpty(userRaw) && !userRaw.StartsWith("#")).Select(userRaw => userRaw.Split('|')))
_gitUsers.Add(new GitUser(parts[0], parts[1], parts[2]));
}
static void loadDepotFromScratch(string depotName, string streamName, string workingDir, int? startingTransaction, bool resume)
{
var xdoc = new XDocument();
var tempFile = string.Format("_{0}_{1}_.depot.hist.xml", depotName, streamName);
if (File.Exists(tempFile))
{
if (resume)
{
xdoc = XDocument.Load(tempFile);
}
else
{
Console.Write("Existing history file found. Re-use it? [y|n] ");
// ReSharper disable PossibleNullReferenceException
if (Console.ReadLine().ToUpper() == "Y")
// ReSharper restore PossibleNullReferenceException
{
xdoc = XDocument.Load(tempFile);
}
}
}
if (xdoc.Document == null || xdoc.Nodes().Any() == false)
{
string temp = "";
if (string.IsNullOrEmpty(streamName))
{
Console.WriteLine("Retrieving complete history of {0} depot from AccuRev server...", depotName);
temp = execAccuRev(string.Format("hist -p \"{0}\" -k promote -fx", depotName), workingDir);
}
else
{
Console.WriteLine("Retrieving complete history of {0} depot, {1} stream, from AccuRev server...", depotName, streamName);
temp = execAccuRev(string.Format("hist -p \"{0}\" -s \"{1}\" -k promote -fx", depotName, streamName), workingDir);
}
File.WriteAllText(tempFile, temp);
xdoc = XDocument.Parse(temp);
}
var n = 0;
// ReSharper disable PossibleNullReferenceException
var nodes = xdoc.Document.Descendants("transaction").Where(t => Int32.Parse(t.Attribute("id").Value) >= startingTransaction).OrderBy(t => Int32.Parse(t.Attribute("id").Value));
if (resume)
{
Console.WriteLine("Attempting to resume from last completed transaction.");
int lastTransaction;
var result = execGitRaw("config --get accurev2git.lasttran", workingDir);
if (!Int32.TryParse(result, out lastTransaction))
throw new ApplicationException("Cannot resume - no last transaction was found in git config key 'accurev2git.lasttran'.");
Console.WriteLine("Resuming from last completed transaction {0}.", lastTransaction);
string temp = "";
if (string.IsNullOrEmpty(streamName))
{
Console.WriteLine("Retrieving history as of {0} for {1} depot from AccuRev server...", lastTransaction, depotName);
temp = execAccuRev(string.Format("hist -p \"{0}\" -k promote -t {1}-now -fx", depotName, lastTransaction), workingDir);
}
else
{
Console.WriteLine("Retrieving history as of {0} for {1} depot, {2} stream, from AccuRev server...", lastTransaction, depotName, streamName);
temp = execAccuRev(string.Format("hist -p \"{0}\" -s \"{1}\" -k promote -t {2}-now -fx", depotName, streamName, lastTransaction), workingDir);
}
File.WriteAllText(tempFile, temp);
xdoc = XDocument.Parse(temp);
nodes = xdoc.Document.Descendants("transaction").Where(t => Int32.Parse(t.Attribute("id").Value) > lastTransaction).OrderBy(t => Int32.Parse(t.Attribute("id").Value));
try
{
startingTransaction = nodes.Select(t => Int32.Parse(t.Attribute("id").Value)).First();
}
catch (Exception)
{
Console.WriteLine("No transactions found after last transaction of {0}.", lastTransaction);
return;
}
}
else
{
startingTransaction = startingTransaction ?? 0;
}
var nCount = nodes.Count();
if (startingTransaction == 0)
{
var initialDate = long.Parse(nodes.First().Attribute("time").Value) - 60;
var defaultGitUserName = ConfigurationManager.AppSettings.Get("DefaultGitUserName");
var defaultGitUser = _gitUsers.SingleOrDefault(u => u.Name.Equals(defaultGitUserName, StringComparison.OrdinalIgnoreCase));
if (defaultGitUser == null)
throw new ApplicationException("Cannot initialize new repository without a DefaultGitUserName specified!");
if (Directory.Exists(Path.Combine(workingDir, ".git")))
throw new ApplicationException(string.Format("Cannot initialize new repository; repository already exists in '{0}'.", workingDir));
execGitRaw("init", workingDir);
File.WriteAllText(Path.Combine(workingDir, ".gitignore"), "#empty");
execGitRaw("add --all", workingDir);
execGitCommit(string.Format("commit --date={0} --author={1} -m \"Initial git commit.\"", initialDate, defaultGitUser), workingDir, initialDate.ToString(), defaultGitUser);
execGitRaw(string.Format("checkout -b \"{0}\"", streamName), workingDir, true);
execGitRaw(string.Format("config accurev2git.lasttran {0}", 1), workingDir);
}
foreach (var transaction in nodes)
{
n++;
var transactionId = Convert.ToInt32(transaction.Attribute("id").Value);
Console.Write("Loading transaction {0} of {1} [id={2}]\x0D", n.ToString("00000"), nCount, transactionId.ToString("00000"));
loadTransaction(depotName, streamName, transactionId, workingDir, transaction);
}
// ReSharper restore PossibleNullReferenceException
Console.WriteLine();
}
static void loadTransaction(string depotName, string streamName, int transactionId, string workingDir, XElement transaction)
{
// ReSharper disable PossibleNullReferenceException
string accurevUser = transaction.Attribute("user").Value;
GitUser gitUser = translateUser(accurevUser);
if (gitUser == null)
{
throw new ApplicationException(string.Format("Accurev user {0} not recognised in Git", accurevUser));
}
Int64 unixDate = long.Parse(transaction.Attribute("time").Value);
// ReSharper restore PossibleNullReferenceException
var issueNumNodes = transaction.Descendants("version").Descendants("issueNum");
var issueNums = (issueNumNodes == null || issueNumNodes.Count() == 0 ? string.Empty : issueNumNodes.Select(n => n.Value).Distinct().Aggregate(string.Empty, (seed, num) => seed + ", " + num).Substring(2));
var commentNode = transaction.Descendants("comment").FirstOrDefault();
string comment = (commentNode == null ? string.Empty : commentNode.Value);
comment = string.IsNullOrEmpty(comment) ? "[no original comment]" : comment;
string[] commentLines = comment.Split(new[] { "\n" }, 2, StringSplitOptions.None);
if (commentLines.Length > 1)
comment = commentLines[0] + Environment.NewLine + Environment.NewLine + commentLines[1];
comment += string.Format("{0}{0}[AccuRev Transaction #{1}]", Environment.NewLine, transactionId);
if (!string.IsNullOrEmpty(issueNums))
comment += string.Format("{0}[Issue #s: {1}]", Environment.NewLine, issueNums);
var commentFile = string.Format(".\\_{0}_Comment.txt", depotName);
var commentFilePath = Path.GetFullPath(commentFile);
File.WriteAllText(commentFile, comment);
execClean(workingDir);
if (string.IsNullOrEmpty(streamName))
{
execAccuRev(string.Format("pop -R -O -v \"{0}\" -L . -t {1} .", depotName, transactionId), workingDir);
}
else
{
execAccuRev(string.Format("pop -R -O -v \"{0}\" -L . -t {1} .", streamName, transactionId), workingDir);
}
execGitRaw("add --all", workingDir);
execGitCommit(string.Format("commit --date={0} --author={1} --file=\"{2}\"", unixDate, gitUser, commentFilePath), workingDir, unixDate.ToString(), gitUser);
execGitRaw(string.Format("config accurev2git.lasttran {0}", transactionId), workingDir);
}
static GitUser translateUser(string accurevUser)
{
return _gitUsers.SingleOrDefault(u => u.AccuRevUserName.Equals(accurevUser, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Depth-first recursive delete, with handling for descendant
/// directories open in Windows Explorer.
/// See http://stackoverflow.com/a/1703799/264822
/// </summary>
public static void deleteDirectory(string path)
{
Mono.Unix.UnixSymbolicLinkInfo info = new Mono.Unix.UnixSymbolicLinkInfo(path);
if (info.FileType == FileTypes.Directory)
{
foreach (string directory in Directory.GetDirectories(path))
{
deleteDirectory(directory);
}
}
try
{
Directory.Delete(path, true);
}
catch (IOException)
{
Directory.Delete(path, true);
}
catch (UnauthorizedAccessException)
{
Directory.Delete(path, true);
}
}
static void execClean(string workingDir)
{
foreach (var dir in Directory.GetDirectories(workingDir).Where(dir => !dir.EndsWith(".git")))
{
//Directory.Delete(dir, true);
deleteDirectory(dir);
}
foreach (var file in Directory.GetFiles(workingDir).Where(file => !file.EndsWith(".gitignore")))
{
File.Delete(file);
}
}
static string execAccuRev(string arguments, string workingDir)
{
var accuRevPath = ConfigurationManager.AppSettings["AccuRevPath"];
var process = new Process
{
StartInfo = new ProcessStartInfo(accuRevPath, arguments)
};
if (!string.IsNullOrEmpty(workingDir))
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
if (!process.StartInfo.EnvironmentVariables.ContainsKey("ACCUREV_PRINCIPAL"))
process.StartInfo.EnvironmentVariables.Add("ACCUREV_PRINCIPAL", _accurevUsername);
process.Start();
var result = process.StandardOutput.ReadToEnd();
if (process.StandardError.EndOfStream == false)
{
var errors = process.StandardError.ReadToEnd();
if (!errors.Contains("is defunct") && !errors.Contains("No element named") && !errors.Contains("Unable to proceed with annotate.") && !errors.Contains("Specified version not found for:"))
{
Debug.WriteLine(errors);
if (!errors.Contains("Failed to create symbolic-link"))
{
throw new ApplicationException(string.Format("AccuRev has returned an error: {0}", errors));
}
}
}
return result;
}
static string execGitRaw(string arguments, string workingDir, bool ignoreErrors = false)
{
var gitPath = ConfigurationManager.AppSettings["GitPath"];
var process = new Process
{
StartInfo = new ProcessStartInfo(gitPath, arguments)
};
if (!string.IsNullOrEmpty(workingDir))
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
if (!ignoreErrors && process.StandardError.EndOfStream == false)
{
var errors = process.StandardError.ReadToEnd();
throw new ApplicationException(string.Format("Git has returned an error: {0}", errors));
}
return output;
}
static void execGitCommit(string arguments, string workingDir, string commitDate, GitUser gitUser)
{
var gitPath = ConfigurationManager.AppSettings["GitPath"];
var process = new Process
{
StartInfo = new ProcessStartInfo(gitPath, arguments)
};
if (!string.IsNullOrEmpty(workingDir))
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.EnvironmentVariables.Add("GIT_COMMITTER_DATE", commitDate);
process.StartInfo.EnvironmentVariables.Add("GIT_COMMITTER_NAME", gitUser.Name);
process.StartInfo.EnvironmentVariables.Add("GIT_COMMITTER_EMAIL", gitUser.Email);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
if (process.StandardError.EndOfStream == false)
{
var errors = process.StandardError.ReadToEnd();
throw new ApplicationException(string.Format("Git has returned an error: {0}", errors));
}
}
internal class GitUser
{
public string Name { get; set; }
public string Email { get; set; }
public string AccuRevUserName { get; set; }
public GitUser(string accuRevUserName, string name, string email)
{
Name = name;
Email = email;
AccuRevUserName = accuRevUserName;
}
public override string ToString()
{
return string.Format("\"{0} <{1}>\"", Name, Email);
}
}
}
}