cmCTestP4.cxx 13 KB

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