cmCTestP4.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "cmMakefile.h"
  14. #include "cmRange.h"
  15. #include "cmSystemTools.h"
  16. cmCTestP4::cmCTestP4(cmCTest* ct, cmMakefile* mf, std::ostream& log)
  17. : cmCTestGlobalVC(ct, mf, log)
  18. {
  19. this->PriorRev = this->Unknown;
  20. }
  21. cmCTestP4::~cmCTestP4() = default;
  22. class cmCTestP4::IdentifyParser : public cmCTestVC::LineParser
  23. {
  24. public:
  25. IdentifyParser(cmCTestP4* p4, const char* prefix, std::string& rev)
  26. : Rev(rev)
  27. {
  28. this->SetLog(&p4->Log, prefix);
  29. this->RegexIdentify.compile("^Change ([0-9]+) on");
  30. }
  31. private:
  32. std::string& Rev;
  33. cmsys::RegularExpression RegexIdentify;
  34. bool ProcessLine() override
  35. {
  36. if (this->RegexIdentify.find(this->Line)) {
  37. this->Rev = this->RegexIdentify.match(1);
  38. return false;
  39. }
  40. return true;
  41. }
  42. };
  43. class cmCTestP4::ChangesParser : public cmCTestVC::LineParser
  44. {
  45. public:
  46. ChangesParser(cmCTestP4* p4, const char* prefix)
  47. : P4(p4)
  48. {
  49. this->SetLog(&this->P4->Log, prefix);
  50. this->RegexIdentify.compile("^Change ([0-9]+) on");
  51. }
  52. private:
  53. cmsys::RegularExpression RegexIdentify;
  54. cmCTestP4* P4;
  55. bool ProcessLine() override
  56. {
  57. if (this->RegexIdentify.find(this->Line)) {
  58. this->P4->ChangeLists.push_back(this->RegexIdentify.match(1));
  59. }
  60. return true;
  61. }
  62. };
  63. class cmCTestP4::UserParser : public cmCTestVC::LineParser
  64. {
  65. public:
  66. UserParser(cmCTestP4* p4, const char* prefix)
  67. : P4(p4)
  68. {
  69. this->SetLog(&this->P4->Log, prefix);
  70. this->RegexUser.compile("^(.+) <(.*)> \\((.*)\\) accessed (.*)$");
  71. }
  72. private:
  73. cmsys::RegularExpression RegexUser;
  74. cmCTestP4* P4;
  75. bool ProcessLine() override
  76. {
  77. if (this->RegexUser.find(this->Line)) {
  78. User NewUser;
  79. NewUser.UserName = this->RegexUser.match(1);
  80. NewUser.EMail = this->RegexUser.match(2);
  81. NewUser.Name = this->RegexUser.match(3);
  82. NewUser.AccessTime = this->RegexUser.match(4);
  83. this->P4->Users[this->RegexUser.match(1)] = NewUser;
  84. return false;
  85. }
  86. return true;
  87. }
  88. };
  89. /* Diff format:
  90. ==== //depot/file#rev - /absolute/path/to/file ====
  91. (diff data)
  92. ==== //depot/file2#rev - /absolute/path/to/file2 ====
  93. (diff data)
  94. ==== //depot/file3#rev - /absolute/path/to/file3 ====
  95. ==== //depot/file4#rev - /absolute/path/to/file4 ====
  96. (diff data)
  97. */
  98. class cmCTestP4::DiffParser : public cmCTestVC::LineParser
  99. {
  100. public:
  101. DiffParser(cmCTestP4* p4, const char* prefix)
  102. : P4(p4)
  103. {
  104. this->SetLog(&this->P4->Log, prefix);
  105. this->RegexDiff.compile("^==== (.*)#[0-9]+ - (.*)");
  106. }
  107. private:
  108. cmCTestP4* P4;
  109. bool AlreadyNotified = false;
  110. std::string CurrentPath;
  111. cmsys::RegularExpression RegexDiff;
  112. bool ProcessLine() override
  113. {
  114. if (!this->Line.empty() && this->Line[0] == '=' &&
  115. this->RegexDiff.find(this->Line)) {
  116. this->CurrentPath = this->RegexDiff.match(1);
  117. this->AlreadyNotified = false;
  118. } else {
  119. if (!this->AlreadyNotified) {
  120. this->P4->DoModification(PathModified, this->CurrentPath);
  121. this->AlreadyNotified = true;
  122. }
  123. }
  124. return true;
  125. }
  126. };
  127. cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
  128. {
  129. auto it = this->Users.find(username);
  130. if (it == this->Users.end()) {
  131. std::vector<std::string> p4_users;
  132. this->SetP4Options(p4_users);
  133. p4_users.emplace_back("users");
  134. p4_users.emplace_back("-m");
  135. p4_users.emplace_back("1");
  136. p4_users.push_back(username);
  137. UserParser out(this, "users-out> ");
  138. OutputLogger err(this->Log, "users-err> ");
  139. this->RunChild(p4_users, &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<std::string>& CommandOptions)
  263. {
  264. if (this->P4Options.empty()) {
  265. std::string p4 = this->CommandLineTool;
  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->Makefile->GetSafeDefinition("CTEST_P4_CLIENT");
  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->Makefile->GetSafeDefinition("CTEST_P4_OPTIONS");
  281. cm::append(this->P4Options, cmSystemTools::ParseArguments(opts));
  282. }
  283. CommandOptions = this->P4Options;
  284. }
  285. std::string cmCTestP4::GetWorkingRevision()
  286. {
  287. std::vector<std::string> p4_identify;
  288. this->SetP4Options(p4_identify);
  289. p4_identify.emplace_back("changes");
  290. p4_identify.emplace_back("-m");
  291. p4_identify.emplace_back("1");
  292. p4_identify.emplace_back("-t");
  293. std::string source = this->SourceDirectory + "/...#have";
  294. p4_identify.push_back(source);
  295. std::string rev;
  296. IdentifyParser out(this, "p4_changes-out> ", rev);
  297. OutputLogger err(this->Log, "p4_changes-err> ");
  298. bool result = this->RunChild(p4_identify, &out, &err);
  299. // If there was a problem contacting the server return "<unknown>"
  300. if (!result) {
  301. return "<unknown>";
  302. }
  303. if (rev.empty()) {
  304. return "0";
  305. }
  306. return rev;
  307. }
  308. bool cmCTestP4::NoteOldRevision()
  309. {
  310. this->OldRevision = this->GetWorkingRevision();
  311. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  312. " Old revision of repository is: " << this->OldRevision
  313. << "\n");
  314. this->PriorRev.Rev = this->OldRevision;
  315. return true;
  316. }
  317. bool cmCTestP4::NoteNewRevision()
  318. {
  319. this->NewRevision = this->GetWorkingRevision();
  320. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  321. " New revision of repository is: " << this->NewRevision
  322. << "\n");
  323. return true;
  324. }
  325. bool cmCTestP4::LoadRevisions()
  326. {
  327. std::vector<std::string> p4_changes;
  328. this->SetP4Options(p4_changes);
  329. // Use 'p4 changes ...@old,new' to get a list of changelists
  330. std::string range = this->SourceDirectory + "/...";
  331. // If any revision is unknown it means we couldn't contact the server.
  332. // Do not process updates
  333. if (this->OldRevision == "<unknown>" || this->NewRevision == "<unknown>") {
  334. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  335. " At least one of the revisions "
  336. << "is unknown. No repository changes will be reported.\n");
  337. return false;
  338. }
  339. range.append("@")
  340. .append(this->OldRevision)
  341. .append(",")
  342. .append(this->NewRevision);
  343. p4_changes.emplace_back("changes");
  344. p4_changes.push_back(range);
  345. ChangesParser out(this, "p4_changes-out> ");
  346. OutputLogger err(this->Log, "p4_changes-err> ");
  347. this->ChangeLists.clear();
  348. this->RunChild(p4_changes, &out, &err);
  349. if (this->ChangeLists.empty()) {
  350. return true;
  351. }
  352. // p4 describe -s ...@1111111,2222222
  353. std::vector<std::string> p4_describe;
  354. for (std::string const& i : cmReverseRange(this->ChangeLists)) {
  355. this->SetP4Options(p4_describe);
  356. p4_describe.emplace_back("describe");
  357. p4_describe.emplace_back("-s");
  358. p4_describe.push_back(i);
  359. DescribeParser outDescribe(this, "p4_describe-out> ");
  360. OutputLogger errDescribe(this->Log, "p4_describe-err> ");
  361. this->RunChild(p4_describe, &outDescribe, &errDescribe);
  362. }
  363. return true;
  364. }
  365. bool cmCTestP4::LoadModifications()
  366. {
  367. std::vector<std::string> p4_diff;
  368. this->SetP4Options(p4_diff);
  369. p4_diff.emplace_back("diff");
  370. // Ideally we would use -Od but not all clients support it
  371. p4_diff.emplace_back("-dn");
  372. std::string source = this->SourceDirectory + "/...";
  373. p4_diff.push_back(source);
  374. DiffParser out(this, "p4_diff-out> ");
  375. OutputLogger err(this->Log, "p4_diff-err> ");
  376. this->RunChild(p4_diff, &out, &err);
  377. return true;
  378. }
  379. bool cmCTestP4::UpdateCustom(const std::string& custom)
  380. {
  381. cmList p4_custom_command{ custom, cmList::EmptyElements::Yes };
  382. std::vector<std::string> p4_custom;
  383. p4_custom.reserve(p4_custom_command.size());
  384. cm::append(p4_custom, p4_custom_command);
  385. OutputLogger custom_out(this->Log, "p4_customsync-out> ");
  386. OutputLogger custom_err(this->Log, "p4_customsync-err> ");
  387. return this->RunUpdateCommand(p4_custom, &custom_out, &custom_err);
  388. }
  389. bool cmCTestP4::UpdateImpl()
  390. {
  391. std::string custom =
  392. this->Makefile->GetSafeDefinition("CTEST_P4_UPDATE_CUSTOM");
  393. if (!custom.empty()) {
  394. return this->UpdateCustom(custom);
  395. }
  396. // If we couldn't get a revision number before updating, abort.
  397. if (this->OldRevision == "<unknown>") {
  398. this->UpdateCommandLine = "Unknown current revision";
  399. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown current revision\n");
  400. return false;
  401. }
  402. std::vector<std::string> p4_sync;
  403. this->SetP4Options(p4_sync);
  404. p4_sync.emplace_back("sync");
  405. // Get user-specified update options.
  406. std::string opts = this->Makefile->GetSafeDefinition("CTEST_UPDATE_OPTIONS");
  407. if (opts.empty()) {
  408. opts = this->Makefile->GetSafeDefinition("CTEST_P4_UPDATE_OPTIONS");
  409. }
  410. std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
  411. cm::append(p4_sync, args);
  412. std::string source = this->SourceDirectory + "/...";
  413. // Specify the start time for nightly testing.
  414. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  415. std::string date = this->GetNightlyTime();
  416. // CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  417. std::replace(date.begin(), date.end(), '-', '/');
  418. // Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  419. source.append("@\"").append(date).append("\"");
  420. }
  421. p4_sync.push_back(source);
  422. OutputLogger out(this->Log, "p4_sync-out> ");
  423. OutputLogger err(this->Log, "p4_sync-err> ");
  424. return this->RunUpdateCommand(p4_sync, &out, &err);
  425. }