forked from CamTechConsultants/CvsntGitImporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CvsLogParser.cs
270 lines (243 loc) · 7.6 KB
/
CvsLogParser.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
/*
* John Hall <john.hall@camtechconsultants.com>
* Copyright (c) Cambridge Technology Consultants Ltd. All rights reserved.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
namespace CTC.CvsntGitImporter
{
/// <summary>
/// CVS log file parser.
/// </summary>
class CvsLogParser
{
private const string LogSeparator = "----------------------------";
private const string FileSeparator = "=============================================================================";
private static readonly char[] FieldDelimiter = new[] { ';' };
private readonly string m_sandboxPath;
private readonly CvsLogReader m_reader;
private readonly List<FileInfo> m_files = new List<FileInfo>();
private readonly HashSet<string> m_excludedTags = new HashSet<string>();
private readonly InclusionMatcher m_branchMatcher;
private readonly HashSet<string> m_excludedBranches = new HashSet<string>();
private string m_repo;
private string m_fullRepoPath;
private CvsLogParser(string sandboxPath, CvsLogReader reader, InclusionMatcher branchMatcher)
{
m_sandboxPath = sandboxPath;
m_reader = reader;
m_branchMatcher = branchMatcher;
}
public CvsLogParser(string sandboxPath, string logFile, InclusionMatcher branchMatcher)
: this(sandboxPath, new CvsLogReader(logFile), branchMatcher)
{
}
public CvsLogParser(string sandboxPath, TextReader reader, InclusionMatcher branchMatcher)
: this(sandboxPath, new CvsLogReader(reader), branchMatcher)
{
}
/// <summary>
/// Gets a list of all the files.
/// </summary>
public IEnumerable<FileInfo> Files
{
get { return m_files; }
}
/// <summary>
/// Gets a list of all the tags that were excluded.
/// </summary>
public IEnumerable<string> ExcludedTags
{
get { return m_excludedTags; }
}
/// <summary>
/// Gets a list of all the branches that were excluded.
/// </summary>
public IEnumerable<string> ExcludedBranches
{
get { return m_excludedBranches; }
}
/// <summary>
/// Parse the log returning a list of the individual commits to the individual files.
/// </summary>
public IEnumerable<FileRevision> Parse()
{
var state = State.Start;
FileInfo currentFile = null;
Revision revision = Revision.Empty;
FileRevision commit = null;
m_repo = GetCvsRepo();
foreach (var line in m_reader)
{
switch (state)
{
case State.Start:
if (line.StartsWith("RCS file: "))
{
currentFile = new FileInfo(ExtractFileName(line));
m_files.Add(currentFile);
state = State.InFileHeader;
}
break;
case State.InFileHeader:
if (line == LogSeparator)
state = State.ExpectCommitRevision;
else if (line == "symbolic names:")
state = State.InTags;
break;
case State.InTags:
if (!line.StartsWith("\t"))
{
state = State.InFileHeader;
}
else
{
var tagMatch = Regex.Match(line, @"^\t(\S+): (\S+)");
if (!tagMatch.Success)
throw MakeParseException("Invalid tag line: '{0}'", line);
var tagName = tagMatch.Groups[1].Value;
var tagRevision = Revision.Create(tagMatch.Groups[2].Value);
if (tagRevision.IsBranch)
{
if (m_branchMatcher.Match(tagName))
currentFile.AddBranchTag(tagName, tagRevision);
else
m_excludedBranches.Add(tagName);
}
else
{
currentFile.AddTag(tagName, tagRevision);
}
}
break;
case State.ExpectCommitRevision:
if (line.StartsWith("revision "))
{
revision = Revision.Create(line.Substring(9));
state = State.ExpectCommitInfo;
}
else
{
throw MakeParseException("Expected revision line, found '{0}'", line);
}
break;
case State.ExpectCommitInfo:
commit = ParseFields(currentFile, revision, line);
state = State.ExpectCommitMessage;
break;
case State.ExpectCommitMessage:
if (line == LogSeparator)
{
if (commit != null)
yield return commit;
state = State.ExpectCommitRevision;
}
else if (line == FileSeparator)
{
if (commit != null)
yield return commit;
state = State.Start;
}
else if (!line.StartsWith("branches: "))
{
if (commit != null)
commit.AddMessage(line);
}
break;
}
}
}
private string GetCvsRepo()
{
try
{
var repositoryFile = Path.Combine(m_sandboxPath, @"CVS\Repository");
return File.ReadAllText(repositoryFile).Trim() + "/";
}
catch (FileNotFoundException)
{
throw new ImportFailedException(String.Format("Unable to find CVS sandbox: {0}", m_sandboxPath));
}
catch (UnauthorizedAccessException uae)
{
throw new IOException(uae.Message, uae);
}
catch (System.Security.SecurityException se)
{
throw new IOException(se.Message, se);
}
}
private string ExtractFileName(string line)
{
var match = Regex.Match(line, @"^RCS file: (.*),v");
if (!match.Success)
throw MakeParseException("Invalid RCS file line: '{0}'", line);
var filepath = match.Groups[1].Value;
if (m_fullRepoPath == null)
{
var i = filepath.IndexOf(m_repo);
if (i < 0)
throw MakeParseException("CVS rlog file does not seem to match the repository");
m_fullRepoPath = filepath.Remove(i + m_repo.Length);
}
if (!filepath.StartsWith(m_fullRepoPath))
throw MakeParseException("File path does not seem to match the repository: {0}", filepath);
return filepath.Substring(m_fullRepoPath.Length);
}
/// <summary>
/// Parse a line of the CVS log containing data about a commit.
/// </summary>
/// <returns>The commit, or null if the commit is to be ignored</returns>
private FileRevision ParseFields(FileInfo currentFile, Revision revision, string line)
{
var fields = line.Split(FieldDelimiter, StringSplitOptions.RemoveEmptyEntries);
string author = null;
string commitId = null;
string dateStr = null;
string mergepointStr = null;
string state = null;
foreach (var field in fields)
{
var separator = field.IndexOf(':');
if (separator <= 0 || separator >= field.Length - 1)
throw MakeParseException("Invalid field: '{0}'", field);
var key = field.Remove(separator).Trim();
var value = field.Substring(separator + 1).Trim();
switch (key)
{
case "author": author = value; break;
case "commitid": commitId = value; break;
case "date": dateStr = value; break;
case "mergepoint": mergepointStr = value; break;
case "state": state = value; break;
}
}
var time = DateTime.ParseExact(dateStr, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
var mergepoint = mergepointStr == null ? Revision.Empty : Revision.Create(mergepointStr);
return new FileRevision(
file: currentFile,
revision: revision,
mergepoint: mergepoint,
time: time,
author: author,
commitId: commitId ?? "",
isDead: state == "dead");
}
private ParseException MakeParseException(string format, params object[] args)
{
return new ParseException(String.Format("Line {0}: {1}", m_reader.LineNumber, String.Format(format, args)));
}
private enum State
{
Start = 0,
InFileHeader,
InTags,
ExpectCommitRevision,
ExpectCommitInfo,
ExpectCommitMessage,
}
}
}