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 "cmCTest.h"
  5. #include "cmCTestVC.h"
  6. #include "cmProcessTools.h"
  7. #include "cmRange.h"
  8. #include "cmSystemTools.h"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <algorithm>
  11. #include <ostream>
  12. #include <time.h>
  13. #include <utility>
  14. cmCTestP4::cmCTestP4(cmCTest* ct, std::ostream& log)
  15. : cmCTestGlobalVC(ct, log)
  16. {
  17. this->PriorRev = this->Unknown;
  18. }
  19. cmCTestP4::~cmCTestP4() = default;
  20. class cmCTestP4::IdentifyParser : public cmCTestVC::LineParser
  21. {
  22. public:
  23. IdentifyParser(cmCTestP4* p4, const char* prefix, std::string& rev)
  24. : Rev(rev)
  25. {
  26. this->SetLog(&p4->Log, prefix);
  27. this->RegexIdentify.compile("^Change ([0-9]+) on");
  28. }
  29. private:
  30. std::string& Rev;
  31. cmsys::RegularExpression RegexIdentify;
  32. bool ProcessLine() override
  33. {
  34. if (this->RegexIdentify.find(this->Line)) {
  35. this->Rev = this->RegexIdentify.match(1);
  36. return false;
  37. }
  38. return true;
  39. }
  40. };
  41. class cmCTestP4::ChangesParser : public cmCTestVC::LineParser
  42. {
  43. public:
  44. ChangesParser(cmCTestP4* p4, const char* prefix)
  45. : P4(p4)
  46. {
  47. this->SetLog(&P4->Log, prefix);
  48. this->RegexIdentify.compile("^Change ([0-9]+) on");
  49. }
  50. private:
  51. cmsys::RegularExpression RegexIdentify;
  52. cmCTestP4* P4;
  53. bool ProcessLine() override
  54. {
  55. if (this->RegexIdentify.find(this->Line)) {
  56. P4->ChangeLists.push_back(this->RegexIdentify.match(1));
  57. }
  58. return true;
  59. }
  60. };
  61. class cmCTestP4::UserParser : public cmCTestVC::LineParser
  62. {
  63. public:
  64. UserParser(cmCTestP4* p4, const char* prefix)
  65. : P4(p4)
  66. {
  67. this->SetLog(&P4->Log, prefix);
  68. this->RegexUser.compile("^(.+) <(.*)> \\((.*)\\) accessed (.*)$");
  69. }
  70. private:
  71. cmsys::RegularExpression RegexUser;
  72. cmCTestP4* P4;
  73. bool ProcessLine() override
  74. {
  75. if (this->RegexUser.find(this->Line)) {
  76. User NewUser;
  77. NewUser.UserName = this->RegexUser.match(1);
  78. NewUser.EMail = this->RegexUser.match(2);
  79. NewUser.Name = this->RegexUser.match(3);
  80. NewUser.AccessTime = this->RegexUser.match(4);
  81. P4->Users[this->RegexUser.match(1)] = NewUser;
  82. return false;
  83. }
  84. return true;
  85. }
  86. };
  87. /* Diff format:
  88. ==== //depot/file#rev - /absolute/path/to/file ====
  89. (diff data)
  90. ==== //depot/file2#rev - /absolute/path/to/file2 ====
  91. (diff data)
  92. ==== //depot/file3#rev - /absolute/path/to/file3 ====
  93. ==== //depot/file4#rev - /absolute/path/to/file4 ====
  94. (diff data)
  95. */
  96. class cmCTestP4::DiffParser : public cmCTestVC::LineParser
  97. {
  98. public:
  99. DiffParser(cmCTestP4* p4, const char* prefix)
  100. : P4(p4)
  101. , AlreadyNotified(false)
  102. {
  103. this->SetLog(&P4->Log, prefix);
  104. this->RegexDiff.compile("^==== (.*)#[0-9]+ - (.*)");
  105. }
  106. private:
  107. cmCTestP4* P4;
  108. bool AlreadyNotified;
  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. CurrentPath = this->RegexDiff.match(1);
  116. AlreadyNotified = false;
  117. } else {
  118. if (!AlreadyNotified) {
  119. P4->DoModification(PathModified, CurrentPath);
  120. AlreadyNotified = true;
  121. }
  122. }
  123. return true;
  124. }
  125. };
  126. cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
  127. {
  128. std::map<std::string, cmCTestP4::User>::const_iterator it =
  129. Users.find(username);
  130. if (it == Users.end()) {
  131. std::vector<char const*> p4_users;
  132. SetP4Options(p4_users);
  133. p4_users.push_back("users");
  134. p4_users.push_back("-m");
  135. p4_users.push_back("1");
  136. p4_users.push_back(username.c_str());
  137. p4_users.push_back(nullptr);
  138. UserParser out(this, "users-out> ");
  139. OutputLogger err(this->Log, "users-err> ");
  140. RunChild(&p4_users[0], &out, &err);
  141. // The user should now be added to the map. Search again.
  142. it = Users.find(username);
  143. if (it == Users.end()) {
  144. return cmCTestP4::User();
  145. }
  146. }
  147. return it->second;
  148. }
  149. /* Commit format:
  150. Change 1111111 by user@client on 2013/09/26 11:50:36
  151. text
  152. text
  153. Affected files ...
  154. ... //path/to/file#rev edit
  155. ... //path/to/file#rev add
  156. ... //path/to/file#rev delete
  157. ... //path/to/file#rev integrate
  158. */
  159. class cmCTestP4::DescribeParser : public cmCTestVC::LineParser
  160. {
  161. public:
  162. DescribeParser(cmCTestP4* p4, const char* prefix)
  163. : LineParser('\n', false)
  164. , P4(p4)
  165. , Section(SectionHeader)
  166. {
  167. this->SetLog(&P4->Log, prefix);
  168. this->RegexHeader.compile("^Change ([0-9]+) by (.+)@(.+) on (.*)$");
  169. this->RegexDiff.compile(R"(^\.\.\. (.*)#[0-9]+ ([^ ]+)$)");
  170. }
  171. private:
  172. cmsys::RegularExpression RegexHeader;
  173. cmsys::RegularExpression RegexDiff;
  174. cmCTestP4* P4;
  175. typedef cmCTestP4::Revision Revision;
  176. typedef cmCTestP4::Change Change;
  177. std::vector<Change> Changes;
  178. enum SectionType
  179. {
  180. SectionHeader,
  181. SectionBody,
  182. SectionDiffHeader,
  183. SectionDiff,
  184. SectionCount
  185. };
  186. SectionType Section;
  187. Revision Rev;
  188. bool ProcessLine() override
  189. {
  190. if (this->Line.empty()) {
  191. this->NextSection();
  192. } else {
  193. switch (this->Section) {
  194. case SectionHeader:
  195. this->DoHeaderLine();
  196. break;
  197. case SectionBody:
  198. this->DoBodyLine();
  199. break;
  200. case SectionDiffHeader:
  201. break; // nothing to do
  202. case SectionDiff:
  203. this->DoDiffLine();
  204. break;
  205. case SectionCount:
  206. break; // never happens
  207. }
  208. }
  209. return true;
  210. }
  211. void NextSection()
  212. {
  213. if (this->Section == SectionDiff) {
  214. this->P4->DoRevision(this->Rev, this->Changes);
  215. this->Rev = Revision();
  216. }
  217. this->Section = SectionType((this->Section + 1) % SectionCount);
  218. }
  219. void DoHeaderLine()
  220. {
  221. if (this->RegexHeader.find(this->Line)) {
  222. this->Rev.Rev = this->RegexHeader.match(1);
  223. this->Rev.Date = this->RegexHeader.match(4);
  224. cmCTestP4::User user = P4->GetUserData(this->RegexHeader.match(2));
  225. this->Rev.Author = user.Name;
  226. this->Rev.EMail = user.EMail;
  227. this->Rev.Committer = this->Rev.Author;
  228. this->Rev.CommitterEMail = this->Rev.EMail;
  229. this->Rev.CommitDate = this->Rev.Date;
  230. }
  231. }
  232. void DoBodyLine()
  233. {
  234. if (this->Line[0] == '\t') {
  235. this->Rev.Log += this->Line.substr(1);
  236. }
  237. this->Rev.Log += "\n";
  238. }
  239. void DoDiffLine()
  240. {
  241. if (this->RegexDiff.find(this->Line)) {
  242. Change change;
  243. std::string Path = this->RegexDiff.match(1);
  244. if (Path.length() > 2 && Path[0] == '/' && Path[1] == '/') {
  245. size_t found = Path.find('/', 2);
  246. if (found != std::string::npos) {
  247. Path = Path.substr(found + 1);
  248. }
  249. }
  250. change.Path = Path;
  251. std::string action = this->RegexDiff.match(2);
  252. if (action == "add") {
  253. change.Action = 'A';
  254. } else if (action == "delete") {
  255. change.Action = 'D';
  256. } else if (action == "edit" || action == "integrate") {
  257. change.Action = 'M';
  258. }
  259. Changes.push_back(change);
  260. }
  261. }
  262. };
  263. void cmCTestP4::SetP4Options(std::vector<char const*>& CommandOptions)
  264. {
  265. if (P4Options.empty()) {
  266. const char* p4 = this->CommandLineTool.c_str();
  267. P4Options.emplace_back(p4);
  268. // The CTEST_P4_CLIENT variable sets the P4 client used when issuing
  269. // Perforce commands, if it's different from the default one.
  270. std::string client = this->CTest->GetCTestConfiguration("P4Client");
  271. if (!client.empty()) {
  272. P4Options.emplace_back("-c");
  273. P4Options.push_back(client);
  274. }
  275. // Set the message language to be English, in case the P4 admin
  276. // has localized them
  277. P4Options.emplace_back("-L");
  278. P4Options.emplace_back("en");
  279. // The CTEST_P4_OPTIONS variable adds additional Perforce command line
  280. // options before the main command
  281. std::string opts = this->CTest->GetCTestConfiguration("P4Options");
  282. std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
  283. P4Options.insert(P4Options.end(), args.begin(), args.end());
  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;
  391. cmSystemTools::ExpandListArgument(custom, p4_custom_command, true);
  392. std::vector<char const*> p4_custom;
  393. p4_custom.reserve(p4_custom_command.size() + 1);
  394. for (std::string const& i : p4_custom_command) {
  395. p4_custom.push_back(i.c_str());
  396. }
  397. p4_custom.push_back(nullptr);
  398. OutputLogger custom_out(this->Log, "p4_customsync-out> ");
  399. OutputLogger custom_err(this->Log, "p4_customsync-err> ");
  400. return this->RunUpdateCommand(&p4_custom[0], &custom_out, &custom_err);
  401. }
  402. bool cmCTestP4::UpdateImpl()
  403. {
  404. std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
  405. if (!custom.empty()) {
  406. return this->UpdateCustom(custom);
  407. }
  408. // If we couldn't get a revision number before updating, abort.
  409. if (this->OldRevision == "<unknown>") {
  410. this->UpdateCommandLine = "Unknown current revision";
  411. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown current revision\n");
  412. return false;
  413. }
  414. std::vector<char const*> p4_sync;
  415. SetP4Options(p4_sync);
  416. p4_sync.push_back("sync");
  417. // Get user-specified update options.
  418. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  419. if (opts.empty()) {
  420. opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
  421. }
  422. std::vector<std::string> args = cmSystemTools::ParseArguments(opts);
  423. for (std::string const& arg : args) {
  424. p4_sync.push_back(arg.c_str());
  425. }
  426. std::string source = this->SourceDirectory + "/...";
  427. // Specify the start time for nightly testing.
  428. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  429. std::string date = this->GetNightlyTime();
  430. // CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  431. std::replace(date.begin(), date.end(), '-', '/');
  432. // Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  433. source.append("@\"").append(date).append("\"");
  434. }
  435. p4_sync.push_back(source.c_str());
  436. p4_sync.push_back(nullptr);
  437. OutputLogger out(this->Log, "p4_sync-out> ");
  438. OutputLogger err(this->Log, "p4_sync-err> ");
  439. return this->RunUpdateCommand(&p4_sync[0], &out, &err);
  440. }