-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExportReader.cs
244 lines (232 loc) · 12 KB
/
ExportReader.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace GitImporter
{
class ExportReader
{
public static TraceSource Logger = Program.Logger;
private readonly DateTime _originDate;
private readonly LabelFilter _labelFilter;
private static readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private readonly Regex _elementNameRegex = new Regex(@"^Name \d+:(.*)", RegexOptions.Compiled);
private readonly Regex _versionIdRegex = new Regex(@"^VersionId \d+:\\(.*)\\(\d+)", RegexOptions.Compiled);
private readonly Regex _userRegex = new Regex(@"^EventUser \d+:(.*)", RegexOptions.Compiled);
private readonly Regex _userNameRegex = new Regex(@"^EventName \d+:(.*)", RegexOptions.Compiled);
private readonly Regex _timeRegex = new Regex(@"^EventTime (\d+)", RegexOptions.Compiled);
private readonly Regex _commentRegex = new Regex(@"^Comment (\d+):(.*)", RegexOptions.Compiled);
private readonly Regex _labelRegex = new Regex(@"^Label \d+:(.*)", RegexOptions.Compiled);
private readonly Regex _subBranchRegex = new Regex(@"^SubBranch \d+:(.*)", RegexOptions.Compiled);
private readonly Regex _mergeRegex = new Regex(@"^(F)?Merge \d+:([^\\]+\\)*(\d+)", RegexOptions.Compiled);
public IList<Element> Elements { get; private set; }
public ExportReader(string originDate, IEnumerable<string> labels)
{
Elements = new List<Element>();
_originDate = string.IsNullOrEmpty(originDate) ? DateTime.UtcNow : DateTime.Parse(originDate).ToUniversalTime();
_labelFilter = new LabelFilter(labels);
}
/// <summary>
/// Semantic of the file parameter is that the "directory" part (if present) is the path relative to the global clearcase root
/// the file itself should therefore be in the working directory
/// (although we could cheat using '/' for the root vs '\' for the actual file path)
/// </summary>
/// <param name="file"></param>
public void ReadFile(string file)
{
string root = "";
int pos = file.LastIndexOf('/');
if (pos != -1)
{
root = file.Substring(0, pos + 1).Replace('/', '\\');
file = file.Substring(pos + 1);
}
Logger.TraceData(TraceEventType.Start | TraceEventType.Information, (int)TraceId.ReadExport, "Start reading export file", file);
TextReader reader = new StreamReader(file);
string line;
string currentElementName = null;
Element currentElement = null;
ElementBranch currentBranch = null;
ElementVersion currentVersion = null;
List<Tuple<ElementVersion, string, int, bool>> currentElementMerges = new List<Tuple<ElementVersion, string, int, bool>>();
Match match;
int lineNb = 0;
int missingCommentChars = 0;
string currentComment = null;
// hack around end of lines
string eol;
while ((line = ReadLine(reader, out eol)) != null)
{
lineNb++;
if (missingCommentChars > 0)
{
Debug.Assert(currentVersion != null);
currentComment += line;
missingCommentChars -= line.Length;
if (missingCommentChars < 0)
throw new Exception(file + ", line " + lineNb + " : Unexpected comment length");
if (missingCommentChars > 0)
{
currentComment += eol;
missingCommentChars -= eol.Length;
}
if (missingCommentChars == 0)
{
currentVersion.Comment = string.Intern(currentComment);
currentComment = null;
}
continue;
}
if (line == "ELEMENT_BEGIN")
{
currentElementName = null;
currentBranch = null;
currentElement = null;
currentVersion = null;
currentElementMerges.Clear();
continue;
}
if (currentElement == null && (match = _elementNameRegex.Match(line)).Success)
{
currentElementName = root + match.Groups[1].Value;
currentElement = new Element(currentElementName, false); // no directories in export files
Elements.Add(currentElement);
Logger.TraceData(TraceEventType.Start | TraceEventType.Verbose, (int)TraceId.ReadExport, "Start reading element", currentElementName);
continue;
}
if (line == "ELEMENT_END")
{
if (currentElement == null)
throw new Exception(file + ", line " + lineNb + " : Unexpected ELEMENT_END before it was named");
foreach (var merge in currentElementMerges)
{
var version = merge.Item1;
var other = currentElement.GetVersion(merge.Item2, merge.Item3);
if (other == null || version.Date > _originDate)
// skip merges to or from skipped versions (that were too recent)
continue;
(merge.Item4 ? version.MergesTo : version.MergesFrom).Add(other);
}
Logger.TraceData(TraceEventType.Stop | TraceEventType.Verbose, (int)TraceId.ReadExport, "Stop reading element", currentElementName);
continue;
}
if (line == "VERSION_BEGIN" || line == "VERSION_END")
{
currentVersion = null;
continue;
}
if (currentElement != null && currentVersion == null && (match = _versionIdRegex.Match(line)).Success)
{
string[] branchPath = match.Groups[1].Value.Split('\\');
string branchName = branchPath[branchPath.Length - 1];
if (currentBranch == null || (currentBranch.BranchName != branchName && !currentElement.Branches.TryGetValue(branchName, out currentBranch)))
{
if (branchName != "main")
throw new Exception(file + ", line " + lineNb + " : Unexpected branch " + branchName);
currentBranch = new ElementBranch(currentElement, branchName, null);
currentElement.Branches[branchName] = currentBranch;
}
currentVersion = new ElementVersion(currentBranch, int.Parse(match.Groups[2].Value));
currentBranch.Versions.Add(currentVersion);
Logger.TraceData(TraceEventType.Verbose, (int)TraceId.ReadExport, "Creating version", currentVersion);
continue;
}
if (currentVersion != null && (match = _userRegex.Match(line)).Success)
{
currentVersion.AuthorLogin = string.Intern(match.Groups[1].Value);
continue;
}
if (currentVersion != null && (match = _userNameRegex.Match(line)).Success)
{
currentVersion.AuthorName = string.Intern(match.Groups[1].Value);
continue;
}
if (currentVersion != null && (match = _timeRegex.Match(line)).Success)
{
currentVersion.Date = _epoch.AddSeconds(long.Parse(match.Groups[1].Value));
if (currentVersion.Date > _originDate)
{
Logger.TraceData(TraceEventType.Information, (int)TraceId.ReadExport,
string.Format("Skipping version {0} : {1} > {2}", currentVersion, currentVersion.Date, _originDate));
currentBranch.Versions.Remove(currentVersion);
}
continue;
}
if (currentVersion != null && (match = _labelRegex.Match(line)).Success)
{
var label = string.Intern(match.Groups[1].Value);
if (_labelFilter.ShouldKeep(label))
currentVersion.Labels.Add(label);
continue;
}
if (currentVersion != null && (match = _commentRegex.Match(line)).Success)
{
currentComment = match.Groups[2].Value;
missingCommentChars = int.Parse(match.Groups[1].Value) - currentComment.Length;
if (missingCommentChars > 0)
{
currentComment += eol;
missingCommentChars -= eol.Length;
}
if (missingCommentChars == 0 && currentComment.Length > 0)
{
currentVersion.Comment = string.Intern(currentComment);
currentComment = null;
}
continue;
}
if (currentVersion != null && (match = _subBranchRegex.Match(line)).Success)
{
string branchName = match.Groups[1].Value;
if (currentElement.Branches.ContainsKey(branchName))
throw new Exception(file + ", line " + lineNb + " : Duplicated branch " + branchName);
currentElement.Branches[branchName] = new ElementBranch(currentElement, branchName, currentVersion);
continue;
}
if (currentVersion != null && (match = _mergeRegex.Match(line)).Success)
{
bool mergeTo = match.Groups[1].Success;
// Groups[i].Value is the last capture : ok here
string branchCapture = match.Groups[2].Value;
string branchName = string.IsNullOrEmpty(branchCapture) ? "main" : branchCapture.Substring(0, branchCapture.Length - 1);
int versionNumber = int.Parse(match.Groups[3].Value);
// not interested in merges from same branch
if (branchName != currentBranch.BranchName)
currentElementMerges.Add(new Tuple<ElementVersion, string, int, bool>(currentVersion, branchName, versionNumber, mergeTo));
continue;
}
}
Logger.TraceData(TraceEventType.Start | TraceEventType.Information, (int)TraceId.ReadExport, "Stop reading export file", file);
}
private static string ReadLine(TextReader reader, out string eol)
{
int c;
var sb = new StringBuilder();
while ((c = reader.Read()) != -1 && c != '\r' && c != '\n')
sb.Append((char)c);
if (c == -1)
eol = null;
else if (c == '\r')
{
c = reader.Peek();
if (c != '\n')
{
Logger.TraceData(TraceEventType.Warning, (int)TraceId.ReadExport, "Unexpected CR not followed by NL");
eol = "\r";
}
else
{
reader.Read();
eol = "\r\n";
}
}
else
eol = "\n";
if (sb.Length == 0 && eol == null)
return null;
return sb.ToString();
}
}
}