1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmCTestBZR.h"
4
5 #include <cstdlib>
6 #include <list>
7 #include <map>
8 #include <ostream>
9 #include <vector>
10
11 #include <cmext/algorithm>
12
13 #include <cm3p/expat.h>
14
15 #include "cmsys/RegularExpression.hxx"
16
17 #include "cmCTest.h"
18 #include "cmCTestVC.h"
19 #include "cmProcessTools.h"
20 #include "cmSystemTools.h"
21 #include "cmXMLParser.h"
22
cmBZRXMLParserUnknownEncodingHandler(void *,const XML_Char * name,XML_Encoding * info)23 extern "C" int cmBZRXMLParserUnknownEncodingHandler(void* /*unused*/,
24 const XML_Char* name,
25 XML_Encoding* info)
26 {
27 static const int latin1[] = {
28 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
29 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011,
30 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A,
31 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023,
32 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C,
33 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
34 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E,
35 0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
36 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050,
37 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
38 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0062,
39 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B,
40 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
41 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D,
42 0x007E, 0x007F, 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020,
43 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F,
44 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC,
45 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178, 0x00A0, 0x00A1,
46 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA,
47 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3,
48 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC,
49 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5,
50 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE,
51 0x00CF, 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
52 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, 0x00E0,
53 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9,
54 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x00F0, 0x00F1, 0x00F2,
55 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB,
56 0x00FC, 0x00FD, 0x00FE, 0x00FF
57 };
58
59 // The BZR xml output plugin can use some encodings that are not
60 // recognized by expat. This will lead to an error, e.g. "Error
61 // parsing bzr log xml: unknown encoding", the following is a
62 // workaround for these unknown encodings.
63 if (name == std::string("ascii") || name == std::string("cp1252") ||
64 name == std::string("ANSI_X3.4-1968")) {
65 for (unsigned int i = 0; i < 256; ++i) {
66 info->map[i] = latin1[i];
67 }
68 return 1;
69 }
70
71 return 0;
72 }
73
cmCTestBZR(cmCTest * ct,std::ostream & log)74 cmCTestBZR::cmCTestBZR(cmCTest* ct, std::ostream& log)
75 : cmCTestGlobalVC(ct, log)
76 {
77 this->PriorRev = this->Unknown;
78 // Even though it is specified in the documentation, with bzr 1.13
79 // BZR_PROGRESS_BAR has no effect. In the future this bug might be fixed.
80 // Since it doesn't hurt, we specify this environment variable.
81 cmSystemTools::PutEnv("BZR_PROGRESS_BAR=none");
82 }
83
84 cmCTestBZR::~cmCTestBZR() = default;
85
86 class cmCTestBZR::InfoParser : public cmCTestVC::LineParser
87 {
88 public:
InfoParser(cmCTestBZR * bzr,const char * prefix)89 InfoParser(cmCTestBZR* bzr, const char* prefix)
90 : BZR(bzr)
91 , CheckOutFound(false)
92 {
93 this->SetLog(&bzr->Log, prefix);
94 this->RegexCheckOut.compile("checkout of branch: *([^\t\r\n]+)$");
95 this->RegexParent.compile("parent branch: *([^\t\r\n]+)$");
96 }
97
98 private:
99 cmCTestBZR* BZR;
100 bool CheckOutFound;
101 cmsys::RegularExpression RegexCheckOut;
102 cmsys::RegularExpression RegexParent;
ProcessLine()103 bool ProcessLine() override
104 {
105 if (this->RegexCheckOut.find(this->Line)) {
106 this->BZR->URL = this->RegexCheckOut.match(1);
107 this->CheckOutFound = true;
108 } else if (!this->CheckOutFound && this->RegexParent.find(this->Line)) {
109 this->BZR->URL = this->RegexParent.match(1);
110 }
111 return true;
112 }
113 };
114
115 class cmCTestBZR::RevnoParser : public cmCTestVC::LineParser
116 {
117 public:
RevnoParser(cmCTestBZR * bzr,const char * prefix,std::string & rev)118 RevnoParser(cmCTestBZR* bzr, const char* prefix, std::string& rev)
119 : Rev(rev)
120 {
121 this->SetLog(&bzr->Log, prefix);
122 this->RegexRevno.compile("^([0-9]+)$");
123 }
124
125 private:
126 std::string& Rev;
127 cmsys::RegularExpression RegexRevno;
ProcessLine()128 bool ProcessLine() override
129 {
130 if (this->RegexRevno.find(this->Line)) {
131 this->Rev = this->RegexRevno.match(1);
132 }
133 return true;
134 }
135 };
136
LoadInfo()137 std::string cmCTestBZR::LoadInfo()
138 {
139 // Run "bzr info" to get the repository info from the work tree.
140 const char* bzr = this->CommandLineTool.c_str();
141 const char* bzr_info[] = { bzr, "info", nullptr };
142 InfoParser iout(this, "info-out> ");
143 OutputLogger ierr(this->Log, "info-err> ");
144 this->RunChild(bzr_info, &iout, &ierr);
145
146 // Run "bzr revno" to get the repository revision number from the work tree.
147 const char* bzr_revno[] = { bzr, "revno", nullptr };
148 std::string rev;
149 RevnoParser rout(this, "revno-out> ", rev);
150 OutputLogger rerr(this->Log, "revno-err> ");
151 this->RunChild(bzr_revno, &rout, &rerr);
152
153 return rev;
154 }
155
NoteOldRevision()156 bool cmCTestBZR::NoteOldRevision()
157 {
158 this->OldRevision = this->LoadInfo();
159 this->Log << "Revision before update: " << this->OldRevision << "\n";
160 cmCTestLog(this->CTest, HANDLER_OUTPUT,
161 " Old revision of repository is: " << this->OldRevision
162 << "\n");
163 this->PriorRev.Rev = this->OldRevision;
164 return true;
165 }
166
NoteNewRevision()167 bool cmCTestBZR::NoteNewRevision()
168 {
169 this->NewRevision = this->LoadInfo();
170 this->Log << "Revision after update: " << this->NewRevision << "\n";
171 cmCTestLog(this->CTest, HANDLER_OUTPUT,
172 " New revision of repository is: " << this->NewRevision
173 << "\n");
174 this->Log << "URL = " << this->URL << "\n";
175 return true;
176 }
177
178 class cmCTestBZR::LogParser
179 : public cmCTestVC::OutputLogger
180 , private cmXMLParser
181 {
182 public:
LogParser(cmCTestBZR * bzr,const char * prefix)183 LogParser(cmCTestBZR* bzr, const char* prefix)
184 : OutputLogger(bzr->Log, prefix)
185 , BZR(bzr)
186 , EmailRegex("(.*) <([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+)>")
187 {
188 this->InitializeParser();
189 }
~LogParser()190 ~LogParser() override { this->CleanupParser(); }
191
InitializeParser()192 int InitializeParser() override
193 {
194 int res = this->cmXMLParser::InitializeParser();
195 if (res) {
196 XML_SetUnknownEncodingHandler(static_cast<XML_Parser>(this->Parser),
197 cmBZRXMLParserUnknownEncodingHandler,
198 nullptr);
199 }
200 return res;
201 }
202
203 private:
204 cmCTestBZR* BZR;
205
206 using Revision = cmCTestBZR::Revision;
207 using Change = cmCTestBZR::Change;
208 Revision Rev;
209 std::vector<Change> Changes;
210 Change CurChange;
211 std::vector<char> CData;
212
213 cmsys::RegularExpression EmailRegex;
214
ProcessChunk(const char * data,int length)215 bool ProcessChunk(const char* data, int length) override
216 {
217 this->OutputLogger::ProcessChunk(data, length);
218 this->ParseChunk(data, length);
219 return true;
220 }
221
StartElement(const std::string & name,const char **)222 void StartElement(const std::string& name, const char** /*atts*/) override
223 {
224 this->CData.clear();
225 if (name == "log") {
226 this->Rev = Revision();
227 this->Changes.clear();
228 }
229 // affected-files can contain blocks of
230 // modified, unknown, renamed, kind-changed, removed, conflicts, added
231 else if (name == "modified" || name == "renamed" ||
232 name == "kind-changed") {
233 this->CurChange = Change();
234 this->CurChange.Action = 'M';
235 } else if (name == "added") {
236 this->CurChange = Change();
237 this->CurChange = 'A';
238 } else if (name == "removed") {
239 this->CurChange = Change();
240 this->CurChange = 'D';
241 } else if (name == "unknown" || name == "conflicts") {
242 // Should not happen here
243 this->CurChange = Change();
244 }
245 }
246
CharacterDataHandler(const char * data,int length)247 void CharacterDataHandler(const char* data, int length) override
248 {
249 cm::append(this->CData, data, data + length);
250 }
251
EndElement(const std::string & name)252 void EndElement(const std::string& name) override
253 {
254 if (name == "log") {
255 this->BZR->DoRevision(this->Rev, this->Changes);
256 } else if (!this->CData.empty() &&
257 (name == "file" || name == "directory")) {
258 this->CurChange.Path.assign(&this->CData[0], this->CData.size());
259 cmSystemTools::ConvertToUnixSlashes(this->CurChange.Path);
260 this->Changes.push_back(this->CurChange);
261 } else if (!this->CData.empty() && name == "symlink") {
262 // symlinks have an arobase at the end in the log
263 this->CurChange.Path.assign(&this->CData[0], this->CData.size() - 1);
264 cmSystemTools::ConvertToUnixSlashes(this->CurChange.Path);
265 this->Changes.push_back(this->CurChange);
266 } else if (!this->CData.empty() && name == "committer") {
267 this->Rev.Author.assign(&this->CData[0], this->CData.size());
268 if (this->EmailRegex.find(this->Rev.Author)) {
269 this->Rev.Author = this->EmailRegex.match(1);
270 this->Rev.EMail = this->EmailRegex.match(2);
271 }
272 } else if (!this->CData.empty() && name == "timestamp") {
273 this->Rev.Date.assign(&this->CData[0], this->CData.size());
274 } else if (!this->CData.empty() && name == "message") {
275 this->Rev.Log.assign(&this->CData[0], this->CData.size());
276 } else if (!this->CData.empty() && name == "revno") {
277 this->Rev.Rev.assign(&this->CData[0], this->CData.size());
278 }
279 this->CData.clear();
280 }
281
ReportError(int,int,const char * msg)282 void ReportError(int /*line*/, int /*column*/, const char* msg) override
283 {
284 this->BZR->Log << "Error parsing bzr log xml: " << msg << "\n";
285 }
286 };
287
288 class cmCTestBZR::UpdateParser : public cmCTestVC::LineParser
289 {
290 public:
UpdateParser(cmCTestBZR * bzr,const char * prefix)291 UpdateParser(cmCTestBZR* bzr, const char* prefix)
292 : BZR(bzr)
293 {
294 this->SetLog(&bzr->Log, prefix);
295 this->RegexUpdate.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
296 }
297
298 private:
299 cmCTestBZR* BZR;
300 cmsys::RegularExpression RegexUpdate;
301
ProcessChunk(const char * first,int length)302 bool ProcessChunk(const char* first, int length) override
303 {
304 bool last_is_new_line = (*first == '\r' || *first == '\n');
305
306 const char* const last = first + length;
307 for (const char* c = first; c != last; ++c) {
308 if (*c == '\r' || *c == '\n') {
309 if (!last_is_new_line) {
310 // Log this line.
311 if (this->Log && this->Prefix) {
312 *this->Log << this->Prefix << this->Line << "\n";
313 }
314
315 // Hand this line to the subclass implementation.
316 if (!this->ProcessLine()) {
317 this->Line.clear();
318 return false;
319 }
320
321 this->Line.clear();
322 last_is_new_line = true;
323 }
324 } else {
325 // Append this character to the line under construction.
326 this->Line.append(1, *c);
327 last_is_new_line = false;
328 }
329 }
330 return true;
331 }
332
ProcessLine()333 bool ProcessLine() override
334 {
335 if (this->RegexUpdate.find(this->Line)) {
336 this->DoPath(this->RegexUpdate.match(1)[0],
337 this->RegexUpdate.match(2)[0],
338 this->RegexUpdate.match(3)[0], this->RegexUpdate.match(4));
339 }
340 return true;
341 }
342
DoPath(char c0,char c1,char c2,std::string path)343 void DoPath(char c0, char c1, char c2, std::string path)
344 {
345 if (path.empty()) {
346 return;
347 }
348 cmSystemTools::ConvertToUnixSlashes(path);
349
350 const std::string dir = cmSystemTools::GetFilenamePath(path);
351 const std::string name = cmSystemTools::GetFilenameName(path);
352
353 if (c0 == 'C') {
354 this->BZR->Dirs[dir][name].Status = PathConflicting;
355 return;
356 }
357
358 if (c1 == 'M' || c1 == 'K' || c1 == 'N' || c1 == 'D' || c2 == '*') {
359 this->BZR->Dirs[dir][name].Status = PathUpdated;
360 return;
361 }
362 }
363 };
364
UpdateImpl()365 bool cmCTestBZR::UpdateImpl()
366 {
367 // Get user-specified update options.
368 std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
369 if (opts.empty()) {
370 opts = this->CTest->GetCTestConfiguration("BZRUpdateOptions");
371 }
372 std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
373
374 // TODO: if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
375
376 // Use "bzr pull" to update the working tree.
377 std::vector<char const*> bzr_update;
378 bzr_update.push_back(this->CommandLineTool.c_str());
379 bzr_update.push_back("pull");
380
381 for (std::string const& arg : args) {
382 bzr_update.push_back(arg.c_str());
383 }
384
385 bzr_update.push_back(this->URL.c_str());
386
387 bzr_update.push_back(nullptr);
388
389 // For some reason bzr uses stderr to display the update status.
390 OutputLogger out(this->Log, "pull-out> ");
391 UpdateParser err(this, "pull-err> ");
392 return this->RunUpdateCommand(&bzr_update[0], &out, &err);
393 }
394
LoadRevisions()395 bool cmCTestBZR::LoadRevisions()
396 {
397 cmCTestLog(this->CTest, HANDLER_OUTPUT,
398 " Gathering version information (one . per revision):\n"
399 " "
400 << std::flush);
401
402 // We are interested in every revision included in the update.
403 this->Revisions.clear();
404 std::string revs;
405 if (atoi(this->OldRevision.c_str()) <= atoi(this->NewRevision.c_str())) {
406 // DoRevision takes care of discarding the information about OldRevision
407 revs = this->OldRevision + ".." + this->NewRevision;
408 } else {
409 return true;
410 }
411
412 // Run "bzr log" to get all global revisions of interest.
413 const char* bzr = this->CommandLineTool.c_str();
414 const char* bzr_log[] = {
415 bzr, "log", "-v", "-r", revs.c_str(), "--xml", this->URL.c_str(), nullptr
416 };
417 {
418 LogParser out(this, "log-out> ");
419 OutputLogger err(this->Log, "log-err> ");
420 this->RunChild(bzr_log, &out, &err);
421 }
422 cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
423 return true;
424 }
425
426 class cmCTestBZR::StatusParser : public cmCTestVC::LineParser
427 {
428 public:
StatusParser(cmCTestBZR * bzr,const char * prefix)429 StatusParser(cmCTestBZR* bzr, const char* prefix)
430 : BZR(bzr)
431 {
432 this->SetLog(&bzr->Log, prefix);
433 this->RegexStatus.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
434 }
435
436 private:
437 cmCTestBZR* BZR;
438 cmsys::RegularExpression RegexStatus;
ProcessLine()439 bool ProcessLine() override
440 {
441 if (this->RegexStatus.find(this->Line)) {
442 this->DoPath(this->RegexStatus.match(1)[0],
443 this->RegexStatus.match(2)[0],
444 this->RegexStatus.match(3)[0], this->RegexStatus.match(4));
445 }
446 return true;
447 }
448
DoPath(char c0,char c1,char c2,std::string path)449 void DoPath(char c0, char c1, char c2, std::string path)
450 {
451 if (path.empty()) {
452 return;
453 }
454 cmSystemTools::ConvertToUnixSlashes(path);
455
456 if (c0 == 'C') {
457 this->BZR->DoModification(PathConflicting, path);
458 return;
459 }
460
461 if (c0 == '+' || c0 == 'R' || c0 == 'P' || c1 == 'M' || c1 == 'K' ||
462 c1 == 'N' || c1 == 'D' || c2 == '*') {
463 this->BZR->DoModification(PathModified, path);
464 return;
465 }
466 }
467 };
468
LoadModifications()469 bool cmCTestBZR::LoadModifications()
470 {
471 // Run "bzr status" which reports local modifications.
472 const char* bzr = this->CommandLineTool.c_str();
473 const char* bzr_status[] = { bzr, "status", "-SV", nullptr };
474 StatusParser out(this, "status-out> ");
475 OutputLogger err(this->Log, "status-err> ");
476 this->RunChild(bzr_status, &out, &err);
477 return true;
478 }
479