cmCTestP4.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 "cmCTestP4.h"
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <ostream>
  7. #include <utility>
  8. #include <cmext/algorithm>
  9. #include "cmsys/RegularExpression.hxx"
  10. #include "cmCTest.h"
  11. #include "cmCTestVC.h"
  12. #include "cmList.h"
  13. #include "cmRange.h"
  14. #include "cmSystemTools.h"
  15. cmCTestP4::cmCTestP4(cmCTest* ct, std::ostream& log)
  16. : cmCTestGlobalVC(ct, log)
  17. {
  18. this->PriorRev = this->Unknown;
  19. }
  20. cmCTestP4::~cmCTestP4() = default;
  21. class cmCTestP4::IdentifyParser : public cmCTestVC::LineParser
  22. {
  23. public:
  24. IdentifyParser(cmCTestP4* p4, const char* prefix, std::string& rev)
  25. : Rev(rev)
  26. {
  27. this->SetLog(&p4->Log, prefix);
  28. this->RegexIdentify.compile("^Change ([0-9]+) on");
  29. }
  30. private:
  31. std::string& Rev;
  32. cmsys::RegularExpression RegexIdentify;
  33. bool ProcessLine() override
  34. {
  35. if (this->RegexIdentify.find(this->Line)) {
  36. this->Rev = this->RegexIdentify.match(1);
  37. return false;
  38. }
  39. return true;
  40. }
  41. };
  42. class cmCTestP4::ChangesParser : public cmCTestVC::LineParser
  43. {
  44. public:
  45. ChangesParser(cmCTestP4* p4, const char* prefix)
  46. : P4(p4)
  47. {
  48. this->SetLog(&this->P4->Log, prefix);
  49. this->RegexIdentify.compile("^Change ([0-9]+) on");
  50. }
  51. private:
  52. cmsys::RegularExpression RegexIdentify;
  53. cmCTestP4* P4;
  54. bool ProcessLine() override
  55. {
  56. if (this->RegexIdentify.find(this->Line)) {
  57. this->P4->ChangeLists.push_back(this->RegexIdentify.match(1));
  58. }
  59. return true;
  60. }
  61. };
  62. class cmCTestP4::UserParser : public cmCTestVC::LineParser
  63. {
  64. public:
  65. UserParser(cmCTestP4* p4, const char* prefix)
  66. : P4(p4)
  67. {
  68. this->SetLog(&this->P4->Log, prefix);
  69. this->RegexUser.compile("^(.+) <(.*)> \\((.*)\\) accessed (.*)$");
  70. }
  71. private:
  72. cmsys::RegularExpression RegexUser;
  73. cmCTestP4* P4;
  74. bool ProcessLine() override
  75. {
  76. if (this->RegexUser.find(this->Line)) {
  77. User NewUser;
  78. NewUser.UserName = this->RegexUser.match(1);
  79. NewUser.EMail = this->RegexUser.match(2);
  80. NewUser.Name = this->RegexUser.match(3);
  81. NewUser.AccessTime = this->RegexUser.match(4);
  82. this->P4->Users[this->RegexUser.match(1)] = NewUser;
  83. return false;
  84. }
  85. return true;
  86. }
  87. };
  88. /* Diff format:
  89. ==== //depot/file#rev - /absolute/path/to/file ====
  90. (diff data)
  91. ==== //depot/file2#rev - /absolute/path/to/file2 ====
  92. (diff data)
  93. ==== //depot/file3#rev - /absolute/path/to/file3 ====
  94. ==== //depot/file4#rev - /absolute/path/to/file4 ====
  95. (diff data)
  96. */
  97. class cmCTestP4::DiffParser : public cmCTestVC::LineParser
  98. {
  99. public:
  100. DiffParser(cmCTestP4* p4, const char* prefix)
  101. : P4(p4)
  102. {
  103. this->SetLog(&this->P4->Log, prefix);
  104. this->RegexDiff.compile("^==== (.*)#[0-9]+ - (.*)");
  105. }
  106. private:
  107. cmCTestP4* P4;
  108. bool AlreadyNotified = false;
  109. std::string CurrentPath;
  110. cmsys::RegularExpression RegexDiff;
  111. bool ProcessLine() override
  112. {
  113. if (!this->Line.empty() && this->Line[0] == '=' &&
  114. this->RegexDiff.find(this->Line)) {
  115. this->CurrentPath = this->RegexDiff.match(1);
  116. this->AlreadyNotified = false;
  117. } else {
  118. if (!this->AlreadyNotified) {
  119. this->P4->DoModification(PathModified, this->CurrentPath);
  120. this->AlreadyNotified = true;
  121. }
  122. }
  123. return true;
  124. }
  125. };
  126. cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
  127. {
  128. auto it = this->Users.find(username);
  129. if (it == this->Users.end()) {
  130. std::vector<char const*> p4_users;
  131. this->SetP4Options(p4_users);
  132. p4_users.push_back("users");
  133. p4_users.push_back("-m");
  134. p4_users.push_back("1");
  135. p4_users.push_back(username.c_str());
  136. p4_users.push_back(nullptr);
  137. UserParser out(this, "users-out> ");
  138. OutputLogger err(this->Log, "users-err> ");
  139. this->RunChild(p4_users.data(), &out, &err);
  140. // The user should now be added to the map. Search again.
  141. it = this->Users.find(username);
  142. if (it == this->Users.end()) {
  143. return cmCTestP4::User();
  144. }
  145. }
  146. return it->second;
  147. }
  148. /* Commit format:
  149. Change 1111111 by user@client on 2013/09/26 11:50:36
  150. text
  151. text
  152. Affected files ...
  153. ... //path/to/file#rev edit
  154. ... //path/to/file#rev add
  155. ... //path/to/file#rev delete
  156. ... //path/to/file#rev integrate
  157. */
  158. class cmCTestP4::DescribeParser : public cmCTestVC::LineParser
  159. {
  160. public:
  161. DescribeParser(cmCTestP4* p4, const char* prefix)
  162. : LineParser('\n', false)
  163. , P4(p4)
  164. {
  165. this->SetLog(&this->P4->Log, prefix);
  166. this->RegexHeader.compile("^Change ([0-9]+) by (.+)@(.+) on (.*)$");
  167. this->RegexDiff.compile(R"(^\.\.\. (.*)#[0-9]+ ([^ ]+)$)");
  168. }
  169. private:
  170. cmsys::RegularExpression RegexHeader;
  171. cmsys::RegularExpression RegexDiff;
  172. cmCTestP4* P4;
  173. using Revision = cmCTestP4::Revision;
  174. using Change = cmCTestP4::Change;
  175. std::vector<Change> Changes;
  176. enum SectionType
  177. {
  178. SectionHeader,
  179. SectionBody,
  180. SectionDiffHeader,
  181. SectionDiff,
  182. SectionCount
  183. };
  184. SectionType Section = SectionHeader;
  185. Revision Rev;
  186. bool ProcessLine() override
  187. {
  188. if (this->Line.empty()) {
  189. this->NextSection();
  190. } else {
  191. switch (this->Section) {
  192. case SectionHeader:
  193. this->DoHeaderLine();
  194. break;
  195. case SectionBody:
  196. this->DoBodyLine();
  197. break;
  198. case SectionDiffHeader:
  199. break; // nothing to do
  200. case SectionDiff:
  201. this->DoDiffLine();
  202. break;
  203. case SectionCount:
  204. break; // never happens
  205. }
  206. }
  207. return true;
  208. }
  209. void NextSection()
  210. {
  211. if (this->Section == SectionDiff) {
  212. this->P4->DoRevision(this->Rev, this->Changes);
  213. this->Rev = Revision();
  214. }
  215. this->Section =
  216. static_cast<SectionType>((this->Section + 1) % SectionCount);
  217. }
  218. void DoHeaderLine()
  219. {
  220. if (this->RegexHeader.find(this->Line)) {
  221. this->Rev.Rev = this->RegexHeader.match(1);
  222. this->Rev.Date = this->RegexHeader.match(4);
  223. cmCTestP4::User user = this->P4->GetUserData(this->RegexHeader.match(2));
  224. this->Rev.Author = user.Name;
  225. this->Rev.EMail = user.EMail;
  226. this->Rev.Committer = this->Rev.Author;
  227. this->Rev.CommitterEMail = this->Rev.EMail;
  228. this->Rev.CommitDate = this->Rev.Date;
  229. }
  230. }
  231. void DoBodyLine()
  232. {
  233. if (this->Line[0] == '\t') {
  234. this->Rev.Log += this->Line.substr(1);
  235. }
  236. this->Rev.Log += "\n";
  237. }
  238. void DoDiffLine()
  239. {
  240. if (this->RegexDiff.find(this->Line)) {
  241. Change change;
  242. std::string Path = this->RegexDiff.match(1);
  243. if (Path.length() > 2 && Path[0] == '/' && Path[1] == '/') {
  244. size_t found = Path.find('/', 2);
  245. if (found != std::string::npos) {
  246. Path = Path.substr(found + 1);
  247. }
  248. }
  249. change.Path = Path;
  250. std::string action = this->RegexDiff.match(2);
  251. if (action == "add") {
  252. change.Action = 'A';
  253. } else if (action == "delete") {
  254. change.Action = 'D';
  255. } else if (action == "edit" || action == "integrate") {
  256. change.Action = 'M';
  257. }
  258. this->Changes.push_back(change);
  259. }
  260. }
  261. };
  262. void cmCTestP4::SetP4Options(std::vector<char const*>& CommandOptions)
  263. {
  264. if (this->P4Options.empty()) {
  265. const char* p4 = this->CommandLineTool.c_str();
  266. this->P4Options.emplace_back(p4);
  267. // The CTEST_P4_CLIENT variable sets the P4 client used when issuing
  268. // Perforce commands, if it's different from the default one.
  269. std::string client = this->CTest->GetCTestConfiguration("P4Client");
  270. if (!client.empty()) {
  271. this->P4Options.emplace_back("-c");
  272. this->P4Options.push_back(client);
  273. }
  274. // Set the message language to be English, in case the P4 admin
  275. // has localized them
  276. this->P4Options.emplace_back("-L");
  277. this->P4Options.emplace_back("en");
  278. // The CTEST_P4_OPTIONS variable adds additional Perforce command line
  279. // options before the main command
  280. std::string opts = this->CTest->GetCTestConfiguration("P4Options");
  281. cm::append(this->P4Options, cmSystemTools::ParseArguments(opts));
  282. }
  283. CommandOptions.clear();
  284. for (std::string const& o : this->P4Options) {
  285. CommandOptions.push_back(o.c_str());
  286. }
  287. }
  288. std::string cmCTestP4::GetWorkingRevision()
  289. {
  290. std::vector<char const*> p4_identify;
  291. this->SetP4Options(p4_identify);
  292. p4_identify.push_back("changes");
  293. p4_identify.push_back("-m");
  294. p4_identify.push_back("1");
  295. p4_identify.push_back("-t");
  296. std::string source = this->SourceDirectory + "/...#have";
  297. p4_identify.push_back(source.c_str());
  298. p4_identify.push_back(nullptr);
  299. std::string rev;
  300. IdentifyParser out(this, "p4_changes-out> ", rev);
  301. OutputLogger err(this->Log, "p4_changes-err> ");
  302. bool result = this->RunChild(p4_identify.data(), &out, &err);
  303. // If there was a problem contacting the server return "<unknown>"
  304. if (!result) {
  305. return "<unknown>";
  306. }
  307. if (rev.empty()) {
  308. return "0";
  309. }
  310. return rev;
  311. }
  312. bool cmCTestP4::NoteOldRevision()
  313. {
  314. this->OldRevision = this->GetWorkingRevision();
  315. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  316. " Old revision of repository is: " << this->OldRevision
  317. << "\n");
  318. this->PriorRev.Rev = this->OldRevision;
  319. return true;
  320. }
  321. bool cmCTestP4::NoteNewRevision()
  322. {
  323. this->NewRevision = this->GetWorkingRevision();
  324. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  325. " New revision of repository is: " << this->NewRevision
  326. << "\n");
  327. return true;
  328. }
  329. bool cmCTestP4::LoadRevisions()
  330. {
  331. std::vector<char const*> p4_changes;
  332. this->SetP4Options(p4_changes);
  333. // Use 'p4 changes ...@old,new' to get a list of changelists
  334. std::string range = this->SourceDirectory + "/...";
  335. // If any revision is unknown it means we couldn't contact the server.
  336. // Do not process updates
  337. if (this->OldRevision == "<unknown>" || this->NewRevision == "<unknown>") {
  338. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  339. " At least one of the revisions "
  340. << "is unknown. No repository changes will be reported.\n");
  341. return false;
  342. }
  343. range.append("@")
  344. .append(this->OldRevision)
  345. .append(",")
  346. .append(this->NewRevision);
  347. p4_changes.push_back("changes");
  348. p4_changes.push_back(range.c_str());
  349. p4_changes.push_back(nullptr);
  350. ChangesParser out(this, "p4_changes-out> ");
  351. OutputLogger err(this->Log, "p4_changes-err> ");
  352. this->ChangeLists.clear();
  353. this->RunChild(p4_changes.data(), &out, &err);
  354. if (this->ChangeLists.empty()) {
  355. return true;
  356. }
  357. // p4 describe -s ...@1111111,2222222
  358. std::vector<char const*> p4_describe;
  359. for (std::string const& i : cmReverseRange(this->ChangeLists)) {
  360. this->SetP4Options(p4_describe);
  361. p4_describe.push_back("describe");
  362. p4_describe.push_back("-s");
  363. p4_describe.push_back(i.c_str());
  364. p4_describe.push_back(nullptr);
  365. DescribeParser outDescribe(this, "p4_describe-out> ");
  366. OutputLogger errDescribe(this->Log, "p4_describe-err> ");
  367. this->RunChild(p4_describe.data(), &outDescribe, &errDescribe);
  368. }
  369. return true;
  370. }
  371. bool cmCTestP4::LoadModifications()
  372. {
  373. std::vector<char const*> p4_diff;
  374. this->SetP4Options(p4_diff);
  375. p4_diff.push_back("diff");
  376. // Ideally we would use -Od but not all clients support it
  377. p4_diff.push_back("-dn");
  378. std::string source = this->SourceDirectory + "/...";
  379. p4_diff.push_back(source.c_str());
  380. p4_diff.push_back(nullptr);
  381. DiffParser out(this, "p4_diff-out> ");
  382. OutputLogger err(this->Log, "p4_diff-err> ");
  383. this->RunChild(p4_diff.data(), &out, &err);
  384. return true;
  385. }
  386. bool cmCTestP4::UpdateCustom(const std::string& custom)
  387. {
  388. cmList p4_custom_command{ custom, cmList::EmptyElements::Yes };
  389. std::vector<char const*> p4_custom;
  390. p4_custom.reserve(p4_custom_command.size() + 1);
  391. for (std::string const& i : p4_custom_command) {
  392. p4_custom.push_back(i.c_str());
  393. }
  394. p4_custom.push_back(nullptr);
  395. OutputLogger custom_out(this->Log, "p4_customsync-out> ");
  396. OutputLogger custom_err(this->Log, "p4_customsync-err> ");
  397. return this->RunUpdateCommand(p4_custom.data(), &custom_out, &custom_err);
  398. }
  399. bool cmCTestP4::UpdateImpl()
  400. {
  401. std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
  402. if (!custom.empty()) {
  403. return this->UpdateCustom(custom);
  404. }
  405. // If we couldn't get a revision number before updating, abort.
  406. if (this->OldRevision == "<unknown>") {
  407. this->UpdateCommandLine = "Unknown current revision";
  408. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown current revision\n");
  409. return false;
  410. }
  411. std::vector<char const*> p4_sync;
  412. this->SetP4Options(p4_sync);
  413. p4_sync.push_back("sync");
  414. // Get user-specified update options.
  415. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  416. if (opts.empty()) {
  417. opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
  418. }
  419. std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
  420. for (std::string const& arg : args) {
  421. p4_sync.push_back(arg.c_str());
  422. }
  423. std::string source = this->SourceDirectory + "/...";
  424. // Specify the start time for nightly testing.
  425. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  426. std::string date = this->GetNightlyTime();
  427. // CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  428. std::replace(date.begin(), date.end(), '-', '/');
  429. // Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  430. source.append("@\"").append(date).append("\"");
  431. }
  432. p4_sync.push_back(source.c_str());
  433. p4_sync.push_back(nullptr);
  434. OutputLogger out(this->Log, "p4_sync-out> ");
  435. OutputLogger err(this->Log, "p4_sync-err> ");
  436. return this->RunUpdateCommand(p4_sync.data(), &out, &err);
  437. }