-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCleartool.cs
257 lines (236 loc) · 11.1 KB
/
Cleartool.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
namespace GitImporter
{
public class Cleartool : IDisposable
{
private const string _cleartool = "cleartool_tty.exe";
public static TraceSource Logger = Program.Logger;
private readonly Process _process;
private readonly Thread _outputThread;
private readonly Thread _errorThread;
private readonly ManualResetEventSlim _cleartoolAvailable = new ManualResetEventSlim();
private readonly string _clearcaseRoot;
private readonly LabelFilter _labelFilter;
private readonly Regex _directoryEntryRegex = new Regex("^===> name: \"([^\"]+)\"");
private readonly Regex _oidRegex = new Regex(@"cataloged oid: (\S+) \(mtype \d+\)");
private readonly Regex _symlinkRegex = new Regex("^.+ --> (.+)$");
private readonly Regex _mergeRegex = new Regex(@"^(""Merge@\d+@[^""]+"" (<-|->) ""[^""]+\\([^\\]+)\\((CHECKEDOUT\.)?\d+)"" )+$");
private readonly Regex _separator = new Regex("~#~");
private List<string> _currentOutput = new List<string>();
private string _lastError;
private const int _nbRetry = 15;
public Cleartool(string clearcaseRoot, LabelFilter labelFilter)
{
_labelFilter = labelFilter;
var startInfo = new ProcessStartInfo(_cleartool)
{ UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true };
_process = new Process { StartInfo = startInfo };
_process.Start();
_outputThread = new Thread(ReadOutput) { IsBackground = true };
_outputThread.Start();
_errorThread = new Thread(ReadError) { IsBackground = true };
_errorThread.Start();
_cleartoolAvailable.Wait();
_clearcaseRoot = clearcaseRoot;
ExecuteCommand("cd \"" + _clearcaseRoot + "\"");
}
void ReadOutput()
{
int c;
string currentString = "";
const string prompt = "cleartool> ";
int promptLength = prompt.Length;
int currentIndexInPrompt = 0;
while ((c = _process.StandardOutput.Read()) != -1)
{
switch ((char)c)
{
case '\r':
case '\n':
if (!string.IsNullOrWhiteSpace(currentString))
_currentOutput.Add(currentString);
currentString = "";
break;
default:
currentString += (char)c;
if (prompt[currentIndexInPrompt] == (char)c)
{
currentIndexInPrompt++;
if (currentIndexInPrompt == promptLength)
{
string last = currentString.Substring(0, currentString.Length - promptLength);
if (last.Length > 0)
_currentOutput.Add(last);
currentString = "";
currentIndexInPrompt = 0;
_cleartoolAvailable.Set();
}
}
else
// fortunately, there is only one 'c' in the prompt
currentIndexInPrompt = (char)c == prompt[0] ? 1 : 0;
break;
}
}
}
void ReadError()
{
string error;
while ((error = _process.StandardError.ReadLine()) != null)
{
_lastError = error;
Logger.TraceData(TraceEventType.Warning, (int)TraceId.Cleartool, error);
}
}
private List<string> ExecuteCommand(string cmd)
{
for (int i = 0; i < _nbRetry; i++)
{
Logger.TraceData(TraceEventType.Start | TraceEventType.Verbose, (int)TraceId.Cleartool, "Start executing cleartool command", cmd);
_cleartoolAvailable.Reset();
_lastError = null;
_currentOutput = new List<string>();
_process.StandardInput.WriteLine(cmd);
_cleartoolAvailable.Wait();
Logger.TraceData(TraceEventType.Stop | TraceEventType.Verbose, (int)TraceId.Cleartool, "Stop executing cleartool command", cmd);
if (_lastError != null)
{
bool lastTry = i == _nbRetry - 1;
Logger.TraceData(TraceEventType.Warning, (int)TraceId.Cleartool, "Cleartool command failed" + (!lastTry ? ", retrying" : ""), cmd);
if (!lastTry)
Thread.Sleep(2000);
}
else
{
if (i > 0)
Logger.TraceData(TraceEventType.Information, (int)TraceId.Cleartool, "Cleartool command succeeded on retry #" + i, cmd);
var result = _currentOutput;
return result;
}
}
Logger.TraceData(TraceEventType.Error, (int)TraceId.Cleartool, "Cleartool command failed " + _nbRetry + " times, aborting", cmd);
return new List<string>();
}
public List<string> Lsvtree(string element)
{
return ExecuteCommand("lsvtree -short -all -obsolete \"" + element + "\"").Select(v => v.Substring(v.LastIndexOf("@@") + 2)).ToList();
}
/// <summary>
/// List content of a directory (possibly with a version-extended path),
/// as a dictionary <name as it appears in this version, oid of the element>
/// Symbolic links are stored as a string with the SYMLINK prefix
/// </summary>
public Dictionary<string, string> Ls(string element)
{
var result = new Dictionary<string, string>();
string name = null, oid = null;
foreach (var line in ExecuteCommand("ls -dump \"" + element + "\""))
{
Match match;
if ((match = _directoryEntryRegex.Match(line)).Success)
{
if (name != null && oid != null)
result[name] = oid;
name = match.Groups[1].Value;
oid = null;
}
else if ((match = _oidRegex.Match(line)).Success)
oid = match.Groups[1].Value;
else if ((match = _symlinkRegex.Match(line)).Success)
oid = SymLinkElement.SYMLINK + match.Groups[1].Value;
}
if (name != null && oid != null)
result[name] = oid;
return result;
}
public string GetOid(string element)
{
bool isDir;
return GetOid(element, out isDir);
}
public string GetOid(string element, out bool isDir)
{
isDir = false;
if (!element.EndsWith("@@"))
element += "@@";
var result = ExecuteCommand("desc -fmt \"%On" + _separator + "%m\" \"" + element + "\"");
if (result.Count == 0)
return null;
string[] parts = _separator.Split(result[0]);
isDir = parts[1] == "directory element";
return parts[0];
}
public string GetPredecessor(string version)
{
return ExecuteCommand("desc -pred -s \"" + version + "\"").FirstOrDefault();
}
public void GetVersionDetails(ElementVersion version, out List<Tuple<string, int>> mergesTo, out List<Tuple<string, int>> mergesFrom)
{
bool isDir = version.Element.IsDirectory;
// not interested in directory merges
string format = "%Fu" + _separator + "%u" + _separator + "%Nd" + _separator + "%Nc" + _separator + "%Nl" +
(isDir ? "" : _separator + "%[hlink:Merge]p");
// string.Join to handle multi-line comments
string raw = string.Join("\r\n", ExecuteCommand("desc -fmt \"" + format + "\" \"" + version + "\""));
string[] parts = _separator.Split(raw);
version.AuthorName = string.Intern(parts[0]);
version.AuthorLogin = string.Intern(parts[1]);
version.Date = DateTime.ParseExact(parts[2], "yyyyMMdd.HHmmss", null).ToUniversalTime();
version.Comment = string.Intern(parts[3]);
foreach (string label in parts[4].Split(' '))
if (!string.IsNullOrWhiteSpace(label) && _labelFilter.ShouldKeep(label))
version.Labels.Add(string.Intern(label));
mergesTo = mergesFrom = null;
if (isDir || string.IsNullOrEmpty(parts[5]))
return;
Match match = _mergeRegex.Match(parts[5]);
if (!match.Success)
{
Logger.TraceData(TraceEventType.Warning, (int)TraceId.Cleartool, "Failed to parse merge data '" + parts[5] + "'");
return;
}
mergesTo = new List<Tuple<string, int>>();
mergesFrom = new List<Tuple<string, int>>();
int count = match.Groups[1].Captures.Count;
for (int i = 0; i < count; i++)
{
var addTo = match.Groups[2].Captures[i].Value == "->" ? mergesTo : mergesFrom;
var branch = match.Groups[3].Captures[i].Value;
var versionNumber = match.Groups[4].Captures[i].Value;
if (versionNumber.StartsWith("CHECKEDOUT"))
continue;
addTo.Add(new Tuple<string, int>(branch, int.Parse(versionNumber)));
}
}
public string Get(string element)
{
string tmp = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
ExecuteCommand("get -to \"" + tmp + "\" \"" + element + "\"");
return tmp;
}
public string GetElement(string oid)
{
string fullPath = ExecuteCommand("desc -s oid:" + oid).FirstOrDefault();
if (string.IsNullOrEmpty(fullPath))
return null;
// try to normalize to _clearcaseRoot, it depends if we are using a dynamic or snapshot view
string toRemove = _clearcaseRoot + "\\";
while (!fullPath.StartsWith(toRemove))
toRemove = toRemove.Substring(toRemove.IndexOf('\\') + 1);
return fullPath.Substring(toRemove.Length);
}
public void Dispose()
{
_process.StandardInput.WriteLine("quit");
_outputThread.Join();
_errorThread.Join();
_process.Close();
}
}
}